50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
require('dotenv').config();
|
|
const supertest = require('supertest');
|
|
|
|
var { sequelize, Token } = require('../../models/index');
|
|
const app = require('../../app');
|
|
const { createTestToken } = require('../utils');
|
|
|
|
describe('Logout', function () {
|
|
beforeEach(async () => {
|
|
try {
|
|
await sequelize.authenticate();
|
|
await sequelize.sync({ force: 'true' });
|
|
} catch (error) {
|
|
console.log('[ERROR]: Database connection failed');
|
|
}
|
|
});
|
|
|
|
test('[200] Successful logout', async () => {
|
|
const token = await createTestToken({
|
|
firstName: 'Test',
|
|
lastName: 'User',
|
|
password: 'password',
|
|
});
|
|
|
|
await supertest(app)
|
|
.post('/api/auth/logout')
|
|
.send({ token: token.tokenID })
|
|
.expect(200, 'User logged out')
|
|
.set('Accept', 'text/html')
|
|
.expect('Content-Type', /text/);
|
|
});
|
|
|
|
test('[403] Missing token', async () => {
|
|
await supertest(app)
|
|
.post('/api/auth/logout')
|
|
.send()
|
|
.expect(403, 'Unauthorized user')
|
|
.set('Accept', 'text/html')
|
|
.expect('Content-Type', /text/);
|
|
});
|
|
|
|
test('[511] Token was not found', async () => {
|
|
await supertest(app)
|
|
.post('/api/auth/logout')
|
|
.send({ token: 'randomString' })
|
|
.expect(511, 'Session expired')
|
|
.set('Accept', 'text/html')
|
|
.expect('Content-Type', /text/);
|
|
});
|
|
});
|