-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMePermissionsProvider.test.tsx
More file actions
195 lines (171 loc) · 6.43 KB
/
Copy pathMePermissionsProvider.test.tsx
File metadata and controls
195 lines (171 loc) · 6.43 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
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* Tests for MePermissionsProvider: ensures field-level permission
* checks against the `/me/permissions` payload are correctly enforced
* and that consumers receive sensible defaults during load / on error.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import { MePermissionsProvider } from '../MePermissionsProvider';
import { usePermissions } from '../usePermissions';
function Probe({ object, field }: { object: string; field: string }) {
const { checkField, isLoaded } = usePermissions();
return (
<div>
<span data-testid="loaded">{String(isLoaded)}</span>
<span data-testid="read">{String(checkField(object, field, 'read'))}</span>
<span data-testid="write">{String(checkField(object, field, 'write'))}</span>
</div>
);
}
describe('MePermissionsProvider', () => {
beforeEach(() => {
vi.spyOn(global, 'fetch' as any);
});
afterEach(() => {
vi.restoreAllMocks();
});
it('enforces explicit field-level read/write denials', async () => {
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({
authenticated: true,
userId: 'u1',
tenantId: 't1',
roles: ['member'],
permissionSets: ['restricted'],
objects: { '*': { allowRead: true, allowEdit: true } },
fields: {
'account.annual_revenue': { readable: false, editable: false },
'account.name': { readable: true, editable: false },
},
}),
});
render(
<MePermissionsProvider endpoint="/x">
<Probe object="account" field="annual_revenue" />
</MePermissionsProvider>,
);
await waitFor(() => expect(screen.getByTestId('loaded').textContent).toBe('true'));
expect(screen.getByTestId('read').textContent).toBe('false');
expect(screen.getByTestId('write').textContent).toBe('false');
});
it('returns object-level fallback when no field override exists', async () => {
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({
authenticated: true,
userId: 'u1',
tenantId: 't1',
roles: ['viewer'],
permissionSets: ['viewer_readonly'],
objects: { '*': { allowRead: true, allowEdit: false } },
fields: {},
}),
});
render(
<MePermissionsProvider endpoint="/x">
<Probe object="account" field="name" />
</MePermissionsProvider>,
);
await waitFor(() => expect(screen.getByTestId('loaded').textContent).toBe('true'));
expect(screen.getByTestId('read').textContent).toBe('true');
expect(screen.getByTestId('write').textContent).toBe('false');
});
it('renders loadingFallback while fetching and is fail-closed', async () => {
(global.fetch as any).mockReturnValue(new Promise(() => { /* pending */ }));
render(
<MePermissionsProvider endpoint="/x" loadingFallback={<div data-testid="loading">…</div>}>
<Probe object="account" field="name" />
</MePermissionsProvider>,
);
expect(screen.getByTestId('loading')).toBeTruthy();
expect(screen.queryByTestId('read')).toBeNull();
});
// [#2926 ④] Unknown-object default is authentication-gated.
it('fails CLOSED for an authenticated user when the object has no configured perms', async () => {
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({
authenticated: true,
userId: 'u1',
tenantId: 't1',
roles: ['member'],
permissionSets: ['restricted'],
objects: { account: { allowRead: true, allowEdit: true } }, // no '*', nothing for 'project'
fields: {},
}),
});
render(
<MePermissionsProvider endpoint="/x">
<Probe object="project" field="budget" />
</MePermissionsProvider>,
);
await waitFor(() => expect(screen.getByTestId('loaded').textContent).toBe('true'));
expect(screen.getByTestId('read').textContent).toBe('false');
expect(screen.getByTestId('write').textContent).toBe('false');
});
it('keeps the permissive default for anonymous sessions (guest/public surfaces)', async () => {
// The endpoint's no-session response: authenticated:false, NO objects/fields.
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({ authenticated: false }),
});
render(
<MePermissionsProvider endpoint="/x">
<Probe object="showcase_inquiry" field="message" />
</MePermissionsProvider>,
);
await waitFor(() => expect(screen.getByTestId('loaded').textContent).toBe('true'));
// Server still enforces; the anon UI must not brick public forms.
expect(screen.getByTestId('read').textContent).toBe('true');
expect(screen.getByTestId('write').textContent).toBe('true');
});
it('uses the injected fetcher (authenticated fetch) instead of global fetch', async () => {
const fetcher = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
authenticated: true,
userId: 'u1',
tenantId: 't1',
roles: [],
permissionSets: [],
objects: { '*': { allowRead: true, allowEdit: true } },
fields: {},
}),
});
render(
<MePermissionsProvider endpoint="/perm-endpoint" fetcher={fetcher as any}>
<Probe object="account" field="name" />
</MePermissionsProvider>,
);
await waitFor(() => expect(screen.getByTestId('loaded').textContent).toBe('true'));
expect(fetcher).toHaveBeenCalledWith('/perm-endpoint', expect.objectContaining({ credentials: 'include' }));
expect(global.fetch).not.toHaveBeenCalled();
expect(screen.getByTestId('write').textContent).toBe('true');
});
it('skips fetch when initialPermissions provided', () => {
render(
<MePermissionsProvider
endpoint="/x"
initialPermissions={{
authenticated: true,
userId: 'u',
tenantId: 't',
roles: [],
permissionSets: [],
objects: {},
fields: { 'account.secret': { readable: false } },
}}
>
<Probe object="account" field="secret" />
</MePermissionsProvider>,
);
expect(global.fetch).not.toHaveBeenCalled();
expect(screen.getByTestId('loaded').textContent).toBe('true');
expect(screen.getByTestId('read').textContent).toBe('false');
});
});