Skip to content

Commit ca8b213

Browse files
committed
editController: fix
1 parent cd4d0da commit ca8b213

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

packages/ra-core/src/controller/edit/useEditController.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/TestMemoryRouter';
1212
import { EditControllerProps, useEditController } from './useEditController';
13+
import { useAuthState } from '../..';
1314

1415
export default {
1516
title: 'ra-core/controller/useEditController',
@@ -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">Edit</Link>
46+
</div>
47+
);
48+
};
49+
3950
const Post = (props: Partial<EditControllerProps>) => {
4051
const params = useEditController({
4152
id: 1,
@@ -51,6 +62,7 @@ const Post = (props: Partial<EditControllerProps>) => {
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
edit={<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/edit/useEditController.spec.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,5 +1295,39 @@ describe('useEditController', () => {
12951295
expect(dataProvider.getOne).toHaveBeenCalled();
12961296
expect(authProvider.checkAuth).not.toHaveBeenCalled();
12971297
});
1298+
1299+
it('should not call checkAuth nor canAccess when disableAuthentication is true', async () => {
1300+
const authProvider: AuthProvider = {
1301+
checkAuth: jest.fn().mockResolvedValue(true),
1302+
login: () => Promise.resolve(),
1303+
logout: () => Promise.resolve(),
1304+
checkError: () => Promise.resolve(),
1305+
getPermissions: () => Promise.resolve(),
1306+
canAccess: jest.fn().mockResolvedValue(false),
1307+
};
1308+
render(<DisableAuthentication authProvider={authProvider} />);
1309+
await screen.findByText('Post #1 - 90 votes');
1310+
expect(authProvider.checkAuth).not.toHaveBeenCalled();
1311+
expect(authProvider.canAccess).not.toHaveBeenCalled();
1312+
});
1313+
1314+
it('should not call checkAuth nor canAccess when disableAuthentication is true even if useAuthState was called before', async () => {
1315+
const authProvider: AuthProvider = {
1316+
checkAuth: jest.fn().mockResolvedValue(true),
1317+
login: () => Promise.resolve(),
1318+
logout: () => Promise.resolve(),
1319+
checkError: () => Promise.resolve(),
1320+
getPermissions: () => Promise.resolve(),
1321+
canAccess: jest.fn().mockResolvedValue(false),
1322+
};
1323+
render(<DisableAuthentication authProvider={authProvider} />);
1324+
await screen.findByText('Post #1 - 90 votes');
1325+
fireEvent.click(await screen.findByText('List'));
1326+
await screen.findByText('List view');
1327+
fireEvent.click(await screen.findByText('Edit'));
1328+
await screen.findByText('Post #1 - 90 votes');
1329+
expect(authProvider.checkAuth).toHaveBeenCalledTimes(1);
1330+
expect(authProvider.canAccess).not.toHaveBeenCalled();
1331+
});
12981332
});
12991333
});

packages/ra-core/src/controller/edit/useEditController.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ export const useEditController = <
7676
const { isPending: isPendingCanAccess } = useRequireAccess<RecordType>({
7777
action: 'edit',
7878
resource,
79-
// If disableAuthentication is true then isPendingAuthenticated will always be true so this hook is disabled
80-
enabled: !isPendingAuthenticated,
79+
enabled: !disableAuthentication && !isPendingAuthenticated,
8180
});
8281

8382
const getRecordRepresentation = useGetRecordRepresentation(resource);

0 commit comments

Comments
 (0)