-
Notifications
You must be signed in to change notification settings - Fork 88
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(webhook-retries): add unit tests for message, producer, consumer (7) #1988
Merged
mantariksh
merged 4 commits into
webhook-retries/main
from
webhook-retries/test-wh-message
Jun 1, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
285 changes: 285 additions & 0 deletions
285
src/app/modules/webhook/__tests__/webhook.consumer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
import aws from 'aws-sdk' | ||
import { ObjectId } from 'bson' | ||
import { addHours } from 'date-fns' | ||
import mongoose from 'mongoose' | ||
import { errAsync, okAsync } from 'neverthrow' | ||
import { mocked } from 'ts-jest/utils' | ||
|
||
import { getEncryptSubmissionModel } from 'src/app/models/submission.server.model' | ||
import { IWebhookResponse, SubmissionWebhookInfo } from 'src/types' | ||
|
||
import dbHandler from 'tests/unit/backend/helpers/jest-db' | ||
|
||
import { createWebhookQueueHandler } from '../webhook.consumer' | ||
import { WebhookPushToQueueError } from '../webhook.errors' | ||
import { WebhookProducer } from '../webhook.producer' | ||
import * as WebhookService from '../webhook.service' | ||
import { WebhookQueueMessageObject } from '../webhook.types' | ||
|
||
jest.mock('../webhook.service') | ||
const MockWebhookService = mocked(WebhookService, true) | ||
|
||
const EncryptSubmissionModel = getEncryptSubmissionModel(mongoose) | ||
|
||
const MOCK_WEBHOOK_SUCCESS_RESPONSE: IWebhookResponse = { | ||
signature: 'mockSignature', | ||
webhookUrl: 'mockWebhookUrl', | ||
response: { | ||
data: 'mockData', | ||
headers: 'mockHeaders', | ||
status: 200, | ||
}, | ||
} | ||
const MOCK_WEBHOOK_FAILURE_RESPONSE: IWebhookResponse = { | ||
signature: 'mockSignature', | ||
webhookUrl: 'mockWebhookUrl', | ||
response: { | ||
data: 'mockData', | ||
headers: 'mockHeaders', | ||
status: 400, | ||
}, | ||
} | ||
|
||
const SUCCESS_PRODUCER = ({ | ||
sendMessage: jest.fn().mockReturnValue(okAsync(true)), | ||
} as unknown) as WebhookProducer | ||
|
||
const FAILURE_PRODUCER = ({ | ||
sendMessage: jest | ||
.fn() | ||
.mockReturnValue(errAsync(new WebhookPushToQueueError())), | ||
} as unknown) as WebhookProducer | ||
|
||
const VALID_MESSAGE_BODY: WebhookQueueMessageObject = { | ||
submissionId: new ObjectId().toHexString(), | ||
previousAttempts: [Date.now()], | ||
nextAttempt: Date.now(), | ||
_v: 0, | ||
} | ||
|
||
const VALID_SQS_MESSAGE: aws.SQS.Message = { | ||
Body: JSON.stringify(VALID_MESSAGE_BODY), | ||
} | ||
|
||
const MOCK_WEBHOOK_INFO = { | ||
isRetryEnabled: true, | ||
webhookUrl: 'some url', | ||
webhookView: { | ||
data: { | ||
submissionId: VALID_MESSAGE_BODY.submissionId, | ||
}, | ||
}, | ||
} as SubmissionWebhookInfo | ||
|
||
describe('webhook.consumer', () => { | ||
beforeAll(async () => await dbHandler.connect()) | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
jest.restoreAllMocks() | ||
}) | ||
afterAll(async () => await dbHandler.closeDatabase()) | ||
|
||
describe('createWebhookQueueHandler', () => { | ||
it('should reject when message body is undefined', async () => { | ||
const result = createWebhookQueueHandler(SUCCESS_PRODUCER)({}) | ||
|
||
await expect(result).toReject() | ||
}) | ||
|
||
it('should reject when message body cannot be parsed', async () => { | ||
const result = createWebhookQueueHandler(SUCCESS_PRODUCER)({ | ||
Body: 'yoooooooooooo', | ||
}) | ||
|
||
await expect(result).toReject() | ||
}) | ||
|
||
it('should requeue webhook when it is not due', async () => { | ||
const message = { | ||
Body: JSON.stringify({ | ||
...VALID_MESSAGE_BODY, | ||
// next attempt in the future | ||
nextAttempt: addHours(Date.now(), 1).getTime(), | ||
}), | ||
} | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(message), | ||
).toResolve() | ||
expect(MockWebhookService.sendWebhook).not.toHaveBeenCalled() | ||
expect(MockWebhookService.saveWebhookRecord).not.toHaveBeenCalled() | ||
expect(SUCCESS_PRODUCER.sendMessage).toHaveBeenCalled() | ||
}) | ||
|
||
it('should reject when it fails to requeue webhook which is not due', async () => { | ||
const message = { | ||
Body: JSON.stringify({ | ||
...VALID_MESSAGE_BODY, | ||
// next attempt in the future | ||
nextAttempt: addHours(Date.now(), 1).getTime(), | ||
}), | ||
} | ||
|
||
await expect( | ||
createWebhookQueueHandler(FAILURE_PRODUCER)(message), | ||
).toReject() | ||
expect(MockWebhookService.sendWebhook).not.toHaveBeenCalled() | ||
expect(MockWebhookService.saveWebhookRecord).not.toHaveBeenCalled() | ||
expect(FAILURE_PRODUCER.sendMessage).toHaveBeenCalled() | ||
}) | ||
|
||
it('should reject when submission ID cannot be found', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockResolvedValueOnce(null) | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(VALID_SQS_MESSAGE), | ||
).toReject() | ||
expect(MockWebhookService.sendWebhook).not.toHaveBeenCalled() | ||
expect(MockWebhookService.saveWebhookRecord).not.toHaveBeenCalled() | ||
expect(SUCCESS_PRODUCER.sendMessage).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should reject when database error occurs', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockRejectedValueOnce(new Error('')) | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(VALID_SQS_MESSAGE), | ||
).toReject() | ||
expect(MockWebhookService.sendWebhook).not.toHaveBeenCalled() | ||
expect(MockWebhookService.saveWebhookRecord).not.toHaveBeenCalled() | ||
expect(SUCCESS_PRODUCER.sendMessage).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should resolve when form has no webhook URL', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockResolvedValueOnce({ | ||
...MOCK_WEBHOOK_INFO, | ||
webhookUrl: '', | ||
}) | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(VALID_SQS_MESSAGE), | ||
).toResolve() | ||
expect(MockWebhookService.sendWebhook).not.toHaveBeenCalled() | ||
expect(MockWebhookService.saveWebhookRecord).not.toHaveBeenCalled() | ||
expect(SUCCESS_PRODUCER.sendMessage).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should resolve when form does not have retries enabled', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockResolvedValueOnce({ | ||
...MOCK_WEBHOOK_INFO, | ||
isRetryEnabled: false, | ||
}) | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(VALID_SQS_MESSAGE), | ||
).toResolve() | ||
expect(MockWebhookService.sendWebhook).not.toHaveBeenCalled() | ||
expect(MockWebhookService.saveWebhookRecord).not.toHaveBeenCalled() | ||
expect(SUCCESS_PRODUCER.sendMessage).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should resolve without requeuing when webhook succeeds', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockResolvedValueOnce(MOCK_WEBHOOK_INFO) | ||
MockWebhookService.sendWebhook.mockReturnValueOnce( | ||
okAsync(MOCK_WEBHOOK_SUCCESS_RESPONSE), | ||
) | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(VALID_SQS_MESSAGE), | ||
).toResolve() | ||
expect(MockWebhookService.sendWebhook).toHaveBeenCalledWith( | ||
MOCK_WEBHOOK_INFO.webhookView, | ||
MOCK_WEBHOOK_INFO.webhookUrl, | ||
) | ||
expect(MockWebhookService.saveWebhookRecord).toHaveBeenCalledWith( | ||
VALID_MESSAGE_BODY.submissionId, | ||
MOCK_WEBHOOK_SUCCESS_RESPONSE, | ||
) | ||
expect(SUCCESS_PRODUCER.sendMessage).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should requeue webhook when retry fails and there are retries remaining', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockResolvedValueOnce(MOCK_WEBHOOK_INFO) | ||
MockWebhookService.sendWebhook.mockReturnValueOnce( | ||
// note failure response instead of success | ||
okAsync(MOCK_WEBHOOK_FAILURE_RESPONSE), | ||
) | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(VALID_SQS_MESSAGE), | ||
).toResolve() | ||
expect(MockWebhookService.sendWebhook).toHaveBeenCalledWith( | ||
MOCK_WEBHOOK_INFO.webhookView, | ||
MOCK_WEBHOOK_INFO.webhookUrl, | ||
) | ||
expect(MockWebhookService.saveWebhookRecord).toHaveBeenCalledWith( | ||
VALID_MESSAGE_BODY.submissionId, | ||
MOCK_WEBHOOK_FAILURE_RESPONSE, | ||
) | ||
expect(SUCCESS_PRODUCER.sendMessage).toHaveBeenCalled() | ||
}) | ||
|
||
it('should resolve without requeuing when retry fails and there are no retries remaining', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockResolvedValueOnce(MOCK_WEBHOOK_INFO) | ||
MockWebhookService.sendWebhook.mockReturnValueOnce( | ||
okAsync(MOCK_WEBHOOK_SUCCESS_RESPONSE), | ||
) | ||
const message = { | ||
Body: JSON.stringify({ | ||
...VALID_MESSAGE_BODY, | ||
// length greater than max possible number of retries | ||
previousAttempts: Array(10).fill(0), | ||
}), | ||
} | ||
|
||
await expect( | ||
createWebhookQueueHandler(SUCCESS_PRODUCER)(message), | ||
).toResolve() | ||
expect(MockWebhookService.sendWebhook).toHaveBeenCalledWith( | ||
MOCK_WEBHOOK_INFO.webhookView, | ||
MOCK_WEBHOOK_INFO.webhookUrl, | ||
) | ||
expect(MockWebhookService.saveWebhookRecord).toHaveBeenCalledWith( | ||
VALID_MESSAGE_BODY.submissionId, | ||
MOCK_WEBHOOK_SUCCESS_RESPONSE, | ||
) | ||
expect(SUCCESS_PRODUCER.sendMessage).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should reject when retry fails and subsequently fails to be requeued', async () => { | ||
jest | ||
.spyOn(EncryptSubmissionModel, 'retrieveWebhookInfoById') | ||
.mockResolvedValueOnce(MOCK_WEBHOOK_INFO) | ||
MockWebhookService.sendWebhook.mockReturnValueOnce( | ||
okAsync(MOCK_WEBHOOK_FAILURE_RESPONSE), | ||
) | ||
|
||
await expect( | ||
createWebhookQueueHandler(FAILURE_PRODUCER)(VALID_SQS_MESSAGE), | ||
).toReject() | ||
expect(MockWebhookService.sendWebhook).toHaveBeenCalledWith( | ||
MOCK_WEBHOOK_INFO.webhookView, | ||
MOCK_WEBHOOK_INFO.webhookUrl, | ||
) | ||
expect(MockWebhookService.saveWebhookRecord).toHaveBeenCalledWith( | ||
VALID_MESSAGE_BODY.submissionId, | ||
MOCK_WEBHOOK_FAILURE_RESPONSE, | ||
) | ||
expect(FAILURE_PRODUCER.sendMessage).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yoooooooooooooooooooooooo