-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
filter-coercion-and-validation.test.ts
73 lines (70 loc) · 2.02 KB
/
filter-coercion-and-validation.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { text } from '@keystone-next/keystone/fields';
import { list } from '@keystone-next/keystone';
import { setupTestRunner } from '@keystone-next/keystone/testing';
import { apiTestConfig, expectExtensionError } from '../utils';
const runner = setupTestRunner({
config: apiTestConfig({
lists: {
BadAccess: list({
fields: { name: text() },
access: {
filter: {
query: () => {
return {
name: 'blah',
};
},
},
},
}),
Coercion: list({
fields: { name: text() },
access: {
filter: {
query: () => {
return { NOT: { name: { equals: 'blah' } } };
},
},
},
}),
},
}),
});
describe('Access control - Filter', () => {
test(
'findMany - Bad function return value',
runner(async ({ context }) => {
// Valid name
const { data, errors } = await context.graphql.raw({
query: `query { badAccesses { id } }`,
});
// Returns null and throws an error
expect(data).toEqual({ badAccesses: null });
const message =
'Variable "$where" got invalid value "blah" at "where.name"; Expected type "StringFilter" to be an object.';
expectExtensionError('dev', false, false, errors, 'Access control', [
{
path: ['badAccesses'],
messages: [`BadAccess.access.filter.query: ${message}`],
debug: [
{
message,
stacktrace: expect.stringMatching(
new RegExp(`GraphQLError: ${message.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`)
),
},
],
},
]);
})
);
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' },
]);
})
);
});