Skip to content

Commit

Permalink
add missing test for missing item
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Oct 5, 2022
1 parent 2aacbc9 commit be5e789
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
16 changes: 3 additions & 13 deletions packages/core/src/lib/core/mutations/access-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import {
UniquePrismaFilter,
} from '../where-inputs';

function cannotWithFilter(
operation: string,
list: InitialisedList,
uniqueWhere: UniquePrismaFilter
) {
return `You cannot '${operation}' a ${list.listKey} with the filter ${JSON.stringify(
uniqueWhere
)} - it may not exist`;
}

function cannotForItemFields(operation: string, list: InitialisedList, fieldsDenied: string[]) {
return `You cannot '${operation}' that ${
list.listKey
Expand All @@ -43,7 +33,7 @@ async function getFilteredItem(
) {
// early exit if they want to exclude everything
if (accessFilters === false) {
throw accessDeniedError(cannotWithFilter(operation, list, uniqueWhere));
throw accessDeniedError(cannotForItem(operation, list));
}

// merge the filter access control and try to get the item
Expand All @@ -55,7 +45,7 @@ async function getFilteredItem(
const item = await runWithPrisma(context, list, model => model.findFirst({ where }));
if (item !== null) return item;

throw accessDeniedError(cannotWithFilter(operation, list, uniqueWhere));
throw accessDeniedError(cannotForItem(operation, list));
}

export async function checkUniqueItemExists(
Expand All @@ -73,7 +63,7 @@ export async function checkUniqueItemExists(
if (item !== null) return uniqueWhere;
} catch (err) {}

throw accessDeniedError(cannotWithFilter(operation, foreignList, uniqueWhere));
throw accessDeniedError(cannotForItem(operation, foreignList));
}

async function enforceListLevelAccessControl({
Expand Down
30 changes: 30 additions & 0 deletions tests/api-tests/access-control/mutations-list-item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const runner = setupTestRunner({
User: list({
access: {
operation: allowAll,
filter: () => {
return {
name: { not: { equals: 'hidden' } }
}
},
item: {
create: ({ inputData }) => {
return inputData.name !== 'bad';
Expand Down Expand Up @@ -130,6 +135,31 @@ describe('Access control - Item', () => {
})
);

test(
'updateOne - Missing item',
runner(async ({ context }) => {
const user = await context.query.User.createOne({ data: { name: 'hidden' } });
const { data, errors } = await context.graphql.raw({
query: `mutation ($id: ID! $data: UserUpdateInput!) { updateUser(where: { id: $id }, data: $data) { id } }`,
variables: { id: user.id, data: { name: 'something else' } },
});

// Returns null and throws an error
expect(data).toEqual({ updateUser: null });
expectAccessDenied(errors, [
{
path: ['updateUser'],
msg: `You cannot 'update' that User - it may not exist`,
},
]);

// should be unchanged
const userAgain = await context.sudo().db.User.findOne({ where: { id: user.id } });
expect(userAgain).not.toEqual(null);
expect(userAgain!.name).toEqual('hidden');
})
);

test(
'updateOne - Bad function return value',
runner(async ({ context }) => {
Expand Down

0 comments on commit be5e789

Please sign in to comment.