Skip to content

Commit

Permalink
feat(general): test everything possible, 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 9, 2020
1 parent 0410abe commit 40dd4b5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
23 changes: 22 additions & 1 deletion lib/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
} = require('../models');
const { Sequelize } = require('./sequelize');


const JobCallbacks = {
DELETE_NOT_CONFIRMED_USERS: async () => {
const confirmations = await MailConfirmation.findAll({
Expand Down Expand Up @@ -62,6 +61,7 @@ class JobManager {

const id = ++this.currentJob;

/* istanbul ignore next */
const job = cron.schedule(rule, () => this.executeJob(id));

this.jobs[id] = {
Expand Down Expand Up @@ -95,6 +95,27 @@ with the following params: %o`, params);
async registerAllTasks() {
this.addJob(this.JOB_TYPES.DELETE_NOT_CONFIRMED_USERS);
}

// for tests only
/* istanbul ignore next */
cancelJob(id) {
const job = this.jobs[id];
if (!job) {
logger.warn(`Job with ID #${id} is not found.`);
return;
}

logger.info(`Cancelling job #${job.id}: "${job.description}", with rule ${job.rule}.`);
job.job.destroy();
delete this.jobs[id];
}

clearAll() {
const ids = Object.keys(this.jobs);
for (const id of ids) {
this.cancelJob(id);
}
}
}

const manager = new JobManager();
Expand Down
10 changes: 8 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ async function startServer() {
log.info('Up and running, listening on http://localhost:%d', config.port);
await db.authenticate();
log.info('DB connection is successful.');
await cron.registerAllTasks();
log.info('All cron tasks are registered.');

// no need it in test
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'test') {
await cron.registerAllTasks();
log.info('All cron tasks are registered.');
}

return res();
});
/* istanbul ignore next */
Expand Down
2 changes: 2 additions & 0 deletions middlewares/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ exports.ensureAuthorized = async (req, res, next) => {
return next();
};

/* istanbul ignore next */
exports.getMyGlobalPermissions = async (req, res) => {
// TODO: return real permissions.
return res.json({
Expand Down Expand Up @@ -176,6 +177,7 @@ exports.errorHandler = (err, req, res, next) => {
}

// Handling validation errors
/* istanbul ignore else */
if (err.name && ['SequelizeValidationError', 'SequelizeUniqueConstraintError'].includes(err.name)) {
return errors.makeValidationError(res, err);
}
Expand Down
1 change: 1 addition & 0 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ User.prototype.checkPassword = async function checkPassword(password) {
return bcrypt.compare(password, this.password);
};

/* istanbul ignore next */
User.prototype.notValidFields = function notValidFields() {
const missingFields = [];
for (const field of ['date_of_birth', 'gender']) {
Expand Down
75 changes: 75 additions & 0 deletions test/api/cron.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const moment = require('moment');

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

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

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

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

test('should add job', async () => {
expect(Object.keys(cron.jobs).length).toEqual(0);;

cron.addJob(cron.JOB_TYPES.DELETE_NOT_CONFIRMED_USERS);
expect(Object.keys(cron.jobs).length).toEqual(1);
});

test('should execute job', async () => {
expect(Object.keys(cron.jobs).length).toEqual(0);;

cron.addJob(cron.JOB_TYPES.DELETE_NOT_CONFIRMED_USERS);
expect(Object.keys(cron.jobs).length).toEqual(1);

const jobKey = Object.keys(cron.jobs)[0];
await cron.executeJob(jobKey);
});

test('should not execute a job if it\'s not found', async () => {
expect(Object.keys(cron.jobs).length).toEqual(0);;
await cron.executeJob(1337);
});

test('should register all tasks', async () => {
expect(Object.keys(cron.jobs).length).toEqual(0);;

cron.registerAllTasks();
expect(Object.keys(cron.jobs).length).toEqual(1);
});

describe('DELETE_NOT_CONFIRMED_USERS', () => {
test('should work properly', async () => {
const userActivated = await generator.createUser();
const userWithValidConfirmation = await generator.createUser();
const userWithoutValidConfirmation = await generator.createUser();

await generator.createMailConfirmation({ expires_at: moment().add(1, 'day' ) }, userWithValidConfirmation);
await generator.createMailConfirmation({ expires_at: moment().subtract(1, 'day' ) }, userWithoutValidConfirmation);

cron.addJob(cron.JOB_TYPES.DELETE_NOT_CONFIRMED_USERS);
expect(Object.keys(cron.jobs).length).toEqual(1);

const jobKey = Object.keys(cron.jobs)[0];
await cron.executeJob(jobKey);

// first and second should be left as they are, third should be removed.
const usersFromDb = await User.findAll();
expect(usersFromDb.length).toEqual(2);

const ids = usersFromDb.map(u => u.id);
expect(ids).toContain(userActivated.id);
expect(ids).toContain(userWithValidConfirmation.id);
});
});
});

0 comments on commit 40dd4b5

Please sign in to comment.