forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth-header.test.ts
153 lines (142 loc) · 4.52 KB
/
auth-header.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { text, timestamp, password } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { statelessSessions } from '@keystone-next/keystone/session';
import { createAuth } from '@keystone-next/auth';
import type { KeystoneContext, KeystoneConfig } from '@keystone-next/types';
import { setupTestRunner, TestArgs } from '@keystone-next/testing';
import { apiTestConfig, expectAccessDenied } from './utils';
const initialData = {
User: [
{
data: {
name: 'Boris Bozic',
email: '[email protected]',
password: 'correctbattery',
},
},
{
data: {
name: 'Jed Watson',
email: '[email protected]',
password: 'horsestaple',
},
},
],
};
const COOKIE_SECRET = 'qwertyuiopasdfghjlkzxcvbmnm1234567890';
const defaultAccess = ({ context }: { context: KeystoneContext }) => !!context.session?.data;
const auth = createAuth({
listKey: 'User',
identityField: 'email',
secretField: 'password',
sessionData: 'id',
});
const runner = setupTestRunner({
config: apiTestConfig(
auth.withAuth({
lists: createSchema({
Post: list({
fields: {
title: text(),
postedAt: timestamp(),
},
}),
User: list({
fields: {
name: text(),
email: text(),
password: password(),
},
access: {
create: defaultAccess,
read: defaultAccess,
update: defaultAccess,
delete: defaultAccess,
},
}),
}),
session: statelessSessions({ secret: COOKIE_SECRET }),
} as KeystoneConfig)
),
});
async function login(
graphQLRequest: TestArgs['graphQLRequest'],
email: string,
password: string
): Promise<{ sessionToken: string; item: { id: any } }> {
const { body } = await graphQLRequest({
query: `
mutation($email: String!, $password: String!) {
authenticateUserWithPassword(email: $email, password: $password) {
... on UserAuthenticationWithPasswordSuccess {
sessionToken
item { id }
}
}
}
`,
variables: { email, password },
});
return body.data?.authenticateUserWithPassword || { sessionToken: '', item: { id: undefined } };
}
describe('Auth testing', () => {
test(
'Gives access denied when not logged in',
runner(async ({ context }) => {
// seed the db
for (const [listKey, data] of Object.entries(initialData)) {
await context.sudo().lists[listKey].createMany({ data });
}
const { data, errors } = await context.graphql.raw({ query: '{ allUsers { id } }' });
expect(data).toEqual({ allUsers: null });
expectAccessDenied(errors, [{ path: ['allUsers'] }]);
})
);
describe('logged in', () => {
// eslint-disable-next-line jest/no-disabled-tests
test.skip(
'Allows access with bearer token',
runner(async ({ context, graphQLRequest }) => {
for (const [listKey, data] of Object.entries(initialData)) {
await context.sudo().lists[listKey].createMany({ data });
}
const { sessionToken } = await login(
graphQLRequest,
initialData.User[0].data.email,
initialData.User[0].data.password
);
expect(sessionToken).toBeTruthy();
const { body } = await graphQLRequest({ query: '{ allUsers { id } }' }).set(
'Authorization',
`Bearer ${sessionToken}`
);
const { data, errors } = body;
expect(data).toHaveProperty('allUsers');
expect(data.allUsers).toHaveLength(initialData.User.length);
expect(errors).toBe(undefined);
})
);
test(
'Allows access with cookie',
runner(async ({ context, graphQLRequest }) => {
for (const [listKey, data] of Object.entries(initialData)) {
await context.sudo().lists[listKey].createMany({ data });
}
const { sessionToken } = await login(
graphQLRequest,
initialData.User[0].data.email,
initialData.User[0].data.password
);
expect(sessionToken).toBeTruthy();
const { body } = await graphQLRequest({ query: '{ allUsers { id } }' }).set(
'Cookie',
`keystonejs-session=${sessionToken}`
);
const { data, errors } = body;
expect(data).toHaveProperty('allUsers');
expect(data.allUsers).toHaveLength(initialData.User.length);
expect(errors).toBe(undefined);
})
);
});
});