Skip to content

Commit

Permalink
Merge ee7a4a3 into f239c55
Browse files Browse the repository at this point in the history
  • Loading branch information
minega25 committed Aug 27, 2019
2 parents f239c55 + ee7a4a3 commit db0b031
Show file tree
Hide file tree
Showing 18 changed files with 470 additions and 402 deletions.
127 changes: 119 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"undomigration": "babel-node node_modules/.bin/sequelize db:migrate:undo:all",
"runmigrations": "npm run undoseeds && npm run migration && npm run seeds",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"test": "export NODE_ENV=test && npm run undomigration && npm run migration && npm run seeds && nyc --reporter=html --reporter=text mocha ./test --no-timeout --exit --require @babel/register",
"test": "export NODE_ENV=test && npm run undomigration && npm run migration && npm run seeds && nyc --reporter=html --reporter=text mocha ./test --exit --require @babel/register",
"dev": "nodemon --exec babel-node ./src/app.js"
},
"author": "Andela Simulations Programme",
Expand Down Expand Up @@ -59,19 +59,21 @@
"winston": "^3.2.1"
},
"devDependencies": {
"coveralls": "^3.0.5",
"eslint": "^6.1.0",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.18.2",
"nodemon": "^1.19.1",
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/node": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/register": "^7.5.5",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"mocha": "^6.2.0"
"coveralls": "^3.0.5",
"eslint": "^6.1.0",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.18.2",
"mocha": "^6.2.0",
"nodemon": "^1.19.1",
"sinon": "^7.4.1",
"sinon-chai": "^3.3.0"
},
"nyc": {
"exclude": [
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Articles {
static async createArticles(req, res) {
const userId = req.auth.id;
const findUser = await Userservice.getOneUser(userId);
const images = await cloudinaryHelper(req.files);
const images = await cloudinaryHelper.generateCloudinaryUrl(req.files);

if (findUser) {
const { title } = req.body;
Expand Down
5 changes: 1 addition & 4 deletions src/controllers/follow.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ class FollowController {
const url = `${location}/profiles/${followerUser.username}`;
const emailTemplate = newFollowerTemplate(followerUser.username, url);
const message = `Hi ${followedUser.username}, ${followerUser.username} started following you on Authors Haven`;

if (!process.env.NODE_ENV === 'test') {
sendEmail(followedUser.email, `${message}`, emailTemplate);
}
await sendEmail(followedUser.email, `${message}`, emailTemplate);
return res.status(200).json({ status: '200', message: `You are now following ${followedUser.username}` });
}

Expand Down
13 changes: 5 additions & 8 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UserService from '../services/user.service';
import Helper from '../helpers/helper';
import sendEmail from '../helpers/verification-email';
import resetSendMail from '../services/resetpassword.service';
import EmailHelper from '../helpers/verification-email';
import sendPasswordResetEmailHelper from '../services/resetpassword.service';

/**
*
Expand Down Expand Up @@ -124,7 +124,7 @@ class UserController {
const token = await Helper.generateToken(payload);
const verifyUrl = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}/users/verify?
token=${token}`;
const verify = sendEmail(payload.email, username, verifyUrl);
const verify = EmailHelper.sendEmail(payload.email, username, verifyUrl);

return verify
? res.status(201).json({
Expand Down Expand Up @@ -188,7 +188,7 @@ class UserController {
const verifyUrl = `${process.env.BACKEND_URL}/api/${
process.env.API_VERSION
}/users/verify?token=${token}`;
await sendEmail(payload.email, newUser.username, verifyUrl);
await EmailHelper.sendEmail(payload.email, newUser.username, verifyUrl);
return res.status(201).json({
status: 201,
message:
Expand Down Expand Up @@ -386,7 +386,6 @@ class UserController {
static async requestPasswordReset(req, res) {
// check if email provided exists in db
const { email } = req.body;

if (!email) {
return res.status(400).send({
status: 400,
Expand All @@ -408,13 +407,11 @@ class UserController {
};

const token = await Helper.generateToken(payload, (60 * 60));

// create password reset link
const resetUrl = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}/users/reset/${token}`;

// send email to user email address
const emailSent = resetSendMail(user.email, user.username, resetUrl);

const emailSent = await sendPasswordResetEmailHelper.sendEmail(user.email, user.username, resetUrl);
if (!emailSent) { return res.status(500).send({ status: 500, message: 'Failed to send email. Please contact site administrator for support' }); }

return res.status(200).send({
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/cloudinaryHelper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dotenv/config';
import cloudinary from 'cloudinary';

export default async (images = []) => Promise.all(
const generateCloudinaryUrl = async (images = []) => Promise.all(
images.map(async (file) => {
const { secure_url } = process.env.NODE_ENV === 'test'
? 'image.jpg'
: await cloudinary.v2.uploader.upload(file.path);
const { secure_url } = await cloudinary.v2.uploader.upload(file.path);
return secure_url;
})
);
const cloudinaryHelper = { generateCloudinaryUrl };
export default cloudinaryHelper;
6 changes: 3 additions & 3 deletions src/helpers/verification-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const sendEmail = (email, username, url) => {
</div>
</div>`,
};
return process.env.NODE_ENV === 'test' ? true : sgMail.send(msg);
return sgMail.send(msg);
};

export default sendEmail;
const EmailHelper = { sendEmail };
export default EmailHelper;
22 changes: 22 additions & 0 deletions src/seeders/20190808074800-normal-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ export default {
createdAt: new Date(),
updatedAt: new Date()
},
{
firstname: 'userthree',
lastname: 'userthree',
email: 'userthree@gmail.com',
password: hashPassword,
username: 'userthree',
role: 'normal',
verified: true,
createdAt: new Date(),
updatedAt: new Date()
},
{
firstname: 'userfour',
lastname: 'userfour',
email: 'userfour@gmail.com',
password: hashPassword,
username: 'userfour',
role: 'normal',
verified: true,
createdAt: new Date(),
updatedAt: new Date()
},
]),

down: queryInterface => queryInterface.bulkDelete('users', null, {})
Expand Down
6 changes: 3 additions & 3 deletions src/services/resetpassword.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const sendEmail = (email, username, url) => {
</div>
</div>`,
};
return process.env.NODE_ENV === 'test' ? true : sgMail.send(msg);
return sgMail.send(msg);
};

export default sendEmail;
const sendPasswordResetEmailHelper = { sendEmail };
export default sendPasswordResetEmailHelper;

0 comments on commit db0b031

Please sign in to comment.