|
| 1 | +import 'reflect-metadata'; |
| 2 | +import { faker } from '@faker-js/faker'; |
| 3 | +import * as Logger from '@user-office-software/duo-logger'; |
| 4 | +import { container } from 'tsyringe'; |
| 5 | + |
| 6 | +import { Tokens } from '../../config/Tokens'; |
| 7 | +import { ApplicationEvent } from '../../events/applicationEvents'; |
| 8 | +import { Event } from '../../events/event.enum'; |
| 9 | +import { stfcEmailHandler } from './stfcEmailHandler'; |
| 10 | + |
| 11 | +const ORIGINAL_ENV = process.env; |
| 12 | +const spyLogError = jest |
| 13 | + .spyOn(Logger.logger, 'logError') |
| 14 | + .mockImplementation(() => {}); |
| 15 | +const spyLogInfo = jest |
| 16 | + .spyOn(Logger.logger, 'logInfo') |
| 17 | + .mockImplementation(() => {}); |
| 18 | +// Mock MailService |
| 19 | +const mockMailService = { |
| 20 | + sendMail: jest.fn(), |
| 21 | +}; |
| 22 | +describe('stfcEmailHandler', () => { |
| 23 | + beforeAll(() => { |
| 24 | + container.registerInstance(Tokens.MailService, mockMailService); |
| 25 | + }); |
| 26 | + afterEach(() => { |
| 27 | + process.env = ORIGINAL_ENV; |
| 28 | + jest.clearAllMocks(); |
| 29 | + jest.resetModules(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('These are the test for the handler function stfcEmailhandler', () => { |
| 33 | + it('When running Node process does not have env.FBS_EMAIL value', () => { |
| 34 | + process.env.FBS_EMAIL = ''; |
| 35 | + const mockEvent = { |
| 36 | + type: Event.CALL_CREATED, |
| 37 | + call: {}, |
| 38 | + isRejection: false, |
| 39 | + } as ApplicationEvent; |
| 40 | + |
| 41 | + stfcEmailHandler(mockEvent); |
| 42 | + |
| 43 | + expect(process.env.FBS_EMAIL).toBe(''); |
| 44 | + expect(spyLogError).toHaveBeenCalledTimes(1); |
| 45 | + expect(spyLogError).toHaveBeenCalledWith( |
| 46 | + 'Could not send email(s) on call creation, environmental variable (FBS_EMAIL) not found', |
| 47 | + {} |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('mailService.sendMail is sucessful', async () => { |
| 52 | + // When all required settings are valid |
| 53 | + const inviteEmail = faker.internet.email(); |
| 54 | + process.env.FBS_EMAIL = inviteEmail; |
| 55 | + const mockEvent = { |
| 56 | + type: Event.CALL_CREATED, |
| 57 | + call: { |
| 58 | + shortCode: 'string', |
| 59 | + startCall: new Date(2000, 1, 1), |
| 60 | + endCall: new Date(2000, 1, 2), |
| 61 | + }, |
| 62 | + isRejection: false, |
| 63 | + } as ApplicationEvent; |
| 64 | + |
| 65 | + //create mock instances |
| 66 | + mockMailService.sendMail.mockResolvedValue({ success: true }); |
| 67 | + |
| 68 | + await stfcEmailHandler(mockEvent); |
| 69 | + |
| 70 | + expect(process.env.FBS_EMAIL).toBe(inviteEmail); |
| 71 | + expect(mockMailService.sendMail).toHaveBeenCalledWith({ |
| 72 | + content: { template_id: 'call-created-email' }, |
| 73 | + substitution_data: { |
| 74 | + shortCode: 'string', |
| 75 | + startCall: new Date(2000, 1, 1), |
| 76 | + endCall: new Date(2000, 1, 2), |
| 77 | + }, |
| 78 | + recipients: [{ address: inviteEmail }], |
| 79 | + }); |
| 80 | + expect(spyLogInfo).toHaveBeenCalledTimes(1); |
| 81 | + // this result is derived from the SkipSendMailService.ts, as that is what is mapped in the test environment |
| 82 | + expect(spyLogInfo).toHaveBeenCalledWith('Emails sent on call creation:', { |
| 83 | + result: { |
| 84 | + success: true, |
| 85 | + }, |
| 86 | + event: mockEvent, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('mailService.sendMail is not sucessful', async () => { |
| 91 | + // Then mailService.catch is evoked, logError(x) will be present |
| 92 | + const inviteEmail = faker.internet.email(); |
| 93 | + process.env.FBS_EMAIL = inviteEmail; |
| 94 | + const mockEvent = { |
| 95 | + type: Event.CALL_CREATED, |
| 96 | + call: { |
| 97 | + shortCode: 'error', |
| 98 | + }, |
| 99 | + isRejection: false, |
| 100 | + } as ApplicationEvent; |
| 101 | + const forcedError = new Error('SMTP down'); |
| 102 | + |
| 103 | + mockMailService.sendMail.mockRejectedValueOnce(forcedError); |
| 104 | + container.registerInstance(Tokens.MailService, mockMailService); |
| 105 | + |
| 106 | + await stfcEmailHandler(mockEvent); |
| 107 | + // have added this line as the class.method in the handler function is not asynced |
| 108 | + // and the asserts/expects will check before .then or .catch gets process |
| 109 | + await new Promise(setImmediate); |
| 110 | + |
| 111 | + expect(mockMailService.sendMail).toHaveBeenCalledWith({ |
| 112 | + content: { template_id: 'call-created-email' }, |
| 113 | + substitution_data: { |
| 114 | + shortCode: 'error', |
| 115 | + }, |
| 116 | + recipients: [{ address: inviteEmail }], |
| 117 | + }); |
| 118 | + expect(mockMailService.sendMail).toHaveBeenCalled(); |
| 119 | + expect(spyLogError).toHaveBeenCalledTimes(1); |
| 120 | + expect(spyLogError).toHaveBeenCalledWith( |
| 121 | + 'Could not send email(s) on call creation:', |
| 122 | + { |
| 123 | + error: forcedError, |
| 124 | + event: mockEvent, |
| 125 | + } |
| 126 | + ); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments