This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
know-it-all/server/tests/auth/Login.test.js
2022-03-30 09:46:43 -05:00

56 lines
1.8 KiB
JavaScript

require('dotenv').config();
const supertest = require('supertest');
var { sequelize, Token } = require('../../models/index');
const app = require('../../app');
const { createTestUser } = require('../utils');
describe('Login', function () {
beforeEach(async () => {
try {
await sequelize.authenticate();
await sequelize.sync({ force: 'true' });
} catch (error) {
console.log('[ERROR]: Database connection failed');
}
});
test('[200] Successful login', async () => {
const password = 'password';
const user = await createTestUser('Test', 'User', password);
await supertest(app)
.post('/api/auth/login')
.send({ email: user.email, password })
.expect(200)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.then(async (response) => {
const token = await Token.findByPk(response.body.token);
expect(response.body.token).toEqual(token.tokenID);
expect(response.body.userID).toEqual(token.userID);
});
});
test('[400] Request missing fields', async () => {
await supertest(app)
.post('/api/auth/login')
.send()
.expect(400, 'Request lacking information')
.set('Accept', 'text/html')
.expect('Content-Type', /text/);
});
test('[400] Invalid password', async () => {
const password = 'password';
const user = await createTestUser('Test', 'User', password);
await supertest(app)
.post('/api/auth/login')
.send({ email: user.email, password: 'randomString' })
.expect(400, 'Unauthorized user')
.set('Accept', 'text/html')
.expect('Content-Type', /text/);
});
});