forked from ritik4ever/stellar-goal-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.test.ts
More file actions
82 lines (62 loc) · 3 KB
/
Copy pathsecurity.test.ts
File metadata and controls
82 lines (62 loc) · 3 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
import request from 'supertest';
import { describe, it, expect } from 'vitest';
// Set environment before importing app
process.env.DB_PATH = ':memory:';
process.env.NODE_ENV = 'test';
process.env.CONTRACT_ID = 'CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
process.env.SOROBAN_RPC_URL = 'http://localhost:8000';
import { app } from './index';
describe('Security Headers (Helmet)', () => {
it('should set Content-Security-Policy header', async () => {
const response = await request(app).get('/api/health');
expect(response.headers['content-security-policy']).toBeDefined();
expect(response.headers['content-security-policy']).toContain("default-src 'none'");
});
it('should remove X-Powered-By header', async () => {
const response = await request(app).get('/api/health');
expect(response.headers['x-powered-by']).toBeUndefined();
});
it('should set Strict-Transport-Security header', async () => {
const response = await request(app).get('/api/health');
expect(response.headers['strict-transport-security']).toBeDefined();
});
it('should set X-Frame-Options header', async () => {
const response = await request(app).get('/api/health');
expect(response.headers['x-frame-options']).toBeDefined();
});
});
describe('Deep Health Check Endpoint', () => {
it('should return 200 with component status when healthy', async () => {
const response = await request(app).get('/api/health/deep');
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('overall');
expect(response.body).toHaveProperty('components');
expect(response.body.components).toHaveProperty('db');
expect(response.body.components).toHaveProperty('soroban');
expect(response.body.components).toHaveProperty('contract');
});
it('should include component status details', async () => {
const response = await request(app).get('/api/health/deep');
expect(response.body.components.db).toHaveProperty('status');
expect(response.body.components.db).toHaveProperty('details');
expect(['up', 'down']).toContain(response.body.components.db.status);
});
it('should mark contract as up when CONTRACT_ID is configured', async () => {
const response = await request(app).get('/api/health/deep');
expect(response.body.components.contract.status).toBe('up');
expect(response.body.components.contract.details).toContain('configured');
});
it('should include timestamp in response', async () => {
const response = await request(app).get('/api/health/deep');
expect(response.body).toHaveProperty('timestamp');
expect(new Date(response.body.timestamp)).toBeInstanceOf(Date);
});
it('should return 503 if any critical component is down', async () => {
// This test verifies the endpoint structure; actual component failures
// are tested through integration tests
const response = await request(app).get('/api/health/deep');
if (response.body.overall === 'down') {
expect(response.status).toBe(503);
}
});
});