forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertAdminCaller.test.js
More file actions
134 lines (120 loc) · 4.33 KB
/
Copy pathassertAdminCaller.test.js
File metadata and controls
134 lines (120 loc) · 4.33 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
'use strict';
// Feature: admin-auth-privilege-escalation
const fc = require('fast-check');
const assertAdminCaller = require('../middleware/assertAdminCaller');
const AuthorizationError = require('../errors/AuthorizationError');
// ── Unit Tests ────────────────────────────────────────────────────────────────
describe('assertAdminCaller — unit tests', () => {
describe('throws AuthorizationError for non-admin roles', () => {
test.each([
['user'],
['merchant'],
[''],
[null],
[undefined],
])('throws for callerRole = %j', (role) => {
expect(() => assertAdminCaller(role)).toThrow(AuthorizationError);
});
test('thrown error has correct message', () => {
expect(() => assertAdminCaller('user')).toThrow(
'Caller does not have admin privileges'
);
});
test('thrown error has status 403', () => {
try {
assertAdminCaller('merchant');
} catch (err) {
expect(err.status).toBe(403);
}
});
test('thrown error has code "forbidden"', () => {
try {
assertAdminCaller('user');
} catch (err) {
expect(err.code).toBe('forbidden');
}
});
test('thrown error has name "AuthorizationError"', () => {
try {
assertAdminCaller('');
} catch (err) {
expect(err.name).toBe('AuthorizationError');
}
});
});
describe('does not throw for admin role', () => {
test('does not throw when callerRole is "admin"', () => {
expect(() => assertAdminCaller('admin')).not.toThrow();
});
test('returns undefined for "admin"', () => {
expect(assertAdminCaller('admin')).toBeUndefined();
});
});
});
// ── Property-Based Tests ──────────────────────────────────────────────────────
describe('assertAdminCaller — property tests', () => {
// Feature: admin-auth-privilege-escalation, Property 3: Service-layer check rejects any non-admin caller role
// Validates: Requirements 2.1, 2.2, 2.4
test('Property 3: throws AuthorizationError for any non-admin role string', () => {
fc.assert(
fc.property(
// Generate arbitrary strings that are not 'admin'
fc.string().filter((s) => s !== 'admin'),
(role) => {
expect(() => assertAdminCaller(role)).toThrow(AuthorizationError);
}
),
{ numRuns: 100 }
);
});
// Feature: admin-auth-privilege-escalation, Property 3: Service-layer check rejects any non-admin caller role
// Validates: Requirements 2.1, 2.2, 2.4
test('Property 3: throws AuthorizationError for known non-admin role values', () => {
fc.assert(
fc.property(
fc.constantFrom('user', 'merchant'),
(role) => {
expect(() => assertAdminCaller(role)).toThrow(AuthorizationError);
}
),
{ numRuns: 100 }
);
});
// Feature: admin-auth-privilege-escalation, Property 3: Service-layer check rejects any non-admin caller role
// Validates: Requirements 2.1, 2.2, 2.4
test('Property 3: thrown error always has correct message, status, and code for non-admin roles', () => {
fc.assert(
fc.property(
fc.string().filter((s) => s !== 'admin'),
(role) => {
try {
assertAdminCaller(role);
// Should never reach here
return false;
} catch (err) {
expect(err).toBeInstanceOf(AuthorizationError);
expect(err.message).toBe('Caller does not have admin privileges');
expect(err.status).toBe(403);
expect(err.code).toBe('forbidden');
return true;
}
}
),
{ numRuns: 100 }
);
});
// Feature: admin-auth-privilege-escalation, Property 3: Service-layer check rejects any non-admin caller role
// Validates: Requirements 2.1, 2.4
test('Property 3: does not throw for "admin" — verified across repeated calls', () => {
// Verify the positive case is stable
fc.assert(
fc.property(
fc.constant('admin'),
(role) => {
expect(() => assertAdminCaller(role)).not.toThrow();
}
),
{ numRuns: 100 }
);
});
});