add tests

This commit is contained in:
xfarrow 2024-10-31 17:55:51 +01:00
parent 71caaf6886
commit d043869050
2 changed files with 81 additions and 7 deletions

View File

@ -51,14 +51,14 @@ app.use(rateLimiter); // Apply the rate limiter middleware to all routes
===== BEGIN ROUTE HANDLING ===== ===== BEGIN ROUTE HANDLING =====
*/ */
app.use('/api/server', serverRoutes.routes); app.use('/api/server', serverRoutes.routes);
app.use('/api/persons', personRoutes.publicRoutes); app.use('/api/persons', personRoutes.publicRoutes); // TODO: Change in "/people". Idk why I chose "persons"
app.use('/api/persons', personRoutes.protectedRoutes); app.use('/api/persons', personRoutes.protectedRoutes); // TODO: Change in "/people". Idk why I chose "persons"
app.use('/api/organizations', organizationRoutes.routes); app.use('/api/organizations', organizationRoutes.routes);
app.use('/api/organizations', jobOffersRoutes.routes); app.use('/api/organizations', jobOffersRoutes.routes);
app.use('/api/organizations', organizationAdminRoutes.routes); app.use('/api/organizations', organizationAdminRoutes.routes);
app.use('/api/resetpassword', resetPasswordRoutes.routes); app.use('/api/resetpassword', resetPasswordRoutes.routes);
app.use('/api/organizations', applicationRoutes.routes); app.use('/api/organizations', applicationRoutes.routes);
app.use('/api/persons', personContactInfosRoutes.routes); app.use('/api/persons', personContactInfosRoutes.routes); // TODO: Change in "/people". Idk why I chose "persons"
/* /*
===== END ROUTE HANDLING ===== ===== END ROUTE HANDLING =====
@ -78,6 +78,3 @@ app.listen(port, () => {
/* /*
===== END STARTING THE SERVER ===== ===== END STARTING THE SERVER =====
*/ */
// Export the app for testing purposes
module.exports = app;

View File

@ -0,0 +1,77 @@
// To execute tests, call: npx jest
// Note: The API should already be running
// Note: These tests will pollute the database. Run only in test environments
const request = require('supertest');
const apiEndpoint = 'http://localhost:3000/api';
function randomString() {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const length = Math.floor(Math.random() * 10) + 1;
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};
const userEmail = randomString() + "_test@mail.org";
describe('Person tests', () => {
it('should return a 201 status code for POST /persons', async () => {
const userData = {
"email": userEmail,
"password": "password",
"displayName": "Test1",
};
const response = await request(apiEndpoint)
.post('/persons')
.send(userData);
expect(response.status).toBe(201);
});
it('should return a 400 status code for POST /persons', async () => {
const userData = {
"password": "password",
"displayName": "Test1",
};
const response = await request(apiEndpoint)
.post('/persons')
.send(userData);
expect(response.status).toBe(400);
});
it('should return a 400 status code for POST /persons', async () => {
const userData = {
"email": "test1@mail.org",
"displayName": "Test1",
};
const response = await request(apiEndpoint)
.post('/persons')
.send(userData);
expect(response.status).toBe(400);
});
it('should return a 400 status code for POST /persons', async () => {
const userData = {
"email": "test1@mail.org",
"password": "password"
};
const response = await request(apiEndpoint)
.post('/persons')
.send(userData);
expect(response.status).toBe(400);
});
it('should return a 400 status code for POST /persons', async () => {
const userData = {
"email": randomString() + "_test_not_an_email",
"password": "password",
"displayName": "Test1",
};
const response = await request(apiEndpoint)
.post('/persons')
.send(userData);
expect(response.status).toBe(400);
});
});