-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhome.integration.tests.js
More file actions
226 lines (208 loc) · 8.82 KB
/
Copy pathhome.integration.tests.js
File metadata and controls
226 lines (208 loc) · 8.82 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
/**
* Module dependencies.
*/
import { jest } from '@jest/globals';
import axios from 'axios';
import jwt from 'jsonwebtoken';
import mongoose from 'mongoose';
import path from 'path';
import request from 'supertest';
import { bootstrap } from '../../../lib/app.js';
import mongooseService from '../../../lib/services/mongoose.js';
import config from '../../../config/index.js';
/**
* Unit tests
*/
describe('Home integration tests:', () => {
let agent;
let HomeService;
let adminToken;
let adminUser;
let originalOrganizationsEnabled;
// init
beforeAll(async () => {
// Mock GitHub API calls to avoid real network requests in tests
jest.spyOn(axios, 'get').mockImplementation(async (url) => {
if (url.includes('/releases')) {
return { data: [{ name: 'v1.0.0', prerelease: false, published_at: '2024-01-01T00:00:00Z' }] };
}
if (url.includes('/contents/')) {
return { data: { content: Buffer.from('# Changelog\n## v1.0.0\n- First release').toString('base64') } };
}
throw new Error(`Unexpected GitHub API URL: ${url}`);
});
try {
originalOrganizationsEnabled = config.organizations.enabled;
config.organizations.enabled = false;
const init = await bootstrap();
HomeService = (await import(path.resolve('./modules/home/services/home.service.js'))).default;
agent = request.agent(init.app);
// Create admin user and sign JWT for health endpoint test
const User = mongoose.model('User');
await User.deleteOne({ email: 'admin-home-health@test.com' });
adminUser = await User.create({
firstName: 'Admin',
lastName: 'Health',
email: 'admin-home-health@test.com',
password: 'W@os.jsI$Aw3$0m3',
provider: 'local',
roles: ['admin'],
});
adminToken = jwt.sign({ userId: adminUser.id }, config.jwt.secret, { expiresIn: config.jwt.expiresIn });
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}
});
describe('Logout', () => {
test('should be able to get releases', async () => {
const result = await agent.get('/api/home/releases').expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('releases');
expect(result.body.data).toBeInstanceOf(Array);
});
test('should be able to get changelogs', async () => {
const result = await agent.get('/api/home/changelogs').expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('changelogs');
expect(result.body.data).toBeInstanceOf(Array);
});
test('should be able to get team members', async () => {
try {
const result = await agent.get('/api/home/team').expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('team list');
expect(result.body.data).toBeInstanceOf(Array);
} catch (err) {
expect(err).toBeFalsy();
console.log(err);
}
});
test('should be able to get an existing page', async () => {
try {
const result = await agent.get('/api/home/pages/terms').expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('page');
expect(result.body.data[0].title).toBe('Terms');
expect(typeof result.body.data[0].updatedAt).toBe('string');
expect(typeof result.body.data[0].markdown).toBe('string');
} catch (err) {
expect(err).toBeFalsy();
console.log(err);
}
});
test('should be able to catch error of unknown page', async () => {
try {
const result = await agent.get('/api/home/pages/test').expect(404);
expect(result.body.type).toBe('error');
expect(result.body.message).toBe('Not Found');
expect(result.body.description).toBe('No page with that name has been found');
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}
});
test('should return empty releases gracefully when GitHub API fails', async () => {
axios.get.mockRejectedValueOnce(new Error('GitHub API unavailable'));
const result = await agent.get('/api/home/releases').expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('releases');
expect(result.body.data).toEqual([]);
});
test('should return empty changelogs gracefully when GitHub API fails', async () => {
axios.get.mockRejectedValueOnce(new Error('GitHub API unavailable'));
const result = await agent.get('/api/home/changelogs').expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('changelogs');
expect(result.body.data).toEqual([]);
});
test('should use Authorization header when a token is configured for releases', async () => {
const originalRepos = config.repos;
axios.get.mockClear();
// Temporarily set a fake token to cover the token-truthy branch in home.service releases()
config.repos = originalRepos.map((repo) => ({ ...repo, token: 'fake-test-token' }));
const result = await agent.get('/api/home/releases').expect(200);
expect(result.body.type).toBe('success');
const releaseCalls = axios.get.mock.calls.filter(([url]) => url.includes('/releases'));
expect(releaseCalls.length).toBeGreaterThan(0);
releaseCalls.forEach(([, options]) => {
expect(options.headers.Authorization).toBe('token fake-test-token');
});
config.repos = originalRepos;
});
test('should use Authorization header when a token is configured for changelogs', async () => {
const originalRepos = config.repos;
axios.get.mockClear();
config.repos = originalRepos.map((repo) => ({ ...repo, token: 'fake-test-token' }));
const result = await agent.get('/api/home/changelogs').expect(200);
expect(result.body.type).toBe('success');
const changelogCalls = axios.get.mock.calls.filter(([url]) => url.includes('/contents/'));
expect(changelogCalls.length).toBeGreaterThan(0);
changelogCalls.forEach(([, options]) => {
expect(options.headers.Authorization).toBe('token fake-test-token');
});
config.repos = originalRepos;
});
test('should return minimal health status without auth', async () => {
const result = await agent.get('/api/health').expect(200);
expect(result.body.type).toBe('success');
expect(result.body.data.status).toBe('ok');
expect(result.body.data.db).toBeUndefined();
expect(result.body.data.memory).toBeUndefined();
});
test('should return detailed health status for admin', async () => {
const result = await agent.get('/api/health').set('Cookie', `TOKEN=${adminToken}`).expect(200);
expect(result.body.type).toBe('success');
expect(result.body.data.status).toBe('ok');
expect(result.body.data.db).toBe('connected');
expect(typeof result.body.data.uptime).toBe('number');
expect(result.body.data.memory).toBeDefined();
expect(result.body.data.memory.heapUsed).toBeDefined();
});
test('should return 503 when health status is degraded', async () => {
jest.spyOn(HomeService, 'getHealthStatus').mockReturnValueOnce({
status: 'degraded',
db: 'disconnected',
uptime: 0,
version: '0.0.0',
memory: process.memoryUsage(),
});
const result = await agent.get('/api/health').expect(503);
expect(result.body.type).toBe('error');
expect(result.body.message).toBe('Service Unavailable');
});
});
describe('Errors', () => {
test('should return 422 when team service fails', async () => {
jest.spyOn(HomeService, 'team').mockRejectedValueOnce(new Error('DB error'));
const result = await agent.get('/api/home/team').expect(422);
expect(result.body.type).toBe('error');
expect(result.body.message).toBe('Unprocessable Entity');
expect(result.body.description).toBe('DB error.');
});
test('should handle error in pageByName when page service fails', async () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(HomeService, 'page').mockRejectedValueOnce(new Error('DB error'));
const result = await agent.get('/api/home/pages/terms').expect(500);
expect(result.body.message).toBe('DB error');
consoleSpy.mockRestore();
});
});
// Cleanup and mongoose disconnect
afterAll(async () => {
jest.restoreAllMocks();
config.organizations.enabled = originalOrganizationsEnabled;
try {
if (adminUser) {
const User = mongoose.model('User');
await User.deleteOne({ _id: adminUser._id });
}
} catch (_) { /* cleanup – ignore errors */ }
try {
await mongooseService.disconnect();
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}
});
});