Skip to content

Commit

Permalink
test addTrainerToGroup route
Browse files Browse the repository at this point in the history
  • Loading branch information
RamyAlshurafa committed May 27, 2019
1 parent f205157 commit ccbf5cc
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
138 changes: 138 additions & 0 deletions v2/server/__test__/api/addTrainerToGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
const request = require('supertest');
const mongoose = require('mongoose');
const User = require('./../../database/models/User');

const buildDB = require('./../../database/data/test');
const app = require('./../../app');

describe('Tesing for addTrainerToGroup route', () => {
beforeAll(async () => {
// build dummy data
await buildDB();
});

afterAll(async () => {
await mongoose.disconnect();
});

beforeEach(async () => {
// build dummy data
await buildDB();
});

test('test with adding new trainer', async done => {
const localLead = await User.findOne({ email: '[email protected]' });

const localLeadLoginData = {
email: '[email protected]',
password: '123456',
};

const data = {
email: '[email protected]',
name: 'New',
region: 'North East',
localLead: localLead._id,
localLeadName: localLead.name,
newUser: true,
};

request(app)
.post('/api/login')
.send(localLeadLoginData)
.expect('Content-Type', /json/)
.expect(200)
.end((err, result) => {
const token = result.headers['set-cookie'][0].split(';')[0];
request(app)
.post('/api/users/local-leads/group')
.set('Cookie', [token])
.send(data)
.expect(200)
.end(async (error, response) => {
const updatedLocalLead = await User.findOne({
email: '[email protected]',
});

const createdTrainer = await User.findOne({
email: '[email protected]',
});

// new trainer must be stored in DB
expect(createdTrainer.role).toBe('trainer');

// new trainer must be belong to the local lead we sent
expect(createdTrainer.localLead).toEqual(localLead._id);

// local lead group must hove new additional trainer
expect(localLead.trainersGroup).toHaveLength(3);
expect(updatedLocalLead.trainersGroup).toHaveLength(4);

// success message
expect(response.body.success).toBe(
`New has been added to ${localLead.name}'s group`
);

done(err);
});
});
});

test('test with exists trainer', async done => {
const localLead = await User.findOne({ email: '[email protected]' });

const localLeadLoginData = {
email: '[email protected]',
password: '123456',
};

const trainer = await User.findOne({ role: 'trainer' });

const data = {
email: trainer.email,
localLead: localLead._id,
localLeadName: localLead.name,
newUser: false,
};

request(app)
.post('/api/login')
.send(localLeadLoginData)
.expect('Content-Type', /json/)
.expect(200)
.end((err, result) => {
const token = result.headers['set-cookie'][0].split(';')[0];
request(app)
.post('/api/users/local-leads/group')
.set('Cookie', [token])
.send(data)
.expect(200)
.end(async (error, response) => {
const updatedLocalLead = await User.findOne({
email: '[email protected]',
});

const updatedTrainer = await User.findOne({
email: trainer.email,
});

// new trainer must be stored in DB
expect(updatedTrainer.role).toBe('trainer');

// new trainer must be belong to the local lead we sent
expect(updatedTrainer.localLead).toEqual(localLead._id);

// local lead group must hove new additional trainer
expect(localLead.trainersGroup).toHaveLength(0);
expect(updatedLocalLead.trainersGroup).toHaveLength(1);

// success message
expect(response.body.success).toBe(
`${trainer.name} has been added to ${localLead.name}'s group`
);

done(err);
});
});
});
});
5 changes: 4 additions & 1 deletion v2/server/controllers/users/addTrainerToGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
addTrainertoGroup,
} = require('./../../database/queries/users/localLead');

const { getUserByEmail } = require('./../../database/queries/users');
const { getUserByEmail, update } = require('./../../database/queries/users');

module.exports = async (req, res, next) => {
const { name, email, newUser, localLead, region, localLeadName } = req.body;
Expand Down Expand Up @@ -35,6 +35,9 @@ module.exports = async (req, res, next) => {
}

await addTrainertoGroup(localLead, trainer._id);

await update(trainer._id, { localLead });

return res.json({
success: `${trainer.name} has been added to ${localLeadName}'s group`,
});
Expand Down
1 change: 1 addition & 0 deletions v2/server/database/queries/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ const User = require('./../../models/User');

module.exports.getUserByEmail = email => User.findOne({ email });
module.exports.getUserById = id => User.findById(id);
module.exports.update = (id, data) => User.findByIdAndUpdate(id, data);

0 comments on commit ccbf5cc

Please sign in to comment.