forked from GSG-G8/ca-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalumni.test.js
371 lines (352 loc) · 11.1 KB
/
alumni.test.js
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
const request = require('supertest');
const connection = require('../server/database/connection');
const dbBuild = require('../server/database/data/build');
const app = require('../server/app');
const token =
'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTg2MDU0MjM1fQ.ANOdUJz-kWK-m9hdOc4Ee-NA4bx_VaRK-pxehp399G8';
beforeEach(() => dbBuild());
afterAll(() => connection.end());
describe('Alumni, Get all students', () => {
test('Route /alumni status 200, json header, data[0].name = Alaa ', (done) => {
return request(app)
.get('/api/v1/alumni')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { data } = res.body;
expect(data[0].name).toBe('Alaa');
expect(data).toHaveLength(12);
done();
});
});
});
describe('Get student by id', () => {
test('Route /alumni/1 status 200, json header, data.name = Alaa ', (done) => {
return request(app)
.get('/api/v1/alumni/1')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const {
data: { name },
} = res.body;
expect(name).toBe('Alaa');
done();
});
});
test('Route /alumni/8 status 200, json header ', (done) => {
return request(app)
.get('/api/v1/alumni/18')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('There is no student for this id');
done();
});
});
test('Route /alumni/Alaa status 404, json header ', (done) => {
return request(app)
.get('/api/v1/alumni/Alaa')
.expect(404)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('Invalid id');
done();
});
});
});
describe('Put Student By Id', () => {
const validData = {
name: 'Rehab',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
cohortId: 1,
};
const invalidData = {
name: 'Rehab',
email: 'email',
imgUrl: 'img url',
githubLink: 'github link',
cohortId: 1,
};
const duplicateData = {
name: 'Rehab',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
cohortId: 1,
};
test('PUT Route /alumni/1 status 200, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/1')
.set('Cookie', token)
.send(validData)
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
const { rows } = await connection.query(
'SELECT * from student WHERE id = 1',
);
expect(rows).toHaveLength(1);
expect(rows[0]).toEqual({
id: 1,
name: 'Rehab',
email: '[email protected]',
img_url:
'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
github_link: 'https://github.com/rehabas',
cohort_id: 1,
});
expect(message).toBe("Student's data updated successfully");
done();
});
});
test('PUT Route /alumni/1 status 200, json header, put data with same email', (done) => {
request(app)
.put('/api/v1/alumni/1')
.set('Cookie', token)
.send({
name: 'sara',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
cohortId: 1,
})
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
const { rows } = await connection.query(
'SELECT * from student WHERE id = 1',
);
expect(rows).toHaveLength(1);
expect(rows[0]).toEqual({
id: 1,
name: 'sara',
email: '[email protected]',
img_url:
'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
github_link: 'https://github.com/rehabas',
cohort_id: 1,
});
expect(message).toBe("Student's data updated successfully");
done();
});
});
test('PUT Route /alumni/13 status 404, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/13')
.set('Cookie', token)
.send(validData)
.expect(404)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
const { rows } = await connection.query(
'SELECT * from student WHERE id = 13',
);
expect(rows).toHaveLength(0);
expect(message).toBe('There is no student for this id');
done();
});
});
test('PUT Route /alumni/1 status 400, json header, put invalid data ', (done) => {
request(app)
.put('/api/v1/alumni/1')
.set('Cookie', token)
.send(invalidData)
.expect(400)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
await connection.query('SELECT * from student WHERE id = 1');
expect(message).toEqual([
'email must be a valid email',
'imgUrl must be a valid URL',
'githubLink must be a valid URL',
]);
done();
});
});
test('PUT Route /alumni/1 status 409, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/1')
.set('Cookie', token)
.send(duplicateData)
.expect(409)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
expect(message).toBe(
`Key (email)=(${duplicateData.email}) already exists.`,
);
done();
});
});
});
describe('Delete specific student by ID', () => {
test('Route /alumni/1 status 200, data.message = Student deleted successfully ', (done) => {
return request(app)
.delete('/api/v1/alumni/1')
.set('Cookie', token)
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
const { message } = res.body.data;
if (err) return done(err);
const { rows } = await connection.query(
'SELECT * from student WHERE id = 1',
);
expect(rows).toHaveLength(0);
expect(message).toBe('Student deleted successfully');
done();
});
});
test('Route /alumni/13 status 404, data.message = Student does not exist ', (done) => {
return request(app)
.delete('/api/v1/alumni/13')
.set('Cookie', token)
.expect(404)
.expect('Content-Type', /json/)
.end(async (err, res) => {
const { message } = res.body.data;
if (err) return done(err);
expect(message).toBe('Student does not exist');
done();
});
});
test('Route /alumni/Alaa status 404, data.message = You enterd wrong student ID ', (done) => {
return request(app)
.delete('/api/v1/alumni/Alaa')
.set('Cookie', token)
.expect(404)
.expect('Content-Type', /json/)
.end(async (err, res) => {
const { message } = res.body.data;
if (err) return done(err);
expect(message).toBe('You enterd wrong student ID');
done();
});
});
});
describe('Get student projects', () => {
test('Route /alumni/1/projects status 200, json header, data ', (done) => {
return request(app)
.get('/api/v1/alumni/1/projects')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { data } = res.body;
expect(data[0]).toEqual({
id: 1,
name: 'ca-wiki',
description:
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
img_url: 'https://imgur.com/gVwD2Wi.png',
github_link:
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
website_link:
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D',
project_type: 'internal',
cohort_id: 1,
student_id: 1,
project_id: 1,
});
done();
});
});
test('Route /alumni/18/projects status 200, json header ', (done) => {
return request(app)
.get('/api/v1/alumni/18/projects')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('There is no projects for this student id');
done();
});
});
test('Route /alumni/Alaa/projects status 404, json header ', (done) => {
return request(app)
.get('/api/v1/alumni/Alaa/projects')
.expect(404)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('Invalid id');
done();
});
});
});
describe('Assign projects to student', () => {
test('Route /alumni/projects/assign status 200, json header, data.message = Project Assigned successfully ', (done) => {
const reqData = {
projectId: 1,
student1Id: 1,
student2Id: 2,
student3Id: 3,
student4Id: 4,
};
return request(app)
.post('/api/v1/alumni/projects/assign')
.set('Cookie', token)
.send(reqData)
.expect(201)
.expect('Content-Type', /json/)
.end(async (err, res) => {
const { message } = res.body.data;
if (err) return done(err);
expect(message).toBe('Project Assigned successfully');
done();
});
});
test('Route /alumni/projects/assign status 400, json header, data.message = array of errors ', (done) => {
const wrongData = {
projectId: 1,
student1Id: 1,
student2Id: 2,
student3Id: 3,
student4Id: 'Rana',
};
return request(app)
.post('/api/v1/alumni/projects/assign')
.set('Cookie', token)
.send(wrongData)
.expect(400)
.expect('Content-Type', /json/)
.end(async (err, res) => {
const { message } = res.body.data;
if (err) return done(err);
expect(message).toEqual([
'student4Id must be a `number` type, but the final value was: `NaN` (cast from the value `"Rana"`).',
]);
done();
});
});
});