Skip to content

Commit cc3c3bc

Browse files
committed
showController: fix
1 parent ca8b213 commit cc3c3bc

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

packages/ra-core/src/controller/show/useShowController.security.stories.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Resource } from '../../core/Resource';
1010
import { AuthProvider, DataProvider } from '../../types';
1111
import { TestMemoryRouter } from '../../routing';
1212
import { ShowControllerProps, useShowController } from './useShowController';
13+
import { useAuthState } from '../..';
1314

1415
export default {
1516
title: 'ra-core/controller/useShowController',
@@ -36,6 +37,16 @@ const defaultDataProvider = fakeDataProvider(
3637
process.env.NODE_ENV === 'development'
3738
);
3839

40+
const PostList = () => {
41+
useAuthState();
42+
return (
43+
<div style={styles.mainContainer}>
44+
<div>List view</div>
45+
<Link to="/posts/1/show">Show</Link>
46+
</div>
47+
);
48+
};
49+
3950
const Post = (props: Partial<ShowControllerProps>) => {
4051
const params = useShowController({
4152
id: 1,
@@ -51,6 +62,7 @@ const Post = (props: Partial<ShowControllerProps>) => {
5162
{params.record.title} - {params.record.votes} votes
5263
</div>
5364
)}
65+
<Link to="/posts">List</Link>
5466
</div>
5567
);
5668
};
@@ -97,16 +109,33 @@ export const DisableAuthentication = ({
97109
dataProvider={dataProvider}
98110
authProvider={authProvider}
99111
>
100-
<CoreAdminUI>
112+
<CoreAdminUI accessDenied={AccessDenied}>
101113
<Resource
102114
name="posts"
115+
list={<PostList />}
103116
show={<Post disableAuthentication />}
104117
/>
105118
</CoreAdminUI>
106119
</CoreAdminContext>
107120
</TestMemoryRouter>
108121
);
109122
};
123+
DisableAuthentication.args = {
124+
authProvider: undefined,
125+
};
126+
DisableAuthentication.argTypes = {
127+
authProvider: {
128+
options: ['default', 'canAccess'],
129+
mapping: {
130+
default: undefined,
131+
canAccess: {
132+
...defaultAuthProvider,
133+
canAccess: () => Promise.resolve(false),
134+
},
135+
},
136+
control: { type: 'inline-radio' },
137+
},
138+
};
110139

111140
export const CanAccess = ({
112141
authProviderDelay = 300,

packages/ra-core/src/controller/show/useShowController.spec.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,39 @@ describe('useShowController', () => {
254254
expect(dataProvider.getOne).toHaveBeenCalled();
255255
expect(authProvider.checkAuth).not.toHaveBeenCalled();
256256
});
257+
258+
it('should not call checkAuth nor canAccess when disableAuthentication is true', async () => {
259+
const authProvider: AuthProvider = {
260+
checkAuth: jest.fn().mockResolvedValue(true),
261+
login: () => Promise.resolve(),
262+
logout: () => Promise.resolve(),
263+
checkError: () => Promise.resolve(),
264+
getPermissions: () => Promise.resolve(),
265+
canAccess: jest.fn().mockResolvedValue(false),
266+
};
267+
render(<DisableAuthentication authProvider={authProvider} />);
268+
await screen.findByText('Post #1 - 90 votes');
269+
expect(authProvider.checkAuth).not.toHaveBeenCalled();
270+
expect(authProvider.canAccess).not.toHaveBeenCalled();
271+
});
272+
273+
it('should not call checkAuth nor canAccess when disableAuthentication is true even if useAuthState was called before', async () => {
274+
const authProvider: AuthProvider = {
275+
checkAuth: jest.fn().mockResolvedValue(true),
276+
login: () => Promise.resolve(),
277+
logout: () => Promise.resolve(),
278+
checkError: () => Promise.resolve(),
279+
getPermissions: () => Promise.resolve(),
280+
canAccess: jest.fn().mockResolvedValue(false),
281+
};
282+
render(<DisableAuthentication authProvider={authProvider} />);
283+
await screen.findByText('Post #1 - 90 votes');
284+
fireEvent.click(await screen.findByText('List'));
285+
await screen.findByText('List view');
286+
fireEvent.click(await screen.findByText('Show'));
287+
await screen.findByText('Post #1 - 90 votes');
288+
expect(authProvider.checkAuth).toHaveBeenCalledTimes(1);
289+
expect(authProvider.canAccess).not.toHaveBeenCalled();
290+
});
257291
});
258292
});

packages/ra-core/src/controller/show/useShowController.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ export const useShowController = <
7474
const { isPending: isPendingCanAccess } = useRequireAccess<RecordType>({
7575
action: 'show',
7676
resource,
77-
// If disableAuthentication is true then isPendingAuthenticated will always be true so this hook is disabled
78-
enabled: !isPendingAuthenticated,
77+
enabled: !disableAuthentication && !isPendingAuthenticated,
7978
});
8079

8180
const getRecordRepresentation = useGetRecordRepresentation(resource);

0 commit comments

Comments
 (0)