Skip to content

Commit

Permalink
feat(test): testing login
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 5, 2020
1 parent 0459e09 commit e8261f1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
2 changes: 1 addition & 1 deletion middlewares/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports.login = async (req, res) => {
});

if (!user) {
return errors.makeNotFoundError(res, 'User is not found.');
return errors.makeUnauthorizedError(res, 'User is not found.');
}

if (!await user.checkPassword(req.body.password)) {
Expand Down
3 changes: 1 addition & 2 deletions migrations/20200128175455-create-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ module.exports = {
},
mail_confirmed_at: {
type: Sequelize.DATE,
allowNull: true,
defaultValue: Sequelize.NOW
allowNull: true
},
active: {
type: Sequelize.BOOLEAN,
Expand Down
3 changes: 1 addition & 2 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ const User = sequelize.define('user', {
},
mail_confirmed_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW
allowNull: true
},
active: {
type: Sequelize.BOOLEAN,
Expand Down
88 changes: 88 additions & 0 deletions test/api/authorization.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { startServer, stopServer } = require('../../lib/server.js');
const { request } = require('../scripts/helpers');
const generator = require('../scripts/generator');

describe('Authorization', () => {
beforeAll(async () => {
await startServer();
});

afterAll(async () => {
await stopServer();
});

afterEach(async () => {
await generator.clearAll();
});

test('should fail if the user is not found', async () => {
const res = await request({
uri: '/login/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
username: 'non-existant@test.io',
password: 'aaaa'
}
});

expect(res.statusCode).toEqual(401);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
});

test('should fail if the password is wrong', async () => {
const user = await generator.createUser({ password: 'test' })
const res = await request({
uri: '/login/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
username: user.email,
password: 'test2'
}
});

expect(res.statusCode).toEqual(401);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
});

test('should fail if the email is not confirmed', async () => {
const user = await generator.createUser({ password: 'test', mail_confirmed_at: null })
const res = await request({
uri: '/login/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
username: user.email,
password: 'test'
}
});

expect(res.statusCode).toEqual(401);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
});

test('should succeed if everything is okay', async () => {
const user = await generator.createUser({ password: 'test', mail_confirmed_at: new Date() })
const res = await request({
uri: '/login/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
username: user.email,
password: 'test'
}
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');
});
});
10 changes: 9 additions & 1 deletion test/scripts/generator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const faker = require('faker');

const { User, Campaign, MailConfirmation } = require('../../models');
const {
User,
Campaign,
MailConfirmation,
AccessToken,
RefreshToken,
} = require('../../models');

const notSet = (field) => typeof field === 'undefined';

Expand Down Expand Up @@ -36,6 +42,8 @@ exports.createCampaign = (options = {}) => {
};

exports.clearAll = async () => {
await AccessToken.destroy({ where: {}, truncate: { cascade: true } });
await RefreshToken.destroy({ where: {}, truncate: { cascade: true } });
await MailConfirmation.destroy({ where: {}, truncate: { cascade: true } });
await User.destroy({ where: {}, truncate: { cascade: true } });
await Campaign.destroy({ where: {}, truncate: { cascade: true } });
Expand Down

0 comments on commit e8261f1

Please sign in to comment.