-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealthcheck.test.ts
More file actions
136 lines (119 loc) · 5.38 KB
/
Copy pathhealthcheck.test.ts
File metadata and controls
136 lines (119 loc) · 5.38 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
import request from 'supertest';
import app from '../../../src/app';
import { initPassport } from '../../../src/middleware/passport-auth';
import { SUPPORTED_LOCALES } from '../../../src/middleware/translation';
import { Locale } from '../../../src/enums/locale';
import { ensureWorkerDataSources, resetDatabase } from '../../helpers/reset-database';
import { getTestUser } from '../../helpers/get-test-user';
import { getAuthHeader } from '../../helpers/auth-header';
import { UserDTO } from '../../../src/dtos/user/user-dto';
jest.mock('../../../src/services/blob-storage', () => {
return function BlobStorage() {
return {
getServiceClient: jest.fn().mockReturnValue({
getProperties: jest.fn().mockResolvedValue(true)
})
};
};
});
describe('Healthcheck', () => {
beforeAll(async () => {
await ensureWorkerDataSources();
await resetDatabase();
await initPassport();
});
describe('Server up', () => {
test('/healthcheck/ returns success', async () => {
const res = await request(app).get('/healthcheck/');
expect(res.status).toBe(200);
expect(res.body).toEqual({ message: 'success' });
});
});
describe('Server readiness', () => {
test('/healthcheck/ready returns success with session store status', async () => {
const res = await request(app).get('/healthcheck/ready');
expect(res.status).toBe(200);
expect(res.body.message).toBe('success');
expect(res.body.sessionStore).toEqual({ type: 'memory', connected: true });
});
});
describe('Server liveness', () => {
test('/healthcheck/live returns success', async () => {
const res = await request(app).get('/healthcheck/live');
expect(res.status).toBe(200);
expect(res.body).toEqual({ message: 'success' });
});
});
describe('Language detection', () => {
test('/healthcheck/language detects language as en if no header sent', async () => {
const res = await request(app).get('/healthcheck/language');
expect(res.status).toBe(200);
expect(res.body).toEqual({ lang: Locale.English, supported: SUPPORTED_LOCALES });
});
test('/healthcheck/language detects language as en if en header sent', async () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const res = await request(app).get('/healthcheck/language').set({ 'accept-language': Locale.English });
expect(res.status).toBe(200);
expect(res.body).toEqual({ lang: Locale.English, supported: SUPPORTED_LOCALES });
});
test('/healthcheck/language detects language as en-gb if en-gb header sent', async () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const res = await request(app).get('/healthcheck/language').set({ 'accept-language': Locale.EnglishGb });
expect(res.status).toBe(200);
expect(res.body).toEqual({ lang: Locale.EnglishGb, supported: SUPPORTED_LOCALES });
});
test('/healthcheck/language detects language as cy if cy header sent', async () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const res = await request(app).get('/healthcheck/language').set({ 'accept-language': Locale.Welsh });
expect(res.status).toBe(200);
expect(res.body).toEqual({ lang: Locale.Welsh, supported: SUPPORTED_LOCALES });
});
test('/healthcheck/language detects language as cy-gb if CY-GB header sent', async () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const res = await request(app).get('/healthcheck/language').set({ 'accept-language': Locale.WelshGb });
expect(res.status).toBe(200);
expect(res.body).toEqual({ lang: Locale.WelshGb, supported: SUPPORTED_LOCALES });
});
});
describe('Database pools', () => {
test('/healthcheck/db returns an array of pool stats', async () => {
const res = await request(app).get('/healthcheck/db');
expect(res.status).toBe(200);
expect(Array.isArray(res.body.pools)).toBe(true);
expect(res.body.pools).toHaveLength(3);
for (const pool of res.body.pools) {
expect(pool).toEqual(
expect.objectContaining({
name: expect.any(String),
connectionTimeout: expect.stringMatching(/^\d+ms$/),
idleTimeout: expect.stringMatching(/^\d+ms$/),
clients: expect.objectContaining({
min: expect.any(Number),
max: expect.any(Number),
idle: expect.any(Number),
waiting: expect.any(Number),
expired: expect.any(Number),
total: expect.any(Number),
isFull: expect.any(Boolean)
})
})
);
}
});
});
// Smoke test that /healthcheck/jwt is wired to JWT auth and returns 200 for a valid user. The full
// set of JWT strategy branches (missing / invalid / expired token, unknown user, changed permissions)
// lives in test/integration/routes/auth.test.ts alongside the other passport-auth tests.
describe('Authentication', () => {
test('/heathcheck/jwt returns 200 with a valid bearer token', async () => {
const testUser = getTestUser();
await testUser.save();
const res = await request(app).get('/healthcheck/jwt').set(getAuthHeader(testUser));
expect(res.status).toBe(200);
expect(res.body).toEqual({
message: 'success',
user: UserDTO.fromUser(testUser, Locale.English)
});
});
});
});