-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.test.js
More file actions
430 lines (365 loc) · 15.3 KB
/
api.test.js
File metadata and controls
430 lines (365 loc) · 15.3 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// TESTS FOR GENERAL USER CREATION / JWT
// USE npm install --save-dev jest supertest
// npx jest (after setting up server)
const request = require("supertest");
const BASE_URL = `http://localhost:${process.env.PORT || 3000}`;
let adminAccessToken;
let userAccessToken;
let userRefreshToken;
describe("JWT and permissions API tests", () => {
const regularUsername =
"regular_user_" + Math.random().toString(36).substring(2, 15);
const adminUsername =
"regular_user_" + Math.random().toString(36).substring(2, 15);
const testUsername =
"test_user_" + Math.random().toString(36).substring(2, 15);
const reg_email = "user"+regularUsername+"@example.com"
const test_email = "user"+testUsername+"@example.com"
const admin_email = "user"+adminUsername+"@example.com"
beforeAll(async () => {
// Create an admin user and a regular user for testing
// await request(BASE_URL).post("/api/accounts/register").send({
// username: adminUsername,
// password: "adminPass123$",
// role: "ADMIN",
// email: admin_email,
// });
await request(BASE_URL).post("/api/accounts/register").send({
username: regularUsername,
password: "userPass123&",
role: "USER",
email: reg_email,
});
// Log in as admin and user to obtain tokens
const adminLoginResponse = await request(BASE_URL)
.post("/api/accounts/login")
.send({
username: "SUDOMASTER",
password: "SUDOMaSTER123$$$",
email: "SUDOMASTER@MASTER.com",
});
adminAccessToken = adminLoginResponse.body.accessToken;
const userLoginResponse = await request(BASE_URL)
.post("/api/accounts/login")
.send({
username: regularUsername,
password: "userPass123&",
email: reg_email,
});
userAccessToken = userLoginResponse.body.accessToken;
userRefreshToken = userLoginResponse.body.refreshToken;
});
describe("1: User registration", () => {
it("should create a new user", async () => {
const response = await request(BASE_URL)
.post("/api/accounts/register")
.send({
username: testUsername,
password: "testPass123*",
role: "USER",
email: test_email,
output_bool: true,
});
expect(response.status).toBe(201);
expect(response.body.user).toHaveProperty("id");
expect(response.body.user.username).toBe(testUsername);
});
it("should fail to create a user with an existing username", async () => {
const response = await request(BASE_URL)
.post("/api/accounts/register")
.send({
username: testUsername,
password: "anotherPass123(",
email: reg_email,
role: "USER",
});
expect(response.status).toBe(400);
expect(response.body).toHaveProperty("error");
});
});
describe("2: User login and JWT generation", () => {
it("should log in and return a JWT", async () => {
const response = await request(BASE_URL).post("/api/accounts/login").send({
username: testUsername,
password: "testPass123*",
email: test_email,
});
expect(response.status).toBe(200);
expect(response.body).toHaveProperty("accessToken");
});
it("should fail to log in with incorrect credentials", async () => {
const response = await request(BASE_URL).post("/api/accounts/login").send({
username: testUsername,
password: "wrongPass123(",
email: test_email,
});
expect(response.status).toBe(401);
expect(response.body).toHaveProperty("error");
});
});
describe("3: Protecting routes with JWT middleware", () => {
it("should allow access to protected route with valid token", async () => {
const response = await request(BASE_URL)
.get("/api/protected")
.set("authorization", `Bearer ${adminAccessToken}`);
expect(response.status).toBe(200);
});
it("should deny access to protected route with no token", async () => {
const response = await request(BASE_URL).get("/api/protected");
expect(response.status).toBe(401);
});
it("should deny access to protected route with invalid token", async () => {
const response = await request(BASE_URL)
.get("/api/protected")
.set("authorization", `Bearer invalidToken`);
expect(response.status).toBe(401);
});
});
describe("4: Role-based permissions", () => {
it("should allow access to admin route with ADMIN role", async () => {
const response = await request(BASE_URL)
.get("/api/admin/protected")
.set("authorization", `Bearer ${adminAccessToken}`);
expect(response.status).toBe(200);
});
it("should deny access to admin route with USER role", async () => {
const response = await request(BASE_URL)
.get("/api/admin/protected")
.set("authorization", `Bearer ${userAccessToken}`);
expect(response.status).toBe(403);
});
});
describe("5: Refreshing JWTs", () => {
it("should refresh the token", async () => {
const refreshResponse = await request(BASE_URL)
.post("/api/accounts/refresh")
.send({
refreshToken: userRefreshToken,
});
expect(refreshResponse.status).toBe(200);
expect(refreshResponse.body).toHaveProperty("accessToken");
});
it("should deny refresh with expired or invalid token", async () => {
const refreshResponse = await request(BASE_URL)
.post("/api/accounts/refresh")
.send({
refreshToken: "expiredOrInvalidToken",
});
expect(refreshResponse.status).toBe(401);
expect(refreshResponse.body).toHaveProperty("error");
});
});
});
describe("API Tests for users", () => {
const regularUsername =
"regular_user_to_be_deleted" + Math.random().toString(36).substring(2, 15);
const adminUsername =
"regular_user_" + Math.random().toString(36).substring(2, 15);
const testUsername =
"test_user_" + Math.random().toString(36).substring(2, 15);
const email = "user@example"+Math.random().toString(36).substring(2, 15) +".com"
describe("6: Admin Account creation", () => {
it("should create a new ADMIN user", async () => {
const response = await request(BASE_URL).post("/api/admin/admin_register").set("authorization", `Bearer ${adminAccessToken}`).send({
username: regularUsername,
password: "myPass123&&",
email: email,
firstName: "Henry",
lastName: "Chen",
avatar: "https://henrytchen.com/images/Profile3_compressed.jpg",
phoneNumber: "123+456+7899",
role: "ADMIN",
output_bool: true,
});
expect(response.status).toBe(201);
expect(response.body.user).toHaveProperty("id");
expect(response.body.user).toHaveProperty("createdAt");
expect(response.body.user.username).toBe(regularUsername);
expect(response.body.user.email).toBe(email);
expect(response.body.user.firstName).toBe("Henry");
expect(response.body.user.lastName).toBe("Chen");
expect(response.body.user.avatar).toBe("https://henrytchen.com/images/Profile3_compressed.jpg");
expect(response.body.user.phoneNumber).toBe("123+456+7899");
expect(response.body.user.role).toBe("ADMIN");
});
it("should fail to create a user with an existing username", async () => {
const response = await request(BASE_URL).post("/api/admin/admin_register").set("authorization", `Bearer ${adminAccessToken}`).send({
username: regularUsername,
password: "myPass123&&",
email: email,
firstName: "Henry",
lastName: "Chen",
avatar: "https://henrytchen.com/images/Profile3_compressed.jpg",
phoneNumber: "123+456+7899",
role: "ADMIN",
});
expect(response.status).toBe(400);
expect(response.body).toHaveProperty("error");
});
});
describe("7: Retrieve User", () => {
it("should retrieve specified user", async () => {
const response = await request(BASE_URL).get("/api/accounts/users").send({
username: regularUsername,
firstName_bool: true,
lastName_bool: true,
email_bool: true,
avatar_bool: true,
phoneNumber_bool: true,
role_bool: true,
createdAt_bool: true,
});
expect(response.status).toBe(200);
expect(response.body[0]).toHaveProperty("username");
expect(response.body[0]).toHaveProperty("createdAt");
// expect(response.body[0]).toHaveProperty("updatedAt");
expect(response.body[0].username).toBe(regularUsername);
expect(response.body[0].email).toBe(email);
expect(response.body[0].firstName).toBe("Henry");
expect(response.body[0].lastName).toBe("Chen");
expect(response.body[0].avatar).toBe("https://henrytchen.com/images/Profile3_compressed.jpg");
expect(response.body[0].phoneNumber).toBe("123+456+7899");
expect(response.body[0].role).toBe("ADMIN");
});
it("should fail to retrieve a non-existent username", async () => {
const response = await request(BASE_URL).get("/api/accounts/users").send({
username: "JEE",
});
expect(response.status).toBe(200);
expect(response.body.message).toBe("No users could be found!");
});
// it("should retrieve users filtered by firstName", async () => {
// const response = await request(BASE_URL).get(
// "/api/users?firstName=John"
// );
// expect(response.status).toBe(200);
// expect(response.body.length).toBeGreaterThan(0);
// expect(response.body[0].firstName).toBe("John");
// });
// it("should retrieve books filtered by title", async () => {
// const response = await request(BASE_URL).get(
// "/api/books?title=JavaScript"
// );
// expect(response.status).toBe(200);
// expect(response.body.length).toBeGreaterThan(0);
// expect(response.body[0].title).toContain("JavaScript");
// });
// it("should retrieve books, filtered by userId", async () => {
// const responseAllBooks = await request(BASE_URL).get("/api/books");
// expect(responseAllBooks.status).toBe(200);
// expect(responseAllBooks.body.length).toBeGreaterThan(0);
// const responseFilteredBooks = await request(BASE_URL).get(
// `/api/books?userId=${username}`
// );
// expect(responseFilteredBooks.status).toBe(200);
// expect(responseFilteredBooks.body.length).toBeGreaterThan(0);
// expect(responseFilteredBooks.body[0].userId).toBe(username);
// });
});
describe("8: Updating an existing user using ADMIN PERMS", () => {
it("should update an existing user", async () => {
// const response = await request(BASE_URL)
// .put(`/api/users/${username}`)
// .send({
// firstName: "Jane",
// bio: "Updated bio",
// });
// expect(response.status).toBe(200);
// expect(response.body.firstName).toBe("Jane");
// expect(response.body.bio).toBe("Updated bio");
const response = await request(BASE_URL).put("/api/admin/admin_users").set("authorization", `Bearer ${adminAccessToken}`).send({
username: regularUsername,
password: "myUpdatedPass123&",
email: "updated" + email,
firstName: "HE",
lastName: "CC",
avatar: "UPDATEDhttps://henrytchen.com/images/Profile3_compressed.jpg",
phoneNumber: "122+456+7899",
role: "USER",
});
expect(response.status).toBe(201);
expect(response.body.updated_user).toHaveProperty("id");
expect(response.body.updated_user).toHaveProperty("createdAt");
expect(response.body.updated_user.username).toBe(regularUsername);
expect(response.body.updated_user.email).toBe("updated" + email);
expect(response.body.updated_user.firstName).toBe("HE");
expect(response.body.updated_user.lastName).toBe("CC");
expect(response.body.updated_user.avatar).toBe("UPDATEDhttps://henrytchen.com/images/Profile3_compressed.jpg");
expect(response.body.updated_user.phoneNumber).toBe("122+456+7899");
expect(response.body.updated_user.role).toBe("USER");
});
it("should update an user with partial data using ADMIN PERMS", async () => {
// const response = await request(BASE_URL)
// .put(`/api/users/${username}`)
// .send({});
// expect(response.status).toBe(200);
// expect(response.body.firstName).toBe("Jane");
const response = await request(BASE_URL).put("/api/admin/admin_users").set("authorization", `Bearer ${adminAccessToken}`).send({
username: regularUsername,
firstName: "HERR",
});
expect(response.status).toBe(201);
expect(response.body.updated_user).toHaveProperty("id");
expect(response.body.updated_user).toHaveProperty("createdAt");
expect(response.body.updated_user.username).toBe(regularUsername);
expect(response.body.updated_user.email).toBe("updated" + email);
expect(response.body.updated_user.firstName).toBe("HERR");
expect(response.body.updated_user.lastName).toBe("CC");
expect(response.body.updated_user.avatar).toBe("UPDATEDhttps://henrytchen.com/images/Profile3_compressed.jpg");
expect(response.body.updated_user.phoneNumber).toBe("122+456+7899");
expect(response.body.updated_user.role).toBe("USER");
});
// it("should update an existing book", async () => {
// const response = await request(BASE_URL)
// .put(`/api/books/${bookId}`)
// .send({
// title: "Advanced JavaScript",
// available: false,
// });
// expect(response.status).toBe(200);
// expect(response.body.title).toBe("Advanced JavaScript");
// expect(response.body.available).toBe(false);
// });
// it("should fail to update a book with a duplicate ISBN", async () => {
// const newIsbn =
// "isbn-" +
// Math.round(Math.random() * 10000).toString() +
// Math.round(Math.random() * 10000).toString();
// // First, create another book
// const anotherBookResponse = await request(BASE_URL)
// .post("/api/books")
// .send({
// title: "Another Book",
// isbn: newIsbn,
// publishedDate: "2024-01-01T00:00:00.000Z",
// available: true,
// userId: username,
// });
// expect(anotherBookResponse.status).toBe(201);
// // Attempt to update the first book's ISBN to the same as the second book
// const response = await request(BASE_URL)
// .put(`/api/books/${bookId}`)
// .send({
// isbn: newIsbn, // Duplicate ISBN
// });
// expect(response.status).toBe(400); // Assuming your API returns 400 for duplicate ISBN
// });
});
describe("9: Delete USER using ADMIN PERMS", () => {
it("should soft delete a user", async () => {
// Delete the user
const deleteuserResponse = await request(BASE_URL).delete("/api/admin/admin_users").set("authorization", `Bearer ${adminAccessToken}`).send({
username: regularUsername,
});
expect(deleteuserResponse.status).toBe(200);
expect(deleteuserResponse.body.message).toBe(
"User deleted successfully"
);
// // Attempt to retrieve the book (should fail or return empty)
// const response = await request(BASE_URL).get(
// `/api/books?userId=${username}`
// );
// expect(response.body.length).toBe(0);
});
});
});