From a9951d50b697dc8e8ff1e78817e489e9d95240cc Mon Sep 17 00:00:00 2001 From: Jade Wilson Date: Thu, 4 Apr 2024 17:44:08 +0100 Subject: [PATCH] Remove admin undefined access (#857) --- .../__tests__/admin-unauthorised.test.tsx | 2 +- .../admin/__tests__/admin-undefined.test.tsx | 52 +++++++++++++++++++ .../src/pages/admin/__tests__/admin.test.tsx | 2 +- client/src/pages/admin/admin.tsx | 2 +- 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 client/src/pages/admin/__tests__/admin-undefined.test.tsx diff --git a/client/src/pages/admin/__tests__/admin-unauthorised.test.tsx b/client/src/pages/admin/__tests__/admin-unauthorised.test.tsx index 2513312bd..511adaa1e 100644 --- a/client/src/pages/admin/__tests__/admin-unauthorised.test.tsx +++ b/client/src/pages/admin/__tests__/admin-unauthorised.test.tsx @@ -1,4 +1,4 @@ -import { describe, expect, test, vi } from 'vitest'; +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import { render, screen } from '@testing-library/react'; import Admin from '@/pages/admin/admin'; import '@testing-library/jest-dom'; diff --git a/client/src/pages/admin/__tests__/admin-undefined.test.tsx b/client/src/pages/admin/__tests__/admin-undefined.test.tsx new file mode 100644 index 000000000..41840a714 --- /dev/null +++ b/client/src/pages/admin/__tests__/admin-undefined.test.tsx @@ -0,0 +1,52 @@ +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import Admin from '@/pages/admin/admin'; +import '@testing-library/jest-dom'; +import React from 'react'; +import { AllProviders } from '@/mocks/setup-tests'; +import { MemoryRouter } from 'react-router-dom'; + +describe('Test Admin Undefined', () => { + beforeEach(() => { + vi.mock('@azure/msal-react', async () => { + return { + useMsal: () => { + return { + instance: { + getActiveAccount: () => { + return { + idTokenClaims: { + }, + }; + }, + }, + }; + }, + }; + }); + }); + + afterEach(() => { + vi.resetAllMocks(); + }); + + test('Does not show menu items', async () => { + render(, { wrapper: AllProviders }); + expect(screen.queryByText('Users Menu Item')).toBeNull(); + expect(screen.queryByText('Data Sources Menu Item')).toBeNull(); + expect(screen.queryByText('Plugins Menu Item')).toBeNull(); + expect(screen.queryByText('Configuration Menu Item')).toBeNull(); + }); + + test('Shows unauthorised page', async () => { + const route = '/admin'; + + render( + + + + ); + expect(await screen.findByText('You are unauthorized to view this page.')).toBeInTheDocument(); + expect(await screen.findByText('Please contact your administrator.')).toBeInTheDocument(); + }); +}); diff --git a/client/src/pages/admin/__tests__/admin.test.tsx b/client/src/pages/admin/__tests__/admin.test.tsx index 6245fde4a..02d980e44 100644 --- a/client/src/pages/admin/__tests__/admin.test.tsx +++ b/client/src/pages/admin/__tests__/admin.test.tsx @@ -1,4 +1,4 @@ -import { describe, expect, test, vi } from 'vitest'; +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import { render, screen } from '@testing-library/react'; import Admin from '@/pages/admin/admin'; import '@testing-library/jest-dom'; diff --git a/client/src/pages/admin/admin.tsx b/client/src/pages/admin/admin.tsx index 6b1337224..2a1205fb9 100644 --- a/client/src/pages/admin/admin.tsx +++ b/client/src/pages/admin/admin.tsx @@ -32,7 +32,7 @@ export const Admin = () => { if ( activeAccount === null || activeAccount === undefined || - activeAccount?.idTokenClaims?.roles?.includes('IQEngine-Admin') === false + activeAccount?.idTokenClaims?.roles?.includes('IQEngine-Admin') !== true ) { return ( <>