forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauditTrail.test.ts
More file actions
312 lines (257 loc) · 8.8 KB
/
Copy pathauditTrail.test.ts
File metadata and controls
312 lines (257 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Tests for the audit trail service and AuditLog model (Issue #145).
*
* Uses jest mocking so no live MongoDB connection is required.
*/
jest.mock("../models/AuditLog", () => {
const mockCreate = jest.fn();
const mockFind = jest.fn();
const mockChain = {
sort: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
lean: jest.fn().mockResolvedValue([]),
};
mockFind.mockReturnValue(mockChain);
return {
AuditLog: {
create: mockCreate,
find: mockFind,
__chain: mockChain,
},
};
});
import { AuditLog } from "../models/AuditLog";
import { hashWalletAddress, recordAuditEvent, queryAuditEvents } from "../services/auditTrail";
const mockCreate = AuditLog.create as jest.MockedFunction<typeof AuditLog.create>;
const mockFind = AuditLog.find as jest.MockedFunction<typeof AuditLog.find>;
const mockChain = (AuditLog as any).__chain;
beforeEach(() => {
jest.clearAllMocks();
mockFind.mockReturnValue(mockChain);
mockChain.sort.mockReturnValue(mockChain);
mockChain.limit.mockReturnValue(mockChain);
mockChain.lean.mockResolvedValue([]);
});
// ---------------------------------------------------------------------------
// recordAuditEvent
// ---------------------------------------------------------------------------
describe("recordAuditEvent", () => {
it("persists a challenge_issued event with all fields", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "challenge_issued",
result: "success",
promptId: "42",
walletAddress: "GABCDE",
requestId: "req-001",
clientIp: "127.0.0.1",
reason: null,
});
expect(mockCreate).toHaveBeenCalledTimes(1);
expect(mockCreate).toHaveBeenCalledWith({
action: "challenge_issued",
result: "success",
promptId: "42",
walletAddress: hashWalletAddress("GABCDE"),
requestId: "req-001",
clientIp: "127.0.0.1",
reason: null,
});
});
it("persists an unlock_success event", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "unlock_success",
result: "success",
promptId: "7",
walletAddress: "GX123",
requestId: "req-002",
clientIp: "10.0.0.1",
reason: null,
});
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({ action: "unlock_success", result: "success" }),
);
});
it("persists unlock_invalid_signature with reason code", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "unlock_invalid_signature",
result: "failure",
promptId: "7",
walletAddress: "GX123",
requestId: "req-003",
clientIp: "10.0.0.1",
reason: "invalid_signature",
});
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
action: "unlock_invalid_signature",
result: "failure",
reason: "invalid_signature",
}),
);
});
it("persists unlock_expired_challenge with reason code", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "unlock_expired_challenge",
result: "failure",
promptId: "7",
walletAddress: "GX123",
requestId: "req-004",
clientIp: "10.0.0.1",
reason: "expired_challenge",
});
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
action: "unlock_expired_challenge",
reason: "expired_challenge",
}),
);
});
it("persists unlock_no_access with reason code", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "unlock_no_access",
result: "failure",
promptId: "7",
walletAddress: "GX123",
requestId: "req-005",
clientIp: "10.0.0.1",
reason: "no_access",
});
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({ action: "unlock_no_access", reason: "no_access" }),
);
});
it("persists unlock_integrity_failure with reason code", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "unlock_integrity_failure",
result: "failure",
promptId: "7",
walletAddress: "GX123",
requestId: "req-006",
clientIp: "10.0.0.1",
reason: "integrity_failure",
});
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
action: "unlock_integrity_failure",
reason: "integrity_failure",
}),
);
});
it("persists unlock_rate_limited (blocked result)", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "unlock_rate_limited",
result: "blocked",
promptId: null,
walletAddress: null,
requestId: null,
clientIp: "10.0.0.1",
reason: "ip_rate_limit_exceeded",
});
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
action: "unlock_rate_limited",
result: "blocked",
reason: "ip_rate_limit_exceeded",
}),
);
});
it("does not reject when DB write fails (fire-and-forget)", async () => {
mockCreate.mockRejectedValueOnce(new Error("DB down"));
// Should resolve without throwing.
await expect(
recordAuditEvent({
action: "unlock_success",
result: "success",
promptId: "1",
walletAddress: "GABC",
requestId: null,
clientIp: null,
reason: null,
}),
).resolves.toBeUndefined();
});
it("does NOT store plaintext, keys, or signature fields", async () => {
mockCreate.mockResolvedValueOnce({} as never);
await recordAuditEvent({
action: "unlock_success",
result: "success",
promptId: "1",
walletAddress: "GABC",
requestId: null,
clientIp: null,
reason: null,
// Intentionally passing NO sensitive fields — the type signature enforces this.
});
const callArg = mockCreate.mock.calls[0][0] as Record<string, unknown>;
expect(callArg).not.toHaveProperty("plaintext");
expect(callArg).not.toHaveProperty("privateKey");
expect(callArg).not.toHaveProperty("signedMessage");
expect(callArg).not.toHaveProperty("challengeSecret");
expect(callArg).not.toHaveProperty("encryptedPrompt");
});
});
// ---------------------------------------------------------------------------
// queryAuditEvents
// ---------------------------------------------------------------------------
describe("queryAuditEvents", () => {
it("queries by walletAddress (lowercased) and returns results", async () => {
const fakeRecord = { action: "unlock_success", walletAddress: "gabcde" };
mockChain.lean.mockResolvedValueOnce([fakeRecord]);
const results = await queryAuditEvents({ walletAddress: "GABCDE" });
expect(mockFind).toHaveBeenCalledWith(
expect.objectContaining({ walletAddress: hashWalletAddress("GABCDE") }),
);
expect(results).toEqual([fakeRecord]);
});
it("queries by promptId", async () => {
mockChain.lean.mockResolvedValueOnce([]);
await queryAuditEvents({ promptId: "42" });
expect(mockFind).toHaveBeenCalledWith(
expect.objectContaining({ promptId: "42" }),
);
});
it("queries by action and result", async () => {
mockChain.lean.mockResolvedValueOnce([]);
await queryAuditEvents({ action: "unlock_no_access", result: "failure" });
expect(mockFind).toHaveBeenCalledWith(
expect.objectContaining({ action: "unlock_no_access", result: "failure" }),
);
});
it("applies since/until date range", async () => {
mockChain.lean.mockResolvedValueOnce([]);
const since = new Date("2025-01-01");
const until = new Date("2025-12-31");
await queryAuditEvents({ since, until });
expect(mockFind).toHaveBeenCalledWith(
expect.objectContaining({
createdAt: { $gte: since, $lte: until },
}),
);
});
it("respects custom limit", async () => {
mockChain.lean.mockResolvedValueOnce([]);
await queryAuditEvents({ limit: 25 });
expect(mockChain.limit).toHaveBeenCalledWith(25);
});
it("defaults limit to 100", async () => {
mockChain.lean.mockResolvedValueOnce([]);
await queryAuditEvents({});
expect(mockChain.limit).toHaveBeenCalledWith(100);
});
it("correlates records by requestId across wallet and promptId", async () => {
const rec1 = { action: "challenge_issued", requestId: "req-x", walletAddress: "gx1" };
const rec2 = { action: "unlock_success", requestId: "req-x", walletAddress: "gx1" };
mockChain.lean.mockResolvedValueOnce([rec1, rec2]);
const results = await queryAuditEvents({ walletAddress: "GX1" });
expect(results).toHaveLength(2);
expect(results[0].requestId).toBe("req-x");
expect(results[1].requestId).toBe("req-x");
});
});