From c37f6fdedbad942a4d0424bb5cecbc7f751fd1f9 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 7 May 2020 07:54:04 +0900 Subject: [PATCH 1/2] #16: fixes mailer and test --- services/mailer.js | 32 +++++++++++++++++++++++++++++ services/mailer.test.js | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 services/mailer.js create mode 100644 services/mailer.test.js diff --git a/services/mailer.js b/services/mailer.js new file mode 100644 index 00000000..ffcdd344 --- /dev/null +++ b/services/mailer.js @@ -0,0 +1,32 @@ +const mailgun = require('mailgun-js'); +const logger = require('../lib/log')(__filename) +require('dotenv').config() + +const mg = mailgun({apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN}); + +const mgModule = {} + +mgModule.sendConfirmationEmail = (receiver, token) => { + const link = `https://learndatabases.dev/emailConfirmation/${token}` + const data = { + from: 'admin@learndatabases.dev', + to: receiver, + subject: 'Congratulations!', + text: 'Welcome to C0D3', + html: ` +

Confirm your Email

+

+ Click Here +

+

Or visit this link: ${link}

+ ` + }; + return mg.messages().send(data).then((returnedData) => { + logger.info('Confirmation Email successfully sent', returnedData) + }).catch((err) => { + logger.error('Confirmation Email Error:',err) + return err + }) +} + +module.exports = mgModule \ No newline at end of file diff --git a/services/mailer.test.js b/services/mailer.test.js new file mode 100644 index 00000000..756f3a37 --- /dev/null +++ b/services/mailer.test.js @@ -0,0 +1,45 @@ +jest.mock('../lib/log') +const logGen = require('../lib/log') +const logger = { + info: jest.fn(), + error: jest.fn() +} +logGen.mockReturnValue(logger) + +jest.mock('mailgun-js') +const mailgun = require('mailgun-js'); +const messages = { + send: jest.fn().mockImplementation((data) => { + if (data.to) return Promise.resolve('hello') + return Promise.reject('rejected') + }) +} +mailgun.mockImplementation(() => { + return { + messages: () => { + return messages + } + } +}) +const email = require('./mailer'); + +describe('Test mailgun', ()=>{ + beforeEach(() => { + jest.clearAllMocks() + }) + + it('should test if mocksend and mailgun is called', async () => { + await email.sendConfirmationEmail('paul@github.com', 'token123') + expect(messages.send).toHaveBeenCalledTimes(1) + expect(messages.send.mock.calls[0][0]).toMatchSnapshot() + expect(logger.info).toHaveBeenCalledTimes(1) + expect(logger.info.mock.calls[0][0]).toEqual('Confirmation Email successfully sent') + }) + + it('should call logger.error when function is called with invalid argument', async () => { + await email.sendConfirmationEmail(null, null) + expect(logger.error).toHaveBeenCalledTimes(1) + expect(logger.error.mock.calls[0][0]).toEqual('Confirmation Email Error:') + }) + +}) From 932ea20ce39a9018cb044e8a9b50209f9f6e76f9 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 7 May 2020 09:57:16 +0900 Subject: [PATCH 2/2] #16: fixes mailer test --- services/mailer.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/services/mailer.test.js b/services/mailer.test.js index 756f3a37..de7964eb 100644 --- a/services/mailer.test.js +++ b/services/mailer.test.js @@ -8,12 +8,7 @@ logGen.mockReturnValue(logger) jest.mock('mailgun-js') const mailgun = require('mailgun-js'); -const messages = { - send: jest.fn().mockImplementation((data) => { - if (data.to) return Promise.resolve('hello') - return Promise.reject('rejected') - }) -} +const messages = {} mailgun.mockImplementation(() => { return { messages: () => { @@ -21,6 +16,7 @@ mailgun.mockImplementation(() => { } } }) + const email = require('./mailer'); describe('Test mailgun', ()=>{ @@ -29,6 +25,7 @@ describe('Test mailgun', ()=>{ }) it('should test if mocksend and mailgun is called', async () => { + messages.send = jest.fn().mockReturnValue(Promise.resolve('hello')) await email.sendConfirmationEmail('paul@github.com', 'token123') expect(messages.send).toHaveBeenCalledTimes(1) expect(messages.send.mock.calls[0][0]).toMatchSnapshot() @@ -37,6 +34,7 @@ describe('Test mailgun', ()=>{ }) it('should call logger.error when function is called with invalid argument', async () => { + messages.send = jest.fn().mockReturnValue(Promise.reject('rejected')) await email.sendConfirmationEmail(null, null) expect(logger.error).toHaveBeenCalledTimes(1) expect(logger.error.mock.calls[0][0]).toEqual('Confirmation Email Error:')