From e858eae87a16b62dbcc1ba70b5945557342162a9 Mon Sep 17 00:00:00 2001 From: Sachin Chaurasiya Date: Fri, 9 Jun 2023 13:35:07 +0530 Subject: [PATCH] chore(ui): only fetch permission when logged in user data is available (#11920) * chore(ui): only fetch permission when logged in user data is available * add unit test --- .../PermissionProvider.test.tsx | 83 +++++++++++++++++++ .../PermissionProvider/PermissionProvider.tsx | 9 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.test.tsx diff --git a/openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.test.tsx b/openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.test.tsx new file mode 100644 index 00000000000..1ed021c31fe --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.test.tsx @@ -0,0 +1,83 @@ +/* + * Copyright 2023 Collate. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { render, screen } from '@testing-library/react'; +import AppState from 'AppState'; +import { User } from 'generated/entity/teams/user'; +import React from 'react'; +import { + getEntityPermissionByFqn, + getEntityPermissionById, + getLoggedInUserPermissions, + getResourcePermission, +} from 'rest/permissionAPI'; +import PermissionProvider from './PermissionProvider'; + +jest.mock('rest/permissionAPI', () => ({ + getLoggedInUserPermissions: jest + .fn() + .mockImplementation(() => Promise.resolve({ data: [] })), + getEntityPermissionById: jest + .fn() + .mockImplementation(() => Promise.resolve({})), + getEntityPermissionByFqn: jest + .fn() + .mockImplementation(() => Promise.resolve({})), + getResourcePermission: jest + .fn() + .mockImplementation(() => Promise.resolve({})), +})); + +describe('PermissionProvider', () => { + it('Should render children and call apis when current user is present', async () => { + const currentUser = { id: '123', name: 'Test User' }; + const getUserDetailsSpy = jest + .spyOn(AppState, 'getCurrentUserDetails') + .mockReturnValue(currentUser as User); + + render( + +
Children
+
+ ); + + // Verify that the API methods were called + expect(getLoggedInUserPermissions).toHaveBeenCalled(); + expect(getEntityPermissionById).not.toHaveBeenCalled(); + expect(getEntityPermissionByFqn).not.toHaveBeenCalled(); + expect(getResourcePermission).not.toHaveBeenCalled(); + + expect(screen.getByTestId('children')).toBeInTheDocument(); + + getUserDetailsSpy.mockRestore(); + }); + + it('Should not call apis when current user is undefined', async () => { + const getUserDetailsSpy = jest + .spyOn(AppState, 'getCurrentUserDetails') + .mockReturnValue(undefined); + + render( + +
Children
+
+ ); + + // Verify that the API methods were not called + expect(getLoggedInUserPermissions).not.toHaveBeenCalled(); + expect(getEntityPermissionById).not.toHaveBeenCalled(); + expect(getEntityPermissionByFqn).not.toHaveBeenCalled(); + expect(getResourcePermission).not.toHaveBeenCalled(); + + getUserDetailsSpy.mockRestore(); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.tsx b/openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.tsx index 92d71e53310..c0efb17b391 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/PermissionProvider/PermissionProvider.tsx @@ -174,7 +174,14 @@ const PermissionProvider: FC = ({ children }) => { }; useEffect(() => { - if (isProtectedRoute(location.pathname)) { + /** + * Only fetch permissions if current user is present + */ + if ( + isProtectedRoute(location.pathname) && + !isUndefined(currentUser) && + !isEmpty(currentUser) + ) { fetchLoggedInUserPermissions(); } if (isUndefined(currentUser) || isEmpty(currentUser)) {