-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetPublicKey.test.ts
More file actions
70 lines (58 loc) · 2.33 KB
/
Copy pathgetPublicKey.test.ts
File metadata and controls
70 lines (58 loc) · 2.33 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
import {
type ApiServiceAccount,
type PrismaClient,
prisma,
} from "@gcforms/database";
import { getPublicKey } from "@lib/formsClient/getPublicKey.js";
import {
getValueFromRedis,
setValueInRedis,
} from "@lib/integration/redis/redisClientAdapter.js";
import { logMessage } from "@lib/logging/logger.js";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { type DeepMockProxy, mockReset } from "vitest-mock-extended";
vi.mock("@lib/integration/redis/redisClientAdapter");
const getValueFromRedisMock = vi.mocked(getValueFromRedis);
const setValueInRedisMock = vi.mocked(setValueInRedis);
const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>;
describe("getPublicKey should", () => {
beforeEach(() => {
mockReset(prismaMock);
vi.clearAllMocks();
});
describe("return a public key if there is one associated to the service account id", () => {
it("when it does exist in cache", async () => {
getValueFromRedisMock.mockResolvedValueOnce(
"RkS8hzu0MtwL+Qs2lK7KX9CLK7v6lxYpqs7ns5MwuOs=",
);
const publicKey = await getPublicKey("254354365464565461");
expect(publicKey).toEqual("RkS8hzu0MtwL+Qs2lK7KX9CLK7v6lxYpqs7ns5MwuOs=");
});
it("when it does not exist in cache", async () => {
prismaMock.apiServiceAccount.findUnique.mockResolvedValue({
publicKey: "RkS8hzu0MtwL+Qs2lK7KX9CLK7v6lxYpqs7ns5MwuOs=",
} as unknown as ApiServiceAccount);
const publicKey = await getPublicKey("254354365464565461");
expect(publicKey).toEqual("RkS8hzu0MtwL+Qs2lK7KX9CLK7v6lxYpqs7ns5MwuOs=");
expect(setValueInRedisMock).toHaveBeenCalledWith(
"api:publicKey:254354365464565461",
"RkS8hzu0MtwL+Qs2lK7KX9CLK7v6lxYpqs7ns5MwuOs=",
300,
);
});
});
it("throw an error if database has an internal failure", async () => {
const customError = new Error("custom error");
prismaMock.apiServiceAccount.findUnique.mockRejectedValueOnce(customError);
const logMessageSpy = vi.spyOn(logMessage, "info");
await expect(() => getPublicKey("254354365464565461")).rejects.toThrow(
customError,
);
expect(logMessageSpy).toHaveBeenCalledWith(
customError,
expect.stringContaining(
"[formsClient] Failed to retrieve public key. ServiceAccountId: 254354365464565461",
),
);
});
});