diff --git a/src/components/layout/SideNav/hooks/__tests__/useGetSideNavItems.test.ts b/src/components/layout/SideNav/hooks/__tests__/useGetSideNavItems.test.ts index 45839a4b2..407d93905 100644 --- a/src/components/layout/SideNav/hooks/__tests__/useGetSideNavItems.test.ts +++ b/src/components/layout/SideNav/hooks/__tests__/useGetSideNavItems.test.ts @@ -305,4 +305,38 @@ describe('useGetSideNavItems', () => { expect(result.current).not.toContain('TENANT_CERTIFIER') }) + + it("should not include 'DELEGATIONS' routes if the user is not a PA", () => { + mockUseJwt({ + jwt: { + externalId: { + origin: 'test', + value: 'value', + }, + }, + currentRoles: ['admin'], + }) + + const { result } = renderHook(() => useGetSideNavItems()) + + expect(result.current.some((routeKey) => routeKey.children?.includes('DELEGATIONS'))).toBe( + false + ) + }) + + it("should include 'DELEGATIONS' routes if the user is a PA", () => { + mockUseJwt({ + jwt: { + externalId: { + origin: 'IPA', + value: 'value', + }, + }, + currentRoles: ['admin'], + }) + + const { result } = renderHook(() => useGetSideNavItems()) + + expect(result.current.some((routeKey) => routeKey.children?.includes('DELEGATIONS'))).toBe(true) + }) }) diff --git a/src/router/components/RoutesWrapper/__tests__/AuthGuard.test.tsx b/src/router/components/RoutesWrapper/__tests__/AuthGuard.test.tsx index c0f0f7e92..bda6205cc 100644 --- a/src/router/components/RoutesWrapper/__tests__/AuthGuard.test.tsx +++ b/src/router/components/RoutesWrapper/__tests__/AuthGuard.test.tsx @@ -128,51 +128,57 @@ describe('AuthGuard', () => { expect(getByText('children')).toBeInTheDocument() }) - it('Should able to access when user try to access on delegations routes and he is a PA', () => { - const props: AuthGuardTestProps = { - ...defaultProps, - isSupport: false, + it.each(['DELEGATIONS', 'DELEGATION_DETAILS', 'CREATE_DELEGATION'] as const)( + 'Should able to access when user try to access on %s route and he is a PA', + (routeKey) => { + const props: AuthGuardTestProps = { + ...defaultProps, + isSupport: false, + } + + useAuthGuardSpy.mockReturnValue({ + isPublic: false, + authLevels: [], + isUserAuthorized: () => true, + }) + mockUseCurrentRoute({ + routeKey, + }) + mockUseGetActiveUserParty({ + data: { + externalId: { origin: 'IPA' }, + }, + }) + + const { getByText } = renderAuthGuard(props) + expect(getByText('children')).toBeInTheDocument() } + ) - useAuthGuardSpy.mockReturnValue({ - isPublic: false, - authLevels: [], - isUserAuthorized: () => true, - }) - mockUseCurrentRoute({ - routeKey: 'DELEGATIONS' || 'DELEGATION_DETAILS' || 'CREATE_DELEGATION', - }) - mockUseGetActiveUserParty({ - data: { - externalId: { origin: 'IPA' }, - }, - }) - - const { getByText } = renderAuthGuard(props) - expect(getByText('children')).toBeInTheDocument() - }) - - it('Should render Error component when user try to access on delegations routes and he is not a PA', () => { - const props: AuthGuardTestProps = { - ...defaultProps, - isSupport: false, + it.each(['DELEGATIONS', 'DELEGATION_DETAILS', 'CREATE_DELEGATION'] as const)( + 'Should render Error component when user try to access on delegations routes and he is not a PA', + (routeKey) => { + const props: AuthGuardTestProps = { + ...defaultProps, + isSupport: false, + } + + useAuthGuardSpy.mockReturnValue({ + isPublic: false, + authLevels: [], + isUserAuthorized: () => false, + }) + mockUseCurrentRoute({ + routeKey, + }) + mockUseGetActiveUserParty({ + data: { + externalId: { origin: '' }, + }, + }) + + const { getByText } = renderAuthGuard(props) + expect(getByText('error')).toBeInTheDocument() } - - useAuthGuardSpy.mockReturnValue({ - isPublic: false, - authLevels: [], - isUserAuthorized: () => false, - }) - mockUseCurrentRoute({ - routeKey: 'DELEGATIONS' || 'DELEGATION_DETAILS' || 'CREATE_DELEGATION', - }) - mockUseGetActiveUserParty({ - data: { - externalId: { origin: '' }, - }, - }) - - const { getByText } = renderAuthGuard(props) - expect(getByText('error')).toBeInTheDocument() - }) + ) })