From d0438690502ef1231f34644a8d9dad7f7fb77546 Mon Sep 17 00:00:00 2001 From: xfarrow <49845537+xfarrow@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:55:51 +0100 Subject: [PATCH] add tests --- backend/apis/nodejs/src/app.js | 11 ++-- backend/apis/nodejs/src/tests/test.js | 77 +++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 backend/apis/nodejs/src/tests/test.js diff --git a/backend/apis/nodejs/src/app.js b/backend/apis/nodejs/src/app.js index 6eb4d44..44f2765 100644 --- a/backend/apis/nodejs/src/app.js +++ b/backend/apis/nodejs/src/app.js @@ -51,14 +51,14 @@ app.use(rateLimiter); // Apply the rate limiter middleware to all routes ===== BEGIN ROUTE HANDLING ===== */ app.use('/api/server', serverRoutes.routes); -app.use('/api/persons', personRoutes.publicRoutes); -app.use('/api/persons', personRoutes.protectedRoutes); +app.use('/api/persons', personRoutes.publicRoutes); // TODO: Change in "/people". Idk why I chose "persons" +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', jobOffersRoutes.routes); app.use('/api/organizations', organizationAdminRoutes.routes); app.use('/api/resetpassword', resetPasswordRoutes.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 ===== @@ -77,7 +77,4 @@ app.listen(port, () => { /* ===== END STARTING THE SERVER ===== -*/ - -// Export the app for testing purposes -module.exports = app; \ No newline at end of file +*/ \ No newline at end of file diff --git a/backend/apis/nodejs/src/tests/test.js b/backend/apis/nodejs/src/tests/test.js new file mode 100644 index 0000000..a3d4d4d --- /dev/null +++ b/backend/apis/nodejs/src/tests/test.js @@ -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); + }); +}); \ No newline at end of file