mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-08-30 20:06:19 +00:00
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
This commit is contained in:
parent
c9cd4101dc
commit
e858eae87a
@ -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(
|
||||||
|
<PermissionProvider>
|
||||||
|
<div data-testid="children">Children</div>
|
||||||
|
</PermissionProvider>
|
||||||
|
);
|
||||||
|
|
||||||
|
// 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(
|
||||||
|
<PermissionProvider>
|
||||||
|
<div data-testid="children">Children</div>
|
||||||
|
</PermissionProvider>
|
||||||
|
);
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
});
|
||||||
|
});
|
@ -174,7 +174,14 @@ const PermissionProvider: FC<PermissionProviderProps> = ({ children }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isProtectedRoute(location.pathname)) {
|
/**
|
||||||
|
* Only fetch permissions if current user is present
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
isProtectedRoute(location.pathname) &&
|
||||||
|
!isUndefined(currentUser) &&
|
||||||
|
!isEmpty(currentUser)
|
||||||
|
) {
|
||||||
fetchLoggedInUserPermissions();
|
fetchLoggedInUserPermissions();
|
||||||
}
|
}
|
||||||
if (isUndefined(currentUser) || isEmpty(currentUser)) {
|
if (isUndefined(currentUser) || isEmpty(currentUser)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user