From 1c26277264dc5801e9a027de228ef7235f7af616 Mon Sep 17 00:00:00 2001 From: Jori Lindell Date: Fri, 3 May 2024 14:40:08 +0300 Subject: [PATCH] feat: allow only admin users to see registration instructions --- src/domain/admin/layout/AdminPageLayout.tsx | 6 +- .../app/routes/adminRoutes/AdminRoutes.tsx | 32 +++++---- .../routes/helpPageRoutes/HelpPageRoutes.tsx | 68 +++++++++++-------- .../__tests__/HelpPageRoutes.test.tsx | 14 ++-- .../app/routes/localeRoutes/LocaleRoutes.tsx | 10 +-- src/domain/help/layout/HelpPageLayout.tsx | 15 ++-- .../layout/__tests__/HelpPageLayout.test.tsx | 10 +++ src/domain/user/permissions.ts | 5 +- 8 files changed, 91 insertions(+), 69 deletions(-) diff --git a/src/domain/admin/layout/AdminPageLayout.tsx b/src/domain/admin/layout/AdminPageLayout.tsx index 61d45cd69..f5efcb4d3 100644 --- a/src/domain/admin/layout/AdminPageLayout.tsx +++ b/src/domain/admin/layout/AdminPageLayout.tsx @@ -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 = ({ children }) => { const { t } = useTranslation(); @@ -22,13 +22,13 @@ const AdminPageLayout: React.FC = ({ 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 }] : []), ]; diff --git a/src/domain/app/routes/adminRoutes/AdminRoutes.tsx b/src/domain/app/routes/adminRoutes/AdminRoutes.tsx index 94a2278c8..c869d04f6 100644 --- a/src/domain/app/routes/adminRoutes/AdminRoutes.tsx +++ b/src/domain/app/routes/adminRoutes/AdminRoutes.tsx @@ -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( @@ -99,18 +99,22 @@ const AdminPageRoutes: React.FC = () => { element={} /> - } - /> - } - /> - } - /> + {areFinancialRoutesAllowed(user) && ( + <> + } + /> + } + /> + } + /> + + )} {featureFlagUtils.isFeatureEnabled('SHOW_PLACE_PAGES') && ( <> { )} - {arePriceGroupRoutesAllowed(user) && ( + {areFinancialRoutesAllowed(user) && ( <> import('../../../help/pages/documentationPage/DocumentationPage') @@ -26,6 +30,7 @@ const InstructionsRoutes: React.FC = ({ locale }) => { const getInstructionsRoutePath = (path: string) => path.replace(ROUTES.INSTRUCTIONS, ''); const getLocalePath = (path: string) => `/${locale}${path}`; + const { user } = useUser(); return ( @@ -39,15 +44,18 @@ const InstructionsRoutes: React.FC = ({ locale }) => { path={getInstructionsRoutePath(ROUTES.INSTRUCTIONS_EVENTS)} element={} /> + {areRegistrationRoutesAllowed(user) && ( + } + /> + )} } /> } - /> - } /> @@ -79,8 +87,8 @@ const TechnologyRoutes: React.FC = ({ locale }) => { path={getTechnologyRoutePath(ROUTES.TECHNOLOGY_DOCUMENTATION)} element={} /> - } /> @@ -121,6 +129,7 @@ const SupportRoutes: React.FC = ({ locale }) => { element={} /> } /> @@ -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 ( - - } - /> - } - /> - } - /> - } - /> - } - /> - + + + + } + /> + } + /> + } + /> + } + /> + } + /> + + + ); }; diff --git a/src/domain/app/routes/helpPageRoutes/__tests__/HelpPageRoutes.test.tsx b/src/domain/app/routes/helpPageRoutes/__tests__/HelpPageRoutes.test.tsx index adec156f2..21fcbb160 100644 --- a/src/domain/app/routes/helpPageRoutes/__tests__/HelpPageRoutes.test.tsx +++ b/src/domain/app/routes/helpPageRoutes/__tests__/HelpPageRoutes.test.tsx @@ -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, @@ -21,14 +21,6 @@ import HelpPageRoutes from '../HelpPageRoutes'; configure({ defaultHidden: true }); -afterEach(() => { - vi.resetAllMocks(); -}); - -beforeEach(() => { - mockUnauthenticatedLoginState(); -}); - let initialHeadInnerHTML: string | null = null; beforeEach(async () => { @@ -38,10 +30,14 @@ beforeEach(async () => { document.head.innerHTML = ''; await i18n.changeLanguage('fi'); + + mockAuthenticatedLoginState(); }); afterEach(() => { document.head.innerHTML = initialHeadInnerHTML || ''; + + vi.resetAllMocks(); }); const mocks = [ diff --git a/src/domain/app/routes/localeRoutes/LocaleRoutes.tsx b/src/domain/app/routes/localeRoutes/LocaleRoutes.tsx index 0e47a47be..89a17d7b0 100644 --- a/src/domain/app/routes/localeRoutes/LocaleRoutes.tsx +++ b/src/domain/app/routes/localeRoutes/LocaleRoutes.tsx @@ -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'; @@ -108,14 +107,7 @@ const LocaleRoutes: React.FC = () => { element={} > )} - - - - } - /> + } /> } diff --git a/src/domain/help/layout/HelpPageLayout.tsx b/src/domain/help/layout/HelpPageLayout.tsx index c9549d7be..134811d0a 100644 --- a/src/domain/help/layout/HelpPageLayout.tsx +++ b/src/domain/help/layout/HelpPageLayout.tsx @@ -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 { @@ -12,16 +14,21 @@ interface Props { const HelpPageLayout: React.FC = ({ 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, diff --git a/src/domain/help/layout/__tests__/HelpPageLayout.test.tsx b/src/domain/help/layout/__tests__/HelpPageLayout.test.tsx index ac873a010..74dc4c2df 100644 --- a/src/domain/help/layout/__tests__/HelpPageLayout.test.tsx +++ b/src/domain/help/layout/__tests__/HelpPageLayout.test.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { ROUTES } from '../../../../constants'; +import { mockAuthenticatedLoginState } from '../../../../utils/mockLoginHooks'; import { configure, render, @@ -8,13 +9,22 @@ import { 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(Content, { + mocks, routes: [route], }); diff --git a/src/domain/user/permissions.ts b/src/domain/user/permissions.ts index 00f065997..fb70bb5ec 100644 --- a/src/domain/user/permissions.ts +++ b/src/domain/user/permissions.ts @@ -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