diff --git a/src/components/CheckReadPermissions.tsx b/src/components/CheckReadPermissions.tsx index 5ca5d16d..3fe544df 100644 --- a/src/components/CheckReadPermissions.tsx +++ b/src/components/CheckReadPermissions.tsx @@ -22,6 +22,10 @@ export const CheckReadPermissions: React.FunctionComponent { + 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( + +
{childText}
+
, + { 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( + +
{childText}
+
, + { 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( + +
{childText}
+
, + { 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( + +
{childText}
+
, + { 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( + +
{childText}
+
, + { wrapper: Wrapper } + ); + + expect(screen.queryByText(childText)).not.toBeInTheDocument(); + expect(screen.getByRole('link', { name: /User Preferences/i })).toBeInTheDocument(); + }); +});