Skip to content

Commit

Permalink
Merge pull request #145 from softrams/136-email
Browse files Browse the repository at this point in the history
test(email.service.ts): unit tests for email service
  • Loading branch information
Whamo12 committed Jul 18, 2020
2 parents abdc876 + 799132f commit d3f42a0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/routes/authentication.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { Response } from 'express';
import jwt = require('jsonwebtoken');
import { generateHash, passwordSchema, compare } from '../utilities/password.utility';
import { passwordRequirement } from '../enums/message-enum';
// tslint:disable-next-line: no-var-requires
const emailService = require('../services/email.service');
import * as emailService from '../services/email.service';

/**
* @description Login to the application
Expand Down
5 changes: 1 addition & 4 deletions src/routes/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { v4 as uuidv4 } from 'uuid';
import { validate } from 'class-validator';
import { passwordRequirement } from '../enums/message-enum';
import { generateHash, passwordSchema, updatePassword } from '../utilities/password.utility';

// tslint:disable-next-line: no-var-requires
const emailService = require('../services/email.service');

import * as emailService from '../services/email.service';
/**
* @description Register user
* @param {UserRequest} req
Expand Down
39 changes: 39 additions & 0 deletions src/services/email.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import nodemailer = require('nodemailer');
import * as emailService from './email.service';

describe('email service', () => {
test('send email failure missing credentials', async () => {
const mailOptions = {
from: 'pentester3@gmail.com',
subject: 'Bulwark - Please confirm your email address',
text: `Please confirm your email address
\n A Bulwark account was created with the email: pentester@gmail.com.
As an extra security measure, please verify this is the correct email address
linked to Bulwark by clicking the link below.
\n dev/api/user/verify/123`,
to: 'pentester2@gmail.com'
};
await expect(emailService.sendEmail(mailOptions)).rejects.toContain('Error sending email');
});
test('send verification email success', async () => {
const uuid = 'abc';
const userEmail = 'pentester@gmail.com';
const spy = jest.spyOn(emailService, 'sendEmail');
await emailService.sendVerificationEmail(uuid, userEmail);
expect(spy).toHaveBeenCalled();
});
test('send forgot password email success', async () => {
const uuid = 'abc';
const userEmail = 'pentester@gmail.com';
const spy = jest.spyOn(emailService, 'sendEmail');
await emailService.sendForgotPasswordEmail(uuid, userEmail);
expect(spy).toHaveBeenCalled();
});
test('send invitation email', async () => {
const uuid = 'abc';
const userEmail = 'pentester@gmail.com';
const spy = jest.spyOn(emailService, 'sendEmail');
await emailService.sendInvitationEmail(uuid, userEmail);
expect(spy).toHaveBeenCalled();
});
});
45 changes: 20 additions & 25 deletions src/services/email.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
auth: {
pass: process.env.FROM_EMAIL_PASSWORD,
user: process.env.FROM_EMAIL
},
service: 'Gmail'
});

/**
* @description Send email
* @param {object} mailOptions
* @returns string
*/
const sendEmail = (mailOptions) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
return 'Error sending email';
} else {
return 'email sent successfully';
}
export const sendEmail = (mailOptions): Promise<string> => {
return new Promise((resolve, reject) => {
const transporter = nodemailer.createTransport({
auth: {
pass: process.env.FROM_EMAIL_PASSWORD,
user: process.env.FROM_EMAIL
},
service: 'Gmail'
});
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
reject('Error sending email');
} else {
resolve('email sent successfully');
}
});
});
};

Expand All @@ -29,7 +30,7 @@ const sendEmail = (mailOptions) => {
* @param {string} uuid
* @returns string
*/
const sendVerificationEmail = (uuid, userEmail) => {
export const sendVerificationEmail = (uuid: string, userEmail: string) => {
const mailOptions = {
from: process.env.FROM_EMAIL,
subject: 'Bulwark - Please confirm your email address',
Expand All @@ -48,7 +49,7 @@ const sendVerificationEmail = (uuid, userEmail) => {
* @param {string} uuid
* @returns string
*/
const sendForgotPasswordEmail = (uuid, userEmail) => {
export const sendForgotPasswordEmail = (uuid, userEmail) => {
const mailOptions = {
from: process.env.FROM_EMAIL,
subject: 'Bulwark - Forgot Password Request',
Expand All @@ -66,7 +67,7 @@ const sendForgotPasswordEmail = (uuid, userEmail) => {
* @param {string} uuid
* @returns string
*/
const sendInvitationEmail = (uuid, userEmail) => {
export const sendInvitationEmail = (uuid, userEmail) => {
const mailOptions = {
from: process.env.FROM_EMAIL,
subject: 'Bulwark - Welcome!',
Expand All @@ -77,9 +78,3 @@ const sendInvitationEmail = (uuid, userEmail) => {
};
sendEmail(mailOptions);
};

module.exports = {
sendForgotPasswordEmail,
sendVerificationEmail,
sendInvitationEmail
};

0 comments on commit d3f42a0

Please sign in to comment.