|
1 | 1 | import { expect, test, describe, vi } from "vitest"; |
2 | | -import { createAuditLogEntry } from "../../../src/api/functions/auditLog"; |
| 2 | +import { createAuditLogEntry, buildAuditLogTransactPut } from "../../../src/api/functions/auditLog"; |
3 | 3 | import { mockClient } from "aws-sdk-client-mock"; |
4 | 4 | import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; |
5 | 5 | import { afterEach, beforeEach } from "node:test"; |
@@ -45,6 +45,110 @@ describe("Audit Log tests", () => { |
45 | 45 | }); |
46 | 46 | }); |
47 | 47 |
|
| 48 | +describe("Audit Log Transaction tests", () => { |
| 49 | + test("Audit log transaction item is correctly created with all fields", () => { |
| 50 | + // Setup mock date |
| 51 | + const mockDate = new Date(2025, 3, 20, 12, 0, 0); |
| 52 | + const mockTimestamp = mockDate.getTime(); |
| 53 | + vi.spyOn(Date, 'now').mockReturnValue(mockTimestamp); |
| 54 | + |
| 55 | + const timestamp = Math.floor(mockTimestamp / 1000); |
| 56 | + const expireAt = timestamp + Math.floor((365 * 24 * 60 * 60 * 1000) / 1000); |
| 57 | + |
| 58 | + // Create test payload |
| 59 | + const payload = { |
| 60 | + module: Modules.IAM, |
| 61 | + |
| 62 | + |
| 63 | + requestId: 'abcdef', |
| 64 | + message: "Created user" |
| 65 | + }; |
| 66 | + |
| 67 | + // Expected marshalled item |
| 68 | + const expectedItem = marshall({ |
| 69 | + ...payload, |
| 70 | + createdAt: timestamp, |
| 71 | + expireAt: expireAt |
| 72 | + }); |
| 73 | + |
| 74 | + // Call the function being tested |
| 75 | + const transactItem = buildAuditLogTransactPut({ entry: payload }); |
| 76 | + |
| 77 | + // Verify the result |
| 78 | + expect(transactItem).toStrictEqual({ |
| 79 | + Put: { |
| 80 | + TableName: genericConfig.AuditLogTable, |
| 81 | + Item: expectedItem |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test("Audit log transaction item with minimal fields is correctly created", () => { |
| 87 | + // Setup mock date |
| 88 | + const mockDate = new Date(2025, 3, 20, 12, 0, 0); |
| 89 | + const mockTimestamp = mockDate.getTime(); |
| 90 | + vi.spyOn(Date, 'now').mockReturnValue(mockTimestamp); |
| 91 | + |
| 92 | + const timestamp = Math.floor(mockTimestamp / 1000); |
| 93 | + const expireAt = timestamp + Math.floor((365 * 24 * 60 * 60 * 1000) / 1000); |
| 94 | + |
| 95 | + // Create test payload with only required fields |
| 96 | + const payload = { |
| 97 | + module: Modules.IAM, |
| 98 | + |
| 99 | + message: "Deleted resource" |
| 100 | + }; |
| 101 | + |
| 102 | + // Expected marshalled item |
| 103 | + const expectedItem = marshall({ |
| 104 | + ...payload, |
| 105 | + createdAt: timestamp, |
| 106 | + expireAt: expireAt |
| 107 | + }); |
| 108 | + |
| 109 | + // Call the function being tested |
| 110 | + const transactItem = buildAuditLogTransactPut({ entry: payload }); |
| 111 | + |
| 112 | + // Verify the result |
| 113 | + expect(transactItem).toStrictEqual({ |
| 114 | + Put: { |
| 115 | + TableName: genericConfig.AuditLogTable, |
| 116 | + Item: expectedItem |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + test("Audit log transaction item correctly calculates expiration timestamp", () => { |
| 122 | + // Setup mock date |
| 123 | + const mockDate = new Date(2025, 3, 20, 12, 0, 0); |
| 124 | + const mockTimestamp = mockDate.getTime(); |
| 125 | + vi.spyOn(Date, 'now').mockReturnValue(mockTimestamp); |
| 126 | + |
| 127 | + const timestamp = Math.floor(mockTimestamp / 1000); |
| 128 | + // Manually calculate the expected expiration |
| 129 | + const retentionDays = 365; |
| 130 | + const secondsInDay = 24 * 60 * 60; |
| 131 | + const millisecondsInDay = secondsInDay * 1000; |
| 132 | + const expectedExpireAt = timestamp + Math.floor((retentionDays * millisecondsInDay) / 1000); |
| 133 | + |
| 134 | + // Create test payload |
| 135 | + const payload = { |
| 136 | + module: Modules.IAM, |
| 137 | + |
| 138 | + message: "Modified settings" |
| 139 | + }; |
| 140 | + |
| 141 | + // Call the function being tested |
| 142 | + const transactItem = buildAuditLogTransactPut({ entry: payload }); |
| 143 | + |
| 144 | + // Extract and verify the expiration timestamp |
| 145 | + const marshalledItem = transactItem.Put.Item; |
| 146 | + const unmarshalledItem = require('@aws-sdk/util-dynamodb').unmarshall(marshalledItem); |
| 147 | + |
| 148 | + expect(unmarshalledItem.expireAt).toBe(expectedExpireAt); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
48 | 152 | beforeEach(() => { |
49 | 153 | ddbMock.reset(); |
50 | 154 | }); |
|
0 commit comments