Skip to content

Commit 8f2d3a2

Browse files
committed
Add a story to test access control in a sub path
1 parent b06972b commit 8f2d3a2

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

packages/react-admin/src/Admin.stories.tsx

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
} from 'ra-ui-materialui';
1111
import { Box, Typography, Button } from '@mui/material';
1212
import fakeRestDataProvider from 'ra-data-fakerest';
13-
import { useQueryClient } from '@tanstack/react-query';
13+
import { useQueryClient, QueryClient } from '@tanstack/react-query';
1414

15-
import { Admin } from './Admin';
15+
import { Admin, AdminProps } from './Admin';
1616

1717
export default {
1818
title: 'react-admin/Admin',
@@ -122,7 +122,32 @@ const dataProvider = fakeRestDataProvider({
122122
],
123123
});
124124

125-
export const AccessControl = () => {
125+
export const AccessControl = () => <AccessControlAdmin />;
126+
export const AccessControlInSubPath = () => (
127+
<TestMemoryRouter>
128+
<Routes>
129+
<Route
130+
path="/"
131+
element={
132+
<>
133+
<h1>Main</h1>
134+
<div>
135+
<Link to="/admin">Go to admin</Link>
136+
</div>
137+
</>
138+
}
139+
/>
140+
<Route
141+
path="/admin/*"
142+
element={
143+
<AccessControlAdmin AdminProps={{ basename: '/admin' }} />
144+
}
145+
/>
146+
</Routes>
147+
</TestMemoryRouter>
148+
);
149+
150+
const AccessControlAdmin = ({ AdminProps }: { AdminProps?: AdminProps }) => {
126151
const readerPermissions = [
127152
{ action: 'list', resource: 'books' },
128153
{ action: 'show', resource: 'books' },
@@ -148,6 +173,8 @@ export const AccessControl = () => {
148173
{ action: 'delete', resource: 'users' },
149174
];
150175
const [permissions, setPermissions] = React.useState(readerPermissions);
176+
const [triggerAccessControlError, setTriggerAccessControlError] =
177+
React.useState(false);
151178
const authProvider: AuthProvider = {
152179
// authentication
153180
async login() {},
@@ -160,6 +187,9 @@ export const AccessControl = () => {
160187
async handleCallback() {}, // for third-party authentication only
161188
// authorization (optional)
162189
async canAccess({ resource, action }) {
190+
if (triggerAccessControlError) {
191+
throw new Error('Access control error');
192+
}
163193
return permissions.some(
164194
p => p.resource === resource && p.action === action
165195
);
@@ -225,6 +255,18 @@ export const AccessControl = () => {
225255
>
226256
View as admin
227257
</Button>
258+
<Button
259+
variant="outlined"
260+
size="small"
261+
onClick={() => {
262+
setTriggerAccessControlError(prev => !prev);
263+
queryClient.invalidateQueries({
264+
queryKey: ['auth', 'canAccess'],
265+
});
266+
}}
267+
>
268+
Toggle Access Control Error
269+
</Button>
228270
</Box>
229271
<Layout>{children}</Layout>
230272
</div>
@@ -235,6 +277,14 @@ export const AccessControl = () => {
235277
dataProvider={dataProvider}
236278
authProvider={authProvider}
237279
layout={CustomLayout}
280+
queryClient={
281+
new QueryClient({
282+
defaultOptions: {
283+
queries: { retry: false },
284+
},
285+
})
286+
}
287+
{...AdminProps}
238288
>
239289
<Resource
240290
name="books"

0 commit comments

Comments
 (0)