|
1 | | -import type { SQSEvent } from "aws-lambda"; |
2 | | -import { supplierConfigHandler } from ".."; |
| 1 | +import type { SQSEvent, SQSRecord } from "aws-lambda"; |
| 2 | +import createSupplierConfigIngressHandler from "../handler/supplier-config-ingress-handler"; |
| 3 | +import { Deps } from "../config/deps"; |
| 4 | + |
| 5 | +function createSqsRecord( |
| 6 | + messageId: string, |
| 7 | + type: string, |
| 8 | + data: Record<string, unknown>, |
| 9 | +): SQSRecord { |
| 10 | + const snsMessage = { |
| 11 | + Message: JSON.stringify({ type, data }), |
| 12 | + }; |
| 13 | + return { |
| 14 | + messageId, |
| 15 | + body: JSON.stringify(snsMessage), |
| 16 | + } as unknown as SQSRecord; |
| 17 | +} |
| 18 | + |
| 19 | +function createValidSupplierConfig() { |
| 20 | + return { |
| 21 | + id: "supplier-123", |
| 22 | + name: "Supplier supplier-123", |
| 23 | + channelType: "LETTER", |
| 24 | + dailyCapacity: 2000, |
| 25 | + status: "PROD", |
| 26 | + }; |
| 27 | +} |
3 | 28 |
|
4 | 29 | describe("supplierConfigHandler", () => { |
5 | | - it("returns an empty batchItemFailures list", async () => { |
| 30 | + let mockDeps: Deps; |
| 31 | + let handler: ReturnType<typeof createSupplierConfigIngressHandler>; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + mockDeps = { |
| 35 | + logger: { |
| 36 | + error: jest.fn(), |
| 37 | + info: jest.fn(), |
| 38 | + } as unknown as Deps["logger"], |
| 39 | + supplierConfigRepo: { |
| 40 | + upsertSupplierConfig: jest.fn().mockResolvedValue("CREATED"), |
| 41 | + } as unknown as Deps["supplierConfigRepo"], |
| 42 | + env: { SUPPLIER_CONFIG_TABLE_NAME: "test-table" }, |
| 43 | + }; |
| 44 | + handler = createSupplierConfigIngressHandler(mockDeps); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns an empty batchItemFailures list when there are no records", async () => { |
6 | 48 | const event = { Records: [] } as unknown as SQSEvent; |
7 | 49 |
|
8 | | - const result = await supplierConfigHandler(event); |
| 50 | + const result = await handler(event); |
9 | 51 |
|
10 | 52 | expect(result).toEqual({ batchItemFailures: [] }); |
11 | 53 | }); |
| 54 | + |
| 55 | + it("upserts supplier config from an SNS message in an SQS record", async () => { |
| 56 | + const data = createValidSupplierConfig(); |
| 57 | + const record = createSqsRecord( |
| 58 | + "msg-1", |
| 59 | + "uk.nhs.notify.supplier-config.supplier", |
| 60 | + data, |
| 61 | + ); |
| 62 | + const event = { Records: [record] } as unknown as SQSEvent; |
| 63 | + |
| 64 | + const result = await handler(event); |
| 65 | + |
| 66 | + expect(result).toEqual({ batchItemFailures: [] }); |
| 67 | + expect( |
| 68 | + mockDeps.supplierConfigRepo.upsertSupplierConfig, |
| 69 | + ).toHaveBeenCalledWith("supplier", data); |
| 70 | + }); |
| 71 | + |
| 72 | + it("reports failed records in batchItemFailures", async () => { |
| 73 | + ( |
| 74 | + mockDeps.supplierConfigRepo.upsertSupplierConfig as jest.Mock |
| 75 | + ).mockRejectedValue(new Error("DynamoDB error")); |
| 76 | + const record = createSqsRecord( |
| 77 | + "msg-fail", |
| 78 | + "uk.nhs.notify.supplier-config.supplier", |
| 79 | + { id: "supplier-123" }, |
| 80 | + ); |
| 81 | + const event = { Records: [record] } as unknown as SQSEvent; |
| 82 | + |
| 83 | + const result = await handler(event); |
| 84 | + |
| 85 | + expect(result).toEqual({ |
| 86 | + batchItemFailures: [{ itemIdentifier: "msg-fail" }], |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("rejects a type field containing no dots", async () => { |
| 91 | + const data = createValidSupplierConfig(); |
| 92 | + const record = createSqsRecord("msg-1", "look-no-dots", data); |
| 93 | + const event = { Records: [record] } as unknown as SQSEvent; |
| 94 | + |
| 95 | + const result = await handler(event); |
| 96 | + |
| 97 | + expect(result).toEqual({ |
| 98 | + batchItemFailures: [{ itemIdentifier: "msg-1" }], |
| 99 | + }); |
| 100 | + expect( |
| 101 | + mockDeps.supplierConfigRepo.upsertSupplierConfig, |
| 102 | + ).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("rejects a type field not matching the name of any entity", async () => { |
| 106 | + const data = createValidSupplierConfig(); |
| 107 | + const record = createSqsRecord( |
| 108 | + "msg-1", |
| 109 | + "uk.nhs.notify.supplier-config.suppler", |
| 110 | + data, |
| 111 | + ); |
| 112 | + const event = { Records: [record] } as unknown as SQSEvent; |
| 113 | + |
| 114 | + const result = await handler(event); |
| 115 | + |
| 116 | + expect(result).toEqual({ |
| 117 | + batchItemFailures: [{ itemIdentifier: "msg-1" }], |
| 118 | + }); |
| 119 | + expect( |
| 120 | + mockDeps.supplierConfigRepo.upsertSupplierConfig, |
| 121 | + ).not.toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("rejects an entity not matching the appropriate schema", async () => { |
| 125 | + const data = createValidSupplierConfig(); |
| 126 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 127 | + const { dailyCapacity, ...invalidData } = data; |
| 128 | + const record = createSqsRecord( |
| 129 | + "msg-1", |
| 130 | + "uk.nhs.notify.supplier-config.supplier", |
| 131 | + invalidData, |
| 132 | + ); |
| 133 | + const event = { Records: [record] } as unknown as SQSEvent; |
| 134 | + |
| 135 | + const result = await handler(event); |
| 136 | + |
| 137 | + expect(result).toEqual({ |
| 138 | + batchItemFailures: [{ itemIdentifier: "msg-1" }], |
| 139 | + }); |
| 140 | + expect( |
| 141 | + mockDeps.supplierConfigRepo.upsertSupplierConfig, |
| 142 | + ).not.toHaveBeenCalled(); |
| 143 | + }); |
12 | 144 | }); |
0 commit comments