-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.js
More file actions
366 lines (300 loc) · 12 KB
/
Copy pathserver.test.js
File metadata and controls
366 lines (300 loc) · 12 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { describe, test, expect, beforeAll } from 'vitest';
import request from 'supertest';
describe('Server API', () => {
let app;
beforeAll(() => {
// Set environment variables before loading server
process.env.PORT = '3001';
process.env.FRONTEND_URL = 'http://localhost:3000';
process.env.BACKEND_URL = 'http://localhost:3001';
process.env.SPOTIFY_CLIENT_SECRET = 'test_spotify_secret';
// Load server
app = require('./server.js');
});
describe('Health Check', () => {
test('should return health status with correct structure', async () => {
const response = await request(app).get('/health');
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('status', 'ok');
expect(response.body).toHaveProperty('timestamp');
expect(response.body).toHaveProperty('uptime');
expect(response.body).toHaveProperty('youtube');
expect(response.body.youtube).toHaveProperty('configured');
expect(response.body.youtube).toHaveProperty('authenticated');
});
test('should return valid timestamp in ISO format', async () => {
const response = await request(app).get('/health');
expect(response.status).toBe(200);
expect(new Date(response.body.timestamp).toISOString()).toBe(response.body.timestamp);
});
test('should return positive uptime', async () => {
const response = await request(app).get('/health');
expect(response.status).toBe(200);
expect(response.body.uptime).toBeGreaterThan(0);
});
});
describe('Security Headers', () => {
test('should set Content-Security-Policy header', async () => {
const response = await request(app).get('/health');
expect(response.headers).toHaveProperty('content-security-policy');
expect(response.headers['content-security-policy']).toContain("default-src 'self'");
});
test('should set X-Frame-Options to DENY', async () => {
const response = await request(app).get('/health');
expect(response.headers).toHaveProperty('x-frame-options', 'DENY');
});
test('should set X-Content-Type-Options to nosniff', async () => {
const response = await request(app).get('/health');
expect(response.headers).toHaveProperty('x-content-type-options', 'nosniff');
});
test('should set X-XSS-Protection header', async () => {
const response = await request(app).get('/health');
expect(response.headers).toHaveProperty('x-xss-protection', '1; mode=block');
});
test('should set Referrer-Policy header', async () => {
const response = await request(app).get('/health');
expect(response.headers).toHaveProperty('referrer-policy', 'strict-origin-when-cross-origin');
});
});
describe('Spotify Token Exchange - Input Validation', () => {
test('should reject empty code parameter', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: '',
codeVerifier: 'valid_verifier',
redirectUri: 'http://localhost:3000/spotify',
clientId: 'test_client_id'
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid code parameter');
});
test('should reject code that is too long', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: 'x'.repeat(1001),
codeVerifier: 'valid_verifier',
redirectUri: 'http://localhost:3000/spotify',
clientId: 'test_client_id'
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid code parameter');
});
test('should reject non-string code', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: 123,
codeVerifier: 'valid_verifier',
redirectUri: 'http://localhost:3000/spotify',
clientId: 'test_client_id'
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid code parameter');
});
test('should reject empty codeVerifier', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: 'valid_code',
codeVerifier: '',
redirectUri: 'http://localhost:3000/spotify',
clientId: 'test_client_id'
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid codeVerifier parameter');
});
test('should reject codeVerifier that is too long', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: 'valid_code',
codeVerifier: 'x'.repeat(201),
redirectUri: 'http://localhost:3000/spotify',
clientId: 'test_client_id'
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid codeVerifier parameter');
});
test('should reject invalid redirectUri format', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: 'valid_code',
codeVerifier: 'valid_verifier',
redirectUri: 'not-a-url',
clientId: 'test_client_id'
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid redirectUri parameter');
});
test('should reject empty clientId', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: 'valid_code',
codeVerifier: 'valid_verifier',
redirectUri: 'http://localhost:3000/spotify',
clientId: ''
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid clientId parameter');
});
test('should reject clientId that is too long', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({
code: 'valid_code',
codeVerifier: 'valid_verifier',
redirectUri: 'http://localhost:3000/spotify',
clientId: 'x'.repeat(201)
});
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid clientId parameter');
});
test('should reject missing parameters', async () => {
const response = await request(app)
.post('/api/spotify/token')
.send({});
expect(response.status).toBe(400);
expect(response.body.error).toContain('Invalid');
});
});
describe('YouTube Config Status', () => {
test('should return configuration status', async () => {
const response = await request(app).get('/api/youtube/config/status');
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('configured');
expect(response.body).toHaveProperty('reason');
expect(typeof response.body.configured).toBe('boolean');
expect(typeof response.body.reason).toBe('string');
});
});
describe('YouTube Auth URL', () => {
test('should handle missing credentials gracefully', async () => {
const response = await request(app).get('/api/youtube/auth-url');
// Should either return an auth URL or an error
if (response.status === 200) {
expect(response.body).toHaveProperty('authUrl');
expect(response.body.authUrl).toContain('accounts.google.com');
} else {
expect(response.status).toBe(400);
expect(response.body).toHaveProperty('error');
}
});
});
describe('YouTube Token Status', () => {
test('should return token validity status', async () => {
const response = await request(app).get('/api/youtube/token/status');
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('valid');
expect(response.body).toHaveProperty('reason');
expect(typeof response.body.valid).toBe('boolean');
});
test('should include expiry when token exists', async () => {
const response = await request(app).get('/api/youtube/token/status');
expect(response.status).toBe(200);
// If valid is true, expiry should be present
if (response.body.valid) {
expect(response.body).toHaveProperty('expiry');
}
});
});
describe('YouTube Token Get', () => {
test('should return error or token', async () => {
const response = await request(app).get('/api/youtube/token');
// Should either return a token or an error
if (response.status === 200) {
expect(response.body).toHaveProperty('access_token');
expect(response.body).toHaveProperty('expires_in');
expect(response.body.expires_in).toBeGreaterThan(0);
} else {
expect([400, 401]).toContain(response.status);
expect(response.body).toHaveProperty('error');
}
});
});
describe('YouTube Callback - Input Validation', () => {
test('should reject missing code parameter', async () => {
const response = await request(app).get('/api/youtube/callback');
expect(response.status).toBe(400);
expect(response.text).toContain('Invalid');
});
test('should reject code that is too long', async () => {
const response = await request(app)
.get('/api/youtube/callback')
.query({ code: 'x'.repeat(1001) });
// May be rate limited (429) or rejected (400)
expect([400, 429]).toContain(response.status);
if (response.status === 400) {
expect(response.text).toContain('Invalid');
}
});
});
describe('YouTube Refresh Token', () => {
test('should return error when no token available', async () => {
const response = await request(app).post('/api/youtube/refresh');
// Without a valid token, should return an error (or rate limited)
expect([400, 401, 429]).toContain(response.status);
if (response.status !== 429) {
expect(response.body).toHaveProperty('error');
}
});
});
describe('Rate Limiting', () => {
test('should apply rate limiting to API routes', async () => {
// Make multiple requests quickly
const requests = [];
for (let i = 0; i < 5; i++) {
requests.push(request(app).get('/api/youtube/config/status'));
}
const responses = await Promise.all(requests);
// All should succeed within reasonable limits
responses.forEach(response => {
expect(response.status).toBe(200);
});
// Check that rate limit headers are present
const lastResponse = responses[responses.length - 1];
expect(lastResponse.headers).toHaveProperty('ratelimit-limit');
expect(lastResponse.headers).toHaveProperty('ratelimit-remaining');
});
});
describe('CORS Configuration', () => {
test('should include CORS headers', async () => {
const response = await request(app)
.get('/health')
.set('Origin', 'http://localhost:3000');
// CORS middleware should add appropriate headers
expect(response.headers).toHaveProperty('access-control-allow-origin');
});
});
describe('JSON Size Limit', () => {
test('should accept reasonably sized JSON payloads', async () => {
const smallPayload = {
code: 'test_code',
codeVerifier: 'test_verifier',
redirectUri: 'http://localhost:3000/spotify',
clientId: 'test_client'
};
const response = await request(app)
.post('/api/spotify/token')
.send(smallPayload);
// Should process the request (even if it fails validation)
expect(response.status).toBeLessThan(500);
});
});
describe('Error Handling', () => {
test('should return 404 for unknown routes', async () => {
const response = await request(app).get('/api/unknown/endpoint');
expect(response.status).toBe(404);
});
test('should handle malformed JSON', async () => {
const response = await request(app)
.post('/api/spotify/token')
.set('Content-Type', 'application/json')
.send('invalid json{');
expect(response.status).toBe(400);
});
});
});