Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(email.service.ts): unit tests for email service #145

Merged
merged 1 commit into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Whamo12

Something you could do to reuse code would be to put the userEmail, uuid & spy all into a beforeAll

Ex:

let uuid;
let userEmail;
let spy;
BeforeAll(() => {
   // 
  // 
  //
}

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
};