Skip to content

Commit

Permalink
#204 - garden admin button permissions (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
obr42 authored Nov 14, 2022
1 parent b8d422d commit b20b396
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 21 deletions.
78 changes: 78 additions & 0 deletions src/pages/GardenAdmin/GardenAdminCard.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<AllProviders>
<GardenAdminCard garden={TGarden} setRequestStatus={jest.fn()} />
</AllProviders>,
)
await waitFor(() => {
expect(
screen.queryByText('Edit configurations'),
).not.toBeInTheDocument()
})
})

test('should show edit button when permission', async () => {
mockAxios.onGet(regexUsers).reply(200, TAdmin)
render(
<LoggedInProviders>
<GardenAdminCard garden={TGarden} setRequestStatus={jest.fn()} />
</LoggedInProviders>,
)
await waitFor(() => {
expect(screen.getByText('Edit configurations')).toBeInTheDocument()
})
})

test('should hide delete button when no permission', async () => {
mockAxios.onGet(regexUsers).reply(200, TUser)
render(
<LoggedInProviders>
<GardenAdminCard garden={TGarden} setRequestStatus={jest.fn()} />
</LoggedInProviders>,
)
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(
<LoggedInProviders>
<GardenAdminCard garden={TGarden} setRequestStatus={jest.fn()} />
</LoggedInProviders>,
)
await waitForElementToBeRemoved(() =>
screen.queryByRole('button', { name: 'Delete' }),
)
})
})
})
42 changes: 21 additions & 21 deletions src/pages/GardenAdmin/GardenAdminCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -96,27 +96,27 @@ const GardenAdminCard = ({
{garden.systems.length}
</CardContent>
<CardActions>
<Button
variant="contained"
color="secondary"
component={RouterLink}
to={'/admin/gardens/' + garden.name}
sx={{ width: 0.75, mr: 1 }}
>
Edit configurations
</Button>
{garden.connection_type !== 'LOCAL' ? (
{hasGardenPermission('garden:update', garden) && (
<Button
variant="contained"
color="secondary"
component={RouterLink}
to={'/admin/gardens/' + garden.name}
sx={{ width: 0.75, mr: 1 }}
>
Edit configurations
</Button>
)}
{(garden.connection_type !== 'LOCAL' &&
hasGardenPermission('garden:delete', garden)) ? (
<>
{hasPermission('garden:delete') && (
<Button
variant="contained"
color="error"
onClick={() => setOpen(true)}
>
Delete
</Button>
)}

<Button
variant="contained"
color="error"
onClick={() => setOpen(true)}
>
Delete
</Button>
<Modal
open={open}
onClose={() => setOpen(false)}
Expand Down
1 change: 1 addition & 0 deletions src/test/user-test-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const TAdmin: User = {
global_permissions: [
'system:update',
'user:update',
'garden:delete',
'garden:update',
'request:create',
'user:create',
Expand Down

0 comments on commit b20b396

Please sign in to comment.