-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v16] Conditionally render edit/delete options for Roles (#49904)
Backport #49728 Manual backport due to all the changes that have happened to roles lately (and some missing components)
- Loading branch information
Showing
7 changed files
with
324 additions
and
8 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
web/packages/shared/components/MissingPermissionsTooltip/MissingPermissionsTooltip.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { Box, Text, Flex } from 'design'; | ||
|
||
export const MissingPermissionsTooltip = ({ | ||
missingPermissions, | ||
}: { | ||
missingPermissions: string[]; | ||
}) => { | ||
return ( | ||
<Box> | ||
<Text mb={1}>You do not have all of the required permissions.</Text> | ||
<Box mb={1}> | ||
<Text bold>Missing permissions:</Text> | ||
<Flex gap={2}> | ||
{missingPermissions.map(perm => ( | ||
<Text key={perm}>{perm}</Text> | ||
))} | ||
</Flex> | ||
</Box> | ||
</Box> | ||
); | ||
}; |
19 changes: 19 additions & 0 deletions
19
web/packages/shared/components/MissingPermissionsTooltip/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
export { MissingPermissionsTooltip } from './MissingPermissionsTooltip'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { MemoryRouter } from 'react-router'; | ||
import { render, screen, fireEvent, waitFor } from 'design/utils/testing'; | ||
|
||
import { ContextProvider } from 'teleport'; | ||
import { createTeleportContext } from 'teleport/mocks/contexts'; | ||
|
||
import { Roles } from './Roles'; | ||
import { State } from './useRoles'; | ||
|
||
describe('Roles list', () => { | ||
const defaultState: State = { | ||
save: jest.fn(), | ||
fetch: jest.fn(), | ||
remove: jest.fn(), | ||
rolesAcl: { | ||
read: true, | ||
remove: true, | ||
create: true, | ||
edit: true, | ||
list: true, | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(defaultState, 'fetch').mockResolvedValue({ | ||
startKey: '', | ||
items: [ | ||
{ | ||
content: '', | ||
id: '1', | ||
kind: 'role', | ||
name: 'cool-role', | ||
description: 'coolest-role', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('button is enabled if user has create perms', async () => { | ||
const ctx = createTeleportContext(); | ||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Roles {...defaultState} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/create new role/i)).toBeEnabled(); | ||
}); | ||
}); | ||
|
||
test('displays disabled create button', async () => { | ||
const ctx = createTeleportContext(); | ||
const testState = { | ||
...defaultState, | ||
rolesAcl: { | ||
...defaultState.rolesAcl, | ||
create: false, | ||
}, | ||
}; | ||
|
||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Roles {...testState} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/create new role/i)).toBeDisabled(); | ||
}); | ||
}); | ||
|
||
test('all options available', async () => { | ||
const ctx = createTeleportContext(); | ||
|
||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Roles {...defaultState} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByRole('button', { name: /options/i }) | ||
).toBeInTheDocument(); | ||
}); | ||
const optionsButton = screen.getByRole('button', { name: /options/i }); | ||
fireEvent.click(optionsButton); | ||
const menuItems = screen.queryAllByRole('menuitem'); | ||
expect(menuItems).toHaveLength(2); | ||
}); | ||
|
||
test('hides edit button if no access', async () => { | ||
const ctx = createTeleportContext(); | ||
const testState = { | ||
...defaultState, | ||
rolesAcl: { | ||
...defaultState.rolesAcl, | ||
edit: false, | ||
}, | ||
}; | ||
|
||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Roles {...testState} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByRole('button', { name: /options/i }) | ||
).toBeInTheDocument(); | ||
}); | ||
const optionsButton = screen.getByRole('button', { name: /options/i }); | ||
fireEvent.click(optionsButton); | ||
const menuItems = screen.queryAllByRole('menuitem'); | ||
expect(menuItems).toHaveLength(1); | ||
expect(menuItems.every(item => item.textContent.includes('Edit'))).not.toBe( | ||
true | ||
); | ||
}); | ||
|
||
test('hides delete button if no access', async () => { | ||
const ctx = createTeleportContext(); | ||
const testState = { | ||
...defaultState, | ||
rolesAcl: { | ||
...defaultState.rolesAcl, | ||
remove: false, | ||
}, | ||
}; | ||
|
||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Roles {...testState} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByRole('button', { name: /options/i }) | ||
).toBeInTheDocument(); | ||
}); | ||
const optionsButton = screen.getByRole('button', { name: /options/i }); | ||
fireEvent.click(optionsButton); | ||
const menuItems = screen.queryAllByRole('menuitem'); | ||
expect(menuItems).toHaveLength(1); | ||
expect( | ||
menuItems.every(item => item.textContent.includes('Delete')) | ||
).not.toBe(true); | ||
}); | ||
|
||
test('hides Options button if no permissions to edit or delete', async () => { | ||
const ctx = createTeleportContext(); | ||
const testState = { | ||
...defaultState, | ||
rolesAcl: { | ||
...defaultState.rolesAcl, | ||
remove: false, | ||
edit: false, | ||
}, | ||
}; | ||
|
||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Roles {...testState} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('cool-role')).toBeInTheDocument(); | ||
}); | ||
const menuItems = screen.queryAllByRole('menuitem'); | ||
expect(menuItems).toHaveLength(0); | ||
}); | ||
}); |
Oops, something went wrong.