Skip to content

Commit

Permalink
Merge 3b3b171 into b0223c2
Browse files Browse the repository at this point in the history
  • Loading branch information
zanio committed Sep 10, 2019
2 parents b0223c2 + 3b3b171 commit 5d1a9c4
Show file tree
Hide file tree
Showing 38 changed files with 847 additions and 197 deletions.
9 changes: 4 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ DB_HOST=Host url
PORT=port number to listen for connections
DB_TEST_NAME=name of test database
JWT_SECRET=secret variable to sign and decode tokens
EMAIL =
EMAIL = Email Account of firestar backend
LAYER = Nodemailer package require you provide the LAYER which is the sender mailling service provider. It can be e.g include Yahoo, gmail and your private account mail provider as well.we intend
PASSWORD =
SECRET_KEY_EMAIL_VERIFY_TOKEN =
PASSWORD = password of the associated email account
SECRET_KEY_SIGN_UP = choose any random character
EMAIL_VERIFY_TOKEN_SECRET_KEY = choose any random character
TOKENEXPIRY=18000
CLIENT_URL="https://firestar_backend.herokuapp.com"
HOST="localhost"
NODE_ENV="test"
YOUR_EMAIL="" (description: email to create test account and receive reset email link, must be able to access the email)
SOME_PASSWORD="" (description: password to create a test account, must include no less than 4 characters)
SENDGRID_API_KEY="" (description: sendgrid api key with permission to send mail)
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
"coverage": "nyc report --reporter=text",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint \"**/*.js\" --ignore-pattern node_modules/ --fix",
"build": "babel ./src -d dist",
"clean": "rm -rf ./dist && mkdir dist",
"build": "npm run clean && babel ./src -d dist",
"heroku-postbuild": "npm run build && npm run undo:prod && npm run migrate:prod",
"start": "node dist/index.js",
"start:dev": "nodemon --exec babel-node -- src/index.js",
"test:script": "cross-env NODE_ENV=development babel-node src/helpers/test.js",
"start:dev": "cross-env NODE_ENV=development nodemon --exec babel-node -- src/index.js",
"migrate:dev": "sequelize db:migrate",
"migrate:test": "cross-env NODE_ENV=test sequelize db:migrate",
"migrate:prod": "cross-env NODE_ENV=production sequelize db:migrate",
Expand All @@ -23,8 +26,16 @@
"author": "Andela Simulations Programme",
"license": "MIT",
"dependencies": {
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/node": "^7.6.1",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@hapi/joi": "^15.1.1",
"@sendgrid/mail": "^6.4.0",
"bcrypt": "^3.0.6",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"core-js": "^3.2.1",
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/dummyLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const { assert } = chai;

describe('Login', () => {
before(async () => {
await models.User.sync({ force: true });
await models.User.bulkCreate([users.superAdmin, users.nonadmin]);
await models.users.sync({ force: true });
await models.users.bulkCreate([users.superAdmin, users.nonadmin]);
});
after(async () => {
await models.User.destroy({ where: {} });
await models.users.destroy({ where: {} });
});
it('Should generate Token', async () => {
const res = await chai
Expand All @@ -28,6 +28,7 @@ describe('Login', () => {
});
it('Should not generate Token', async () => {
const res = await chai

.request(server)
.post('/api/v1/auth/login')
.send({ email: 'doesnotexist@gmail.com' });
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './routes/unknownRoute';
import './services/mail';
import './utils/hash';
import './validations/resetPassword';
import './emailverification';
import './routes/users/emailverification';
import './routes/users/signup';
import './dummyLogin';
import './routes/users/userRole';
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import chai from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import app from '../index';
import { handleEmptyEmailBody } from '../middlewares/mail';
import emailverification from '../controllers/emailController';
import { emailVerifyToken } from '../utils/index';
import validation from '../helpers/validation';
import { idUnset, idWrong } from '../__mocks__/emailVerification';
import app from '../../../index';
import { handleEmptyEmailBody } from '../../../middlewares/mail';
import emailverification from '../../../controllers/emailController';
import { emailVerifyToken } from '../../../utils/index';
import validation from '../../../helpers/validation';
import { idUnset, idWrong } from '../../../__mocks__/emailVerification';

chai.use(chaiHttp);
chai.use(sinonChai);
Expand All @@ -25,10 +25,11 @@ describe('EMAIL ROUTE', () => {

afterEach(() => sinon.restore());


describe('EMAIL VERIFICATION ROUTE', () => {
it('should have a status of 200 when message is sent succesfully', async () => {
const body = {
email: 'akp.ani@yahoo.com',
email: 'akp.axcni@yahoo.com',
firstName: 'Aniefiok',
lastName: 'Akpan'
};
Expand All @@ -52,7 +53,7 @@ describe('EMAIL ROUTE', () => {

it('should have a status of 403 and a messsage of "Email, firstName and lastName is required" when some body is not present', async () => {
const body = {
email: 'akp.ani@yahoo.com',
email: 'akp.acni@yahoo.com',
firstName: 'Aniefiok',
lastName: ''
};
Expand Down Expand Up @@ -83,20 +84,11 @@ describe('EMAIL ROUTE', () => {
}).timeout(0);
});

describe('EMAIL TOKEN CONFIRMATION ROUTE', () => {
it('should have a status of 200 when valid token is sent as query string', async () => {
const id = tokenEmail;
const response = await request.get(`/api/v1/users/email/verify?id=${id}`);
expect(response.body.status).to.equal(200);
expect(response.body).to.be.a('object');
}).timeout(0);
});

describe('EMAIL TOKEN CONFIRMATION ROUTE', () => {
it('should have a status of 400 when invalid token is sent as query string', async () => {
const id = idWrong;
const response = await request.get(`/api/v1/users/email/verify?id=${id}`);
expect(response.body.status).to.equal(400);
expect(response.status).to.equal(400);
expect(response.body).to.be.a('object');
}).timeout(0);
});
Expand All @@ -105,7 +97,7 @@ describe('EMAIL ROUTE', () => {
it('fakes server success for email verification controller', async () => {
const req = {
body: {
email: 'akp.ani@yahoo.com',
email: 'akp.anvfi@yahoo.com',
firstName: 'Aniefiok',
lastName: 'Akpan'
},
Expand Down
39 changes: 22 additions & 17 deletions src/__tests__/routes/users/resetPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import moment from 'moment';
import app from '../../../index';
import models, { sequelize } from '../../../models';
import db from '../../../models';
import { sendResetMail, sendSignupMail } from '../../../services/sendMail';

const { expect } = chai;
Expand All @@ -19,33 +19,37 @@ const resetToken = '12ererfbuib23iub328o7rg8hbiuva';

// Create table and seed database
const seedTestDb = async () => {
await models.User.create({
await db.users.create({
firstName: 'futhermaths',
lastName: 'Physics',
email: 'youremail3@andela.com',
role: 'passenger'
roleId: 5
});

await models.User.create({
await db.users.create({
firstName: 'futhermaths',
lastName: 'Physics',
email: 'youremail4@andela.com',
role: 'passenger'
roleId: 5
});

await models.Login.create({
await db.logins.create({
email: 'youremail3@andela.com',
password: 'password'
});

await models.Login.create({
await db.logins.create({
email: 'youremail4@andela.com',
password: 'password'
});

await models.Reset.create({
await db.resets.create({
email: 'youremail3@andela.com',
expireTime: new Date(),
resetToken: '$2a$10$Yc4fNidn3ih0Z0wRajFhq.AwneQLYR2RWWYQT7PGJdJj4UN1BGJ1K'
}),

await models.Reset.create({
await db.resets.create({
email: 'youremail4@andela.com',
expireTime: moment
.utc()
Expand All @@ -58,9 +62,9 @@ const seedTestDb = async () => {
// Clear tables of seed
const clearTestDb = async () => {
try {
await models.User.destroy({ where: {}, force: true });
await models.Login.destroy({ where: {}, force: true });
await models.Reset.destroy({ where: {}, force: true });
await db.users.destroy({ where: {}, force: true });
await db.logins.destroy({ where: {}, force: true });
await db.resets.destroy({ where: {}, force: true });
} catch (err) {
throw err;
}
Expand All @@ -69,7 +73,9 @@ const clearTestDb = async () => {
// clear database and seed data before test
before(async () => {
try {
await clearTestDb();
await db.users.sync({ force: true });
await db.logins.sync({ force: true });
await db.resets.sync({ force: true });
await seedTestDb();
} catch (err) {
throw err;
Expand Down Expand Up @@ -110,7 +116,7 @@ describe('Forgot and Reset Password Test', () => {
.post(`${forgotPasswordURL}`)
.send({ email: newReset.email })
.end(async () => {
expect(await resetMailStub).to.be.true;
expect(await resetMailStub).to.be.equal(true);
});
});

Expand All @@ -121,14 +127,13 @@ describe('Forgot and Reset Password Test', () => {
.post(`${forgotPasswordURL}`)
.send({ email: newReset2.email })
.end(async (err, res) => {
expect(await signupMailStub).to.be.true;
expect(await signupMailStub).to.be.equal(true);
expect(await signupMailStub.firstCall.args[0]).to.equal(newReset2.email);
expect(res).to.have.status(200);
expect(res.body.status).to.be.equal('success');
expect(res.body.message).to.be.equal(
'Check your mail for further instruction'
);
done();
});
});
});
Expand Down Expand Up @@ -168,4 +173,4 @@ describe('Forgot and Reset Password Test', () => {
});
});
});
});
});
Loading

0 comments on commit 5d1a9c4

Please sign in to comment.