Skip to content

Commit

Permalink
Add operation argument to validateAuthAccessControl (#4865)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Feb 17, 2021
1 parent 370c0ee commit 4eb4753
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/late-nails-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/access-control': major
'@keystonejs/keystone': patch
---

Updated `validateAuthAccessControl` to now require an explicit `operation` argument.
4 changes: 2 additions & 2 deletions packages/access-control/src/access-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ export async function validateAuthAccessControl({
authentication,
gqlName,
context,
}: { access: AuthAccess<AuthAccessArgs> } & Omit<AuthAccessArgs, 'operation'>) {
const operation = 'auth';
operation,
}: { access: AuthAccess<AuthAccessArgs> } & AuthAccessArgs) {
// Either a boolean or an object describing a where clause
let result: Static | Declarative = false;
const acc = access[operation];
Expand Down
21 changes: 15 additions & 6 deletions packages/access-control/tests/access-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,19 +513,19 @@ describe('Access control package tests', () => {
// Test the static case: returning a boolean
const authArgs = { listKey: 'listKey', authentication: {}, gqlName: 'gqlName', context: {} };
await expect(
validateAuthAccessControl({ access: { [operation]: true }, ...authArgs })
validateAuthAccessControl({ access: { [operation]: true }, operation, ...authArgs })
).resolves.toBe(true);
await expect(
validateAuthAccessControl({ access: { [operation]: false }, ...authArgs })
validateAuthAccessControl({ access: { [operation]: false }, operation, ...authArgs })
).resolves.toBe(false);
await expect(
// @ts-ignore
validateAuthAccessControl({ access: { [operation]: 10 }, ...authArgs })
validateAuthAccessControl({ access: { [operation]: 10 }, operation, ...authArgs })
).rejects.toThrow(Error);

const accessFn = jest.fn(() => true);

await validateAuthAccessControl({ access: { [operation]: accessFn }, ...authArgs });
await validateAuthAccessControl({ access: { [operation]: accessFn }, operation, ...authArgs });

expect(accessFn).toHaveBeenCalledTimes(1);

Expand All @@ -535,13 +535,15 @@ describe('Access control package tests', () => {
await expect(
validateAuthAccessControl({
access: { [operation]: () => true },
operation,
...authArgs,
authentication,
})
).resolves.toBe(true);
await expect(
validateAuthAccessControl({
access: { [operation]: () => false },
operation,
...authArgs,
authentication,
})
Expand All @@ -550,6 +552,7 @@ describe('Access control package tests', () => {
await expect(
validateAuthAccessControl({
access: { [operation]: () => ({ a: 1 }) },
operation,
...authArgs,
authentication,
})
Expand All @@ -560,15 +563,21 @@ describe('Access control package tests', () => {
validateAuthAccessControl({
// @ts-ignore
access: { create: () => ({ a: 1 }) },
operation,
...authArgs,
authentication,
})
).rejects.toThrow(Error);

// Number function
await expect(
// @ts-ignore
validateAuthAccessControl({ access: { create: () => 10 }, ...authArgs, authentication })
validateAuthAccessControl({
// @ts-ignore
access: { create: () => 10 },
operation,
...authArgs,
authentication,
})
).rejects.toThrow(Error);
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ module.exports = class Keystone {
listKey,
gqlName,
context,
operation: 'auth',
});
},
{ isPromise: true }
Expand Down

1 comment on commit 4eb4753

@vercel
Copy link

@vercel vercel bot commented on 4eb4753 Feb 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.