feat(tests): add Jest+Supertest infrastructure with jump route suite (14 tests)

- Extract Express app creation into src/createApp.js to enable testing without starting the server
- Add Jest, Supertest, test helpers (createTestApp, auth token generator)
- Write 14 integration tests for skydive/jumps: auth protection, CRUD, ownership checks

Bugs caught and fixed by the test suite:
- MongoDB models: author field typed as Schema.Types.UUID (Mongoose 6 rejects string UUIDs) → changed to String
- Jump.toJSONFor: called toProfileJSONFor on a UUID string → guarded with typeof check
- exceptions.handler: used err.statusCode but express-jwt throws err.status → returns 500 on 401 errors
- Jump.count(query): deprecated Mongoose method was ignoring the filter → replaced with countDocuments
- jumps.routes GET /: $or query included non-schema field "title" stripped by strictQuery, leaving {} (match-all) → replaced with slug/lieu search
- $regex: was passing a JS RegExp object which serializes to {} in MongoDB → now passes raw string
This commit is contained in:
2026-04-26 01:23:01 +02:00
parent 4c765945b3
commit da1f9437ab
21 changed files with 4068 additions and 163 deletions
+19
View File
@@ -0,0 +1,19 @@
const jwt = require('jsonwebtoken');
const TEST_USER_ID = 'a0000000-0000-0000-0000-000000000001';
const TEST_USERNAME = 'test_user';
function generateToken(userId = TEST_USER_ID, username = TEST_USERNAME) {
const secret = process.env.SECRET;
const exp = Math.floor(Date.now() / 1000) + 86400;
return jwt.sign({ id: userId, username, exp }, secret, { algorithm: 'HS256' });
}
const mockUser = {
id: TEST_USER_ID,
username: TEST_USERNAME,
email: 'test@example.com',
role: 'Admin',
};
module.exports = { generateToken, mockUser, TEST_USER_ID, TEST_USERNAME };
+26
View File
@@ -0,0 +1,26 @@
const mongoose = require('mongoose');
const createApp = require('../../src/createApp');
const config = require('../../src/config');
const exceptionHandler = require('../../src/middlewares/exceptions.handler');
async function connectMongo() {
require('../../src/database/models/mongo');
await mongoose.connect(process.env.MONGODB_URI);
}
async function disconnectMongo() {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
}
function buildApp(routePrefix, routeModule) {
config.initAuth();
const app = createApp();
app.use(routePrefix, routeModule);
app.use(exceptionHandler.notFoundErrorHandler);
app.use(exceptionHandler.logErrorHandler);
app.use(exceptionHandler.fianlErrorHandler);
return app;
}
module.exports = { connectMongo, disconnectMongo, buildApp };