|
1 |
| -import app from "../app"; |
2 |
| -import request from "supertest"; |
3 |
| -import { connectionToDatabase } from "../database/config/db.config"; |
4 |
| -import { deleteTableData } from "../utils/database.utils"; |
5 |
| -import { User } from "../database/models/User"; |
6 |
| -import { |
7 |
| - login_user, |
8 |
| - login_user_invalid_email, |
9 |
| - login_user_wrong_credentials, |
10 |
| - register_user, |
11 |
| -} from "../mock/static"; |
12 |
| - |
13 |
| -jest.setTimeout(30000); |
14 |
| - |
15 |
| -function logErrors( |
16 |
| - err: { stack: any }, |
17 |
| - _req: any, |
18 |
| - _res: any, |
19 |
| - next: (arg0: any) => void |
20 |
| -) { |
21 |
| - console.log(err.stack); |
22 |
| - next(err); |
23 |
| -} |
24 |
| - |
25 |
| -const Jest_request = request(app.use(logErrors)); |
26 |
| - |
27 |
| -describe("USER API TEST", () => { |
28 |
| - beforeAll(async () => { |
29 |
| - await connectionToDatabase(); |
30 |
| - }); |
31 |
| - |
32 |
| - afterAll(async () => { |
33 |
| - await deleteTableData(User, "users"); |
34 |
| - }); |
35 |
| - |
36 |
| - it("should create a new user", async () => { |
37 |
| - // Your test implementation goes here |
38 |
| - const { body } = await Jest_request.post("/api/v1/users/register") |
39 |
| - .send(register_user) |
40 |
| - .expect(201); |
41 |
| - }); |
42 |
| - |
43 |
| - /** |
44 |
| - * ---------------------------- LOGIN -------------------------------------------- |
45 |
| - */ |
46 |
| - |
47 |
| - it("should successfully login a user and return 200", async () => { |
48 |
| - const { body } = await Jest_request.post("/api/v1/users/login") |
49 |
| - .send(login_user) |
50 |
| - .expect(200); |
51 |
| - expect(body.status).toStrictEqual("SUCCESS"); |
52 |
| - expect(body.message).toStrictEqual( |
53 |
| - "Logged in to your account successfully!" |
54 |
| - ); |
55 |
| - expect(body.token).toBeDefined(); |
56 |
| - }); |
57 |
| - |
58 |
| - it("should return 404 when a user login with wrong credentials", async () => { |
59 |
| - const { body } = await Jest_request.post("/api/v1/users/login") |
60 |
| - .send(login_user_wrong_credentials) |
61 |
| - .expect(404); |
62 |
| - expect(body.status).toStrictEqual("NOT FOUND"); |
63 |
| - expect(body.message).toStrictEqual("Wrong credentials!"); |
64 |
| - }); |
65 |
| - |
66 |
| - it("should return 400 when a user user enter invalid email (login validation purposes)", async () => { |
67 |
| - const { body } = await Jest_request.post("/api/v1/users/login") |
68 |
| - .send(login_user_invalid_email) |
69 |
| - .expect(400); |
70 |
| - expect(body.status).toStrictEqual("BAD REQUEST"); |
71 |
| - expect(body.message).toStrictEqual("Invalid email!"); |
72 |
| - }); |
73 |
| - |
74 |
| - it("should return 400 when request body is invalid", async () => { |
75 |
| - const { body } = await Jest_request.post("/api/v1/users/login") |
76 |
| - .send({}) |
77 |
| - .expect(400); |
78 |
| - expect(body.status).toStrictEqual("BAD REQUEST"); |
79 |
| - expect(body.message).toBeDefined(); |
80 |
| - }) |
81 |
| -}); |
| 1 | +import app from "../app"; |
| 2 | +import request from "supertest"; |
| 3 | +import { connectionToDatabase } from "../database/config/db.config"; |
| 4 | +import { deleteTableData } from "../utils/database.utils"; |
| 5 | +import { User } from "../database/models/User"; |
| 6 | +import { |
| 7 | + login_user, |
| 8 | + login_user_invalid_email, |
| 9 | + login_user_wrong_credentials, |
| 10 | + NewUser, |
| 11 | + user_bad_request, |
| 12 | +} from "../mock/static"; |
| 13 | + |
| 14 | +jest.setTimeout(30000); |
| 15 | + |
| 16 | +function logErrors( |
| 17 | + err: { stack: any }, |
| 18 | + _req: any, |
| 19 | + _res: any, |
| 20 | + next: (arg0: any) => void, |
| 21 | +) { |
| 22 | + console.log(err.stack); |
| 23 | + next(err); |
| 24 | +} |
| 25 | + |
| 26 | +const Jest_request = request(app.use(logErrors)); |
| 27 | + |
| 28 | +describe("USER API TEST", () => { |
| 29 | + beforeAll(async () => { |
| 30 | + await connectionToDatabase(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterAll(async () => { |
| 34 | + await deleteTableData(User, "users"); |
| 35 | + }); |
| 36 | + it("it should register a user and return 201", async () => { |
| 37 | + const { body } = await Jest_request.post("/api/v1/users/register") |
| 38 | + .send(NewUser) |
| 39 | + .expect(201); |
| 40 | + expect(body.status).toStrictEqual("SUCCESS"); |
| 41 | + expect(body.message).toStrictEqual("Account Created successfully!"); |
| 42 | + expect(body.token).toBeDefined(); |
| 43 | + }); |
| 44 | + it("it should return a user not found and status 400", async () => { |
| 45 | + const { body } = await Jest_request.post("/api/v1/users/register") |
| 46 | + .send(user_bad_request) |
| 47 | + .expect(400); |
| 48 | + }); |
| 49 | + |
| 50 | + it("it should return a user exist and status 409 when Email is already used in database", async () => { |
| 51 | + const { body } = await Jest_request.post("/api/v1/users/register") |
| 52 | + .send(NewUser) |
| 53 | + .expect(409); |
| 54 | + expect(body.status).toStrictEqual("CONFLICT"); |
| 55 | + expect(body.message).toStrictEqual("User already exist!"); |
| 56 | + }); |
| 57 | + |
| 58 | + /** |
| 59 | + * ---------------------------- LOGIN -------------------------------------------- |
| 60 | + */ |
| 61 | + |
| 62 | + it("should successfully login a user and return 200", async () => { |
| 63 | + const { body } = await Jest_request.post("/api/v1/users/login") |
| 64 | + .send(login_user) |
| 65 | + .expect(200); |
| 66 | + expect(body.status).toStrictEqual("SUCCESS"); |
| 67 | + expect(body.message).toStrictEqual( |
| 68 | + "Logged in to your account successfully!", |
| 69 | + ); |
| 70 | + expect(body.token).toBeDefined(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should return 404 when a user login with wrong credentials", async () => { |
| 74 | + const { body } = await Jest_request.post("/api/v1/users/login") |
| 75 | + .send(login_user_wrong_credentials) |
| 76 | + .expect(404); |
| 77 | + expect(body.status).toStrictEqual("NOT FOUND"); |
| 78 | + expect(body.message).toStrictEqual("Wrong credentials!"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should return 400 when a user user enter invalid email (login validation purposes)", async () => { |
| 82 | + const { body } = await Jest_request.post("/api/v1/users/login") |
| 83 | + .send(login_user_invalid_email) |
| 84 | + .expect(400); |
| 85 | + expect(body.status).toStrictEqual("BAD REQUEST"); |
| 86 | + expect(body.message).toStrictEqual("Invalid email!"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should return 400 when request body is invalid", async () => { |
| 90 | + const { body } = await Jest_request.post("/api/v1/users/login") |
| 91 | + .send({}) |
| 92 | + .expect(400); |
| 93 | + expect(body.status).toStrictEqual("BAD REQUEST"); |
| 94 | + expect(body.message).toBeDefined(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments