From 8321e3121c5cbb3e8bf2c4e9739af32ffccea28b Mon Sep 17 00:00:00 2001 From: xfarrow <49845537+xfarrow@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:21:07 +0100 Subject: [PATCH] Update test.js --- backend/apis/nodejs/src/tests/test.js | 91 +++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 4 deletions(-) diff --git a/backend/apis/nodejs/src/tests/test.js b/backend/apis/nodejs/src/tests/test.js index 35476a8..e405f46 100644 --- a/backend/apis/nodejs/src/tests/test.js +++ b/backend/apis/nodejs/src/tests/test.js @@ -5,6 +5,8 @@ const request = require('supertest'); const apiEndpoint = 'http://localhost:3000/api'; +let bearerToken; + function randomString() { const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const length = Math.floor(Math.random() * 10) + 1; @@ -17,21 +19,26 @@ function randomString() { describe('Person tests', () => { const userEmail = randomString() + "_test@mail.org"; + const userPassword = "password" + + // Correct registration it('should return a 201 status code for POST /persons', async () => { const userData = { "email": userEmail, - "password": "password", + "password": userPassword, "displayName": "Test1", }; const response = await request(apiEndpoint) .post('/persons') .send(userData); expect(response.status).toBe(201); + bearerToken = response.body.token; }); + // Registration with no email it('should return a 400 status code for POST /persons', async () => { const userData = { - "password": "password", + "password": userPassword, "displayName": "Test1", }; const response = await request(apiEndpoint) @@ -40,6 +47,7 @@ describe('Person tests', () => { expect(response.status).toBe(400); }); + // Registration with no password it('should return a 400 status code for POST /persons', async () => { const userData = { "email": "test1@mail.org", @@ -51,10 +59,11 @@ describe('Person tests', () => { expect(response.status).toBe(400); }); + // Registration with no display name it('should return a 400 status code for POST /persons', async () => { const userData = { "email": "test1@mail.org", - "password": "password" + "password": userPassword }; const response = await request(apiEndpoint) .post('/persons') @@ -62,10 +71,11 @@ describe('Person tests', () => { expect(response.status).toBe(400); }); + // Registration with an e-mail which is not a valid e-mail it('should return a 400 status code for POST /persons', async () => { const userData = { "email": randomString() + "_test_not_an_email", - "password": "password", + "password": userPassword, "displayName": "Test1", }; const response = await request(apiEndpoint) @@ -73,4 +83,77 @@ describe('Person tests', () => { .send(userData); expect(response.status).toBe(400); }); + + // Correct login + it('should return a 200 status code for POST /persons/me/token', async () => { + const userData = { + "email": userEmail, + "password": userPassword + }; + const response = await request(apiEndpoint) + .post('/persons/me/token') + .send(userData); + expect(response.status).toBe(200); + bearerToken = response.body.token; + }); + + // Login with wrong password + it('should return a 401 status code for POST /persons/me/token', async () => { + const userData = { + "email": userEmail, + "password": randomString() + }; + const response = await request(apiEndpoint) + .post('/persons/me/token') + .send(userData); + expect(response.status).toBe(401); + }); + + // Login without password + it('should return a 400 status code for POST /persons/me/token', async () => { + const userData = { + "email": userEmail + }; + const response = await request(apiEndpoint) + .post('/persons/me/token') + .send(userData); + expect(response.status).toBe(400); + }); + + // Login without email + it('should return a 400 status code for POST /persons/me/token', async () => { + const userData = { + "password": randomString() + }; + const response = await request(apiEndpoint) + .post('/persons/me/token') + .send(userData); + expect(response.status).toBe(400); + }); + + // Get myself + it('should return a 200 status code for POST /persons/me', async () => { + const response = await request(apiEndpoint) + .get('/persons/me') + .set("Authorization", `Bearer ${bearerToken}`) + .send(); + expect(response.status).toBe(200); + }); + + // Get myself without token + it('should return a 401 status code for POST /persons/me', async () => { + const response = await request(apiEndpoint) + .get('/persons/me') + .send(); + expect(response.status).toBe(401); + }); + + // Get myself with invalid token + it('should return a 401 status code for POST /persons/me', async () => { + const response = await request(apiEndpoint) + .get('/persons/me') + .set("Authorization", `Bearer abc`) + .send(); + expect(response.status).toBe(401); + }); }); \ No newline at end of file