Compare commits

..

3 Commits

Author SHA1 Message Date
xfarrow 4ae263662c Update test.js 2024-11-14 17:59:13 +01:00
xfarrow 8321e3121c Update test.js 2024-11-14 17:21:07 +01:00
xfarrow 6b60d4c006 Update test.js 2024-11-14 15:44:58 +01:00
1 changed files with 88 additions and 6 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;
@ -15,24 +17,28 @@ function randomString() {
return result;
};
const userEmail = randomString() + "_test@mail.org";
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)
@ -41,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",
@ -52,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')
@ -63,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)
@ -74,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 GET /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 GET /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);
});
});