-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.middleware.integration.tests.js
More file actions
162 lines (134 loc) · 4.27 KB
/
Copy pathanalytics.middleware.integration.tests.js
File metadata and controls
162 lines (134 loc) · 4.27 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
/**
* Module dependencies.
*/
import { jest, beforeAll, afterAll, beforeEach, describe, test, expect } from '@jest/globals';
import express from 'express';
import request from 'supertest';
/**
* Integration tests for analytics auto-capture middleware.
* Boots a minimal Express app with the middleware mounted to validate
* real HTTP request/response lifecycle behaviour.
*/
describe('Analytics middleware integration tests:', () => {
let app;
let mockTrack;
beforeAll(async () => {
jest.resetModules();
mockTrack = jest.fn();
jest.unstable_mockModule('../services/analytics.service.js', () => ({
default: {
track: mockTrack,
},
}));
const { createAnalyticsMiddleware } = await import('../middlewares/analytics.middleware.js');
app = express();
app.use(createAnalyticsMiddleware());
// Simulate auth middleware populating req.user / req.organization
app.use((req, _res, next) => {
if (req.headers['x-test-user']) {
req.user = { _id: req.headers['x-test-user'] };
}
if (req.headers['x-test-org']) {
req.organization = { _id: req.headers['x-test-org'] };
}
next();
});
app.get('/api/tasks', (_req, res) => res.json({ ok: true }));
app.get('/api/health', (_req, res) => res.json({ status: 'ok' }));
app.get('/public/logo.png', (_req, res) => res.send('img'));
app.get('/favicon.ico', (_req, res) => res.send('ico'));
app.get('/api/error', (_req, res) => res.status(500).json({ error: true }));
});
beforeEach(() => {
mockTrack.mockClear();
});
afterAll(() => {
jest.restoreAllMocks();
});
test('should track a normal API request with user and org', async () => {
await request(app)
.get('/api/tasks')
.set('x-test-user', 'user-abc')
.set('x-test-org', 'org-xyz')
.expect(200);
expect(mockTrack).toHaveBeenCalledTimes(1);
expect(mockTrack).toHaveBeenCalledWith(
'user-abc',
'api_request',
expect.objectContaining({
endpoint: '/api/tasks',
method: 'GET',
statusCode: 200,
responseTime: expect.any(Number),
}),
{ company: 'org-xyz' },
);
});
test('should use "anonymous" when no user header is set', async () => {
await request(app)
.get('/api/tasks')
.expect(200);
expect(mockTrack).toHaveBeenCalledTimes(1);
expect(mockTrack).toHaveBeenCalledWith(
'anonymous',
'api_request',
expect.any(Object),
undefined,
);
});
test('should omit groups when no org header is set', async () => {
await request(app)
.get('/api/tasks')
.set('x-test-user', 'user-abc')
.expect(200);
expect(mockTrack).toHaveBeenCalledTimes(1);
const groups = mockTrack.mock.calls[0][3];
expect(groups).toBeUndefined();
});
test('should not track /api/health requests', async () => {
await request(app)
.get('/api/health')
.expect(200);
expect(mockTrack).not.toHaveBeenCalled();
});
test('should not track /public requests', async () => {
await request(app)
.get('/public/logo.png')
.expect(200);
expect(mockTrack).not.toHaveBeenCalled();
});
test('should not track /favicon requests', async () => {
await request(app)
.get('/favicon.ico')
.expect(200);
expect(mockTrack).not.toHaveBeenCalled();
});
test('should capture real status code for error responses', async () => {
await request(app)
.get('/api/error')
.set('x-test-user', 'user-abc')
.expect(500);
expect(mockTrack).toHaveBeenCalledTimes(1);
expect(mockTrack).toHaveBeenCalledWith(
'user-abc',
'api_request',
expect.objectContaining({ statusCode: 500 }),
undefined,
);
});
test('should capture positive responseTime', async () => {
await request(app)
.get('/api/tasks')
.expect(200);
const properties = mockTrack.mock.calls[0][2];
expect(properties.responseTime).toBeGreaterThanOrEqual(0);
});
test('should strip query strings from endpoint', async () => {
await request(app)
.get('/api/tasks?page=1&token=secret')
.expect(200);
expect(mockTrack).toHaveBeenCalledTimes(1);
const properties = mockTrack.mock.calls[0][2];
expect(properties.endpoint).toBe('/api/tasks');
});
});