💄(frontend) redirect home according to abilities

We try to detect the landing page according to user
permissions (abilities) instead of just the configuration
setting.
This will be improved when the homepage is developed
This commit is contained in:
Quentin BEY
2024-12-06 16:47:28 +01:00
committed by BEY Quentin
parent 5d84e226b7
commit 9953dd2111
5 changed files with 143 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import { useConfigStore } from '@/core';
import { useAuthStore } from '@/core/auth';
import { AppWrapper } from '@/tests/utils';
import Page from '../pages';
@@ -20,6 +21,20 @@ describe('Page', () => {
afterEach(() => jest.clearAllMocks());
it('checks Page rendering with team feature', () => {
useAuthStore.setState({
authenticated: true,
userData: {
id: '1',
email: 'test@example.com',
name: 'Test User',
abilities: {
contacts: { can_view: false },
teams: { can_view: false },
mailboxes: { can_view: false },
},
},
});
useConfigStore.setState({
config: {
RELEASE: '1.0.0',
@@ -35,6 +50,20 @@ describe('Page', () => {
});
it('checks Page rendering without team feature', () => {
useAuthStore.setState({
authenticated: true,
userData: {
id: '1',
email: 'test@example.com',
name: 'Test User',
abilities: {
contacts: { can_view: false },
teams: { can_view: false },
mailboxes: { can_view: false },
},
},
});
useConfigStore.setState({
config: {
RELEASE: '1.0.0',
@@ -48,4 +77,62 @@ describe('Page', () => {
expect(mockedPush).toHaveBeenCalledWith('/mail-domains/');
});
it('checks Page rendering with team permission', () => {
useAuthStore.setState({
authenticated: true,
userData: {
id: '1',
email: 'test@example.com',
name: 'Test User',
abilities: {
contacts: { can_view: false },
teams: { can_view: true },
mailboxes: { can_view: false },
},
},
});
useConfigStore.setState({
config: {
RELEASE: '1.0.0',
COMMIT: 'NA',
FEATURES: { TEAMS_DISPLAY: false },
LANGUAGES: [],
},
});
render(<Page />, { wrapper: AppWrapper });
expect(mockedPush).toHaveBeenCalledWith('/teams/');
});
it('checks Page rendering with mailbox permission', () => {
useAuthStore.setState({
authenticated: true,
userData: {
id: '1',
email: 'test@example.com',
name: 'Test User',
abilities: {
contacts: { can_view: false },
teams: { can_view: false },
mailboxes: { can_view: true },
},
},
});
useConfigStore.setState({
config: {
RELEASE: '1.0.0',
COMMIT: 'NA',
FEATURES: { TEAMS_DISPLAY: true },
LANGUAGES: [],
},
});
render(<Page />, { wrapper: AppWrapper });
expect(mockedPush).toHaveBeenCalledWith('/mail-domains/');
});
});

View File

@@ -1,16 +1,36 @@
import { useRouter as useNavigate } from 'next/navigation';
import { useEffect } from 'react';
import { useAuthStore } from '@/core/auth';
import { useConfigStore } from '@/core/config';
import { NextPageWithLayout } from '@/types/next';
const Page: NextPageWithLayout = () => {
const { config } = useConfigStore();
const { userData } = useAuthStore();
const router = useNavigate();
useEffect(() => {
if (!userData) {
router.push('/authenticate/');
return;
}
// 1. The user can see teams
if (userData.abilities?.teams?.can_view) {
router.push('/teams/');
return;
}
// 2. The user can see mailboxes
if (userData.abilities?.mailboxes?.can_view) {
router.push('/mail-domains/');
return;
}
// Fallback to the default route according to global config
router.push(config?.FEATURES.TEAMS_DISPLAY ? '/teams/' : '/mail-domains/');
}, [config?.FEATURES.TEAMS_DISPLAY, router]);
}, [config?.FEATURES.TEAMS_DISPLAY, userData, router]);
return null;
};