-
Notifications
You must be signed in to change notification settings - Fork 3
CCM-13146 Component tests for handling ttl #145
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
Merged
Ian-Hodges
merged 17 commits into
main
from
feature/CCM-13146_handle-ttl-component-tests
Dec 10, 2025
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a88f075
CCM-13146 simple ttl insertion and deletion
Ian-Hodges e804b6e
CCM-13146 linting
Ian-Hodges efee2df
CCM-13146 small rejig
Ian-Hodges 86d5b7d
Merge branch 'main' into feature/CCM-13146_handle-ttl-component-tests
Ian-Hodges 46e18ee
CCM-13146 give delivery.logs.amazonaws.com access to our cmk
Ian-Hodges e8b9836
CCM-13146 remove the cloudwatch log resource policy
Ian-Hodges 7f04a9b
CCM-13146 remove config that hopefully aws will add for us automatically
Ian-Hodges fdf47c1
CCM-13146 update tests to query cloudwatch
Ian-Hodges 36e4f22
CCM-13146 lint fixes
Ian-Hodges 2adc29b
CCM-13146 fix log filter pattern
Ian-Hodges f1ad1cd
CCM-13146 put log devivery sources back
Ian-Hodges e9790d9
CCM-13146 put log devivery destination back
Ian-Hodges f24670d
CCM-13146 update e2e tests so they pass
Ian-Hodges 9544c65
CCM-13146 add extra e2e test
Ian-Hodges cd1ce45
CCM-13146 remove logging of info generated from process environment v…
Ian-Hodges 0afd062
CCM-13146 update assertion for message search
Ian-Hodges 8f4fd35
CCM-13146 remove nested expectToPassEventually
Ian-Hodges 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
tests/playwright/digital-letters-component-tests/create-ttl.component.spec.ts
|
Ian-Hodges marked this conversation as resolved.
|
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
154 changes: 154 additions & 0 deletions
154
tests/playwright/digital-letters-component-tests/handle-ttl.component.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,154 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { ENV } from 'constants/backend-constants'; | ||
| import { getLogsFromCloudwatch } from 'helpers/cloudwatch-helpers'; | ||
| import { deleteTtl, putTtl } from 'helpers/dynamodb-helpers'; | ||
| import expectToPassEventually from 'helpers/expectations'; | ||
| import { expectMessageContainingString, purgeQueue } from 'helpers/sqs-helpers'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| test.describe('Digital Letters - Handle TTL', () => { | ||
| const handleTtlDlqName = `nhs-${ENV}-dl-ttl-handle-expiry-errors-queue`; | ||
|
|
||
| test.beforeAll(async () => { | ||
| await purgeQueue(handleTtlDlqName); | ||
| }); | ||
|
|
||
| const baseEvent = { | ||
| profileversion: '1.0.0', | ||
| profilepublished: '2025-10', | ||
| specversion: '1.0', | ||
| source: '/nhs/england/notify/production/primary/data-plane/digital-letters', | ||
| subject: | ||
| 'customer/920fca11-596a-4eca-9c47-99f624614658/recipient/769acdd4-6a47-496f-999f-76a6fd2c3959', | ||
| type: 'uk.nhs.notify.digital.letters.mesh.inbox.message.downloaded.v1', | ||
| time: '2023-06-20T12:00:00Z', | ||
| recordedtime: '2023-06-20T12:00:00.250Z', | ||
| severitynumber: 2, | ||
| traceparent: '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01', | ||
| datacontenttype: 'application/json', | ||
| dataschema: | ||
| 'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10/digital-letter-base-data.schema.json', | ||
| dataschemaversion: '1.0', | ||
| severitytext: 'INFO', | ||
| data: { | ||
| messageReference: 'ref1', | ||
| senderId: 'sender1', | ||
| }, | ||
| }; | ||
|
gareth-allan marked this conversation as resolved.
|
||
|
|
||
| test('should handle withdrawn item', async () => { | ||
| const letterId = uuidv4(); | ||
| const messageUri = `https://example.com/ttl/resource/${letterId}`; | ||
|
|
||
| const event = { | ||
| ...baseEvent, | ||
| id: letterId, | ||
| data: { | ||
| ...baseEvent.data, | ||
| messageUri, | ||
| 'digital-letter-id': letterId, | ||
|
gareth-allan marked this conversation as resolved.
|
||
| }, | ||
| }; | ||
|
|
||
| const ttlItem = { | ||
| PK: messageUri, | ||
| SK: 'TTL', | ||
| dateOfExpiry: '2023-12-31#0', | ||
| event, | ||
| ttl: Date.now() / 1000 + 3600, | ||
| withdrawn: true, | ||
| }; | ||
|
|
||
| const putResponseCode = await putTtl(ttlItem); | ||
| expect(putResponseCode).toBe(200); | ||
|
|
||
| const deleteResponseCode = await deleteTtl(messageUri); | ||
| expect(deleteResponseCode).toBe(200); | ||
|
|
||
| await expectToPassEventually(async () => { | ||
| const eventLogEntry = await getLogsFromCloudwatch( | ||
| `/aws/lambda/nhs-${ENV}-dl-ttl-handle-expiry`, | ||
| [ | ||
| `$.message.messageUri = "${messageUri}"`, | ||
| '$.message.description = "ItemDequeued event not sent as item withdrawn"', | ||
| ], | ||
| ); | ||
|
|
||
| expect(eventLogEntry.length).toEqual(1); | ||
| }); | ||
| }); | ||
|
|
||
| test('should handle expired item', async () => { | ||
| const letterId = uuidv4(); | ||
| const messageUri = `https://example.com/ttl/resource/${letterId}`; | ||
|
|
||
| const event = { | ||
| ...baseEvent, | ||
| id: letterId, | ||
| data: { | ||
| ...baseEvent.data, | ||
| messageUri, | ||
| 'digital-letter-id': letterId, | ||
| }, | ||
| }; | ||
|
|
||
| const ttlItem = { | ||
| PK: messageUri, | ||
| SK: 'TTL', | ||
| dateOfExpiry: '2023-12-31#0', | ||
| event, | ||
| ttl: Date.now() / 1000 + 3600, | ||
| }; | ||
|
|
||
| const putResponseCode = await putTtl(ttlItem); | ||
| expect(putResponseCode).toBe(200); | ||
|
|
||
| const deleteResponseCode = await deleteTtl(messageUri); | ||
| expect(deleteResponseCode).toBe(200); | ||
|
|
||
| await expectToPassEventually(async () => { | ||
| await expectToPassEventually(async () => { | ||
| const eventLogEntry = await getLogsFromCloudwatch( | ||
| `/aws/vendedlogs/events/event-bus/nhs-${ENV}-dl`, | ||
| [ | ||
| '$.message_type = "EVENT_RECEIPT"', | ||
| '$.details.detail_type = "uk.nhs.notify.digital.letters.queue.item.dequeued.v1"', | ||
| `$.details.event_detail = "*\\"messageUri\\":\\"${messageUri}\\"*"`, | ||
| ], | ||
| ); | ||
|
|
||
| expect(eventLogEntry.length).toEqual(1); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test('should send invalid item to dlq', async () => { | ||
| const letterId = uuidv4(); | ||
| const messageUri = `https://example.com/ttl/resource/${letterId}`; | ||
|
|
||
| const eventWithNoMessageUri = { | ||
| ...baseEvent, | ||
| id: letterId, | ||
| data: { | ||
| ...baseEvent.data, | ||
| 'digital-letter-id': letterId, | ||
| }, | ||
| }; | ||
|
|
||
| const ttlItem = { | ||
| PK: messageUri, | ||
| SK: 'TTL', | ||
| dateOfExpiry: '2023-12-31#0', | ||
| event: eventWithNoMessageUri, | ||
| ttl: Date.now() / 1000 + 3600, | ||
| }; | ||
|
|
||
| const putResponseCode = await putTtl(ttlItem); | ||
| expect(putResponseCode).toBe(200); | ||
|
|
||
| const deleteResponseCode = await deleteTtl(messageUri); | ||
| expect(deleteResponseCode).toBe(200); | ||
|
|
||
| await expectMessageContainingString(handleTtlDlqName, letterId); | ||
| }); | ||
| }); | ||
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,36 @@ | ||
| import { | ||
| CloudWatchLogsClient, | ||
| FilterLogEventsCommand, | ||
| } from '@aws-sdk/client-cloudwatch-logs'; | ||
| import { region } from 'utils'; | ||
| import { test } from '@playwright/test'; | ||
|
|
||
| const client = new CloudWatchLogsClient({ region: region() }); | ||
|
|
||
| let testStartTime = new Date(); | ||
|
|
||
| test.beforeEach(() => { | ||
| testStartTime = new Date(); | ||
| }); | ||
|
|
||
| /** | ||
| * @param logGroupName e.g. '/aws/lambda/nhs-main-dl-apim-key-generation' | ||
| * @param patterns e.g. [ '$.id = "someId"', '$.message.messageUri = "messageUri"' ] | ||
| */ | ||
| export async function getLogsFromCloudwatch( | ||
| logGroupName: string, | ||
| patterns: string[], | ||
| ): Promise<unknown[]> { | ||
| const filterEvents = new FilterLogEventsCommand({ | ||
| logGroupName, | ||
| startTime: testStartTime.getTime() - 60 * 1000, | ||
|
Ian-Hodges marked this conversation as resolved.
|
||
| filterPattern: `{${patterns.join(' && ')}}`, | ||
| limit: 50, | ||
| }); | ||
|
|
||
| const { events = [] } = await client.send(filterEvents); | ||
|
|
||
| return events.flatMap(({ message }) => | ||
| message ? [JSON.parse(message)] : [], | ||
| ); | ||
| } | ||
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
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,64 @@ | ||
| import { | ||
| DeleteMessageBatchCommand, | ||
| ReceiveMessageCommand, | ||
| ReceiveMessageCommandInput, | ||
| } from '@aws-sdk/client-sqs'; | ||
| import { expect } from '@playwright/test'; | ||
| import { SQS_URL_PREFIX } from 'constants/backend-constants'; | ||
| import { sqsClient } from 'utils'; | ||
| import expectToPassEventually from 'helpers/expectations'; | ||
|
|
||
| function getQueueUrl(queueName: string) { | ||
| return `${SQS_URL_PREFIX}${queueName}`; | ||
| } | ||
|
|
||
| export async function expectMessageContainingString( | ||
| queueName: string, | ||
| searchTerm: string, | ||
| ) { | ||
| const input: ReceiveMessageCommandInput = { | ||
| QueueUrl: getQueueUrl(queueName), | ||
| MaxNumberOfMessages: 10, | ||
| WaitTimeSeconds: 1, | ||
| VisibilityTimeout: 2, | ||
| }; | ||
|
|
||
| await expectToPassEventually(async () => { | ||
| const result = await sqsClient.send(new ReceiveMessageCommand(input)); | ||
| const polledMessages = result.Messages || []; | ||
|
|
||
| expect( | ||
| polledMessages.find((m) => (m.Body ?? '').includes(searchTerm)), | ||
| ).toBeDefined(); | ||
|
Ian-Hodges marked this conversation as resolved.
Outdated
|
||
| }); | ||
| } | ||
|
|
||
| export async function purgeQueue(queueName: string) { | ||
| const queueUrl = getQueueUrl(queueName); | ||
|
|
||
| for (;;) { | ||
| const result = await sqsClient.send( | ||
| new ReceiveMessageCommand({ | ||
| QueueUrl: queueUrl, | ||
| MaxNumberOfMessages: 10, | ||
| WaitTimeSeconds: 1, | ||
| }), | ||
| ); | ||
|
|
||
| const messages = result.Messages || []; | ||
|
|
||
| if (messages.length === 0) { | ||
| break; | ||
| } | ||
|
|
||
| await sqsClient.send( | ||
| new DeleteMessageBatchCommand({ | ||
| QueueUrl: queueUrl, | ||
| Entries: messages.map((msg, index) => ({ | ||
| Id: index.toString(), | ||
| ReceiptHandle: msg.ReceiptHandle!, | ||
| })), | ||
| }), | ||
| ); | ||
|
gareth-allan marked this conversation as resolved.
|
||
| } | ||
| } | ||
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.