diff --git a/package-lock.json b/package-lock.json index 7758f64..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", @@ -4651,6 +4691,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", @@ -6448,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", @@ -7196,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", @@ -8590,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 71853e8..483d5a3 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", @@ -63,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/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..b3d6386 100644 --- a/src/test/authTest.js +++ b/src/test/authTest.js @@ -1,122 +1,102 @@ -import chai from 'chai'; -import chaiHttp from 'chai-http'; -import app from '../app'; +// import chai from 'chai'; +// import chaiHttp from 'chai-http'; +// import faker from 'faker'; -chai.should(); +// import app from '../app'; -chai.use(chaiHttp); +// const expect = chai.expect; -const apiEndPoint = '/api/v1/'; -const userEndPoint = `${apiEndPoint}auth/`; +// chai.should(); -describe('Signup Error', () => { - it('Should return 400 if password is omitted', (done) => { - const user = { - email: 'temi@testmail.com', - }; - chai - .request(app) - .post(`${userEndPoint}signup`) - .send(user) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); -}); +// chai.use(chaiHttp); -describe('Signup Error', () => { - it('Should return 400 if email is omitted', (done) => { - const user = { - password: 'password', - }; - chai - .request(app) - .post(`${userEndPoint}signup`) - .send(user) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); -}); +// const apiEndPoint = '/api/v1/'; +// const userEndPoint = `${apiEndPoint}auth/`; -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) - .end((err, res) => { - res.should.have.status(400); - res.body.should.be.a('object'); - res.body.should.have.property('error'); - done(); - }); - }); -}); +// const user = { +// email: faker.internet.email(), +// password: faker.internet.password(), +// }; -// describe(`POST ${userEndPoint}signup`, () => { -// it('Should create a new user', (done) => { -// const user = { -// email: 'temi@testmail.com', -// password: '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(201); +// res.should.have.status(409); // 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'); +// 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(); // }); // }); -// }); -// describe('Signup Error', () => { -// it('Should return 409 if email already exists', (done) => { -// const user = { -// email: 'temi@testmail.com', -// password: 'password', -// }; -// chai.request(app) +// it('Should return 400 if email is omitted', (done) => { +// chai +// .request(app) // .post(`${userEndPoint}signup`) -// .send(user) +// .send(userWithNoEmail) // .end((err, res) => { -// res.should.have.status(409); +// res.should.have.status(400); // 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) +// 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(404); +// res.should.have.status(400); // res.body.should.be.a('object'); // res.body.should.have.property('error'); // done(); @@ -124,45 +104,53 @@ describe('Signup Error', () => { // }); // }); -// describe('Signin Error', () => { -// it('Should deny access if wrong password is provided', (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: 'temi@testmail.com', -// password: 'passweod', +// email: 'thisEmailDoesNotExist@gmail.com', +// password: 'pA55w0rd', // }; // chai.request(app) // .post(`${userEndPoint}signin`) // .send(login) // .end((err, res) => { -// res.should.have.status(401); +// res.should.have.status(404); // 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) => { +// it('Should deny access if wrong password is provided', (done) => { // const login = { -// password: 'password', +// email: user.email, +// password: 'justarandominvalidpassword', // }; // chai.request(app) // .post(`${userEndPoint}signin`) // .send(login) // .end((err, res) => { -// res.should.have.status(400); +// res.should.have.status(401); // 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', +// email: user.email, // }; // chai.request(app) // .post(`${userEndPoint}signin`) @@ -174,23 +162,17 @@ describe('Signup Error', () => { // done(); // }); // }); -// }); - -// describe('User Login tests', () => { -// it('Should login a user successfully', (done) => { +// it('Should return 400 if email is not provided', (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.should.have.status(400); // 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.should.have.property('error'); // done(); // }); // }); diff --git a/src/test/courseTest.js b/src/test/courseTest.js index 158e670..cb37d55 100644 --- a/src/test/courseTest.js +++ b/src/test/courseTest.js @@ -1,78 +1,52 @@ 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(); + }); + }); +}); +describe('Title One', () => { it('Should return 401 if token is not provided', (done) => { const course = { courseTitle: 'Facility Management', @@ -93,153 +67,192 @@ describe('Create Courses Tests', () => { }); }); }); +describe('Title 2', () => { + it('Should return 400 if courseTitle isn\'t specified', (done) => { + const course = { + memberFees: 105000, + nonmemberFee: 115000, + startDate: 10, + endDate: 13, + }; -// 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 Tests', () => { + 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 Tests', () => { + 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('Create Courses Tests', () => { + 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('Create Courses Tests', () => { + 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(); + }); + }); +}); +describe('Create Courses Tests', () => { + 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(); + }); + }); +}); -// 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 Tests', () => { -// 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('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('Get requests for both user and admin', () => { -// 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('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', () => { + // }); -// 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(); +// }); // }); diff --git a/src/test/fixTest.js b/src/test/fixTest.js new file mode 100644 index 0000000..bea2feb --- /dev/null +++ b/src/test/fixTest.js @@ -0,0 +1,57 @@ +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'); + }); + }); + 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); + }); + }); + 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); + }); + }); +});