forked from 4ctarus/integration-nestjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.e2e-spec.ts
267 lines (239 loc) · 8.47 KB
/
app.e2e-spec.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
import { Repository } from 'typeorm';
import { UserEntity } from '../src/user/user.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import { EmailEntity } from '../src/email/email.entity';
const knownUserId = '0f9fcea9-f618-44e5-b182-0e3c83586f8b';
const knownUser = {
id: knownUserId,
name: 'Moi Même',
status: 'active',
birthdate: new Date(1989, 3, 8).toISOString(),
emails: [
{
userId: knownUserId,
id: 'f7176922-9ae8-4ac7-b1d9-d6b8ed75475b',
address: '[email protected]',
},
{
userId: knownUserId,
id: 'f6035e0a-25b4-40a9-818d-cb7420495d26',
address: '[email protected]',
},
{
userId: knownUserId,
id: '1983ee1f-e64e-4dfd-9a28-ee4827d68993',
address: '[email protected]',
},
],
};
const [email1, email2, email3] = knownUser.emails;
describe('Tests e2e', () => {
let app: INestApplication;
let userRepo: Repository<UserEntity>;
let emailRepo: Repository<EmailEntity>;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.useGlobalPipes(new ValidationPipe());
userRepo = moduleFixture.get<Repository<UserEntity>>(
getRepositoryToken(UserEntity),
);
emailRepo = moduleFixture.get<Repository<EmailEntity>>(
getRepositoryToken(EmailEntity),
);
const { emails, ...user } = knownUser;
await userRepo.insert(user);
await emailRepo.insert(emails);
await app.init();
});
afterEach(async () => {
await emailRepo.query(`DELETE FROM emails`);
await userRepo.query(`DELETE FROM users`);
});
describe('UserResolver', () => {
describe('[Query] user', () => {
it(`[01] Devrait retourner l'utilisateur connu en base de données`, () => {
const user = {
id: knownUserId,
name: knownUser.name,
birthdate: knownUser.birthdate,
};
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{user(userId:"${knownUserId}"){id name birthdate}}`,
})
.expect(200)
.expect((res) => {
expect(res.body.data.user).toStrictEqual(user);
});
});
it.skip(`[02] Devrait pouvoir résoudre les e-mails de l'utilisateur`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{user(userId:"${knownUserId}"){id emails{id address}}}`,
})
.expect(200)
.expect((res) => {
expect(res.body.errors?.[0]?.message).not.toBe(
'Cannot return null for non-nullable field UserEmail.id.',
);
expect(res.body.data.user.emails.length).toBe(3);
});
});
it.skip(`[03] Devrait pouvoir résoudre les e-mails avec les filtres`, () => {
const { userId, ...email } = email1;
const knownUser = {
id: knownUserId,
emails: [email],
};
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{user(userId:"${knownUser.id}"){id emails(address:{equal: "${email.address}"}){id address}}}`,
})
.expect(200)
.expect((res) => {
expect(res.body.data.user).toStrictEqual(knownUser);
});
});
it(`[04] Devrait retourner une erreur de validation si aucun identifiant d'utilisateur n'est défini`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{user(userId:""){id}}`,
})
.expect(200)
.expect((res) => {
expect(
res.body.errors?.[0].extensions?.originalError?.message,
).toContain("L'identifiant de l'utilisateur doit être défini");
});
});
it(`[05] Devrait retourner une erreur de validation si l'identifiant de l'utilisateur n'est pas un UUID`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{user(userId:"NotAnUUID"){id}}`,
})
.expect(200)
.expect((res) => {
expect(
res.body.errors?.[0]?.extensions?.originalError?.message,
).toContain("L'identifiant de l'utilisateur doit être un UUID");
});
});
it.skip(`[06] Devrait retourner les emails égaux tout comme les emails contenus`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{user(userId:"${knownUserId}"){
emails(address: {
equal: "${email2.address}",
in: ["${email1.address}", "${email3.address}"]
}){id}}}`,
})
.expect(200)
.expect((res) => {
expect(res.body.data.user.emails.length).toBe(3);
});
});
});
describe('[Mutation] addUser', () => {
it(`[07] Devrait ajouter un utilisateur`, () => {
const date = new Date(2001, 11, 12).toISOString();
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `mutation {addUser(name:"User Name", birthdate: "${date}")}`,
})
.expect(200)
.expect((res) => {
expect(res.body.data.addUser).toBeDefined();
});
});
it.skip(`[08] Devrait retourner une erreur de validation si le nom n'est pas défini`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `mutation {addUser(name:"")}`,
})
.expect(200)
.expect((res) => {
expect(
res.body.errors?.[0].extensions?.originalError?.message,
).toContain("Le nom de l'utilisateur n'est pas défini");
});
});
it.skip(`[09] Devrait retourner une erreur de validation si la date de naissance est définie dans le future`, () => {
const date = new Date(2050, 1, 1).toISOString();
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `mutation {addUser(name:"User Name" birthdate:"${date}")}`,
})
.expect(200)
.expect((res) => {
expect(
res.body.errors?.[0]?.extensions?.originalError?.message,
).toContain(
'La date de naissance ne peut pas être définie dans le future',
);
});
});
});
});
describe('EmailResolver', () => {
describe('[Query] email', () => {
it.skip(`[10] Devrait retourner l'email connu en base de données`, () => {
const { userId, ...email } = email3;
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{email(emailId:"${email.id}"){id address}}`,
})
.expect(200)
.expect((res) => {
expect(res.body.errors?.[0]?.message).not.toBe('Not Implemented');
expect(res.body.data?.email).toStrictEqual(email);
});
});
});
describe('[Query] emailsList', () => {
it.skip(`[11] Devrait retourner les emails correspondants aux filtres`, () => {
const { userId, ...email } = email2;
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{emailsList(address: {equal: "${email.address}"}){id address}}`,
})
.expect(200)
.expect((res) => {
expect(res.body.errors?.[0]?.message).not.toBe('Not Implemented');
expect(res.body.data?.emailsList[0]).toStrictEqual(email);
});
});
});
describe('[FieldResolver] user', () => {
it.skip(`[12] Devrait retourner l'utilisateur de l'email`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
query: `{emailsList(address:{equal:"${email1.address}"}){user{id}}}`,
})
.expect(200)
.expect((res) => {
expect(res.body.errors?.[0]?.message).not.toBe('Not Implemented');
expect(res.body.data?.emailsList[0].user.id).toBe(knownUserId);
});
});
});
});
});