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

refactor: convert Bounce module to use neverthrow #1591

Merged
merged 6 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
155 changes: 63 additions & 92 deletions src/app/modules/bounce/__tests__/bounce.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ObjectId } from 'bson'
import mongoose from 'mongoose'
import { errAsync, okAsync } from 'neverthrow'
import { errAsync, ok, okAsync } from 'neverthrow'
import { mocked } from 'ts-jest/utils'

import getFormModel from 'src/app/models/form.server.model'
// eslint-disable-next-line import/first
mantariksh marked this conversation as resolved.
Show resolved Hide resolved
import { handleSns } from 'src/app/modules/bounce/bounce.controller'
import getBounceModel from 'src/app/modules/bounce/bounce.model'
import * as BounceService from 'src/app/modules/bounce/bounce.service'
import * as FormService from 'src/app/modules/form/form.service'
Expand All @@ -19,6 +21,7 @@ import dbHandler from 'tests/unit/backend/helpers/jest-db'
import expressHandler from 'tests/unit/backend/helpers/jest-express'

import { DatabaseError } from '../../core/core.errors'
import { InvalidNotificationError } from '../bounce.errors'

const Bounce = getBounceModel(mongoose)
const FormModel = getFormModel(mongoose)
Expand All @@ -39,8 +42,6 @@ jest.doMock('mongoose', () => ({
VersionError: MockVersionError,
},
}))
// eslint-disable-next-line import/first
import { handleSns } from 'src/app/modules/bounce/bounce.controller'

