From b20b3967912965d2e6c75a398563acefdf23fc9a Mon Sep 17 00:00:00 2001
From: obr42 <107439850+obr42@users.noreply.github.com>
Date: Mon, 14 Nov 2022 18:52:30 +0000
Subject: [PATCH] #204 - garden admin button permissions (#237)
---
.../GardenAdmin/GardenAdminCard.test.tsx | 78 +++++++++++++++++++
src/pages/GardenAdmin/GardenAdminCard.tsx | 42 +++++-----
src/test/user-test-values.ts | 1 +
3 files changed, 100 insertions(+), 21 deletions(-)
create mode 100644 src/pages/GardenAdmin/GardenAdminCard.test.tsx
diff --git a/src/pages/GardenAdmin/GardenAdminCard.test.tsx b/src/pages/GardenAdmin/GardenAdminCard.test.tsx
new file mode 100644
index 00000000..c4411dbc
--- /dev/null
+++ b/src/pages/GardenAdmin/GardenAdminCard.test.tsx
@@ -0,0 +1,78 @@
+import {
+ render,
+ screen,
+ waitFor,
+ waitForElementToBeRemoved,
+} from '@testing-library/react'
+import { mockAxios, regexUsers } from 'test/axios-mock'
+import { TGarden } from 'test/garden-test-values'
+import { TServerAuthConfig, TServerConfig } from 'test/test-values'
+import { AllProviders, LoggedInProviders } from 'test/testMocks'
+import { TAdmin, TUser } from 'test/user-test-values'
+
+import { GardenAdminCard } from './GardenAdminCard'
+
+describe('GardenAdminCard', () => {
+ describe('permission checks', () => {
+ beforeAll(() => {
+ mockAxios.onGet('/config').reply(200, TServerAuthConfig)
+ })
+
+ afterAll(() => {
+ mockAxios.onGet('/config').reply(200, TServerConfig)
+ mockAxios.onGet(regexUsers).reply(200, TUser)
+ })
+
+ test('should hide edit button when no permission', async () => {
+ mockAxios.onGet(regexUsers).reply(200, TUser)
+ render(
+
+
+ ,
+ )
+ await waitFor(() => {
+ expect(
+ screen.queryByText('Edit configurations'),
+ ).not.toBeInTheDocument()
+ })
+ })
+
+ test('should show edit button when permission', async () => {
+ mockAxios.onGet(regexUsers).reply(200, TAdmin)
+ render(
+
+
+ ,
+ )
+ await waitFor(() => {
+ expect(screen.getByText('Edit configurations')).toBeInTheDocument()
+ })
+ })
+
+ test('should hide delete button when no permission', async () => {
+ mockAxios.onGet(regexUsers).reply(200, TUser)
+ render(
+
+
+ ,
+ )
+ await waitFor(() => {
+ expect(
+ screen.queryByRole('button', { name: 'Delete' }),
+ ).not.toBeInTheDocument()
+ })
+ })
+
+ test('should show delete button when permission', async () => {
+ mockAxios.onGet(regexUsers).reply(200, TAdmin)
+ render(
+
+
+ ,
+ )
+ await waitForElementToBeRemoved(() =>
+ screen.queryByRole('button', { name: 'Delete' }),
+ )
+ })
+ })
+})
diff --git a/src/pages/GardenAdmin/GardenAdminCard.tsx b/src/pages/GardenAdmin/GardenAdminCard.tsx
index a074f4d1..9ab2b546 100644
--- a/src/pages/GardenAdmin/GardenAdminCard.tsx
+++ b/src/pages/GardenAdmin/GardenAdminCard.tsx
@@ -31,7 +31,7 @@ const GardenAdminCard = ({
const [open, setOpen] = useState(false)
const { deleteGarden } = useGardens()
- const { hasPermission } = PermissionsContainer.useContainer()
+ const { hasGardenPermission } = PermissionsContainer.useContainer()
const modalStyle = {
position: 'absolute' as const,
@@ -96,27 +96,27 @@ const GardenAdminCard = ({
{garden.systems.length}
-
- {garden.connection_type !== 'LOCAL' ? (
+ {hasGardenPermission('garden:update', garden) && (
+
+ )}
+ {(garden.connection_type !== 'LOCAL' &&
+ hasGardenPermission('garden:delete', garden)) ? (
<>
- {hasPermission('garden:delete') && (
-
- )}
-
+
setOpen(false)}
diff --git a/src/test/user-test-values.ts b/src/test/user-test-values.ts
index 720fa47b..6ffc1866 100644
--- a/src/test/user-test-values.ts
+++ b/src/test/user-test-values.ts
@@ -48,6 +48,7 @@ export const TAdmin: User = {
global_permissions: [
'system:update',
'user:update',
+ 'garden:delete',
'garden:update',
'request:create',
'user:create',