Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/CheckReadPermissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export const CheckReadPermissions: React.FunctionComponent<React.PropsWithChildr
return rbac?.canReadEvents;
}

if (location.pathname === linkTo.notificationsLog()) {
return true;
}

return rbac?.canReadNotifications;
}

Expand Down
122 changes: 122 additions & 0 deletions src/components/__tests__/CheckReadPermissions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { render, screen } from '@testing-library/react';
import fetchMock from 'fetch-mock';
import * as React from 'react';

import {
appWrapperCleanup,
appWrapperSetup,
getConfiguredAppWrapper,
} from '../../../test/AppWrapper';
import { CheckReadPermissions } from '../CheckReadPermissions';

const mockGetApp = jest.fn();

jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => {
return () => ({
getApp: mockGetApp,
isBeta: () => false,
getEnvironment: () => 'ci',
});
});

const childText = 'Authorized Content';

describe('CheckReadPermissions', () => {
beforeEach(() => {
appWrapperSetup();
fetchMock.get(`/api/featureflags/v0`, {
body: { toggles: [] },
});
});

afterEach(() => {
appWrapperCleanup();
});

it('Shows children when user has canReadNotifications for notification pages', () => {
mockGetApp.mockReturnValue('notifications');
const Wrapper = getConfiguredAppWrapper({
router: { initialEntries: ['/rhel'] },
appContext: { rbac: { canReadNotifications: true } },
});

render(
<CheckReadPermissions>
<div>{childText}</div>
</CheckReadPermissions>,
{ wrapper: Wrapper }
);

expect(screen.getByText(childText)).toBeVisible();
});

it('Shows unauthorized when user lacks canReadNotifications for notification pages', () => {
mockGetApp.mockReturnValue('notifications');
const Wrapper = getConfiguredAppWrapper({
router: { initialEntries: ['/rhel'] },
appContext: { rbac: { canReadNotifications: false } },
});

render(
<CheckReadPermissions>
<div>{childText}</div>
</CheckReadPermissions>,
{ wrapper: Wrapper }
);

expect(screen.queryByText(childText)).not.toBeInTheDocument();
expect(screen.getByRole('link', { name: /User Preferences/i })).toBeInTheDocument();
});

it('Shows children for /notificationslog regardless of canReadNotifications', () => {
mockGetApp.mockReturnValue('notifications');
const Wrapper = getConfiguredAppWrapper({
router: { initialEntries: ['/notificationslog'] },
appContext: { rbac: { canReadNotifications: false } },
});

render(
<CheckReadPermissions>
<div>{childText}</div>
</CheckReadPermissions>,
{ wrapper: Wrapper }
);

expect(screen.getByText(childText)).toBeVisible();
});

it('Shows children for /eventlog when user has canReadEvents', () => {
mockGetApp.mockReturnValue('notifications');
const Wrapper = getConfiguredAppWrapper({
router: { initialEntries: ['/eventlog'] },
appContext: { rbac: { canReadEvents: true } },
});

render(
<CheckReadPermissions>
<div>{childText}</div>
</CheckReadPermissions>,
{ wrapper: Wrapper }
);

expect(screen.getByText(childText)).toBeVisible();
});

it('Shows unauthorized for /eventlog when user lacks canReadEvents', () => {
mockGetApp.mockReturnValue('notifications');
const Wrapper = getConfiguredAppWrapper({
router: { initialEntries: ['/eventlog'] },
appContext: { rbac: { canReadEvents: false } },
});

render(
<CheckReadPermissions>
<div>{childText}</div>
</CheckReadPermissions>,
{ wrapper: Wrapper }
);

expect(screen.queryByText(childText)).not.toBeInTheDocument();
expect(screen.getByRole('link', { name: /User Preferences/i })).toBeInTheDocument();
});
});
Loading