const MOCK_NOTIFICATION = { notificationType: 'Bounce' } as IEmailNotification
const MOCK_REQ = expressHandler.mockRequest({
Expand Down Expand Up @@ -100,69 +101,60 @@ describe('handleSns', () => {
beforeEach(() => {
jest.resetAllMocks()
// Default mocks
MockBounceService.isValidSnsRequest.mockResolvedValue(true)
MockBounceService.validateSnsRequest.mockReturnValue(okAsync(true))
MockBounceService.safeParseNotification.mockReturnValue(
ok(MOCK_NOTIFICATION),
)
MockBounceService.extractEmailType.mockReturnValue(EmailType.AdminResponse)
MockBounceService.getUpdatedBounceDoc.mockResolvedValue(mockBounceDoc)
MockBounceService.getUpdatedBounceDoc.mockReturnValue(
okAsync(mockBounceDoc),
)
MockFormService.retrieveFullFormById.mockResolvedValue(okAsync(mockForm))
mockBounceDoc.isCriticalBounce.mockReturnValue(true)
MockBounceService.getEditorsWithContactNumbers.mockResolvedValue(
MOCK_CONTACTS,
MockBounceService.getEditorsWithContactNumbers.mockReturnValue(
okAsync(MOCK_CONTACTS),
)
mockBounceDoc.hasNotified.mockReturnValue(false)
MockBounceService.notifyAdminsOfBounce.mockResolvedValue({
emailRecipients: MOCK_EMAIL_RECIPIENTS,
smsRecipients: MOCK_CONTACTS,
})
MockBounceService.sendEmailBounceNotification.mockReturnValue(
okAsync(MOCK_EMAIL_RECIPIENTS),
)
MockBounceService.sendSmsBounceNotification.mockReturnValue(
okAsync(MOCK_CONTACTS),
)
MockBounceService.saveBounceDoc.mockReturnValue(okAsync(mockBounceDoc))
// Note that this is true to simulate permanent bounce
mockBounceDoc.areAllPermanentBounces.mockReturnValue(true)
})

afterEach(async () => await dbHandler.clearDatabase())

it('should return immediately when requests are invalid', async () => {
MockBounceService.isValidSnsRequest.mockResolvedValueOnce(false)
await handleSns(MOCK_REQ, MOCK_RES, jest.fn())
expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
it('should return 401 when requests are invalid', async () => {
MockBounceService.validateSnsRequest.mockReturnValueOnce(
errAsync(new InvalidNotificationError()),
)
expect(MockBounceService.logEmailNotification).not.toHaveBeenCalled()
expect(MockFormService.retrieveFullFormById).not.toHaveBeenCalled()
expect(
MockBounceService.getEditorsWithContactNumbers,
).not.toHaveBeenCalled()
expect(MockBounceService.notifyAdminsOfBounce).not.toHaveBeenCalled()
expect(MockFormService.deactivateForm).not.toHaveBeenCalled()
expect(MockBounceService.notifyAdminsOfDeactivation).not.toHaveBeenCalled()
expect(MockBounceService.logCriticalBounce).not.toHaveBeenCalled()
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(403)
})

it('should return 400 when errors are thrown in isValidSnsRequest', async () => {
MockBounceService.isValidSnsRequest.mockImplementation(() => {
throw new Error()
})
await handleSns(MOCK_REQ, MOCK_RES, jest.fn())
expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
expect(MockBounceService.validateSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).not.toHaveBeenCalled()
expect(MockFormService.retrieveFullFormById).not.toHaveBeenCalled()
expect(
MockBounceService.getEditorsWithContactNumbers,
).not.toHaveBeenCalled()
expect(MockBounceService.notifyAdminsOfBounce).not.toHaveBeenCalled()
expect(MockBounceService.sendEmailBounceNotification).not.toHaveBeenCalled()
expect(MockBounceService.sendSmsBounceNotification).not.toHaveBeenCalled()
expect(MockFormService.deactivateForm).not.toHaveBeenCalled()
expect(MockBounceService.notifyAdminsOfDeactivation).not.toHaveBeenCalled()
expect(MockBounceService.logCriticalBounce).not.toHaveBeenCalled()
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(400)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(401)
})

it('should call services correctly for permanent critical bounces', async () => {
// Note that default mocks simulate permanent critical bounce, so no changes
// to mocks are needed
await handleSns(MOCK_REQ, MOCK_RES, jest.fn())

expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
expect(MockBounceService.validateSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).toHaveBeenCalledWith(
Expand All @@ -181,7 +173,11 @@ describe('handleSns', () => {
expect(MockBounceService.getEditorsWithContactNumbers).toHaveBeenCalledWith(
mockForm,
)
expect(MockBounceService.notifyAdminsOfBounce).toHaveBeenCalledWith(
expect(MockBounceService.sendEmailBounceNotification).toHaveBeenCalledWith(
mockBounceDoc,
mockForm,
)
expect(MockBounceService.sendSmsBounceNotification).toHaveBeenCalledWith(
mockBounceDoc,
mockForm,
MOCK_CONTACTS,
Expand All @@ -205,7 +201,7 @@ describe('handleSns', () => {
autoSmsRecipients: MOCK_CONTACTS,
hasDeactivated: true,
})
expect(mockBounceDoc.save).toHaveBeenCalled()
expect(MockBounceService.saveBounceDoc).toHaveBeenCalledWith(mockBounceDoc)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(200)
})

Expand All @@ -215,7 +211,7 @@ describe('handleSns', () => {

await handleSns(MOCK_REQ, MOCK_RES, jest.fn())

expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
expect(MockBounceService.validateSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).toHaveBeenCalledWith(
Expand All @@ -234,7 +230,11 @@ describe('handleSns', () => {
expect(MockBounceService.getEditorsWithContactNumbers).toHaveBeenCalledWith(
mockForm,
)
expect(MockBounceService.notifyAdminsOfBounce).toHaveBeenCalledWith(
expect(MockBounceService.sendEmailBounceNotification).toHaveBeenCalledWith(
mockBounceDoc,
mockForm,
)
expect(MockBounceService.sendSmsBounceNotification).toHaveBeenCalledWith(
mockBounceDoc,
mockForm,
MOCK_CONTACTS,
Expand All @@ -254,16 +254,16 @@ describe('handleSns', () => {
autoSmsRecipients: MOCK_CONTACTS,
hasDeactivated: false,
})
expect(mockBounceDoc.save).toHaveBeenCalled()
expect(MockBounceService.saveBounceDoc).toHaveBeenCalledWith(mockBounceDoc)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(200)
})

it('should return 400 when errors are thrown in getUpdatedBounceDoc', async () => {
MockBounceService.getUpdatedBounceDoc.mockImplementationOnce(() => {
throw new Error()
})
it('should return 200 even when errors are thrown in getUpdatedBounceDoc', async () => {
MockBounceService.getUpdatedBounceDoc.mockReturnValueOnce(
errAsync(new DatabaseError()),
)
await handleSns(MOCK_REQ, MOCK_RES, jest.fn())
expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
expect(MockBounceService.validateSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).toHaveBeenCalledWith(
Expand All @@ -275,7 +275,7 @@ describe('handleSns', () => {
expect(MockBounceService.getUpdatedBounceDoc).toHaveBeenCalledWith(
MOCK_NOTIFICATION,
)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(400)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(200)
})

it('should return 500 early if error occurs while retrieving form', async () => {
Expand All @@ -285,7 +285,7 @@ describe('handleSns', () => {

await handleSns(MOCK_REQ, MOCK_RES, jest.fn())

expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
expect(MockBounceService.validateSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).toHaveBeenCalledWith(
Expand All @@ -303,19 +303,20 @@ describe('handleSns', () => {
expect(
MockBounceService.getEditorsWithContactNumbers,
).not.toHaveBeenCalled()
expect(MockBounceService.notifyAdminsOfBounce).not.toHaveBeenCalled()
expect(MockBounceService.sendEmailBounceNotification).not.toHaveBeenCalled()
expect(MockBounceService.sendSmsBounceNotification).not.toHaveBeenCalled()
expect(MockBounceService.notifyAdminsOfDeactivation).not.toHaveBeenCalled()
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(500)
})

it('should return 400 when errors are thrown in deactivateForm', async () => {
MockFormService.deactivateForm.mockImplementationOnce(() => {
throw new Error()
})
it('should return 200 even when errors are returned from deactivateForm', async () => {
MockFormService.deactivateForm.mockReturnValueOnce(
errAsync(new DatabaseError()),
)

await handleSns(MOCK_REQ, MOCK_RES, jest.fn())

expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
expect(MockBounceService.validateSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).toHaveBeenCalledWith(
Expand All @@ -338,41 +339,7 @@ describe('handleSns', () => {
expect(MockFormService.deactivateForm).toHaveBeenCalledWith(
mockBounceDoc.formId,
)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(400)
})

it('should return 400 when errors are thrown in notifyAdminsOfBounce', async () => {
MockBounceService.notifyAdminsOfBounce.mockImplementationOnce(() => {
throw new Error()
})

await handleSns(MOCK_REQ, MOCK_RES, jest.fn())

expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).toHaveBeenCalledWith(
MOCK_NOTIFICATION,
)
expect(MockBounceService.extractEmailType).toHaveBeenCalledWith(
MOCK_NOTIFICATION,
)
expect(MockBounceService.getUpdatedBounceDoc).toHaveBeenCalledWith(
MOCK_NOTIFICATION,
)
expect(MockFormService.retrieveFullFormById).toHaveBeenCalledWith(
mockBounceDoc.formId,
)
expect(mockBounceDoc.isCriticalBounce).toHaveBeenCalled()
expect(MockBounceService.getEditorsWithContactNumbers).toHaveBeenCalledWith(
mockForm,
)
expect(MockBounceService.notifyAdminsOfBounce).toHaveBeenCalledWith(
mockBounceDoc,
mockForm,
MOCK_CONTACTS,
)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(400)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(200)
})

it('should return 200 when VersionErrors are thrown', async () => {
Expand All @@ -382,7 +349,7 @@ describe('handleSns', () => {

await handleSns(MOCK_REQ, MOCK_RES, jest.fn())

expect(MockBounceService.isValidSnsRequest).toHaveBeenCalledWith(
expect(MockBounceService.validateSnsRequest).toHaveBeenCalledWith(
MOCK_REQ.body,
)
expect(MockBounceService.logEmailNotification).toHaveBeenCalledWith(
Expand All @@ -405,7 +372,11 @@ describe('handleSns', () => {
expect(MockFormService.deactivateForm).toHaveBeenCalledWith(
mockBounceDoc.formId,
)
expect(MockBounceService.notifyAdminsOfBounce).toHaveBeenCalledWith(
expect(MockBounceService.sendEmailBounceNotification).toHaveBeenCalledWith(
mockBounceDoc,
mockForm,
)
expect(MockBounceService.sendSmsBounceNotification).toHaveBeenCalledWith(
mockBounceDoc,
mockForm,
MOCK_CONTACTS,
Expand All @@ -421,7 +392,7 @@ describe('handleSns', () => {
autoSmsRecipients: MOCK_CONTACTS,
hasDeactivated: true,
})
expect(mockBounceDoc.save).toHaveBeenCalled()
expect(MockBounceService.saveBounceDoc).toHaveBeenCalledWith(mockBounceDoc)
expect(MOCK_RES.sendStatus).toHaveBeenCalledWith(200)
})
})
Loading