|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { sendSnsEvent } from "tests/helpers/send-sns-event"; |
| 3 | +import { createPreparedV1Event } from "tests/helpers/event-fixtures"; |
| 4 | +import { randomUUID } from "node:crypto"; |
| 5 | +import { logger } from "tests/helpers/pino-logger"; |
| 6 | +import { createValidRequestHeaders } from "tests/constants/request-headers"; |
| 7 | +import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper"; |
| 8 | +import { SUPPLIER_LETTERS } from "tests/constants/api-constants"; |
| 9 | +import { |
| 10 | + pollSupplierAllocatorLogForResolvedSpec, |
| 11 | + pollUpsertLetterLogForError, |
| 12 | +} from "tests/helpers/aws-cloudwatch-helper"; |
| 13 | + |
| 14 | +let baseUrl: string; |
| 15 | + |
| 16 | +test.beforeAll(async () => { |
| 17 | + baseUrl = await getRestApiGatewayBaseUrl(); |
| 18 | +}); |
| 19 | + |
| 20 | +test.describe("Event Subscription SNS Tests", () => { |
| 21 | + test("Verify that the publish event to nhs-main-supapi-eventsub topic inserts data into db", async ({ |
| 22 | + request, |
| 23 | + }) => { |
| 24 | + const domainId = randomUUID(); |
| 25 | + logger.info(`Testing event subscription with domainId: ${domainId}`); |
| 26 | + const preparedEvent = createPreparedV1Event({ domainId }); |
| 27 | + const response = await sendSnsEvent(preparedEvent); |
| 28 | + const RETRY_DELAY_MS = 30_000; |
| 29 | + |
| 30 | + expect(response.MessageId).toBeTruthy(); |
| 31 | + |
| 32 | + // poll supplier allocator to check if supplier has been allocated |
| 33 | + const message = await pollSupplierAllocatorLogForResolvedSpec(domainId); |
| 34 | + const supplierAllocatorLog = JSON.parse(message) as { |
| 35 | + msg?: { supplierSpec?: { supplierId?: string } }; |
| 36 | + }; |
| 37 | + const supplierId = supplierAllocatorLog.msg?.supplierSpec?.supplierId; |
| 38 | + |
| 39 | + logger.info( |
| 40 | + `Supplier ${supplierId} allocated for domainId ${domainId} in supplier allocator lambda`, |
| 41 | + ); |
| 42 | + if (!supplierId) { |
| 43 | + throw new Error("supplierId was not found in supplier allocator log"); |
| 44 | + } |
| 45 | + |
| 46 | + const headers = createValidRequestHeaders(supplierId); |
| 47 | + let statusCode = 0; |
| 48 | + let letterStatus: string | undefined; |
| 49 | + |
| 50 | + for (let attempt = 1; attempt <= 3; attempt++) { |
| 51 | + const getLetterResponse = await request.get( |
| 52 | + `${baseUrl}/${SUPPLIER_LETTERS}/${domainId}`, |
| 53 | + { |
| 54 | + headers, |
| 55 | + }, |
| 56 | + ); |
| 57 | + |
| 58 | + statusCode = getLetterResponse.status(); |
| 59 | + const responseBody = (await getLetterResponse.json()) as { |
| 60 | + data?: { attributes?: { status?: string } }; |
| 61 | + }; |
| 62 | + letterStatus = responseBody.data?.attributes?.status; |
| 63 | + |
| 64 | + if (statusCode === 200 && letterStatus === "PENDING") { |
| 65 | + logger.info( |
| 66 | + `Attempt ${attempt}: Received status code ${statusCode} for domainId: ${domainId}`, |
| 67 | + ); |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + if (attempt < 3) { |
| 72 | + logger.info( |
| 73 | + `Attempt ${attempt}: Received status code ${statusCode} for domainId: ${domainId}. Retrying after ${RETRY_DELAY_MS / 1000} seconds...`, |
| 74 | + ); |
| 75 | + await new Promise((resolve) => { |
| 76 | + setTimeout(resolve, RETRY_DELAY_MS); // Wait for 30 seconds before the next attempt |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + expect(statusCode).toBe(200); |
| 81 | + expect(letterStatus).toBe("PENDING"); |
| 82 | + }); |
| 83 | + |
| 84 | + test("Verify that the publish event with 'CANCELLED' status throws error", async ({ |
| 85 | + request, |
| 86 | + }) => { |
| 87 | + const domainId = randomUUID(); |
| 88 | + logger.info(`Testing event subscription with domainId: ${domainId}`); |
| 89 | + const preparedEvent = createPreparedV1Event({ |
| 90 | + domainId, |
| 91 | + status: "CANCELLED", |
| 92 | + }); |
| 93 | + const response = await sendSnsEvent(preparedEvent); |
| 94 | + |
| 95 | + expect(response.MessageId).toBeTruthy(); |
| 96 | + |
| 97 | + // poll supplier allocator to check if supplier has been allocated |
| 98 | + const message = await pollSupplierAllocatorLogForResolvedSpec(domainId); |
| 99 | + const supplierAllocatorLog = JSON.parse(message) as { |
| 100 | + msg?: { supplierSpec?: { supplierId?: string } }; |
| 101 | + }; |
| 102 | + const supplierId = supplierAllocatorLog.msg?.supplierSpec?.supplierId; |
| 103 | + |
| 104 | + logger.info( |
| 105 | + `Supplier ${supplierId} allocated for domainId ${domainId} in supplier allocator lambda`, |
| 106 | + ); |
| 107 | + if (!supplierId) { |
| 108 | + throw new Error("supplierId was not found in supplier allocator log"); |
| 109 | + } |
| 110 | + |
| 111 | + const headers = createValidRequestHeaders(supplierId); |
| 112 | + |
| 113 | + const getLetterResponse = await request.get( |
| 114 | + `${baseUrl}/${SUPPLIER_LETTERS}/${domainId}`, |
| 115 | + { |
| 116 | + headers, |
| 117 | + }, |
| 118 | + ); |
| 119 | + |
| 120 | + expect(getLetterResponse.status()).toBe(500); |
| 121 | + await pollUpsertLetterLogForError( |
| 122 | + "Message did not match an expected schema", |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + test("Verify that the duplicate event throws an error", async () => { |
| 127 | + const domainId = randomUUID(); |
| 128 | + logger.info(`Testing event subscription with domainId: ${domainId}`); |
| 129 | + const preparedEvent = createPreparedV1Event({ |
| 130 | + domainId, |
| 131 | + status: "PREPARED", |
| 132 | + }); |
| 133 | + const response = await sendSnsEvent(preparedEvent); |
| 134 | + |
| 135 | + expect(response.MessageId).toBeTruthy(); |
| 136 | + |
| 137 | + // poll supplier allocator to check if supplier has been allocated |
| 138 | + const message = await pollSupplierAllocatorLogForResolvedSpec(domainId); |
| 139 | + const supplierAllocatorLog = JSON.parse(message) as { |
| 140 | + msg?: { supplierSpec?: { supplierId?: string } }; |
| 141 | + }; |
| 142 | + const supplierId = supplierAllocatorLog.msg?.supplierSpec?.supplierId; |
| 143 | + |
| 144 | + logger.info( |
| 145 | + `Supplier ${supplierId} allocated for domainId ${domainId} in supplier allocator lambda`, |
| 146 | + ); |
| 147 | + if (!supplierId) { |
| 148 | + throw new Error("supplierId was not found in supplier allocator log"); |
| 149 | + } |
| 150 | + |
| 151 | + // send same event again to simulate duplicate event |
| 152 | + const duplicateResponse = await sendSnsEvent(preparedEvent); |
| 153 | + expect(duplicateResponse.MessageId).toBeTruthy(); |
| 154 | + |
| 155 | + // poll supplier upsert to check if duplicate event was processed |
| 156 | + await pollUpsertLetterLogForError( |
| 157 | + `Letter with id ${domainId} already exists for supplier ${supplierId}"`, |
| 158 | + ); |
| 159 | + }); |
| 160 | +}); |
0 commit comments