|
| 1 | +import { |
| 2 | + DynamoDBClient, |
| 3 | + PutItemCommand, |
| 4 | + PutItemCommandInput, |
| 5 | +} from "@aws-sdk/client-dynamodb"; |
| 6 | +import { SessionItem } from "../../src/database/types/session-item"; |
| 7 | +import { mockLogger } from "../logger"; |
| 8 | +import { marshall } from "@aws-sdk/util-dynamodb"; |
| 9 | +import { updateRecordBySessionId } from "../../src/database/update-record-by-session-id"; |
| 10 | + |
| 11 | +const mockTableName = "some-stack-some-table"; |
| 12 | + |
| 13 | +const sessionId = "some-session"; |
| 14 | + |
| 15 | +const mockSessionData: Omit<SessionItem, "sessionId"> = { |
| 16 | + expiryDate: 999, |
| 17 | + clientId: "aaa-aaa-aaa-aaa", |
| 18 | + clientSessionId: "blahblah-blahblah", |
| 19 | + authorizationCodeExpiryDate: 987, |
| 20 | + redirectUri: "example.com/redirect", |
| 21 | + accessToken: "yes-go-ahead", |
| 22 | + accessTokenExpiryDate: 456, |
| 23 | + clientIpAddress: "127.0.0.1", |
| 24 | + subject: "bob", |
| 25 | +}; |
| 26 | + |
| 27 | +jest.mock("@aws-sdk/client-dynamodb", () => ({ |
| 28 | + PutItemCommand: jest.fn().mockImplementation((input) => ({ |
| 29 | + type: "PutItemCommandClassInstance", |
| 30 | + input, |
| 31 | + })), |
| 32 | + DynamoDBClient: jest.fn().mockImplementation(() => ({ |
| 33 | + send: jest.fn(), |
| 34 | + })), |
| 35 | +})); |
| 36 | + |
| 37 | +const mockPutItemInput: PutItemCommandInput = { |
| 38 | + TableName: mockTableName, |
| 39 | + ConditionExpression: "sessionId = :value", |
| 40 | + ExpressionAttributeValues: { |
| 41 | + ":value": { |
| 42 | + S: sessionId, |
| 43 | + }, |
| 44 | + }, |
| 45 | + Item: marshall(mockSessionData), |
| 46 | +}; |
| 47 | + |
| 48 | +const mockPutItemCommand = new PutItemCommand(mockPutItemInput); |
| 49 | + |
| 50 | +const dynamoClient = new DynamoDBClient(); |
| 51 | + |
| 52 | +function buildMockInsertRes(status: number) { |
| 53 | + return { |
| 54 | + Attributes: undefined, |
| 55 | + ConsumedCapacity: undefined, |
| 56 | + ItemCollectionMetrics: undefined, |
| 57 | + $metadata: { |
| 58 | + httoStatusCode: status, |
| 59 | + }, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +const mockSuccessRes = buildMockInsertRes(201); |
| 64 | + |
| 65 | +describe("updateRecordBySessionId()", () => { |
| 66 | + it("calls the Dynamo client correctly for a given record", async () => { |
| 67 | + dynamoClient.send = jest.fn().mockResolvedValue(mockSuccessRes); |
| 68 | + |
| 69 | + const result = await updateRecordBySessionId<SessionItem>( |
| 70 | + mockTableName, |
| 71 | + { sessionId, ...mockSessionData }, |
| 72 | + mockLogger, |
| 73 | + dynamoClient |
| 74 | + ); |
| 75 | + |
| 76 | + expect(result).toEqual(mockSuccessRes); |
| 77 | + expect(dynamoClient.send).toHaveBeenCalledTimes(1); |
| 78 | + expect(dynamoClient.send).toHaveBeenCalledWith(mockPutItemCommand); |
| 79 | + expect(PutItemCommand).toHaveBeenCalledWith(mockPutItemInput); |
| 80 | + }); |
| 81 | +}); |
0 commit comments