Skip to content

Commit de5f9c9

Browse files
committed
feat(rbac): hide RBACPage behind read permission again
Signed-off-by: Patrick <[email protected]>
1 parent 6c12f2f commit de5f9c9

File tree

3 files changed

+74
-43
lines changed

3 files changed

+74
-43
lines changed

workspaces/rbac/plugins/rbac-backend/src/database/role-metadata.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ export class DataBaseRoleMetadataStorage implements RoleMetadataStorage {
7373
): Promise<RoleMetadataDao[]> {
7474
let filteredRoleMeta: RoleMetadataDao[];
7575

76-
const roleMetadata: RoleMetadataDao[] = await this.knex.table(
77-
ROLE_METADATA_TABLE,
78-
);
76+
const roleMetadata: RoleMetadataDao[] =
77+
await this.knex.table(ROLE_METADATA_TABLE);
7978

8079
if (filter) {
8180
filteredRoleMeta = roleMetadata.filter(role => {

workspaces/rbac/plugins/rbac/src/components/RbacPage.test.tsx

+45-22
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
*/
1616
import React from 'react';
1717

18-
import {
19-
RequirePermission,
20-
usePermission,
21-
} from '@backstage/plugin-permission-react';
22-
import { renderInTestApp } from '@backstage/test-utils';
18+
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
2319

2420
import { screen } from '@testing-library/react';
2521

2622
import { useCheckIfLicensePluginEnabled } from '../hooks/useCheckIfLicensePluginEnabled';
2723
import { useRoles } from '../hooks/useRoles';
2824
import { RbacPage } from './RbacPage';
2925

26+
import { RBACAPI, rbacApiRef } from '../api/RBACBackendClient';
27+
3028
jest.mock('@backstage/plugin-permission-react', () => ({
3129
usePermission: jest.fn(),
3230
RequirePermission: jest.fn(),
@@ -40,25 +38,33 @@ jest.mock('../hooks/useCheckIfLicensePluginEnabled', () => ({
4038
useCheckIfLicensePluginEnabled: jest.fn(),
4139
}));
4240

43-
const mockUsePermission = usePermission as jest.MockedFunction<
44-
typeof usePermission
45-
>;
46-
4741
const mockUseRoles = useRoles as jest.MockedFunction<typeof useRoles>;
4842

4943
const mockUseCheckIfLicensePluginEnabled =
5044
useCheckIfLicensePluginEnabled as jest.MockedFunction<
5145
typeof useCheckIfLicensePluginEnabled
5246
>;
5347

54-
const RequirePermissionMock = RequirePermission as jest.MockedFunction<
55-
typeof RequirePermission
56-
>;
48+
// Added
49+
let useAsyncMockResult: { loading: boolean; value?: { status: string } } = {
50+
loading: false,
51+
value: { status: 'Authorized' },
52+
};
53+
54+
const mockRbacApi: jest.Mocked<Partial<RBACAPI>> = {
55+
getUserAuthorization: jest.fn().mockImplementation(() => useAsyncMockResult),
56+
};
57+
58+
jest.mock('react-use', () => ({
59+
...jest.requireActual('react-use'),
60+
useAsync: jest.fn().mockImplementation((fn: any, _deps: any) => {
61+
fn();
62+
return useAsyncMockResult;
63+
}),
64+
}));
5765

5866
describe('RbacPage', () => {
5967
it('should render if authorized', async () => {
60-
RequirePermissionMock.mockImplementation(props => <>{props.children}</>);
61-
mockUsePermission.mockReturnValue({ loading: false, allowed: true });
6268
mockUseRoles.mockReturnValue({
6369
loading: true,
6470
data: [],
@@ -79,23 +85,40 @@ describe('RbacPage', () => {
7985
name: '',
8086
},
8187
});
82-
await renderInTestApp(<RbacPage />);
88+
89+
await renderInTestApp(
90+
<TestApiProvider apis={[[rbacApiRef, mockRbacApi]]}>
91+
<RbacPage />
92+
</TestApiProvider>,
93+
);
8394
expect(screen.getByText('RBAC')).toBeInTheDocument();
8495
});
8596

8697
it('should not render if not authorized', async () => {
87-
RequirePermissionMock.mockImplementation(_props => <>Not Found</>);
88-
mockUsePermission.mockReturnValue({ loading: false, allowed: false });
98+
useAsyncMockResult = {
99+
loading: false,
100+
value: { status: 'Unauthorized' },
101+
};
89102

90-
await renderInTestApp(<RbacPage />);
91-
expect(screen.getByText('Not Found')).toBeInTheDocument();
103+
await renderInTestApp(
104+
<TestApiProvider apis={[[rbacApiRef, mockRbacApi]]}>
105+
<RbacPage />
106+
</TestApiProvider>,
107+
);
108+
expect(screen.getByText('ERROR : Not Found')).toBeInTheDocument();
92109
});
93110

94111
it('should not render if loading', async () => {
95-
RequirePermissionMock.mockImplementation(_props => null);
96-
mockUsePermission.mockReturnValue({ loading: false, allowed: false });
112+
useAsyncMockResult = {
113+
loading: true,
114+
value: undefined,
115+
};
97116

98-
const { queryByText } = await renderInTestApp(<RbacPage />);
117+
const { queryByText } = await renderInTestApp(
118+
<TestApiProvider apis={[[rbacApiRef, mockRbacApi]]}>
119+
<RbacPage />
120+
</TestApiProvider>,
121+
);
99122
expect(queryByText('Not Found')).not.toBeInTheDocument();
100123
expect(queryByText('RBAC')).not.toBeInTheDocument();
101124
});

workspaces/rbac/plugins/rbac/src/components/RbacPage.tsx

+27-18
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,35 @@
1616
import React from 'react';
1717

1818
import { Content, Header, Page } from '@backstage/core-components';
19-
import { RequirePermission } from '@backstage/plugin-permission-react';
2019

2120
import { DeleteDialogContextProvider } from '@janus-idp/shared-react';
2221

23-
import { policyEntityCreatePermission } from '@backstage-community/plugin-rbac-common';
24-
2522
import { RolesList } from './RolesList/RolesList';
23+
import { useApi } from '@backstage/core-plugin-api';
24+
import { rbacApiRef } from '../api/RBACBackendClient';
25+
import { useAsync } from 'react-use';
26+
import { ErrorPage } from '@backstage/core-components';
27+
28+
export const RbacPage = ({ useHeader = true }: { useHeader?: boolean }) => {
29+
const rbacApi = useApi(rbacApiRef);
30+
const { loading: isUserLoading, value: result } = useAsync(
31+
async () => await rbacApi.getUserAuthorization(),
32+
[],
33+
);
2634

27-
export const RbacPage = ({ useHeader = true }: { useHeader?: boolean }) => (
28-
<RequirePermission
29-
permission={policyEntityCreatePermission}
30-
resourceRef={policyEntityCreatePermission.resourceType}
31-
>
32-
<Page themeId="tool">
33-
{useHeader && <Header title="RBAC" />}
34-
<Content>
35-
<DeleteDialogContextProvider>
36-
<RolesList />
37-
</DeleteDialogContextProvider>
38-
</Content>
39-
</Page>
40-
</RequirePermission>
41-
);
35+
if (!isUserLoading) {
36+
return result?.status === 'Authorized' ? (
37+
<Page themeId="tool">
38+
{useHeader && <Header title="RBAC" />}
39+
<Content>
40+
<DeleteDialogContextProvider>
41+
<RolesList />
42+
</DeleteDialogContextProvider>
43+
</Content>
44+
</Page>
45+
) : (
46+
<ErrorPage statusMessage="Not Found" />
47+
);
48+
}
49+
return null;
50+
};

0 commit comments

Comments
 (0)