From adf239c030553c2c3152448211a5f15f72a1e2aa Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Sat, 10 Oct 2020 12:22:46 +0100 Subject: [PATCH 01/10] test coverage improved --- package-lock.json | 5 + package.json | 1 + src/services/userServices.js | 10 +- src/test/authTest.js | 280 ++++++++++++------------ src/test/courseTest.js | 400 +++++++++++++++++------------------ 5 files changed, 340 insertions(+), 356 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7758f64..166b0d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4651,6 +4651,11 @@ "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=", "dev": true }, + "faker": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/faker/-/faker-5.1.0.tgz", + "integrity": "sha512-RrWKFSSA/aNLP0g3o2WW1Zez7/MnMr7xkiZmoCfAGZmdkDQZ6l2KtuXHN5XjdvpRjDl8+3vf+Rrtl06Z352+Mw==" + }, "fast-deep-equal": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", diff --git a/package.json b/package.json index 71853e8..2f9a2ef 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "dotenv": "^7.0.0", "errorhandler": "^1.5.1", "express": "^4.17.1", + "faker": "5.1.0", "jsonwebtoken": "^8.5.1", "mocha": "^8.1.1", "moment": "^2.27.0", diff --git a/src/services/userServices.js b/src/services/userServices.js index 2941fe4..74d1abe 100644 --- a/src/services/userServices.js +++ b/src/services/userServices.js @@ -1,4 +1,4 @@ -import { User } from "../models"; +import { User } from '../models'; /** * User service Class @@ -39,7 +39,7 @@ export default class UserServices { const userDetails = await User.update( { [name]: value }, { where: { id } }, - { returning: true } + { returning: true }, ); return userDetails; } @@ -74,7 +74,7 @@ export default class UserServices { Address, gender, phoneNumber, - memberType + memberType, } = attribute; const userDetails = await User.update( { @@ -85,9 +85,9 @@ export default class UserServices { gender, Address, phoneNumber, - memberType + memberType, }, - { where: { email }, returning: true, plain: true } + { where: { email }, returning: true, plain: true }, ); const result = userDetails[1].dataValues; delete result.password; diff --git a/src/test/authTest.js b/src/test/authTest.js index d4c1879..422b255 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -1,7 +1,11 @@ import chai from 'chai'; import chaiHttp from 'chai-http'; +import faker from 'faker'; + import app from '../app'; +// const expect = chai.expect; + chai.should(); chai.use(chaiHttp); @@ -9,15 +13,62 @@ chai.use(chaiHttp); const apiEndPoint = '/api/v1/'; const userEndPoint = `${apiEndPoint}auth/`; -describe('Signup Error', () => { +const user = { + email: faker.internet.email(), + password: faker.internet.password(), +}; + +const userWithNoEmail = { + password: '1234', + firstName: 'ade', + lastName: 'wale', +}; + +const userEmailWithNoKeyValue = { + email: '', + password: '1234', + firstName: 'ade', + lastName: 'wale', +}; + +const userWithNoPassword = { + email: 'ad@gmail.com', + firstName: 'ade', + lastName: 'wale', +}; + +describe('Signup User', () => { + it('Should create a new user', (done) => { + chai.request(app) + .post(`${userEndPoint}signup`) + .send(user) + .end((err, res) => { + res.should.have.status(201); + res.body.should.be.a('object'); + res.body.should.have.property('data'); + res.body.data.should.be.a('object'); + res.body.data.should.have.property('tokens'); + res.body.data.should.have.property('id'); + res.body.data.should.have.property('email'); + done(); + }); + }); + it('Should return 409 if email already exists', (done) => { + chai.request(app) + .post(`${userEndPoint}signup`) + .send(user) + .end((err, res) => { + res.should.have.status(409); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); it('Should return 400 if password is omitted', (done) => { - const user = { - email: 'temi@testmail.com', - }; chai .request(app) .post(`${userEndPoint}signup`) - .send(user) + .send(userWithNoPassword) .end((err, res) => { res.should.have.status(400); res.body.should.be.a('object'); @@ -25,17 +76,12 @@ describe('Signup Error', () => { done(); }); }); -}); -describe('Signup Error', () => { it('Should return 400 if email is omitted', (done) => { - const user = { - password: 'password', - }; chai .request(app) .post(`${userEndPoint}signup`) - .send(user) + .send(userWithNoEmail) .end((err, res) => { res.should.have.status(400); res.body.should.be.a('object'); @@ -43,20 +89,12 @@ describe('Signup Error', () => { done(); }); }); -}); -describe('Signup Error', () => { it('Should return 400 if email key is provided without value', (done) => { - const user = { - firstName: 'temi', - lastName: 'otokurfor', - email: ' ', - password: 'password', - }; chai .request(app) .post(`${userEndPoint}signup`) - .send(user) + .send(userEmailWithNoKeyValue) .end((err, res) => { res.should.have.status(400); res.body.should.be.a('object'); @@ -66,132 +104,76 @@ describe('Signup Error', () => { }); }); -// describe(`POST ${userEndPoint}signup`, () => { -// it('Should create a new user', (done) => { -// const user = { -// email: 'temi@testmail.com', -// password: 'password', -// }; -// chai.request(app) -// .post(`${userEndPoint}signup`) -// .send(user) -// .end((err, res) => { -// res.should.have.status(201); -// res.body.should.be.a('object'); -// res.body.should.have.property('data'); -// res.body.data.should.be.a('object'); -// res.body.data.should.have.property('tokens'); -// res.body.data.should.have.property('id'); -// res.body.data.should.have.property('email'); -// done(); -// }); -// }); -// }); - -// describe('Signup Error', () => { -// it('Should return 409 if email already exists', (done) => { -// const user = { -// email: 'temi@testmail.com', -// password: 'password', -// }; -// chai.request(app) -// .post(`${userEndPoint}signup`) -// .send(user) -// .end((err, res) => { -// res.should.have.status(409); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); - -// describe('Signin Error', () => { -// it('Should deny access if wrong email is provided', (done) => { -// const login = { -// email: 'kcmykirl@gmail.com', -// password: 'pA55w0rd', -// }; -// chai.request(app) -// .post(`${userEndPoint}signin`) -// .send(login) -// .end((err, res) => { -// res.should.have.status(404); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); - -// describe('Signin Error', () => { -// it('Should deny access if wrong password is provided', (done) => { -// const login = { -// email: 'temi@testmail.com', -// password: 'passweod', -// }; -// chai.request(app) -// .post(`${userEndPoint}signin`) -// .send(login) -// .end((err, res) => { -// res.should.have.status(401); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); - -// describe('Signin Errror', () => { -// it('Should return 400 if email is not provided', (done) => { -// const login = { -// password: 'password', -// }; -// chai.request(app) -// .post(`${userEndPoint}signin`) -// .send(login) -// .end((err, res) => { -// res.should.have.status(400); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); - -// describe('Signin Error', () => { -// it('Should return 400 if password is ommited', (done) => { -// const login = { -// email: 'temi@testmail.com', -// }; -// chai.request(app) -// .post(`${userEndPoint}signin`) -// .send(login) -// .end((err, res) => { -// res.should.have.status(400); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); - -// describe('User Login tests', () => { -// it('Should login a user successfully', (done) => { -// const login = { -// email: 'temi@testmail.com', -// password: 'password', -// }; -// chai.request(app) -// .post(`${userEndPoint}signin`) -// .send(login) -// .end((err, res) => { -// res.should.have.status(200); -// res.body.should.be.a('object'); -// res.body.should.have.property('data'); -// res.body.data.should.be.a('object'); -// res.body.data.should.have.property('tokens'); -// done(); -// }); -// }); -// }); +describe('Signin', () => { + it('Should login a user successfully', (done) => { + chai.request(app) + .post(`${userEndPoint}signin`) + .send(user) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a('object'); + res.body.should.have.property('data'); + res.body.data.should.be.a('object'); + res.body.data.should.have.property('tokens'); + done(); + }); + }); + it('Should deny access if wrong email is provided', (done) => { + const login = { + email: 'thisEmailDoesNotExist@gmail.com', + password: 'pA55w0rd', + }; + chai.request(app) + .post(`${userEndPoint}signin`) + .send(login) + .end((err, res) => { + res.should.have.status(404); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + it('Should deny access if wrong password is provided', (done) => { + const login = { + email: user.email, + password: 'justarandominvalidpassword', + }; + chai.request(app) + .post(`${userEndPoint}signin`) + .send(login) + .end((err, res) => { + res.should.have.status(401); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + it('Should return 400 if password is ommited', (done) => { + const login = { + email: user.email, + }; + chai.request(app) + .post(`${userEndPoint}signin`) + .send(login) + .end((err, res) => { + res.should.have.status(400); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + it('Should return 400 if email is not provided', (done) => { + const login = { + password: 'password', + }; + chai.request(app) + .post(`${userEndPoint}signin`) + .send(login) + .end((err, res) => { + res.should.have.status(400); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); +}); diff --git a/src/test/courseTest.js b/src/test/courseTest.js index 158e670..4e118ff 100644 --- a/src/test/courseTest.js +++ b/src/test/courseTest.js @@ -1,78 +1,50 @@ import chai from 'chai'; import chaiHttp from 'chai-http'; import app from '../app'; -// import Utils from '../utils'; +import Utils from '../utils'; chai.should(); chai.use(chaiHttp); const apiEndPoint = '/api/v1/'; -// const adminEmail = 'victorawotidebe@gmail.com'; -// const notAdminEmail = 'rmissen0@adobe.com'; -// const token = Utils.generateToken({ email: adminEmail }); -// const nonAdminToken = Utils.generateToken({ email: notAdminEmail }); +const adminEmail = 'victorawotidebe@gmail.com'; +const notAdminEmail = 'rmissen0@adobe.com'; +const token = Utils.generateToken({ email: adminEmail }); +const nonAdminToken = Utils.generateToken({ email: notAdminEmail }); -// describe('Create Courses Tests', () => { -// it('Should return 403 if user is not an admin', (done) => { -// const course = { -// CourseTitle: 'Facility Management', -// memberFees: 105000, -// nonmemberFee: 115000, -// startDate: 10, -// endDate: 13, -// }; - -// chai.request(app) -// .post(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${nonAdminToken}`) -// .send(course) -// .end((err, res) => { -// if (err) done(err); -// res.should.have.status(403); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); - -// describe('Create Courses Tests', () => { -// it('Should Successfully Create a Course', (done) => { -// const course = { -// courseTitle: 'Facility Management', -// memberFees: 105000, -// nonMemberFees: 115000, -// startDate: 10, -// endDate: 13, -// duration: '10 weeks', -// venue: 'illupeju', -// }; -// chai.request(app) -// .post(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${token}`) -// .send(course) -// .end((err, res) => { -// if (err) done(err); -// res.should.have.status(201); -// res.body.should.be.a('object'); -// res.body.should.have.property('status'); -// res.body.should.have.property('data'); -// res.body.data.should.be.a('object'); -// res.body.data.should.have.property('id'); -// res.body.data.should.have.property('courseTitle'); -// res.body.data.should.have.property('memberFees'); -// res.body.data.should.have.property('duration'); -// res.body.data.should.have.property('venue'); -// res.body.data.should.have.property('nonMemberFees'); -// res.body.data.should.have.property('startDate'); -// res.body.data.should.have.property('endDate'); -// done(); -// }); -// }); -// }); +// const courseData = { +// courseTitle: 'Facility Management', +// memberFees: 105000, +// nonMemberFees: 115000, +// startDate: 10, +// endDate: 13, +// duration: '10 weeks', +// venue: 'illupeju', +// }; describe('Create Courses Tests', () => { + it('Should return 403 if user is not an admin', (done) => { + const course = { + CourseTitle: 'Facility Management', + memberFees: 105000, + nonmemberFee: 115000, + startDate: 10, + endDate: 13, + }; + + chai.request(app) + .post(`${apiEndPoint}courses`) + .set('Authorization', `Bearer ${nonAdminToken}`) + .send(course) + .end((err, res) => { + if (err) done(err); + res.should.have.status(403); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); it('Should return 401 if token is not provided', (done) => { const course = { courseTitle: 'Facility Management', @@ -92,154 +64,178 @@ describe('Create Courses Tests', () => { done(); }); }); + it('Should return 400 if courseTitle isn\'t specified', (done) => { + const course = { + memberFees: 105000, + nonmemberFee: 115000, + startDate: 10, + endDate: 13, + }; + + chai.request(app) + .post(`${apiEndPoint}courses`) + .set('Authorization', `Bearer ${token}`) + .send(course) + .end((err, res) => { + res.should.have.status(400); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + it('Should return 400 if memberFees isn\'t specified', (done) => { + const course = { + CourseTitle: 'Facility Management', + nonmemberFee: 115000, + startDate: 10, + endDate: 13, + }; + chai.request(app) + .post(`${apiEndPoint}courses`) + .set('Authorization', `Bearer ${token}`) + .send(course) + .end((err, res) => { + res.should.have.status(400); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + it('Should return 400 if nonmemberFee isn\'t specified', (done) => { + const course = { + CourseTitle: 'Facility Management', + memberFees: 105000, + startDate: 10, + endDate: 13, + }; + chai.request(app) + .post(`${apiEndPoint}courses`) + .set('Authorization', `Bearer ${token}`) + .send(course) + .end((err, res) => { + res.should.have.status(400); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + it('Should return 400 if endDate isn\'t specified', (done) => { + const course = { + CourseTitle: 'Facility Management', + memberFees: 105000, + nonmemberFee: 115000, + startDate: 10, + }; + chai.request(app) + .post(`${apiEndPoint}courses`) + .set('Authorization', `Bearer ${token}`) + .send(course) + .end((err, res) => { + res.should.have.status(400); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + it('Should return 400 if startDate isn\'t specified', (done) => { + const course = { + CourseTitle: 'Facility Management', + memberFees: 105000, + nonmemberFee: 115000, + endDate: 13, + }; + chai.request(app) + .post(`${apiEndPoint}courses`) + .set('Authorization', `Bearer ${token}`) + .send(course) + .end((err, res) => { + res.should.have.status(400); + res.body.should.be.a('object'); + res.body.should.have.property('error'); + done(); + }); + }); + // it('Should register a user for a course', (done) => { + // chai.request(app) + // .post(`${apiEndPoint}courses/2/register`) + // .set('Authorization', `Bearer ${token}`) + // .send(token) + // .end((err, res) => { + // res.should.have.status(201); + // res.body.should.be.an('object'); + // res.body.should.have.property('status'); + // res.body.should.have.property('data'); + // res.body.data.should.be.an('object'); + // res.body.data.should.have.property('id'); + // res.body.data.should.have.property('UserId'); + // res.body.data.should.have.property('courseId'); + // res.body.data.should.have.property('registeredOn'); + // done(); + // }); + // }); + // it('should return 200 and get all courses', (done) => { + // chai.request(app) + // .get(`${apiEndPoint}courses`) + // .set('Authorization', `Bearer ${token}`) + // .send(token) + // .end((err, res) => { + // res.should.have.status(200); + // res.body.should.be.an('object'); + // res.body.should.have.property('status'); + // res.body.should.have.property('data'); + // res.body.data.should.be.an('array'); + // res.body.data[0].should.have.property('id'); + // res.body.data[0].should.have.property('courseTitle'); + // res.body.data[0].should.have.property('memberFees'); + // res.body.data[0].should.have.property('nonMemberFees'); + // res.body.data[0].should.have.property('startDate'); + // res.body.data[0].should.have.property('endDate'); + // done(); + // }); + // }); }); // describe('Create Courses Tests', () => { -// it('Should return 400 if courseTitle isn\'t specified', (done) => { -// const course = { -// memberFees: 105000, -// nonmemberFee: 115000, -// startDate: 10, -// endDate: 13, -// }; - -// chai.request(app) -// .post(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${token}`) -// .send(course) -// .end((err, res) => { -// res.should.have.status(400); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); -// describe('Create Courses', () => { -// it('Should return 400 if memberFees isn\'t specified', (done) => { -// const course = { -// CourseTitle: 'Facility Management', -// nonmemberFee: 115000, -// startDate: 10, -// endDate: 13, -// }; -// chai.request(app) -// .post(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${token}`) -// .send(course) -// .end((err, res) => { -// res.should.have.status(400); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); // }); -// describe('Create Courses', () => { -// it('Should return 400 if nonmemberFee isn\'t specified', (done) => { -// const course = { -// CourseTitle: 'Facility Management', -// memberFees: 105000, -// startDate: 10, -// endDate: 13, -// }; -// chai.request(app) -// .post(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${token}`) -// .send(course) -// .end((err, res) => { -// res.should.have.status(400); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); +// describe('Get requests for both user and admin', () => { -// describe('Ceate Courses', () => { -// it('Should return 400 if endDate isn\'t specified', (done) => { -// const course = { -// CourseTitle: 'Facility Management', -// memberFees: 105000, -// nonmemberFee: 115000, -// startDate: 10, -// }; -// chai.request(app) -// .post(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${token}`) -// .send(course) -// .end((err, res) => { -// res.should.have.status(400); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); // }); -// describe('Create Courses Tests', () => { -// it('Should return 400 if startDate isn\'t specified', (done) => { -// const course = { -// CourseTitle: 'Facility Management', -// memberFees: 105000, -// nonmemberFee: 115000, -// endDate: 13, -// }; -// chai.request(app) -// .post(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${token}`) -// .send(course) -// .end((err, res) => { -// res.should.have.status(400); -// res.body.should.be.a('object'); -// res.body.should.have.property('error'); -// done(); -// }); -// }); -// }); +// describe('Register a courses', () => { -// describe('Get requests for both user and admin', () => { -// it('should return 200 and get all courses', (done) => { -// chai.request(app) -// .get(`${apiEndPoint}courses`) -// .set('Authorization', `Bearer ${token}`) -// .send(token) -// .end((err, res) => { -// res.should.have.status(200); -// res.body.should.be.an('object'); -// res.body.should.have.property('status'); -// res.body.should.have.property('data'); -// res.body.data.should.be.an('array'); -// res.body.data[0].should.have.property('id'); -// res.body.data[0].should.have.property('courseTitle'); -// res.body.data[0].should.have.property('memberFees'); -// res.body.data[0].should.have.property('nonMemberFees'); -// res.body.data[0].should.have.property('startDate'); -// res.body.data[0].should.have.property('endDate'); -// done(); -// }); -// }); // }); -// describe('Register a courses', () => { -// it('Should register a user for a course', (done) => { -// chai.request(app) -// .post(`${apiEndPoint}courses/2/register`) -// .set('Authorization', `Bearer ${token}`) -// .send(token) -// .end((err, res) => { -// res.should.have.status(201); -// res.body.should.be.an('object'); -// res.body.should.have.property('status'); -// res.body.should.have.property('data'); -// res.body.data.should.be.an('object'); -// res.body.data.should.have.property('id'); -// res.body.data.should.have.property('UserId'); -// res.body.data.should.have.property('courseId'); -// res.body.data.should.have.property('registeredOn'); -// done(); -// }); -// }); -// }); +// it('Should Successfully Create a Course', (done) => { +// const course = { +// courseTitle: 'Facility Management', +// memberFees: 105000, +// nonMemberFees: 115000, +// startDate: 10, +// endDate: 13, +// duration: '10 weeks', +// venue: 'illupeju', +// }; +// chai.request(app) +// .post(`${apiEndPoint}courses`) +// .set('Authorization', `Bearer ${token}`) +// .send(course) +// .end((err, res) => { +// if (err) done(err); +// res.should.have.status(201); +// res.body.should.be.a('object'); +// res.body.should.have.property('status'); +// res.body.should.have.property('data'); +// res.body.data.should.be.a('object'); +// res.body.data.should.have.property('id'); +// res.body.data.should.have.property('courseTitle'); +// res.body.data.should.have.property('memberFees'); +// res.body.data.should.have.property('duration'); +// res.body.data.should.have.property('venue'); +// res.body.data.should.have.property('nonMemberFees'); +// res.body.data.should.have.property('startDate'); +// res.body.data.should.have.property('endDate'); +// done(); +// }); +// }); \ No newline at end of file From 585682c2cb153fa3fcf2110abedc97bb450ccfca Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Tue, 13 Oct 2020 15:47:50 +0100 Subject: [PATCH 02/10] more test --- package.json | 2 +- src/test/authTest.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 2f9a2ef..29d0489 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "node ./src/bin/www", "devstart": "set DEBUG=dev && babel-watch ./src/bin/dev", "build": "rm -rf dist && mkdir dist && babel src -s -d dist", - "test": " set NODE_ENV=test&& npm run reset-db&& nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 10000", + "test": " set NODE_ENV=test && npm run reset-db && nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 10000", "migrate": "./node_modules/.bin/sequelize db:migrate", "drop-db": "./node_modules/.bin/sequelize db:migrate:undo:all", "seeders": "./node_modules/.bin/sequelize db:seed:all", diff --git a/src/test/authTest.js b/src/test/authTest.js index 422b255..ced1e3a 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -43,6 +43,7 @@ describe('Signup User', () => { .post(`${userEndPoint}signup`) .send(user) .end((err, res) => { + console.log(res.body, 'RESPONSE'); res.should.have.status(201); res.body.should.be.a('object'); res.body.should.have.property('data'); From 83d53b8b6d0f45f64b71efd0c252ab2016b31af4 Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Wed, 14 Oct 2020 14:57:43 +0100 Subject: [PATCH 03/10] log travis error --- src/test/authTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/authTest.js b/src/test/authTest.js index ced1e3a..67e8423 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -43,7 +43,7 @@ describe('Signup User', () => { .post(`${userEndPoint}signup`) .send(user) .end((err, res) => { - console.log(res.body, 'RESPONSE'); + console.log(err, 'ERROR RESPONSE'); res.should.have.status(201); res.body.should.be.a('object'); res.body.should.have.property('data'); From 1bf1b615a93dd7f0be44229033ef887891e2c9ff Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Wed, 14 Oct 2020 16:43:37 +0100 Subject: [PATCH 04/10] this should fix it --- src/test/authTest.js | 1 - src/test/courseTest.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/authTest.js b/src/test/authTest.js index 67e8423..422b255 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -43,7 +43,6 @@ describe('Signup User', () => { .post(`${userEndPoint}signup`) .send(user) .end((err, res) => { - console.log(err, 'ERROR RESPONSE'); res.should.have.status(201); res.body.should.be.a('object'); res.body.should.have.property('data'); diff --git a/src/test/courseTest.js b/src/test/courseTest.js index 4e118ff..98ca5fe 100644 --- a/src/test/courseTest.js +++ b/src/test/courseTest.js @@ -238,4 +238,4 @@ describe('Create Courses Tests', () => { // res.body.data.should.have.property('endDate'); // done(); // }); -// }); \ No newline at end of file +// }); From 7f2a3cfddd977111818e820c76b12c33eff43500 Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Wed, 14 Oct 2020 17:03:35 +0100 Subject: [PATCH 05/10] increase script time --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 29d0489..c1e3705 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "node ./src/bin/www", "devstart": "set DEBUG=dev && babel-watch ./src/bin/dev", "build": "rm -rf dist && mkdir dist && babel src -s -d dist", - "test": " set NODE_ENV=test && npm run reset-db && nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 10000", + "test": " set NODE_ENV=test && npm run reset-db && nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 1000000", "migrate": "./node_modules/.bin/sequelize db:migrate", "drop-db": "./node_modules/.bin/sequelize db:migrate:undo:all", "seeders": "./node_modules/.bin/sequelize db:seed:all", From 825b3bf8d135d1c0d847ea1428f8f057fe5a7c3f Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Wed, 14 Oct 2020 18:22:01 +0100 Subject: [PATCH 06/10] async and await test --- package-lock.json | 106 +++++++++++++++ package.json | 4 +- src/test/authTest.js | 318 +++++++++++++++++++++---------------------- src/test/fixTest.js | 53 ++++++++ 4 files changed, 321 insertions(+), 160 deletions(-) create mode 100644 src/test/fixTest.js diff --git a/package-lock.json b/package-lock.json index 166b0d5..8d756eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2155,6 +2155,46 @@ "resolved": "https://registry.npmjs.org/@jsdevtools/ono/-/ono-7.1.3.tgz", "integrity": "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==" }, + "@sinonjs/commons": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.1.tgz", + "integrity": "sha512-892K+kWUUi3cl+LlqEWIDrhvLgdL79tECi8JZUyq6IviKy/DNhuzCRlbHUjxK89f4ypPMMaFnFuR9Ie6DoIMsw==", + "requires": { + "type-detect": "4.0.8" + } + }, + "@sinonjs/fake-timers": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-6.0.1.tgz", + "integrity": "sha512-MZPUxrmFubI36XS1DI3qmI0YdN1gks62JtFZvxR67ljjSNCeK6U08Zx4msEWOXuofgqUt6zPHSi1H9fbjR/NRA==", + "requires": { + "@sinonjs/commons": "^1.7.0" + } + }, + "@sinonjs/formatio": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-5.0.1.tgz", + "integrity": "sha512-KaiQ5pBf1MpS09MuA0kp6KBQt2JUOQycqVG1NZXvzeaXe5LGFqAKueIS0bw4w0P9r7KuBSVdUk5QjXsUdu2CxQ==", + "requires": { + "@sinonjs/commons": "^1", + "@sinonjs/samsam": "^5.0.2" + } + }, + "@sinonjs/samsam": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-5.2.0.tgz", + "integrity": "sha512-CaIcyX5cDsjcW/ab7HposFWzV1kC++4HNsfnEdFJa7cP1QIuILAKV+BgfeqRXhcnSAc76r/Rh/O5C+300BwUIw==", + "requires": { + "@sinonjs/commons": "^1.6.0", + "lodash.get": "^4.4.2", + "type-detect": "^4.0.8" + } + }, + "@sinonjs/text-encoding": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.1.tgz", + "integrity": "sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==" + }, "@types/chai": { "version": "4.1.7", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.1.7.tgz", @@ -6453,6 +6493,11 @@ "verror": "1.10.0" } }, + "just-extend": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-4.1.1.tgz", + "integrity": "sha512-aWgeGFW67BP3e5181Ep1Fv2v8z//iBJfrvyTnq8wG86vEESwmonn1zPBJ0VfmT9CJq2FIT0VsETtrNFm2a+SHA==" + }, "jwa": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", @@ -7201,6 +7246,33 @@ "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==" }, + "nise": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/nise/-/nise-4.0.4.tgz", + "integrity": "sha512-bTTRUNlemx6deJa+ZyoCUTRvH3liK5+N6VQZ4NIw90AgDXY6iPnsqplNFf6STcj+ePk0H/xqxnP75Lr0J0Fq3A==", + "requires": { + "@sinonjs/commons": "^1.7.0", + "@sinonjs/fake-timers": "^6.0.0", + "@sinonjs/text-encoding": "^0.7.1", + "just-extend": "^4.0.2", + "path-to-regexp": "^1.7.0" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "path-to-regexp": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.8.0.tgz", + "integrity": "sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==", + "requires": { + "isarray": "0.0.1" + } + } + } + }, "node-addon-api": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.0.0.tgz", @@ -8595,6 +8667,40 @@ "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" }, + "sinon": { + "version": "9.2.0", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-9.2.0.tgz", + "integrity": "sha512-eSNXz1XMcGEMHw08NJXSyTHIu6qTCOiN8x9ODACmZpNQpr0aXTBXBnI4xTzQzR+TEpOmLiKowGf9flCuKIzsbw==", + "requires": { + "@sinonjs/commons": "^1.8.1", + "@sinonjs/fake-timers": "^6.0.1", + "@sinonjs/formatio": "^5.0.1", + "@sinonjs/samsam": "^5.2.0", + "diff": "^4.0.2", + "nise": "^4.0.4", + "supports-color": "^7.1.0" + }, + "dependencies": { + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + } + } + }, + "sinon-chai": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/sinon-chai/-/sinon-chai-3.5.0.tgz", + "integrity": "sha512-IifbusYiQBpUxxFJkR3wTU68xzBN0+bxCScEaKMjBvAQERg6FnTTc1F17rseLb1tjmkJ23730AXpFI0c47FgAg==" + }, "slash": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz", diff --git a/package.json b/package.json index c1e3705..3420139 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "node ./src/bin/www", "devstart": "set DEBUG=dev && babel-watch ./src/bin/dev", "build": "rm -rf dist && mkdir dist && babel src -s -d dist", - "test": " set NODE_ENV=test && npm run reset-db && nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 1000000", + "test": " set NODE_ENV=test && npm run reset-db && nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 10000", "migrate": "./node_modules/.bin/sequelize db:migrate", "drop-db": "./node_modules/.bin/sequelize db:migrate:undo:all", "seeders": "./node_modules/.bin/sequelize db:seed:all", @@ -64,6 +64,8 @@ "pg": "^7.18.2", "pg-hstore": "^2.3.3", "sequelize": "^5.22.3", + "sinon": "9.2.0", + "sinon-chai": "3.5.0", "swagger-jsdoc": "^4.0.0", "swagger-ui-express": "^4.1.4" }, diff --git a/src/test/authTest.js b/src/test/authTest.js index 422b255..6a8f488 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -1,8 +1,8 @@ import chai from 'chai'; import chaiHttp from 'chai-http'; -import faker from 'faker'; +// import faker from 'faker'; -import app from '../app'; +// import app from '../app'; // const expect = chai.expect; @@ -10,170 +10,170 @@ chai.should(); chai.use(chaiHttp); -const apiEndPoint = '/api/v1/'; -const userEndPoint = `${apiEndPoint}auth/`; +// const apiEndPoint = '/api/v1/'; +// const userEndPoint = `${apiEndPoint}auth/`; -const user = { - email: faker.internet.email(), - password: faker.internet.password(), -}; +// const user = { +// email: faker.internet.email(), +// password: faker.internet.password(), +// }; -const userWithNoEmail = { - password: '1234', - firstName: 'ade', - lastName: 'wale', -}; +// const userWithNoEmail = { +// password: '1234', +// firstName: 'ade', +// lastName: 'wale', +// }; -const userEmailWithNoKeyValue = { - email: '', - password: '1234', - firstName: 'ade', - lastName: 'wale', -}; +// const userEmailWithNoKeyValue = { +// email: '', +// password: '1234', +// firstName: 'ade', +// lastName: 'wale', +// }; -const userWithNoPassword = { - email: 'ad@gmail.com', - firstName: 'ade', - lastName: 'wale', -}; +// const userWithNoPassword = { +// email: 'ad@gmail.com', +// firstName: 'ade', +// lastName: 'wale', +// }; describe('Signup User', () => { - it('Should create a new user', (done) => { - chai.request(app) - .post(`${userEndPoint}signup`) - .send(user) - .end((err, res) => { - res.should.have.status(201); - res.body.should.be.a('object'); - res.body.should.have.property('data'); - res.body.data.should.be.a('object'); - res.body.data.should.have.property('tokens'); - res.body.data.should.have.property('id'); - res.body.data.should.have.property('email'); - done(); - }); - }); - it('Should return 409 if email already exists', (done) => { - chai.request(app) - .post(`${userEndPoint}signup`) - .send(user) - .end((err, res) => { - res.should.have.status(409); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); - it('Should return 400 if password is omitted', (done) => { - chai - .request(app) - .post(`${userEndPoint}signup`) - .send(userWithNoPassword) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); + // it('Should create a new user', (done) => { + // chai.request(app) + // .post(`${userEndPoint}signup`) + // .send(user) + // .end((err, res) => { + // res.should.have.status(201); + // res.body.should.be.a('object'); + // res.body.should.have.property('data'); + // res.body.data.should.be.a('object'); + // res.body.data.should.have.property('tokens'); + // res.body.data.should.have.property('id'); + // res.body.data.should.have.property('email'); + // done(); + // }); + // }); +// it('Should return 409 if email already exists', (done) => { +// chai.request(app) +// .post(`${userEndPoint}signup`) +// .send(user) +// .end((err, res) => { +// res.should.have.status(409); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); +// it('Should return 400 if password is omitted', (done) => { +// chai +// .request(app) +// .post(`${userEndPoint}signup`) +// .send(userWithNoPassword) +// .end((err, res) => { +// res.should.have.status(400); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); - it('Should return 400 if email is omitted', (done) => { - chai - .request(app) - .post(`${userEndPoint}signup`) - .send(userWithNoEmail) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); +// it('Should return 400 if email is omitted', (done) => { +// chai +// .request(app) +// .post(`${userEndPoint}signup`) +// .send(userWithNoEmail) +// .end((err, res) => { +// res.should.have.status(400); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); - it('Should return 400 if email key is provided without value', (done) => { - chai - .request(app) - .post(`${userEndPoint}signup`) - .send(userEmailWithNoKeyValue) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); -}); +// it('Should return 400 if email key is provided without value', (done) => { +// chai +// .request(app) +// .post(`${userEndPoint}signup`) +// .send(userEmailWithNoKeyValue) +// .end((err, res) => { +// res.should.have.status(400); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); +// }); -describe('Signin', () => { - it('Should login a user successfully', (done) => { - chai.request(app) - .post(`${userEndPoint}signin`) - .send(user) - .end((err, res) => { - res.should.have.status(200); - res.body.should.be.a('object'); - res.body.should.have.property('data'); - res.body.data.should.be.a('object'); - res.body.data.should.have.property('tokens'); - done(); - }); - }); - it('Should deny access if wrong email is provided', (done) => { - const login = { - email: 'thisEmailDoesNotExist@gmail.com', - password: 'pA55w0rd', - }; - chai.request(app) - .post(`${userEndPoint}signin`) - .send(login) - .end((err, res) => { - res.should.have.status(404); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); - it('Should deny access if wrong password is provided', (done) => { - const login = { - email: user.email, - password: 'justarandominvalidpassword', - }; - chai.request(app) - .post(`${userEndPoint}signin`) - .send(login) - .end((err, res) => { - res.should.have.status(401); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); - it('Should return 400 if password is ommited', (done) => { - const login = { - email: user.email, - }; - chai.request(app) - .post(`${userEndPoint}signin`) - .send(login) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); - it('Should return 400 if email is not provided', (done) => { - const login = { - password: 'password', - }; - chai.request(app) - .post(`${userEndPoint}signin`) - .send(login) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); +// describe('Signin', () => { +// it('Should login a user successfully', (done) => { +// chai.request(app) +// .post(`${userEndPoint}signin`) +// .send(user) +// .end((err, res) => { +// res.should.have.status(200); +// res.body.should.be.a('object'); +// res.body.should.have.property('data'); +// res.body.data.should.be.a('object'); +// res.body.data.should.have.property('tokens'); +// done(); +// }); +// }); +// it('Should deny access if wrong email is provided', (done) => { +// const login = { +// email: 'thisEmailDoesNotExist@gmail.com', +// password: 'pA55w0rd', +// }; +// chai.request(app) +// .post(`${userEndPoint}signin`) +// .send(login) +// .end((err, res) => { +// res.should.have.status(404); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); +// it('Should deny access if wrong password is provided', (done) => { +// const login = { +// email: user.email, +// password: 'justarandominvalidpassword', +// }; +// chai.request(app) +// .post(`${userEndPoint}signin`) +// .send(login) +// .end((err, res) => { +// res.should.have.status(401); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); +// it('Should return 400 if password is ommited', (done) => { +// const login = { +// email: user.email, +// }; +// chai.request(app) +// .post(`${userEndPoint}signin`) +// .send(login) +// .end((err, res) => { +// res.should.have.status(400); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); +// it('Should return 400 if email is not provided', (done) => { +// const login = { +// password: 'password', +// }; +// chai.request(app) +// .post(`${userEndPoint}signin`) +// .send(login) +// .end((err, res) => { +// res.should.have.status(400); +// res.body.should.be.a('object'); +// res.body.should.have.property('error'); +// done(); +// }); +// }); }); diff --git a/src/test/fixTest.js b/src/test/fixTest.js new file mode 100644 index 0000000..2130379 --- /dev/null +++ b/src/test/fixTest.js @@ -0,0 +1,53 @@ +import chai from 'chai'; +import chaiHttp from 'chai-http'; +import faker from 'faker'; +import sinon from 'sinon'; +import sinonChai from 'sinon-chai'; +import app from '../app'; + +chai.use(chaiHttp); +chai.use(sinonChai); + +const { expect } = chai; + +let request = chai.request(app).keepOpen(); +// let token; + +const apiEndPoint = '/api/v1/'; +const userEndPoint = `${apiEndPoint}auth/`; + +const userData = { + email: faker.internet.email(), + password: faker.internet.password(), +}; + +const userWithNoPassword = { + email: 'ad@gmail.com', +}; + + +describe('AUTH', () => { + before(async () => { + request = chai.request(app).keepOpen(); + }); + + afterEach(async () => { + sinon.restore(); + }); + + describe('SignUp a user', () => { + it('should sign up user', async () => { + const response = await request.post(`${userEndPoint}signup`).send(userData); + expect(response.status).to.equal(201); + expect(response.body).to.be.a('object'); + }).timeout(0); + it('Should return 400 Bad Request', async () => { + const response = await request.post(`${userEndPoint}signup`).send(userWithNoPassword); + expect(response.status).to.equal(400); + }).timeout(0); + it('should throw USER already exist error', async () => { + const response = await request.post(`${userEndPoint}signup`).send(userData); + expect(response.status).to.equal(409); + }).timeout(0); + }); +}); From e60187af84d5be687c425254b26bef9efc94be6f Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Wed, 14 Oct 2020 18:43:39 +0100 Subject: [PATCH 07/10] test --- src/test/authTest.js | 12 ++++++------ src/test/fixTest.js | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/test/authTest.js b/src/test/authTest.js index 6a8f488..6791704 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -1,14 +1,14 @@ -import chai from 'chai'; -import chaiHttp from 'chai-http'; +// import chai from 'chai'; +// import chaiHttp from 'chai-http'; // import faker from 'faker'; // import app from '../app'; // const expect = chai.expect; -chai.should(); +// chai.should(); -chai.use(chaiHttp); +// chai.use(chaiHttp); // const apiEndPoint = '/api/v1/'; // const userEndPoint = `${apiEndPoint}auth/`; @@ -37,7 +37,7 @@ chai.use(chaiHttp); // lastName: 'wale', // }; -describe('Signup User', () => { +// describe('Signup User', () => { // it('Should create a new user', (done) => { // chai.request(app) // .post(`${userEndPoint}signup`) @@ -176,4 +176,4 @@ describe('Signup User', () => { // done(); // }); // }); -}); +// }); diff --git a/src/test/fixTest.js b/src/test/fixTest.js index 2130379..a1c9ee2 100644 --- a/src/test/fixTest.js +++ b/src/test/fixTest.js @@ -41,10 +41,14 @@ describe('AUTH', () => { expect(response.status).to.equal(201); expect(response.body).to.be.a('object'); }).timeout(0); + }); + describe('Bad Request', () => { it('Should return 400 Bad Request', async () => { const response = await request.post(`${userEndPoint}signup`).send(userWithNoPassword); expect(response.status).to.equal(400); }).timeout(0); + }); + describe('User already exist', () => { it('should throw USER already exist error', async () => { const response = await request.post(`${userEndPoint}signup`).send(userData); expect(response.status).to.equal(409); From adafc301947b6e564f5337f15d2f54780e0c0d43 Mon Sep 17 00:00:00 2001 From: dewaleolaoye Date: Wed, 14 Oct 2020 18:57:35 +0100 Subject: [PATCH 08/10] remove timeout --- src/test/fixTest.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/fixTest.js b/src/test/fixTest.js index a1c9ee2..6f08ae8 100644 --- a/src/test/fixTest.js +++ b/src/test/fixTest.js @@ -37,21 +37,22 @@ describe('AUTH', () => { describe('SignUp a user', () => { it('should sign up user', async () => { + const response = await request.post(`${userEndPoint}signup`).send(userData); expect(response.status).to.equal(201); expect(response.body).to.be.a('object'); - }).timeout(0); + }); }); describe('Bad Request', () => { it('Should return 400 Bad Request', async () => { const response = await request.post(`${userEndPoint}signup`).send(userWithNoPassword); expect(response.status).to.equal(400); - }).timeout(0); + }); }); describe('User already exist', () => { it('should throw USER already exist error', async () => { const response = await request.post(`${userEndPoint}signup`).send(userData); expect(response.status).to.equal(409); - }).timeout(0); + }) }); }); From 286f49ed600aa2414dca723eb9cc174f99396cfe Mon Sep 17 00:00:00 2001 From: temisan otokuefor Date: Sat, 17 Oct 2020 20:19:52 +0100 Subject: [PATCH 09/10] Modifies test coverage --- package.json | 2 +- src/test/authTest.js | 30 ++++++++--------- src/test/courseTest.js | 76 +++++++++++++++++++++--------------------- src/test/fixTest.js | 3 +- 4 files changed, 55 insertions(+), 56 deletions(-) diff --git a/package.json b/package.json index 3420139..483d5a3 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "node ./src/bin/www", "devstart": "set DEBUG=dev && babel-watch ./src/bin/dev", "build": "rm -rf dist && mkdir dist && babel src -s -d dist", - "test": " set NODE_ENV=test && npm run reset-db && nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 10000", + "test": " set NODE_ENV=test&& npm run reset-db&& nyc mocha -r @babel/register --recursive src/test/ --exit -timeout 10000", "migrate": "./node_modules/.bin/sequelize db:migrate", "drop-db": "./node_modules/.bin/sequelize db:migrate:undo:all", "seeders": "./node_modules/.bin/sequelize db:seed:all", diff --git a/src/test/authTest.js b/src/test/authTest.js index 6791704..b3d6386 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -38,21 +38,21 @@ // }; // describe('Signup User', () => { - // it('Should create a new user', (done) => { - // chai.request(app) - // .post(`${userEndPoint}signup`) - // .send(user) - // .end((err, res) => { - // res.should.have.status(201); - // res.body.should.be.a('object'); - // res.body.should.have.property('data'); - // res.body.data.should.be.a('object'); - // res.body.data.should.have.property('tokens'); - // res.body.data.should.have.property('id'); - // res.body.data.should.have.property('email'); - // done(); - // }); - // }); +// it('Should create a new user', (done) => { +// chai.request(app) +// .post(`${userEndPoint}signup`) +// .send(user) +// .end((err, res) => { +// res.should.have.status(201); +// res.body.should.be.a('object'); +// res.body.should.have.property('data'); +// res.body.data.should.be.a('object'); +// res.body.data.should.have.property('tokens'); +// res.body.data.should.have.property('id'); +// res.body.data.should.have.property('email'); +// done(); +// }); +// }); // it('Should return 409 if email already exists', (done) => { // chai.request(app) // .post(`${userEndPoint}signup`) diff --git a/src/test/courseTest.js b/src/test/courseTest.js index 98ca5fe..11750ed 100644 --- a/src/test/courseTest.js +++ b/src/test/courseTest.js @@ -155,44 +155,44 @@ describe('Create Courses Tests', () => { done(); }); }); - // it('Should register a user for a course', (done) => { - // chai.request(app) - // .post(`${apiEndPoint}courses/2/register`) - // .set('Authorization', `Bearer ${token}`) - // .send(token) - // .end((err, res) => { - // res.should.have.status(201); - // res.body.should.be.an('object'); - // res.body.should.have.property('status'); - // res.body.should.have.property('data'); - // res.body.data.should.be.an('object'); - // res.body.data.should.have.property('id'); - // res.body.data.should.have.property('UserId'); - // res.body.data.should.have.property('courseId'); - // res.body.data.should.have.property('registeredOn'); - // done(); - // }); - // }); - // it('should return 200 and get all courses', (done) => { - // chai.request(app) - // .get(`${apiEndPoint}courses`) - // .set('Authorization', `Bearer ${token}`) - // .send(token) - // .end((err, res) => { - // res.should.have.status(200); - // res.body.should.be.an('object'); - // res.body.should.have.property('status'); - // res.body.should.have.property('data'); - // res.body.data.should.be.an('array'); - // res.body.data[0].should.have.property('id'); - // res.body.data[0].should.have.property('courseTitle'); - // res.body.data[0].should.have.property('memberFees'); - // res.body.data[0].should.have.property('nonMemberFees'); - // res.body.data[0].should.have.property('startDate'); - // res.body.data[0].should.have.property('endDate'); - // done(); - // }); - // }); + it('Should register a user for a course', (done) => { + chai.request(app) + .post(`${apiEndPoint}courses/2/register`) + .set('Authorization', `Bearer ${token}`) + .send(token) + .end((err, res) => { + res.should.have.status(201); + res.body.should.be.an('object'); + res.body.should.have.property('status'); + res.body.should.have.property('data'); + res.body.data.should.be.an('object'); + res.body.data.should.have.property('id'); + res.body.data.should.have.property('UserId'); + res.body.data.should.have.property('courseId'); + res.body.data.should.have.property('registeredOn'); + done(); + }); + }); + it('should return 200 and get all courses', (done) => { + chai.request(app) + .get(`${apiEndPoint}course`) + .set('Authorization', `Bearer ${token}`) + .send(token) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.an('object'); + res.body.should.have.property('status'); + res.body.should.have.property('data'); + res.body.data.should.be.an('array'); + res.body.data[0].should.have.property('id'); + res.body.data[0].should.have.property('courseTitle'); + res.body.data[0].should.have.property('memberFees'); + res.body.data[0].should.have.property('nonMemberFees'); + res.body.data[0].should.have.property('startDate'); + res.body.data[0].should.have.property('endDate'); + done(); + }); + }); }); // describe('Create Courses Tests', () => { diff --git a/src/test/fixTest.js b/src/test/fixTest.js index 6f08ae8..bea2feb 100644 --- a/src/test/fixTest.js +++ b/src/test/fixTest.js @@ -37,7 +37,6 @@ describe('AUTH', () => { describe('SignUp a user', () => { it('should sign up user', async () => { - const response = await request.post(`${userEndPoint}signup`).send(userData); expect(response.status).to.equal(201); expect(response.body).to.be.a('object'); @@ -53,6 +52,6 @@ describe('AUTH', () => { it('should throw USER already exist error', async () => { const response = await request.post(`${userEndPoint}signup`).send(userData); expect(response.status).to.equal(409); - }) + }); }); }); From 84fb491c21820e287fd2f0074760b493c21330b8 Mon Sep 17 00:00:00 2001 From: temisan otokuefor Date: Mon, 19 Oct 2020 17:01:15 +0100 Subject: [PATCH 10/10] Add more describe blocks --- src/test/courseTest.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/courseTest.js b/src/test/courseTest.js index 11750ed..cb37d55 100644 --- a/src/test/courseTest.js +++ b/src/test/courseTest.js @@ -45,6 +45,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Title One', () => { it('Should return 401 if token is not provided', (done) => { const course = { courseTitle: 'Facility Management', @@ -64,6 +66,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Title 2', () => { it('Should return 400 if courseTitle isn\'t specified', (done) => { const course = { memberFees: 105000, @@ -83,6 +87,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Create Courses Tests', () => { it('Should return 400 if memberFees isn\'t specified', (done) => { const course = { CourseTitle: 'Facility Management', @@ -101,6 +107,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Create Courses Tests', () => { it('Should return 400 if nonmemberFee isn\'t specified', (done) => { const course = { CourseTitle: 'Facility Management', @@ -119,6 +127,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Create Courses Tests', () => { it('Should return 400 if endDate isn\'t specified', (done) => { const course = { CourseTitle: 'Facility Management', @@ -137,6 +147,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Create Courses Tests', () => { it('Should return 400 if startDate isn\'t specified', (done) => { const course = { CourseTitle: 'Facility Management', @@ -155,6 +167,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Create Courses Tests', () => { it('Should register a user for a course', (done) => { chai.request(app) .post(`${apiEndPoint}courses/2/register`) @@ -173,6 +187,8 @@ describe('Create Courses Tests', () => { done(); }); }); +}); +describe('Create Courses Tests', () => { it('should return 200 and get all courses', (done) => { chai.request(app) .get(`${apiEndPoint}course`) @@ -195,6 +211,7 @@ describe('Create Courses Tests', () => { }); }); + // describe('Create Courses Tests', () => { // });