Skip to content

Commit 30fdaaa

Browse files
committed
Merge branch 'feature_auto-delete-test-accounts' into develop
2 parents a907066 + f01caa3 commit 30fdaaa

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

app/authentication.test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,31 @@ describe("POST /api/v1/auth/login", () => {
104104

105105
// TEST ID 6: Cambio password per utente autenticato
106106
test("Cambio password per utente autenticato", async () => {
107-
const loginResponse = await request(app).post("/api/v1/auth/login").send({
108-
email: "sorvegliante@comune.it",
107+
const adminToken = jwt.sign(
108+
{ name: "Admin", email: "admin@comune.it", role: "amministratore" },
109+
process.env.JWT_SECRET,
110+
{ expiresIn: 86400 }
111+
);
112+
113+
const timestamp = Date.now();
114+
const userData = {
115+
email: `testchangepass${timestamp}@comune.it`,
109116
password: "password123",
117+
role: "sorvegliante",
118+
name: "Test Cambio Password"
119+
};
120+
121+
const createResponse = await request(app)
122+
.post("/api/v1/users")
123+
.set("Authorization", `Bearer ${adminToken}`)
124+
.send(userData);
125+
126+
expect(createResponse.status).toBe(201);
127+
const userId = createResponse.body.user._id;
128+
129+
const loginResponse = await request(app).post("/api/v1/auth/login").send({
130+
email: userData.email,
131+
password: userData.password,
110132
});
111133

112134
expect(loginResponse.status).toBe(200);
@@ -120,6 +142,10 @@ describe("POST /api/v1/auth/login", () => {
120142
});
121143

122144
expect(response.status).toBe(403);
145+
146+
await request(app)
147+
.delete(`/api/v1/users/${userId}`)
148+
.set("Authorization", `Bearer ${adminToken}`);
123149
});
124150

125151
// TEST ID 7: Cambio password con token JWT non valido

app/jest.teardown.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require('dotenv').config();
2+
const mongoose = require("mongoose");
3+
const User = require("./models/user");
4+
5+
module.exports = async () => {
6+
try {
7+
await mongoose.connect(process.env.MONGODB_URI);
8+
9+
const result = await User.deleteMany({
10+
$or: [
11+
{ email: { $regex: /^sorvegliante\.test\d+@comune\.it$/ } },
12+
{ email: { $regex: /^dipendente\.test\d+@comune\.it$/ } },
13+
{ email: { $regex: /^testdipendente\d+@comune\.it$/ } },
14+
{ email: { $regex: /^testsorvegliante\d+@comune\.it$/ } },
15+
{ email: { $regex: /^testchangepass\d+@comune\.it$/ } },
16+
{ email: { $regex: /^utente\.eliminazione\d+@comune\.it$/ } },
17+
{ email: { $regex: /^sorvegliante\.modifica\d+@comune\.it$/ } },
18+
{ email: { $regex: /^testuser\d+@comune\.it$/ } }
19+
]
20+
});
21+
22+
console.log(`\nTest cleanup: eliminati ${result.deletedCount} utenti di test`);
23+
24+
await mongoose.connection.close();
25+
} catch (error) {
26+
console.error("Errore durante cleanup:", error);
27+
}
28+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dev": "node -r dotenv/config index.js",
88
"start": "node -r dotenv/config index.js",
99
"buildall": "cd Frontend && npm install && npm run build && cd .. && npm run start",
10-
"test": "jest --setupFiles dotenv/config",
10+
"test": "jest --setupFiles dotenv/config --globalTeardown ./app/jest.teardown.js",
1111
"format": "prettier --write ."
1212
},
1313
"author": "Andrea Bissoli, Kuba Di Quattro, Bilal Soussane",

scripts/cleanup.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require('dotenv').config();
2+
const mongoose = require("mongoose");
3+
const User = require("../app/models/user");
4+
5+
async function cleanupTestUsers() {
6+
try {
7+
console.log("Connection string:", process.env.MONGODB_URI);
8+
await mongoose.connect(process.env.MONGODB_URI);
9+
console.log("Connesso al database...");
10+
11+
const result = await User.deleteMany({
12+
$or: [
13+
{ email: { $regex: /^sorvegliante\.test\d+@comune\.it$/ } },
14+
{ email: { $regex: /^dipendente\.test\d+@comune\.it$/ } },
15+
{ email: { $regex: /^testdipendente\d+@comune\.it$/ } },
16+
{ email: { $regex: /^testsorvegliante\d+@comune\.it$/ } },
17+
{ email: { $regex: /^testchangepass\d+@comune\.it$/ } },
18+
{ email: { $regex: /^utente\.eliminazione\d+@comune\.it$/ } },
19+
{ email: { $regex: /^sorvegliante\.modifica\d+@comune\.it$/ } },
20+
{ email: { $regex: /^testuser\d+@comune\.it$/ } },
21+
{ email: 'sorvegliante.test@comune.it' },
22+
{ email: 'utente.da.eliminare@comune.it' }
23+
]
24+
});
25+
26+
console.log(`Eliminati ${result.deletedCount} utenti di test`);
27+
28+
const remainingUsers = await User.find({}, 'email name role');
29+
console.log(`\nUtenti rimanenti (${remainingUsers.length}):`);
30+
remainingUsers.forEach(user => {
31+
console.log(`- ${user.email} (${user.role})`);
32+
});
33+
34+
} catch (error) {
35+
console.error("Errore:", error);
36+
} finally {
37+
await mongoose.connection.close();
38+
console.log("\nConnessione chiusa");
39+
}
40+
}
41+
42+
cleanupTestUsers();

0 commit comments

Comments
 (0)