Skip to content

Commit

Permalink
feat(seed): add tasks in seed ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed May 15, 2019
1 parent 042deac commit 1b353b6
Show file tree
Hide file tree
Showing 7 changed files with 978 additions and 766 deletions.
7 changes: 7 additions & 0 deletions config/defaults/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ module.exports = {
seed: true,
options: {
logResults: true,
seedTasks: [{
title: 'title1',
description: 'do something about something else',
}, {
title: 'title2',
description: 'do something about something else',
}],
seedUser: {
username: 'seeduser',
provider: 'local',
Expand Down
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ const seedMongoose = async () => {
await mongooseService.connect();
await mongooseService.loadModels();
const UserService = require(path.resolve('./modules/users/services/user.service'));
const TaskService = require(path.resolve('./modules/tasks/services/tasks.service'));
const seed = require(path.resolve('./lib/services/seed'));
await seed.start({
logResults: true,
}, UserService).catch((e) => {
}, UserService, TaskService).catch((e) => {
console.log(e);
});
await mongooseService.disconnect();
Expand Down
1 change: 0 additions & 1 deletion lib/services/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const chalk = require('chalk');
const path = require('path');
const mongoose = require('mongoose');
const config = require('../../config');
// const seed = require('./seed');

/**
* Load all mongoose related models
Expand Down
20 changes: 19 additions & 1 deletion lib/services/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ const seedTheUser = (UserService, user) => async (password) => {
}
};

module.exports.start = async (options, UserService) => {
// save the specified user with the password provided from the resolved promise
const seedTasks = async (TaskService, task, user) => {
try {
const result = await TaskService.create(task, user);
if (seedOptions.logResults) console.log(chalk.bold.red(`Database Seeding:\t\t\tLocal ${task.title} added`));
return result;
} catch (err) {
throw new AppError('Failed to seedTasks.', { code: 'LIB_ERROR' });
}
};

module.exports.start = async (options, UserService, TaskService) => {
let pwd;
const result = [];

Expand All @@ -35,6 +46,8 @@ module.exports.start = async (options, UserService) => {
if (_.has(options, 'logResults')) seedOptions.logResults = options.logResults;
if (_.has(options, 'seedUser')) seedOptions.seedUser = options.seedUser;
if (_.has(options, 'seedAdmin')) seedOptions.seedAdmin = options.seedAdmin;
if (_.has(options, 'seedTasks')) seedOptions.seedTasks = options.seedTasks;


try {
if (process.env.NODE_ENV === 'production') {
Expand All @@ -45,6 +58,11 @@ module.exports.start = async (options, UserService) => {
result.push(await seedTheUser(UserService, seedOptions.seedUser)(pwd));
pwd = await UserService.generateRandomPassphrase();
result.push(await seedTheUser(UserService, seedOptions.seedAdmin)(pwd));

if (process.env.NODE_ENV === 'development') {
result.push(await seedTasks(TaskService, seedOptions.seedTasks[0], result[0]));
result.push(await seedTasks(TaskService, seedOptions.seedTasks[1], result[1]));
}
}
} catch (err) {
console.log(err);
Expand Down
110 changes: 74 additions & 36 deletions modules/core/tests/core.config.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ const seed = require(path.resolve('./lib/services/seed'));
*/
describe('Configuration Tests:', () => {
let UserService = null;
let TaskService = null;

beforeAll(() => mongooseService.connect()
.then(() => {
mongooseService.loadModels();
UserService = require(path.resolve('./modules/users/services/user.service'));
TaskService = require(path.resolve('./modules/tasks/services/tasks.service'));
})
.catch((e) => {
console.log(e);
Expand All @@ -29,6 +31,7 @@ describe('Configuration Tests:', () => {
let admin1;
let userFromSeedConfig;
let adminFromSeedConfig;
let tasksFromSeedConfig;

let originalLogConfig;

Expand Down Expand Up @@ -56,10 +59,11 @@ describe('Configuration Tests:', () => {

userFromSeedConfig = config.seedDB.options.seedUser;
adminFromSeedConfig = config.seedDB.options.seedAdmin;
tasksFromSeedConfig = config.seedDB.options.seedTasks;
done();
});

it('should have seedDB configuration set for "regular" user', (done) => {
it('should have seedDB configuration set for user', (done) => {
expect(userFromSeedConfig).toBeInstanceOf(Object);
expect(typeof userFromSeedConfig.username).toBe('string');
expect(typeof userFromSeedConfig.email).toBe('string');
Expand All @@ -73,22 +77,29 @@ describe('Configuration Tests:', () => {
done();
});

it('should have seedDB configuration set for tasks', (done) => {
expect(tasksFromSeedConfig).toBeInstanceOf(Array);
expect(typeof tasksFromSeedConfig[0].title).toBe('string');
expect(typeof tasksFromSeedConfig[1].title).toBe('string');
done();
});

it('should seed ONLY the admin user account when NODE_ENV is set to "production"', async () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
let seedusers;
let result;

try {
seedusers = await seed.start({ logResults: false }, UserService);
expect(seedusers).toBeInstanceOf(Array);
expect(seedusers).toHaveLength(1);
result = await seed.start({ logResults: false }, UserService, TaskService);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(1);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

try {
await UserService.delete({ id: seedusers[0]._id });
await UserService.delete({ id: result[0]._id });
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
Expand All @@ -97,23 +108,50 @@ describe('Configuration Tests:', () => {
process.env.NODE_ENV = nodeEnv;
});

it('should seed admin, and regular user accounts when NODE_ENV is set to "test"', async () => {
it('should seed admin, user accounts when NODE_ENV is set to "test"', async () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';
let seedusers;
let result;

try {
seedusers = await seed.start({ logResults: false }, UserService);
expect(seedusers).toBeInstanceOf(Array);
expect(seedusers).toHaveLength(2);
result = await seed.start({ logResults: false }, UserService, TaskService);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(2);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

try {
await UserService.delete({ id: seedusers[0]._id });
await UserService.delete({ id: seedusers[1]._id });
await UserService.delete({ id: result[0]._id });
await UserService.delete({ id: result[1]._id });
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

process.env.NODE_ENV = nodeEnv;
});

it('should seed admin, user accounts and tasks when NODE_ENV is set to "development"', async () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
let result;

try {
result = await seed.start({ logResults: false }, UserService, TaskService);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(4);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

try {
await UserService.delete({ id: result[0]._id });
await UserService.delete({ id: result[1]._id });
await TaskService.delete({ id: result[2]._id });
await TaskService.delete({ id: result[3]._id });
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
Expand All @@ -128,7 +166,7 @@ describe('Configuration Tests:', () => {
process.env.NODE_ENV = 'test';
let _user;
let _admin;
let seedusers;
let result;

try {
_user = await UserService.create(userFromSeedConfig);
Expand All @@ -150,17 +188,17 @@ describe('Configuration Tests:', () => {
}

try {
seedusers = await seed.start({ logResults: false }, UserService);
expect(seedusers).toBeInstanceOf(Array);
expect(seedusers).toHaveLength(2);
result = await seed.start({ logResults: false }, UserService, TaskService);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(2);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

try {
await UserService.delete({ id: seedusers[0]._id });
await UserService.delete({ id: seedusers[1]._id });
await UserService.delete({ id: result[0]._id });
await UserService.delete({ id: result[1]._id });
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
Expand All @@ -172,20 +210,20 @@ describe('Configuration Tests:', () => {
it('should ONLY seed admin user account when NODE_ENV is set to "production" with custom admin', async () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
let seedusers;
let result;

try {
seedusers = await seed.start({ logResults: false, seedAdmin: admin1 }, UserService);
expect(seedusers).toBeInstanceOf(Array);
expect(seedusers).toHaveLength(1);
expect(seedusers[0].username).toBe(admin1.username);
result = await seed.start({ logResults: false, seedAdmin: admin1 }, UserService, TaskService);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(1);
expect(result[0].username).toBe(admin1.username);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

try {
await UserService.delete({ id: seedusers[0]._id });
await UserService.delete({ id: result[0]._id });
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
Expand All @@ -197,22 +235,22 @@ describe('Configuration Tests:', () => {
it('should seed admin, and "regular" user accounts when NODE_ENV is set to "test" with custom options', async () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';
let seedusers;
let result;

try {
seedusers = await seed.start({ logResults: false, seedAdmin: admin1, seedUser: user1 }, UserService);
expect(seedusers).toBeInstanceOf(Array);
expect(seedusers).toHaveLength(2);
expect(seedusers[0].username).toBe(user1.username);
expect(seedusers[1].username).toBe(admin1.username);
result = await seed.start({ logResults: false, seedAdmin: admin1, seedUser: user1 }, UserService, TaskService);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(2);
expect(result[0].username).toBe(user1.username);
expect(result[1].username).toBe(admin1.username);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

try {
await UserService.delete({ id: seedusers[0]._id });
await UserService.delete({ id: seedusers[1]._id });
await UserService.delete({ id: result[0]._id });
await UserService.delete({ id: result[1]._id });
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
Expand All @@ -225,7 +263,7 @@ describe('Configuration Tests:', () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
let _admin;
let seedusers;
let result;

try {
_admin = await UserService.create(adminFromSeedConfig);
Expand All @@ -236,8 +274,8 @@ describe('Configuration Tests:', () => {
}

try {
seedusers = await seed.start({ logResults: false }, UserService);
expect(seedusers[0].details[0].message).toBe('Failed due to local account already exists: seedadmin');
result = await seed.start({ logResults: false }, UserService, TaskService);
expect(result[0].details[0].message).toBe('Failed due to local account already exists: seedadmin');
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
Expand Down
Loading

0 comments on commit 1b353b6

Please sign in to comment.