mirror of
				https://github.com/xfarrow/blink
				synced 2025-06-27 09:03:02 +02:00 
			
		
		
		
	Update test.js
This commit is contained in:
		@@ -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);
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user