-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.quota.unit.tests.js
More file actions
197 lines (151 loc) · 6.3 KB
/
billing.quota.unit.tests.js
File metadata and controls
197 lines (151 loc) · 6.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
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
/**
* Module dependencies.
*/
import { jest, describe, test, beforeEach, afterEach, expect } from '@jest/globals';
/**
* Unit tests
*/
describe('requireQuota middleware:', () => {
let requireQuota;
let mockSubscriptionRepository;
let mockBillingUsageService;
let mockConfig;
let req;
let res;
let next;
beforeEach(async () => {
jest.resetModules();
mockSubscriptionRepository = {
findByOrganization: jest.fn(),
};
mockBillingUsageService = {
get: jest.fn(),
};
mockConfig = {
billing: {
quotas: {
free: { scraps: { create: 3, execute: 100 } },
starter: { scraps: { create: 20, execute: 2000 } },
pro: { scraps: { create: Infinity, execute: Infinity } },
},
},
};
jest.unstable_mockModule('../repositories/billing.subscription.repository.js', () => ({
default: mockSubscriptionRepository,
}));
jest.unstable_mockModule('../services/billing.usage.service.js', () => ({
default: mockBillingUsageService,
}));
jest.unstable_mockModule('../../../config/index.js', () => ({
default: mockConfig,
}));
const mod = await import('../middlewares/billing.requireQuota.js');
requireQuota = mod.default;
req = {
organization: { _id: '507f1f77bcf86cd799439011' },
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
next = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
});
test('should allow request when under quota', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'free', status: 'active' });
mockBillingUsageService.get.mockResolvedValue({ counters: { 'scraps_create': 1 } });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).toHaveBeenCalled();
expect(res.status).not.toHaveBeenCalled();
});
test('should return 429 when at quota limit', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'free', status: 'active' });
mockBillingUsageService.get.mockResolvedValue({ counters: { 'scraps_create': 3 } });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(429);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
type: 'error',
message: 'Quota exceeded',
code: 429,
status: 429,
}));
});
test('should return 429 when over quota limit', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'free', status: 'active' });
mockBillingUsageService.get.mockResolvedValue({ counters: { 'scraps_create': 5 } });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(429);
});
test('should treat missing subscription as free plan', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue(null);
mockBillingUsageService.get.mockResolvedValue({ counters: { 'scraps_create': 3 } });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(429);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
type: 'error',
code: 429,
}));
});
test.each(['past_due', 'canceled', 'unpaid', 'incomplete', 'incomplete_expired', 'paused'])(
'should treat %s subscription as free plan',
async (status) => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'starter', status });
mockBillingUsageService.get.mockResolvedValue({ counters: { 'scraps_create': 3 } });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(429);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
type: 'error',
code: 429,
}));
},
);
test('should allow unlimited (Infinity) without checking usage', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'pro', status: 'active' });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).toHaveBeenCalled();
expect(mockBillingUsageService.get).not.toHaveBeenCalled();
});
test('should return correct error payload with upgradeUrl', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'free', status: 'active' });
mockBillingUsageService.get.mockResolvedValue({ counters: { 'scraps_execute': 100 } });
await requireQuota('scraps', 'execute')(req, res, next);
expect(res.status).toHaveBeenCalledWith(429);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
type: 'error',
message: 'Quota exceeded',
code: 429,
status: 429,
description: 'You have reached the usage limit for this resource',
}));
});
test('should return 403 when organization context is missing', async () => {
req.organization = undefined;
await requireQuota('scraps', 'create')(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(403);
});
test('should allow through when no quota is configured for resource', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'free', status: 'active' });
await requireQuota('unknownResource', 'create')(req, res, next);
expect(next).toHaveBeenCalled();
});
test('should use subscription plan when status is trialing', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'starter', status: 'trialing' });
mockBillingUsageService.get.mockResolvedValue({ counters: { 'scraps_create': 15 } });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).toHaveBeenCalled();
expect(res.status).not.toHaveBeenCalled();
});
test('should treat zero usage as under quota', async () => {
mockSubscriptionRepository.findByOrganization.mockResolvedValue({ plan: 'free', status: 'active' });
mockBillingUsageService.get.mockResolvedValue({ counters: {} });
await requireQuota('scraps', 'create')(req, res, next);
expect(next).toHaveBeenCalled();
});
});