-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aff filter coercion and validation back to filter access control (#6678)
- Loading branch information
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@keystone-next/keystone': patch | ||
--- | ||
|
||
Fixed returning filters like `{ NOT: [{ name: { equals: 'blah' } }] }` from filter access control and improve error messages when returning bad filters from filter access control |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/api-tests/access-control/filter-coercion-and-validation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { text } from '@keystone-next/keystone/fields'; | ||
import { list } from '@keystone-next/keystone'; | ||
import { setupTestRunner } from '@keystone-next/keystone/testing'; | ||
import { apiTestConfig } from '../utils'; | ||
|
||
const runner = setupTestRunner({ | ||
config: apiTestConfig({ | ||
lists: { | ||
BadAccess: list({ | ||
fields: { name: text({ isFilterable: true, isOrderable: true }) }, | ||
access: { | ||
filter: { | ||
query: () => { | ||
return { | ||
name: 'blah', | ||
}; | ||
}, | ||
}, | ||
}, | ||
}), | ||
Coercion: list({ | ||
fields: { name: text({ isFilterable: true, isOrderable: true }) }, | ||
access: { | ||
filter: { | ||
query: () => { | ||
return { NOT: { name: { equals: 'blah' } } }; | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}), | ||
}); | ||
|
||
describe('Access control - Filter', () => { | ||
test( | ||
'findMany - Bad function return value', | ||
runner(async ({ graphQLRequest }) => { | ||
// Valid name | ||
const { body } = await graphQLRequest({ | ||
query: `query { badAccesses { id } }`, | ||
}); | ||
|
||
// Returns null and throws an error | ||
expect(body.data).toEqual({ badAccesses: null }); | ||
expect(body.errors).toHaveLength(1); | ||
expect(body.errors[0].path).toEqual(['badAccesses']); | ||
expect(body.errors[0].message).toMatchInlineSnapshot(` | ||
"An error occured while running \\"Access control\\". | ||
- BadAccess.access.filter.query: Variable \\"$where\\" got invalid value \\"blah\\" at \\"where.name\\"; Expected type \\"StringFilter\\" to be an object." | ||
`); | ||
}) | ||
); | ||
test( | ||
'findMany - Coercion', | ||
runner(async ({ context }) => { | ||
await context.query.Coercion.createMany({ data: [{ name: 'something' }, { name: 'blah' }] }); | ||
expect(await context.query.Coercion.findMany({ query: 'name' })).toEqual([ | ||
{ name: 'something' }, | ||
]); | ||
}) | ||
); | ||
}); |