Skip to content

Commit ad26574

Browse files
authored
[Reputation Oracle] test: cover health controller with unit tests (#3106)
1 parent 2e38dca commit ad26574

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { faker } from '@faker-js/faker';
2+
import { ServiceUnavailableException } from '@nestjs/common';
3+
import { Test } from '@nestjs/testing';
4+
import {
5+
HealthIndicatorResult,
6+
HealthIndicatorStatus,
7+
TerminusModule,
8+
TypeOrmHealthIndicator,
9+
} from '@nestjs/terminus';
10+
import { nestLoggerOverride } from '../../logger';
11+
import { ServerConfigService } from '../../config/server-config.service';
12+
import { HealthController } from './health.controller';
13+
14+
const mockServerConfigService = {
15+
gitHash: faker.git.commitSha(),
16+
};
17+
18+
const mockTypeOrmPingCheck = jest.fn();
19+
20+
function generateMockHealthIndicatorResult(
21+
testKey: string,
22+
status: HealthIndicatorStatus,
23+
): HealthIndicatorResult {
24+
return {
25+
[testKey]: {
26+
status,
27+
},
28+
};
29+
}
30+
31+
describe('HealthController', () => {
32+
let healthController: HealthController;
33+
34+
beforeAll(async () => {
35+
const moduleBuilder = Test.createTestingModule({
36+
imports: [TerminusModule],
37+
controllers: [HealthController],
38+
providers: [
39+
{
40+
provide: ServerConfigService,
41+
useValue: mockServerConfigService,
42+
},
43+
{
44+
provide: TypeOrmHealthIndicator,
45+
useValue: {
46+
pingCheck: mockTypeOrmPingCheck,
47+
},
48+
},
49+
],
50+
});
51+
52+
/**
53+
* Terminus uses nest logger internaly,
54+
* so override to omit logs in tests
55+
*/
56+
moduleBuilder.setLogger(nestLoggerOverride);
57+
58+
const moduleRef = await moduleBuilder.compile();
59+
60+
healthController = moduleRef.get(HealthController);
61+
});
62+
63+
it('/ping should return proper info', async () => {
64+
await expect(healthController.ping()).resolves.toEqual({
65+
appName: '@human-protocol/reputation-oracle',
66+
gitHash: mockServerConfigService.gitHash,
67+
nodeEnv: 'test',
68+
});
69+
});
70+
71+
describe('/check', () => {
72+
const expectedDbTestKey = 'database';
73+
const expectedPingCheckOptions = {
74+
timeout: 5000,
75+
};
76+
77+
afterEach(() => {
78+
mockTypeOrmPingCheck.mockReset();
79+
});
80+
81+
it(`returns 'up' status when db is up`, async () => {
82+
const statusUp = 'up';
83+
mockTypeOrmPingCheck.mockResolvedValueOnce(
84+
generateMockHealthIndicatorResult(expectedDbTestKey, statusUp),
85+
);
86+
87+
await expect(healthController.check()).resolves.toEqual(
88+
expect.objectContaining({
89+
status: 'ok',
90+
info: {
91+
[expectedDbTestKey]: {
92+
status: statusUp,
93+
},
94+
},
95+
}),
96+
);
97+
expect(mockTypeOrmPingCheck).toHaveBeenCalledTimes(1);
98+
expect(mockTypeOrmPingCheck).toHaveBeenCalledWith(
99+
expectedDbTestKey,
100+
expectedPingCheckOptions,
101+
);
102+
});
103+
104+
it(`returns 'down' status when db is down`, async () => {
105+
const statusDown = 'down';
106+
107+
mockTypeOrmPingCheck.mockResolvedValueOnce(
108+
generateMockHealthIndicatorResult(expectedDbTestKey, statusDown),
109+
);
110+
let thrownError;
111+
try {
112+
await healthController.check();
113+
} catch (error) {
114+
thrownError = error;
115+
}
116+
117+
expect(thrownError).toBeInstanceOf(ServiceUnavailableException);
118+
expect(thrownError.response).toEqual(
119+
expect.objectContaining({
120+
status: 'error',
121+
info: {},
122+
error: {
123+
[expectedDbTestKey]: {
124+
status: statusDown,
125+
},
126+
},
127+
}),
128+
);
129+
expect(mockTypeOrmPingCheck).toHaveBeenCalledTimes(1);
130+
expect(mockTypeOrmPingCheck).toHaveBeenCalledWith(
131+
expectedDbTestKey,
132+
expectedPingCheckOptions,
133+
);
134+
});
135+
});
136+
});

packages/apps/reputation-oracle/server/src/modules/health/health.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class HealthController {
5252
})
5353
@HealthCheck()
5454
@Get('/check')
55-
readiness(): Promise<HealthCheckResult> {
55+
check(): Promise<HealthCheckResult> {
5656
return this.health.check([
5757
async (): Promise<HealthIndicatorResult> =>
5858
this.db.pingCheck('database', {

0 commit comments

Comments
 (0)