Update test.js

This commit is contained in:
xfarrow 2024-11-14 17:21:07 +01:00
parent 6b60d4c006
commit 8321e3121c
1 changed files with 87 additions and 4 deletions

View File

@ -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);
});
});