Skip to content

Commit

Permalink
feat: allow only admin users to see registration instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
jorilindell committed May 16, 2024
1 parent 4b976bb commit 1c26277
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 69 deletions.
6 changes: 3 additions & 3 deletions src/domain/admin/layout/AdminPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ROUTES } from '../../../constants';
import { featureFlagUtils } from '../../../utils/featureFlags';
import LayoutWithSideNavigation from '../../app/layout/layoutWithSideNavigation/LayoutWithSideNavigation';
import useUser from '../../user/hooks/useUser';
import { arePriceGroupRoutesAllowed } from '../../user/permissions';
import { areFinancialRoutesAllowed } from '../../user/permissions';

const AdminPageLayout: React.FC<PropsWithChildren> = ({ children }) => {
const { t } = useTranslation();
Expand All @@ -22,13 +22,13 @@ const AdminPageLayout: React.FC<PropsWithChildren> = ({ children }) => {
{ label: t('keywordsPage.title'), to: ROUTES.KEYWORDS },
{ label: t('keywordSetsPage.title'), to: ROUTES.KEYWORD_SETS },
{ label: t('imagesPage.title'), to: ROUTES.IMAGES },
...(arePriceGroupRoutesAllowed(user)
...(areFinancialRoutesAllowed(user)
? [{ label: t('organizationsPage.title'), to: ROUTES.ORGANIZATIONS }]
: []),
...(featureFlagUtils.isFeatureEnabled('SHOW_PLACE_PAGES')
? [{ label: t('placesPage.title'), to: ROUTES.PLACES }]
: []),
...(arePriceGroupRoutesAllowed(user)
...(areFinancialRoutesAllowed(user)
? [{ label: t('priceGroupsPage.title'), to: ROUTES.PRICE_GROUPS }]
: []),
];
Expand Down
32 changes: 18 additions & 14 deletions src/domain/app/routes/adminRoutes/AdminRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import NotFoundPage from '../../../notFound/NotFound';
import useUser from '../../../user/hooks/useUser';
import {
areAdminRoutesAllowed,
arePriceGroupRoutesAllowed,
areFinancialRoutesAllowed,
} from '../../../user/permissions';

const CreateImagePage = React.lazy(
Expand Down Expand Up @@ -99,18 +99,22 @@ const AdminPageRoutes: React.FC = () => {
element={<KeywordSetsPage />}
/>

<Route
path={getAdminRoutePath(ROUTES.CREATE_ORGANIZATION)}
element={<CreateOrganizationPage />}
/>
<Route
path={getAdminRoutePath(ROUTES.EDIT_ORGANIZATION)}
element={<EditOrganizationPage />}
/>
<Route
path={getAdminRoutePath(ROUTES.ORGANIZATIONS)}
element={<OrganizationsPage />}
/>
{areFinancialRoutesAllowed(user) && (
<>
<Route
path={getAdminRoutePath(ROUTES.CREATE_ORGANIZATION)}
element={<CreateOrganizationPage />}
/>
<Route
path={getAdminRoutePath(ROUTES.EDIT_ORGANIZATION)}
element={<EditOrganizationPage />}
/>
<Route
path={getAdminRoutePath(ROUTES.ORGANIZATIONS)}
element={<OrganizationsPage />}
/>
</>
)}
{featureFlagUtils.isFeatureEnabled('SHOW_PLACE_PAGES') && (
<>
<Route
Expand All @@ -128,7 +132,7 @@ const AdminPageRoutes: React.FC = () => {
</>
)}

{arePriceGroupRoutesAllowed(user) && (
{areFinancialRoutesAllowed(user) && (
<>
<Route
path={getAdminRoutePath(ROUTES.CREATE_PRICE_GROUP)}
Expand Down
68 changes: 41 additions & 27 deletions src/domain/app/routes/helpPageRoutes/HelpPageRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import { Navigate, Route, Routes } from 'react-router';

import LoadingSpinner from '../../../../common/components/loadingSpinner/LoadingSpinner';
import { ROUTES } from '../../../../constants';
import useLocale from '../../../../hooks/useLocale';
import { Language } from '../../../../types';
import HelpPageLayout from '../../../help/layout/HelpPageLayout';
import AskPermissionPage from '../../../help/pages/askPermissionPage/AskPermissionPage';
import ContactPage from '../../../help/pages/contactPage/ContactPage';
import EventsInstructionsPage from '../../../help/pages/eventsInstructionsPage/EventsInstructionsPage';
Expand All @@ -13,6 +15,8 @@ import ServiceInformationPage from '../../../help/pages/serviceInformation/Servi
import SourceCodePage from '../../../help/pages/sourceCodePage/SourceCodePage';
import TermsOfUsePage from '../../../help/pages/termsOfUsePage/TermsOfUsePage';
import NotFound from '../../../notFound/NotFound';
import useUser from '../../../user/hooks/useUser';
import { areRegistrationRoutesAllowed } from '../../../user/permissions';
/* istanbul ignore next */
const DocumentationPage = React.lazy(
() => import('../../../help/pages/documentationPage/DocumentationPage')
Expand All @@ -26,6 +30,7 @@ const InstructionsRoutes: React.FC<Props> = ({ locale }) => {
const getInstructionsRoutePath = (path: string) =>
path.replace(ROUTES.INSTRUCTIONS, '');
const getLocalePath = (path: string) => `/${locale}${path}`;
const { user } = useUser();

return (
<Routes>
Expand All @@ -39,15 +44,18 @@ const InstructionsRoutes: React.FC<Props> = ({ locale }) => {
path={getInstructionsRoutePath(ROUTES.INSTRUCTIONS_EVENTS)}
element={<EventsInstructionsPage />}
/>
{areRegistrationRoutesAllowed(user) && (
<Route
path={getInstructionsRoutePath(ROUTES.INSTRUCTIONS_REGISTRATION)}
element={<RegistrationInstructions />}
/>
)}
<Route
path={getInstructionsRoutePath(ROUTES.INSTRUCTIONS_FAQ)}
element={<FaqPage />}
/>
<Route
path={getInstructionsRoutePath(ROUTES.INSTRUCTIONS_REGISTRATION)}
element={<RegistrationInstructions />}
/>
<Route
path="*"
element={<NotFound pathAfterSignIn={`/${locale}${ROUTES.HOME}`} />}
/>
</Routes>
Expand Down Expand Up @@ -79,8 +87,8 @@ const TechnologyRoutes: React.FC<Props> = ({ locale }) => {
path={getTechnologyRoutePath(ROUTES.TECHNOLOGY_DOCUMENTATION)}
element={<DocumentationPage />}
/>

<Route
path="*"
element={<NotFound pathAfterSignIn={`/${locale}${ROUTES.HOME}`} />}
/>
</Routes>
Expand Down Expand Up @@ -121,6 +129,7 @@ const SupportRoutes: React.FC<Props> = ({ locale }) => {
element={<TermsOfUsePage />}
/>
<Route
path="*"
element={<NotFound pathAfterSignIn={`/${locale}${ROUTES.HOME}`} />}
/>
</Routes>
Expand All @@ -131,30 +140,35 @@ const HelpPageRoutes: React.FC = () => {
const locale = useLocale();
const getHelpRoutePath = (path: string) => path.replace(ROUTES.HELP, '');
const getLocalePath = (path: string) => `/${locale}${path}`;
const { loading } = useUser();

return (
<Routes>
<Route
path={getHelpRoutePath(ROUTES.HELP)}
element={<Navigate replace to={getLocalePath(ROUTES.SUPPORT)} />}
/>
<Route
path={`${getHelpRoutePath(ROUTES.SUPPORT)}/*`}
element={<SupportRoutes locale={locale} />}
/>
<Route
path={`${getHelpRoutePath(ROUTES.INSTRUCTIONS)}/*`}
element={<InstructionsRoutes locale={locale} />}
/>
<Route
path={`${getHelpRoutePath(ROUTES.TECHNOLOGY)}/*`}
element={<TechnologyRoutes locale={locale} />}
/>
<Route
path="*"
element={<NotFound pathAfterSignIn={`/${locale}${ROUTES.HOME}`} />}
/>
</Routes>
<LoadingSpinner isLoading={loading}>
<HelpPageLayout>
<Routes>
<Route
path={getHelpRoutePath(ROUTES.HELP)}
element={<Navigate replace to={getLocalePath(ROUTES.SUPPORT)} />}
/>
<Route
path={`${getHelpRoutePath(ROUTES.SUPPORT)}/*`}
element={<SupportRoutes locale={locale} />}
/>
<Route
path={`${getHelpRoutePath(ROUTES.INSTRUCTIONS)}/*`}
element={<InstructionsRoutes locale={locale} />}
/>
<Route
path={`${getHelpRoutePath(ROUTES.TECHNOLOGY)}/*`}
element={<TechnologyRoutes locale={locale} />}
/>
<Route
path="*"
element={<NotFound pathAfterSignIn={`/${locale}${ROUTES.HOME}`} />}
/>
</Routes>
</HelpPageLayout>
</LoadingSpinner>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Route, Routes } from 'react-router';

import { ROUTES } from '../../../../../constants';
import { Language } from '../../../../../types';
import { mockUnauthenticatedLoginState } from '../../../../../utils/mockLoginHooks';
import { mockAuthenticatedLoginState } from '../../../../../utils/mockLoginHooks';
import {
configure,
render,
Expand All @@ -21,14 +21,6 @@ import HelpPageRoutes from '../HelpPageRoutes';

configure({ defaultHidden: true });

afterEach(() => {
vi.resetAllMocks();
});

beforeEach(() => {
mockUnauthenticatedLoginState();
});

let initialHeadInnerHTML: string | null = null;

beforeEach(async () => {
Expand All @@ -38,10 +30,14 @@ beforeEach(async () => {
document.head.innerHTML = '';

await i18n.changeLanguage('fi');

mockAuthenticatedLoginState();
});

afterEach(() => {
document.head.innerHTML = initialHeadInnerHTML || '';

vi.resetAllMocks();
});

const mocks = [
Expand Down
10 changes: 1 addition & 9 deletions src/domain/app/routes/localeRoutes/LocaleRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import getValue from '../../../../utils/getValue';
import AccessibilityStatementPage from '../../../accessibilityStatement/AccessibilityStatement';
import LogoutPage from '../../../auth/logoutPage/LogoutPage';
import EventSavedPage from '../../../eventSaved/EventSavedPage';
import HelpPageLayout from '../../../help/layout/HelpPageLayout';
import LandingPage from '../../../landingPage/LandingPage';
import NotFound from '../../../notFound/NotFound';
import PageLayout from '../../layout/pageLayout/PageLayout';
Expand Down Expand Up @@ -108,14 +107,7 @@ const LocaleRoutes: React.FC = () => {
element={<AdminPageRoutes />}
></Route>
)}
<Route
path={`${ROUTES.HELP}/*`}
element={
<HelpPageLayout>
<HelpPageRoutes />
</HelpPageLayout>
}
/>
<Route path={`${ROUTES.HELP}/*`} element={<HelpPageRoutes />} />
<Route
path="*"
element={<NotFound pathAfterSignIn={`/${locale}${ROUTES.HOME}`} />}
Expand Down
15 changes: 11 additions & 4 deletions src/domain/help/layout/HelpPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useTranslation } from 'react-i18next';

import { ROUTES } from '../../../constants';
import LayoutWithSideNavigation from '../../app/layout/layoutWithSideNavigation/LayoutWithSideNavigation';
import useUser from '../../user/hooks/useUser';
import { areRegistrationRoutesAllowed } from '../../user/permissions';
import styles from './helpPageLayout.module.scss';

interface Props {
Expand All @@ -12,16 +14,21 @@ interface Props {

const HelpPageLayout: React.FC<Props> = ({ children }) => {
const { t } = useTranslation();
const { user } = useUser();

const instructionsSubLevels = [
{
label: t('helpPage.sideNavigation.labelEventsInstructions'),
to: ROUTES.INSTRUCTIONS_EVENTS,
},
{
label: t('helpPage.sideNavigation.labelRegistrationInstructions'),
to: ROUTES.INSTRUCTIONS_REGISTRATION,
},
...(areRegistrationRoutesAllowed(user)
? [
{
label: t('helpPage.sideNavigation.labelRegistrationInstructions'),
to: ROUTES.INSTRUCTIONS_REGISTRATION,
},
]
: []),
{
label: t('helpPage.sideNavigation.labelFaq'),
to: ROUTES.INSTRUCTIONS_FAQ,
Expand Down
10 changes: 10 additions & 0 deletions src/domain/help/layout/__tests__/HelpPageLayout.test.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import React from 'react';

import { ROUTES } from '../../../../constants';
import { mockAuthenticatedLoginState } from '../../../../utils/mockLoginHooks';
import {
configure,
render,
screen,
userEvent,
waitFor,
} from '../../../../utils/testUtils';
import { mockedUserResponse } from '../../../user/__mocks__/user';
import HelpPageLayout from '../HelpPageLayout';

configure({ defaultHidden: true });

beforeEach(async () => {
mockAuthenticatedLoginState();
});

const mocks = [mockedUserResponse];

const route = `/fi${ROUTES.SUPPORT_SERVICE_INFORMATION}`;

const renderComponent = () =>
render(<HelpPageLayout>Content</HelpPageLayout>, {
mocks,
routes: [route],
});

Expand Down
5 changes: 2 additions & 3 deletions src/domain/user/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export const areAdminRoutesAllowed = (user?: UserFieldsFragment): boolean =>
hasAdminOrganization(user) ||
hasFinancialAdminOrganization(user);

export const arePriceGroupRoutesAllowed = (
user?: UserFieldsFragment
): boolean => user?.isSuperuser || hasFinancialAdminOrganization(user);
export const areFinancialRoutesAllowed = (user?: UserFieldsFragment): boolean =>
user?.isSuperuser || hasFinancialAdminOrganization(user);

export const areRegistrationRoutesAllowed = (
user?: UserFieldsFragment
Expand Down

0 comments on commit 1c26277

Please sign in to comment.