|
1 |
| -import { vi, describe, it, expect, beforeEach } from "vitest"; |
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; |
2 | 2 | import { getMockReq, getMockRes } from "vitest-mock-express";
|
3 | 3 | import { checkServiceHealthOperation } from "@operations/checkServiceHealth.js";
|
4 |
| -import { DatabaseConnectorClient } from "@lib/integration/databaseConnector.js"; |
5 |
| -import { mockClient } from "aws-sdk-client-mock"; |
6 |
| -import { DynamoDBDocumentClient, ScanCommand } from "@aws-sdk/lib-dynamodb"; |
7 |
| -import { RedisConnector } from "@lib/integration/redis/redisConnector.js"; |
8 |
| -import { logMessage } from "@lib/logging/logger.js"; |
9 |
| -import axios from "axios"; |
10 |
| - |
11 |
| -const dynamoDbMock = mockClient(DynamoDBDocumentClient); |
12 |
| - |
13 |
| -vi.mock("@lib/integration/redis/redisConnector"); |
14 |
| -const redisConnectorMock = vi.mocked(RedisConnector); |
15 |
| - |
16 |
| -// biome-ignore lint/suspicious/noExplicitAny: we need to assign the Redis client and allow mock resolved values |
17 |
| -const redisClient: any = { |
18 |
| - ping: vi.fn(), |
19 |
| -}; |
20 |
| - |
21 |
| -redisConnectorMock.getInstance.mockResolvedValue({ client: redisClient }); |
22 |
| - |
23 |
| -const logMessageSpy = vi.spyOn(logMessage, "info"); |
24 | 4 |
|
25 | 5 | describe("checkServiceHealthOperation handler should", () => {
|
26 |
| - const requestMock = getMockReq(); |
27 | 6 | const { res: responseMock, next: nextMock, clearMockRes } = getMockRes();
|
28 | 7 |
|
29 | 8 | beforeEach(() => {
|
30 |
| - vi.clearAllMocks(); |
31 | 9 | clearMockRes();
|
32 |
| - dynamoDbMock.reset(); |
33 |
| - }); |
34 |
| - |
35 |
| - it("respond with success if all internal services are healthy", async () => { |
36 |
| - dynamoDbMock.on(ScanCommand).resolvesOnce({ Items: [] }); |
37 |
| - redisClient.ping.mockResolvedValueOnce("PONG"); |
38 |
| - vi.spyOn(DatabaseConnectorClient, "query").mockResolvedValueOnce(1); |
39 |
| - vi.spyOn(axios, "get").mockResolvedValueOnce({}); |
40 |
| - |
41 |
| - await checkServiceHealthOperation.handler( |
42 |
| - requestMock, |
43 |
| - responseMock, |
44 |
| - nextMock, |
45 |
| - ); |
46 |
| - |
47 |
| - expect(logMessageSpy).toHaveBeenCalledWith( |
48 |
| - "[service-health] dynamodb => healthy | redis => healthy | postgresql => healthy | zitadel => healthy", |
49 |
| - ); |
50 |
| - expect(responseMock.json).toHaveBeenCalledWith({ status: "healthy" }); |
51 | 10 | });
|
52 | 11 |
|
53 |
| - it("respond with error if DynamoDB service is unhealthy", async () => { |
54 |
| - dynamoDbMock.on(ScanCommand).rejectsOnce(new Error("custom error")); |
55 |
| - redisClient.ping.mockResolvedValueOnce("PONG"); |
56 |
| - vi.spyOn(DatabaseConnectorClient, "query").mockResolvedValueOnce(1); |
57 |
| - vi.spyOn(axios, "get").mockResolvedValueOnce({}); |
| 12 | + it("respond with running status if service is healthy", async () => { |
| 13 | + const requestMock = getMockReq(); |
58 | 14 |
|
59 | 15 | await checkServiceHealthOperation.handler(
|
60 | 16 | requestMock,
|
61 | 17 | responseMock,
|
62 | 18 | nextMock,
|
63 | 19 | );
|
64 | 20 |
|
65 |
| - expect(logMessageSpy).toHaveBeenCalledWith( |
66 |
| - "[service-health] dynamodb => unhealthy | redis => healthy | postgresql => healthy | zitadel => healthy", |
67 |
| - ); |
68 |
| - expect(responseMock.sendStatus).toHaveBeenCalledWith(503); |
69 |
| - }); |
70 |
| - |
71 |
| - it("respond with error if Redis service is unhealthy", async () => { |
72 |
| - dynamoDbMock.on(ScanCommand).resolvesOnce({ Items: [] }); |
73 |
| - redisClient.ping.mockRejectedValueOnce(new Error("custom error")); |
74 |
| - vi.spyOn(DatabaseConnectorClient, "query").mockResolvedValueOnce(1); |
75 |
| - vi.spyOn(axios, "get").mockResolvedValueOnce({}); |
76 |
| - |
77 |
| - await checkServiceHealthOperation.handler( |
78 |
| - requestMock, |
79 |
| - responseMock, |
80 |
| - nextMock, |
81 |
| - ); |
82 |
| - |
83 |
| - expect(logMessageSpy).toHaveBeenCalledWith( |
84 |
| - "[service-health] dynamodb => healthy | redis => unhealthy | postgresql => healthy | zitadel => healthy", |
85 |
| - ); |
86 |
| - expect(responseMock.sendStatus).toHaveBeenCalledWith(503); |
87 |
| - }); |
88 |
| - |
89 |
| - it("respond with error if PostgreSQL service is unhealthy", async () => { |
90 |
| - dynamoDbMock.on(ScanCommand).resolvesOnce({ Items: [] }); |
91 |
| - redisClient.ping.mockResolvedValueOnce("PONG"); |
92 |
| - vi.spyOn(DatabaseConnectorClient, "query").mockRejectedValueOnce( |
93 |
| - new Error("custom error"), |
94 |
| - ); |
95 |
| - vi.spyOn(axios, "get").mockResolvedValueOnce({}); |
96 |
| - |
97 |
| - await checkServiceHealthOperation.handler( |
98 |
| - requestMock, |
99 |
| - responseMock, |
100 |
| - nextMock, |
101 |
| - ); |
102 |
| - |
103 |
| - expect(logMessageSpy).toHaveBeenCalledWith( |
104 |
| - "[service-health] dynamodb => healthy | redis => healthy | postgresql => unhealthy | zitadel => healthy", |
105 |
| - ); |
106 |
| - expect(responseMock.sendStatus).toHaveBeenCalledWith(503); |
107 |
| - }); |
108 |
| - |
109 |
| - it("respond with error if Zitadel service is unhealthy", async () => { |
110 |
| - dynamoDbMock.on(ScanCommand).resolvesOnce({ Items: [] }); |
111 |
| - redisClient.ping.mockResolvedValueOnce("PONG"); |
112 |
| - vi.spyOn(DatabaseConnectorClient, "query").mockResolvedValueOnce(1); |
113 |
| - vi.spyOn(axios, "get").mockRejectedValueOnce(new Error("custom error")); |
114 |
| - |
115 |
| - await checkServiceHealthOperation.handler( |
116 |
| - requestMock, |
117 |
| - responseMock, |
118 |
| - nextMock, |
119 |
| - ); |
120 |
| - |
121 |
| - expect(logMessageSpy).toHaveBeenCalledWith( |
122 |
| - "[service-health] dynamodb => healthy | redis => healthy | postgresql => healthy | zitadel => unhealthy", |
123 |
| - ); |
124 |
| - expect(responseMock.sendStatus).toHaveBeenCalledWith(503); |
| 21 | + expect(responseMock.json).toHaveBeenCalledWith({ status: "running" }); |
125 | 22 | });
|
126 | 23 | });
|
0 commit comments