Skip to content

Commit

Permalink
feat(test): testing mail confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 8, 2020
1 parent cbf3f5a commit bde3689
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
84 changes: 84 additions & 0 deletions test/api/mail-confirmation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const moment = require('moment');

const { startServer, stopServer } = require('../../lib/server.js');
const { request } = require('../scripts/helpers');
const generator = require('../scripts/generator');
const { User, MailConfirmation } = require('../../models');

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

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

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

test('should fail if the token is not provided', async () => {
const res = await request({
uri: '/confirm-email',
method: 'POST',
body: {}
});

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

test('should fail if the confirmation is not found', async () => {
const res = await request({
uri: '/confirm-email',
method: 'POST',
body: { token: 'test' }
});

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

test('should fail if the confirmation is expired', async () => {
const user = await generator.createUser({ mail_confirmed_at: null });
const confirmation = await generator.createMailConfirmation({ expires_at: moment().subtract(1, 'day').toDate() }, user);

const res = await request({
uri: '/confirm-email',
method: 'POST',
body: { token: confirmation.value }
});

expect(res.statusCode).toEqual(403);
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({ mail_confirmed_at: null });
const confirmation = await generator.createMailConfirmation({ expires_at: moment().add(1, 'day').toDate() }, user);

const res = await request({
uri: '/confirm-email',
method: 'POST',
body: { token: confirmation.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('message');
expect(res.body).not.toHaveProperty('errors');

const confirmationFromDb = await MailConfirmation.findByPk(confirmation.id);
expect(confirmationFromDb).toEqual(null);

const userFromDb = await User.findByPk(user.id);
expect(userFromDb.mail_confirmed_at).not.toEqual(null);
});
});
12 changes: 12 additions & 0 deletions test/scripts/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ exports.createUser = (options = {}) => {
return User.create(exports.generateUser(options));
};

exports.generateMailConfirmation = (options = {}, user) => {
if (notSet(options.value)) options.value = faker.random.alphaNumeric(16);
if (notSet(options.expires_at)) options.expires_at = faker.date.future();
if (user) options.user_id = user.id;

return options;
};

exports.createMailConfirmation = (options = {}, user = null) => {
return MailConfirmation.create(exports.generateMailConfirmation(options, user));
};

exports.generateBody = (options = {}) => {
if (notSet(options.name)) options.name = faker.name.firstName();
if (notSet(options.description)) options.description = faker.lorem.paragraph();
Expand Down

0 comments on commit bde3689

Please sign in to comment.