Skip to content

Commit 3c190e0

Browse files
committed
add transaction creator tests
1 parent b03fcb4 commit 3c190e0

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

tests/unit/functions/auditLog.test.ts

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test, describe, vi } from "vitest";
2-
import { createAuditLogEntry } from "../../../src/api/functions/auditLog";
2+
import { createAuditLogEntry, buildAuditLogTransactPut } from "../../../src/api/functions/auditLog";
33
import { mockClient } from "aws-sdk-client-mock";
44
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb";
55
import { afterEach, beforeEach } from "node:test";
@@ -45,6 +45,110 @@ describe("Audit Log tests", () => {
4545
});
4646
});
4747

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+
target: '[email protected]',
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+
48152
beforeEach(() => {
49153
ddbMock.reset();
50154
});

0 commit comments

Comments
 (0)