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)) {