Skip to content

Commit 655a41d

Browse files
authored
Merge pull request #66 from pumba-dev/main
Fix: Fix auth and user tests and describe delete tests.
2 parents d1bbe8c + 97b21c5 commit 655a41d

File tree

3 files changed

+97
-99
lines changed

3 files changed

+97
-99
lines changed

realgram-backend/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "server.js",
66
"scripts": {
77
"serve": "nodemon src/server.js",
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "jest"
99
},
1010
"keywords": [],
1111
"author": "",
@@ -21,5 +21,13 @@
2121
"multer": "^1.4.5-lts.1",
2222
"nodemailer": "^6.9.4",
2323
"sequelize": "^6.32.1"
24+
},
25+
"devDependencies": {
26+
"@babel/preset-typescript": "^7.22.5",
27+
"babel-cli": "^6.26.0",
28+
"babel-preset-env": "^1.7.0",
29+
"jest": "^29.6.1",
30+
"superagent": "^8.0.9",
31+
"supertest": "^6.3.3"
2432
}
2533
}

realgram-backend/src/routes/user.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,11 @@ router.delete("/user/delete/:userId", requireLogin, (req, res) => {
8484

8585
User.findByIdAndRemove(userId)
8686
.then((deletedUser) => {
87-
if (!deletedUser) {
88-
return res.status(404).json({ error: "Usuário não encontrado." });
89-
}
9087
res.status(200).json({ message: "Usuário removido com sucesso." });
9188
})
9289
.catch((err) => {
9390
console.log(err);
94-
res.status(500).json({ error: "Ocorreu um erro ao remover o usuário." });
91+
return res.status(404).json({ error: "Usuário não encontrado." });
9592
});
9693
});
9794

Lines changed: 87 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,129 @@
11
require("dotenv").config();
22
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);
1713
});
1814
});
1915

20-
/* Dropping the database and closing connection after each test. */
21-
afterEach(async () => {
22-
// await mongoose.connection.dropDatabase();
16+
afterAll(async () => {
2317
await mongoose.connection.close();
2418
});
2519

26-
describe("GET /auth/register usuário valido", () => {
20+
describe("GET /auth/register usuário valido", () => {
2721
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+
3930
expect(res.statusCode).toBe(201);
4031
});
4132
});
4233

43-
describe("GET /auth/register usuário vazio", () => {
34+
describe("GET /auth/register usuário vazio", () => {
4435
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);
5643
expect(res.statusCode).toBe(400);
5744
});
5845
});
5946

60-
describe("GET /auth/register usuário que já existe", () => {
47+
describe("GET /auth/register usuário que já existe", () => {
6148
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);
7457
expect(res.statusCode).toBe(409);
7558
});
7659
});
7760

78-
describe("GET /auth/login usuário válido", () => {
61+
describe("GET /auth/login usuário válido", () => {
7962
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+
8973
expect(res.statusCode).toBe(200);
9074
});
9175
});
9276

93-
describe("GET /auth/login usuário senha errada", () => {
77+
describe("GET /auth/login usuário senha errada", () => {
9478
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);
10485
expect(res.statusCode).toBe(422);
10586
});
10687
});
10788

108-
describe("GET /auth/login usuário email errado", () => {
89+
describe("GET /auth/login usuário email errado", () => {
10990
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);
11997
expect(res.statusCode).toBe(422);
12098
});
12199
});
122100

123-
describe("GET /auth/login usuário vazio", () => {
101+
describe("GET /auth/login usuário vazio", () => {
124102
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);
134109
expect(res.statusCode).toBe(400);
135110
});
136111
});
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

Comments
 (0)