generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
CCM-12950: Add Functional Tests for Events #449
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
Open
Namitha-Prabhu
wants to merge
13
commits into
main
Choose a base branch
from
feature/CCM-12950-AddEventsTests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aaf44a9
new tests
Namitha-Prabhu e04ed21
Merge branch 'main' into feature/CCM-12950-AddEventsTests
Namitha-Prabhu a0b989c
Fix vulnerabilities
stevebux e1e8b7d
update
Namitha-Prabhu aaa26c0
update lockfile
Namitha-Prabhu 57cae58
lint errors
Namitha-Prabhu 9d2d436
fix
Namitha-Prabhu fdc6a52
add sandbox tests
Namitha-Prabhu fbc208d
merge main
Namitha-Prabhu 3ba5457
fix
Namitha-Prabhu 56eb911
add test time out
Namitha-Prabhu 52173a2
review fix
Namitha-Prabhu 2aedddb
fix
Namitha-Prabhu 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| [ | ||
| "component" | ||
| "component", | ||
| "sandbox" | ||
| ] |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
tests/component-tests/events-tests/event-subscription.spec.ts
This file contains hidden or 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,140 @@ | ||
| import { expect, test } from "@playwright/test"; | ||
| import { sendSnsEvent } from "tests/helpers/send-sns-event"; | ||
| import { createPreparedV1Event } from "tests/helpers/event-fixtures"; | ||
| import { randomUUID } from "node:crypto"; | ||
| import { logger } from "tests/helpers/pino-logger"; | ||
| import { createValidRequestHeaders } from "tests/constants/request-headers"; | ||
| import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper"; | ||
| import { SUPPLIER_LETTERS, envName } from "tests/constants/api-constants"; | ||
| import { | ||
| pollSupplierAllocatorLogForResolvedSpec, | ||
| pollUpsertLetterLogForError, | ||
| } from "tests/helpers/aws-cloudwatch-helper"; | ||
| import { supplierDataSetup } from "tests/helpers/suppliers-setup-helper"; | ||
| import { pollForLettersInDb } from "tests/helpers/poll-for-letters-helper"; | ||
|
|
||
| let baseUrl: string; | ||
|
|
||
| test.beforeAll(async () => { | ||
| baseUrl = await getRestApiGatewayBaseUrl(); | ||
| }); | ||
|
|
||
| test.describe("Event Subscription SNS Tests", () => { | ||
| test.setTimeout(180_000); // 3 minutes for long running polling | ||
| test(`Verify that the publish event to nhs-${envName}-supapi-eventsub topic inserts data into db`, async ({ | ||
| request, | ||
| }) => { | ||
| const domainId = randomUUID(); | ||
| logger.info(`Testing event subscription with domainId: ${domainId}`); | ||
| const preparedEvent = createPreparedV1Event({ domainId }); | ||
| const response = await sendSnsEvent(preparedEvent); | ||
|
|
||
| expect(response.MessageId).toBeTruthy(); | ||
|
|
||
| // poll supplier allocator to check if supplier has been allocated | ||
| const message = await pollSupplierAllocatorLogForResolvedSpec(domainId); | ||
| const supplierAllocatorLog = JSON.parse(message) as { | ||
| msg?: { supplierSpec?: { supplierId?: string } }; | ||
| }; | ||
| const supplierId = supplierAllocatorLog.msg?.supplierSpec?.supplierId; | ||
|
|
||
| logger.info( | ||
| `Supplier ${supplierId} allocated for domainId ${domainId} in supplier allocator lambda`, | ||
| ); | ||
| if (!supplierId) { | ||
| throw new Error("supplierId was not found in supplier allocator log"); | ||
| } | ||
|
|
||
| // check if supplier exists in suppliers table | ||
| await supplierDataSetup(supplierId); | ||
|
|
||
| // poll for letter to be inserted in db with status PENDING | ||
| const { letterStatus, statusCode } = await pollForLettersInDb( | ||
| request, | ||
| supplierId, | ||
| domainId, | ||
| baseUrl, | ||
| ); | ||
|
|
||
| expect(statusCode).toBe(200); | ||
| expect(letterStatus).toBe("PENDING"); | ||
| }); | ||
|
|
||
| test("Verify that the publish event with 'CANCELLED' status throws error", async ({ | ||
| request, | ||
| }) => { | ||
| const domainId = randomUUID(); | ||
| logger.info(`Testing event subscription with domainId: ${domainId}`); | ||
| const preparedEvent = createPreparedV1Event({ | ||
| domainId, | ||
| status: "CANCELLED", | ||
| }); | ||
| const response = await sendSnsEvent(preparedEvent); | ||
|
|
||
| expect(response.MessageId).toBeTruthy(); | ||
|
|
||
| // poll supplier allocator to check if supplier has been allocated | ||
| const message = await pollSupplierAllocatorLogForResolvedSpec(domainId); | ||
| const supplierAllocatorLog = JSON.parse(message) as { | ||
| msg?: { supplierSpec?: { supplierId?: string } }; | ||
| }; | ||
| const supplierId = supplierAllocatorLog.msg?.supplierSpec?.supplierId; | ||
|
|
||
| logger.info( | ||
| `Supplier ${supplierId} allocated for domainId ${domainId} in supplier allocator lambda`, | ||
| ); | ||
| if (!supplierId) { | ||
| throw new Error("supplierId was not found in supplier allocator log"); | ||
| } | ||
|
|
||
| const headers = createValidRequestHeaders(supplierId); | ||
|
|
||
| const getLetterResponse = await request.get( | ||
| `${baseUrl}/${SUPPLIER_LETTERS}/${domainId}`, | ||
| { | ||
| headers, | ||
| }, | ||
| ); | ||
|
|
||
| expect(getLetterResponse.status()).toBe(500); | ||
Namitha-Prabhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await pollUpsertLetterLogForError( | ||
| "Message did not match an expected schema", | ||
| domainId, | ||
| ); | ||
| }); | ||
|
|
||
| test("Verify that the duplicate event throws an error", async () => { | ||
| const domainId = randomUUID(); | ||
| logger.info(`Testing event subscription with domainId: ${domainId}`); | ||
| const preparedEvent = createPreparedV1Event({ | ||
| domainId, | ||
| status: "PREPARED", | ||
| }); | ||
| const response = await sendSnsEvent(preparedEvent); | ||
|
|
||
| expect(response.MessageId).toBeTruthy(); | ||
|
|
||
| // poll supplier allocator to check if supplier has been allocated | ||
| const message = await pollSupplierAllocatorLogForResolvedSpec(domainId); | ||
| const supplierAllocatorLog = JSON.parse(message) as { | ||
| msg?: { supplierSpec?: { supplierId?: string } }; | ||
| }; | ||
| const supplierId = supplierAllocatorLog.msg?.supplierSpec?.supplierId; | ||
|
|
||
| logger.info( | ||
| `Supplier ${supplierId} allocated for domainId ${domainId} in supplier allocator lambda`, | ||
| ); | ||
| if (!supplierId) { | ||
| throw new Error("supplierId was not found in supplier allocator log"); | ||
| } | ||
|
|
||
| // send same event again to simulate duplicate event | ||
| const duplicateResponse = await sendSnsEvent(preparedEvent); | ||
| expect(duplicateResponse.MessageId).toBeTruthy(); | ||
|
|
||
| // poll supplier upsert to check if duplicate event was processed | ||
| await pollUpsertLetterLogForError( | ||
| `Letter with id ${domainId} already exists for supplier ${supplierId}"`, | ||
Namitha-Prabhu marked this conversation as resolved.
Show resolved
Hide resolved
Namitha-Prabhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.