-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.requireFeatureFlag.unit.tests.js
More file actions
123 lines (96 loc) · 3.34 KB
/
Copy pathanalytics.requireFeatureFlag.unit.tests.js
File metadata and controls
123 lines (96 loc) · 3.34 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
/**
* Module dependencies.
*/
import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals';
/**
* Unit tests for requireFeatureFlag middleware
*/
describe('requireFeatureFlag middleware unit tests:', () => {
let requireFeatureFlag;
let mockFeatureFlagsService;
let req;
let res;
let next;
beforeEach(async () => {
jest.resetModules();
mockFeatureFlagsService = {
isEnabled: jest.fn(),
getVariant: jest.fn(),
};
jest.unstable_mockModule('../services/analytics.featureFlags.service.js', () => ({
default: mockFeatureFlagsService,
}));
const mod = await import('../middlewares/analytics.requireFeatureFlag.js');
requireFeatureFlag = mod.default;
req = {
user: { _id: 'user-123' },
organization: { _id: 'org-456' },
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
next = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
});
test('should call next when flag is enabled', async () => {
mockFeatureFlagsService.isEnabled.mockResolvedValue(true);
const middleware = requireFeatureFlag('beta-feature');
await middleware(req, res, next);
expect(next).toHaveBeenCalledWith();
expect(mockFeatureFlagsService.isEnabled).toHaveBeenCalledWith(
'beta-feature',
'user-123',
{ groups: { company: 'org-456' } },
);
});
test('should return 403 when flag is disabled', async () => {
mockFeatureFlagsService.isEnabled.mockResolvedValue(false);
mockFeatureFlagsService.getVariant.mockResolvedValue(false);
const middleware = requireFeatureFlag('beta-feature');
await middleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(403);
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({
type: 'error',
message: 'Forbidden',
}),
);
});
test('should call next when analytics is not configured (fail-open)', async () => {
mockFeatureFlagsService.isEnabled.mockResolvedValue(false);
mockFeatureFlagsService.getVariant.mockResolvedValue(undefined);
const middleware = requireFeatureFlag('beta-feature');
await middleware(req, res, next);
expect(next).toHaveBeenCalledWith();
});
test('should return 401 when user is not authenticated', async () => {
req.user = undefined;
const middleware = requireFeatureFlag('beta-feature');
await middleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(401);
});
test('should not include groups when organization is absent', async () => {
req.organization = undefined;
mockFeatureFlagsService.isEnabled.mockResolvedValue(true);
const middleware = requireFeatureFlag('beta-feature');
await middleware(req, res, next);
expect(mockFeatureFlagsService.isEnabled).toHaveBeenCalledWith(
'beta-feature',
'user-123',
{},
);
expect(next).toHaveBeenCalledWith();
});
test('should call next(err) on unexpected errors', async () => {
const error = new Error('PostHog timeout');
mockFeatureFlagsService.isEnabled.mockRejectedValue(error);
const middleware = requireFeatureFlag('beta-feature');
await middleware(req, res, next);
expect(next).toHaveBeenCalledWith(error);
});
});