|
1 | 1 | require("dotenv").config(); |
2 | 2 | const supertest = require("supertest"); |
3 | | -const mongoose = require("mongoose") |
4 | | -const jwt = require('jsonwebtoken'); |
5 | | -const app = require("../server.js"); // Replace with the actual path to your Express app file. |
6 | | -const JWT_SECRET = process.env.JWT_SECRET; |
7 | | - |
8 | | -/* Connecting to the database before each test. */ |
9 | | -beforeEach(async () => { |
10 | | - await mongoose.connect("mongodb+srv://realgram:HD2p9wdgrbSpb6wl@google-cloud-sp-realgra.4le4qef.mongodb.net/?retryWrites=true&w=majority") |
11 | | - .then(() => { |
12 | | - console.log('Connected to MongoDB successfully!'); |
13 | | - // Your code here that interacts with the database |
14 | | - }) |
15 | | - .catch((error) => { |
16 | | - console.error('Error connecting to MongoDB:', error); |
| 3 | +const mongoose = require("mongoose"); |
| 4 | +const jwt = require("jsonwebtoken"); |
| 5 | +const app = require("../server.js"); |
| 6 | +const MONGOURI = process.env.MONGOURI; |
| 7 | +let userTestID = ""; |
| 8 | +let sessionToken = ""; |
| 9 | + |
| 10 | +beforeAll(async () => { |
| 11 | + await mongoose.connect(MONGOURI).catch((error) => { |
| 12 | + console.error("Error connecting to MongoDB:", error); |
17 | 13 | }); |
18 | 14 | }); |
19 | 15 |
|
20 | | -/* Dropping the database and closing connection after each test. */ |
21 | | -afterEach(async () => { |
22 | | - // await mongoose.connection.dropDatabase(); |
| 16 | +afterAll(async () => { |
23 | 17 | await mongoose.connection.close(); |
24 | 18 | }); |
25 | 19 |
|
26 | | -describe("GET /auth/register usuário valido", () => { |
| 20 | +describe("GET /auth/register usuário valido", () => { |
27 | 21 | test("should return users", async () => { |
28 | | -const data = { |
29 | | - |
30 | | - "name" : "paulof", |
31 | | - "username" : "pauloi", |
32 | | - "email": "lpauloval@gmail.com", |
33 | | - "password": "paulo1234" |
34 | | -}; |
35 | | -const res = await supertest(app) |
36 | | - .post('/auth/register') |
37 | | - .send(data); |
38 | | - // Use the supertest variable to make the request |
| 22 | + const data = { |
| 23 | + name: "Usuário de Teste", |
| 24 | + username: "unitytester", |
| 25 | + email: "usertest@test.com", |
| 26 | + password: "test123", |
| 27 | + }; |
| 28 | + const res = await supertest(app).post("/auth/register").send(data); |
| 29 | + |
39 | 30 | expect(res.statusCode).toBe(201); |
40 | 31 | }); |
41 | 32 | }); |
42 | 33 |
|
43 | | -describe("GET /auth/register usuário vazio", () => { |
| 34 | +describe("GET /auth/register usuário vazio", () => { |
44 | 35 | test("should return users", async () => { |
45 | | -const data = { |
46 | | - |
47 | | - "name" : "", |
48 | | - "username" : "", |
49 | | - "email": "", |
50 | | - "password": "" |
51 | | -}; |
52 | | -const res = await supertest(app) |
53 | | - .post('/auth/register') |
54 | | - .send(data); |
55 | | - // Use the supertest variable to make the request |
| 36 | + const data = { |
| 37 | + name: "", |
| 38 | + username: "", |
| 39 | + email: "", |
| 40 | + password: "", |
| 41 | + }; |
| 42 | + const res = await supertest(app).post("/auth/register").send(data); |
56 | 43 | expect(res.statusCode).toBe(400); |
57 | 44 | }); |
58 | 45 | }); |
59 | 46 |
|
60 | | -describe("GET /auth/register usuário que já existe", () => { |
| 47 | +describe("GET /auth/register usuário que já existe", () => { |
61 | 48 | test("should return users", async () => { |
62 | | -const data = { |
63 | | - |
64 | | - "name" : "paulof", |
65 | | - "username" : "pauloi", |
66 | | - "email": "lpauloval@gmail.com", |
67 | | - "password": "paulo1234" |
68 | | -}; |
69 | | - |
70 | | -const res = await supertest(app) |
71 | | - .post('/auth/register') |
72 | | - .send(data); |
73 | | - // Use the supertest variable to make the request |
| 49 | + const data = { |
| 50 | + name: "Usuário de Teste", |
| 51 | + username: "unitytester", |
| 52 | + email: "usertest@test.com", |
| 53 | + password: "test123", |
| 54 | + }; |
| 55 | + |
| 56 | + const res = await supertest(app).post("/auth/register").send(data); |
74 | 57 | expect(res.statusCode).toBe(409); |
75 | 58 | }); |
76 | 59 | }); |
77 | 60 |
|
78 | | -describe("GET /auth/login usuário válido", () => { |
| 61 | +describe("GET /auth/login usuário válido", () => { |
79 | 62 | test("should return users", async () => { |
80 | | -const data = { |
81 | | - "email": "lpauloval@gmail.com", |
82 | | - "password": "paulo1234" |
83 | | -}; |
84 | | - |
85 | | -const res = await supertest(app) |
86 | | - .post('/auth/login') |
87 | | - .send(data); |
88 | | - // Use the supertest variable to make the request |
| 63 | + const data = { |
| 64 | + email: "usertest@test.com", |
| 65 | + password: "test123", |
| 66 | + }; |
| 67 | + |
| 68 | + const res = await supertest(app).post("/auth/login").send(data); |
| 69 | + |
| 70 | + userTestID = res.body.data._id; |
| 71 | + sessionToken = res.body.token; |
| 72 | + |
89 | 73 | expect(res.statusCode).toBe(200); |
90 | 74 | }); |
91 | 75 | }); |
92 | 76 |
|
93 | | -describe("GET /auth/login usuário senha errada", () => { |
| 77 | +describe("GET /auth/login usuário senha errada", () => { |
94 | 78 | test("should return users", async () => { |
95 | | -const data = { |
96 | | - "email": "lpauloval@gmail.com", |
97 | | - "password": "paulo1" |
98 | | -}; |
99 | | - |
100 | | -const res = await supertest(app) |
101 | | - .post('/auth/login') |
102 | | - .send(data); |
103 | | - // Use the supertest variable to make the request |
| 79 | + const data = { |
| 80 | + email: "usertest@test.com", |
| 81 | + password: "invalidpassword", |
| 82 | + }; |
| 83 | + |
| 84 | + const res = await supertest(app).post("/auth/login").send(data); |
104 | 85 | expect(res.statusCode).toBe(422); |
105 | 86 | }); |
106 | 87 | }); |
107 | 88 |
|
108 | | -describe("GET /auth/login usuário email errado", () => { |
| 89 | +describe("GET /auth/login usuário email errado", () => { |
109 | 90 | test("should return users", async () => { |
110 | | -const data = { |
111 | | - "email": "lpaulova3l@gmail.com", |
112 | | - "password": "paulo13424" |
113 | | -}; |
114 | | - |
115 | | -const res = await supertest(app) |
116 | | - .post('/auth/login') |
117 | | - .send(data); |
118 | | - // Use the supertest variable to make the request |
| 91 | + const data = { |
| 92 | + email: "2d1312d123g12g1uihdf123ud123d12uf12@gmail.com", |
| 93 | + password: "sojesussalvva", |
| 94 | + }; |
| 95 | + |
| 96 | + const res = await supertest(app).post("/auth/login").send(data); |
119 | 97 | expect(res.statusCode).toBe(422); |
120 | 98 | }); |
121 | 99 | }); |
122 | 100 |
|
123 | | -describe("GET /auth/login usuário vazio", () => { |
| 101 | +describe("GET /auth/login usuário vazio", () => { |
124 | 102 | test("should return users", async () => { |
125 | | -const data = { |
126 | | - "email": "", |
127 | | - "password": "" |
128 | | -}; |
129 | | - |
130 | | -const res = await supertest(app) |
131 | | - .post('/auth/login') |
132 | | - .send(data); |
133 | | - // Use the supertest variable to make the request |
| 103 | + const data = { |
| 104 | + email: "", |
| 105 | + password: "", |
| 106 | + }; |
| 107 | + |
| 108 | + const res = await supertest(app).post("/auth/login").send(data); |
134 | 109 | expect(res.statusCode).toBe(400); |
135 | 110 | }); |
136 | 111 | }); |
| 112 | + |
| 113 | +describe("GET /user/delete", () => { |
| 114 | + test("should alert for invalid id", async () => { |
| 115 | + const res = await supertest(app) |
| 116 | + .delete(`/user/delete/${"invalidID"}`) |
| 117 | + .set("Authorization", `Bearer ${sessionToken}`); |
| 118 | + expect(res.statusCode).toBe(404); |
| 119 | + }); |
| 120 | + |
| 121 | + test("should to be deleted", async () => { |
| 122 | + const deleteRoute = "/user/delete/" + userTestID; |
| 123 | + const res = await supertest(app) |
| 124 | + .delete(deleteRoute) |
| 125 | + .set("Authorization", `Bearer ${sessionToken}`); |
| 126 | + console.log("############## AQUI MANO", res); |
| 127 | + expect(res.statusCode).toBe(200); |
| 128 | + }); |
| 129 | +}); |
0 commit comments