forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.test.ts
199 lines (184 loc) · 7.29 KB
/
example.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { KeystoneContext } from '@keystone-6/core/types';
import { setupTestEnv, setupTestRunner, TestEnv } from '@keystone-6/core/testing';
import config from './keystone';
// Setup a test runner which will provide a clean test environment
// with access to our GraphQL API for each test.
const runner = setupTestRunner({ config });
describe('Example tests using test runner', () => {
test(
'Create a Person using the Query API',
runner(async ({ context }) => {
// We can use the context argument provided by the test runner to access
// the full context API.
const person = await context.query.Person.createOne({
data: { name: 'Alice', email: 'alice@example.com', password: 'super-secret' },
query: 'id name email password { isSet }',
});
expect(person.name).toEqual('Alice');
expect(person.email).toEqual('alice@example.com');
expect(person.password.isSet).toEqual(true);
})
);
test(
'Create a Person using a hand-crafted GraphQL query sent over HTTP',
runner(async ({ graphQLRequest }) => {
// We can use the graphQLRequest argument provided by the test runner
// to execute HTTP requests to our GraphQL API and get a supertest
// "Test" object back. https://github.com/visionmedia/supertest
const { body } = await graphQLRequest({
query: `mutation {
createPerson(data: { name: "Alice", email: "alice@example.com", password: "super-secret" }) {
id name email password { isSet }
}
}`,
}).expect(200);
const person = body.data.createPerson;
expect(person.name).toEqual('Alice');
expect(person.email).toEqual('alice@example.com');
expect(person.password.isSet).toEqual(true);
})
);
test(
'Check that trying to create user with no name (required field) fails',
runner(async ({ context }) => {
// The context.graphql.raw API is useful when we expect to recieve an
// error from an operation.
const { data, errors } = await context.graphql.raw({
query: `mutation {
createPerson(data: { email: "alice@example.com", password: "super-secret" }) {
id name email password { isSet }
}
}`,
});
expect(data!.createPerson).toBe(null);
expect(errors).toHaveLength(1);
expect(errors![0].path).toEqual(['createPerson']);
expect(errors![0].message).toEqual(
'You provided invalid data for this operation.\n - Person.name: Name must not be empty'
);
})
);
test(
'Check access control by running updateTask as a specific user via context.withSession()',
runner(async ({ context }) => {
// We can modify the value of context.session via context.withSession() to masquerade
// as different logged in users. This allows us to test that our access control rules
// are behaving as expected.
// Create some users
const [alice, bob] = await context.query.Person.createMany({
data: [
{ name: 'Alice', email: 'alice@example.com', password: 'super-secret' },
{ name: 'Bob', email: 'bob@example.com', password: 'super-secret' },
],
query: 'id name',
});
expect(alice.name).toEqual('Alice');
expect(bob.name).toEqual('Bob');
// Create a task assigned to Alice
const task = await context.query.Task.createOne({
data: {
label: 'Experiment with Keystone',
priority: 'high',
isComplete: false,
assignedTo: { connect: { id: alice.id } },
},
query: 'id label priority isComplete assignedTo { name }',
});
expect(task.label).toEqual('Experiment with Keystone');
expect(task.priority).toEqual('high');
expect(task.isComplete).toEqual(false);
expect(task.assignedTo.name).toEqual('Alice');
// Check that we can't update the task (not logged in)
{
const { data, errors } = await context.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
id
}
}`,
variables: { id: task.id },
});
expect(data!.updateTask).toBe(null);
expect(errors).toHaveLength(1);
expect(errors![0].path).toEqual(['updateTask']);
expect(errors![0].message).toEqual(
`Access denied: You cannot perform the 'update' operation on the item '{"id":"${task.id}"}'. It may not exist.`
);
}
{
// Check that we can update the task when logged in as Alice
const { data, errors } = await context
.withSession({ itemId: alice.id, data: {} })
.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
id
}
}`,
variables: { id: task.id },
});
expect(data!.updateTask.id).toEqual(task.id);
expect(errors).toBe(undefined);
}
// Check that we can't update the task when logged in as Bob
{
const { data, errors } = await context
.withSession({ itemId: bob.id, data: {} })
.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
id
}
}`,
variables: { id: task.id },
});
expect(data!.updateTask).toBe(null);
expect(errors).toHaveLength(1);
expect(errors![0].path).toEqual(['updateTask']);
expect(errors![0].message).toEqual(
`Access denied: You cannot perform the 'update' operation on the item '{"id":"${task.id}"}'. It may not exist.`
);
}
})
);
});
describe('Example tests using test environment', () => {
// The test runner provided by setupTestRunner will drop all the data from the
// database and then provide a fresh connection for every test.
//
// If we want to use the same database for multiple tests, without deleting data
// between each test, we can use setupTestEnv in our `beforeAll()` block and
// manage the connection and disconnection ourselves.
//
// This gives us the opportunity to seed test data once up front and use it in
// multiple tests.
let testEnv: TestEnv, context: KeystoneContext;
let person: { id: string };
beforeAll(async () => {
testEnv = await setupTestEnv({ config });
context = testEnv.testArgs.context;
await testEnv.connect();
// Create a person in the database to be used in multiple tests
person = (await context.query.Person.createOne({
data: { name: 'Alice', email: 'alice@example.com', password: 'super-secret' },
})) as { id: string };
});
afterAll(async () => {
await testEnv.disconnect();
});
test('Check that the persons password is set', async () => {
const { password } = await context.query.Person.findOne({
where: { id: person.id },
query: 'password { isSet }',
});
expect(password.isSet).toEqual(true);
});
test('Update the persons email address', async () => {
const { email } = await context.query.Person.updateOne({
where: { id: person.id },
data: { email: 'new-email@example.com' },
query: 'email',
});
expect(email).toEqual('new-email@example.com');
});
});