-
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 User edit/delete actions in the web UI
Backport #49645
- Loading branch information
Showing
9 changed files
with
330 additions
and
67 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -18,14 +18,23 @@ | |
|
||
import React from 'react'; | ||
import { MemoryRouter } from 'react-router'; | ||
import { render, screen, userEvent } from 'design/utils/testing'; | ||
import { render, screen, userEvent, fireEvent } from 'design/utils/testing'; | ||
|
||
import { ContextProvider } from 'teleport'; | ||
import { createTeleportContext } from 'teleport/mocks/contexts'; | ||
import { Access } from 'teleport/services/user'; | ||
|
||
import { Users } from './Users'; | ||
import { State } from './useUsers'; | ||
|
||
const defaultAcl: Access = { | ||
read: true, | ||
edit: true, | ||
remove: true, | ||
list: true, | ||
create: true, | ||
}; | ||
|
||
describe('invite collaborators integration', () => { | ||
const ctx = createTeleportContext(); | ||
|
||
|
@@ -59,6 +68,7 @@ describe('invite collaborators integration', () => { | |
EmailPasswordReset: null, | ||
showMauInfo: false, | ||
onDismissUsersMauNotice: () => null, | ||
usersAcl: defaultAcl, | ||
}; | ||
}); | ||
|
||
|
@@ -142,6 +152,7 @@ test('Users not equal to MAU Notice', async () => { | |
EmailPasswordReset: null, | ||
showMauInfo: true, | ||
onDismissUsersMauNotice: jest.fn(), | ||
usersAcl: defaultAcl, | ||
}; | ||
|
||
const { rerender } = render( | ||
|
@@ -205,6 +216,7 @@ describe('email password reset integration', () => { | |
EmailPasswordReset: null, | ||
showMauInfo: false, | ||
onDismissUsersMauNotice: () => null, | ||
usersAcl: defaultAcl, | ||
}; | ||
}); | ||
|
||
|
@@ -245,3 +257,158 @@ describe('email password reset integration', () => { | |
expect(screen.getByTestId('new-reset-ui')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('permission handling', () => { | ||
const ctx = createTeleportContext(); | ||
|
||
let props: State; | ||
beforeEach(() => { | ||
props = { | ||
attempt: { | ||
message: 'success', | ||
isSuccess: true, | ||
isProcessing: false, | ||
isFailed: false, | ||
}, | ||
users: [ | ||
{ | ||
name: 'tester', | ||
roles: [], | ||
isLocal: true, | ||
}, | ||
], | ||
fetchRoles: () => Promise.resolve([]), | ||
operation: { | ||
type: 'reset', | ||
user: { name: '[email protected]', roles: ['foo'] }, | ||
}, | ||
|
||
onStartCreate: () => undefined, | ||
onStartDelete: () => undefined, | ||
onStartEdit: () => undefined, | ||
onStartReset: () => undefined, | ||
onStartInviteCollaborators: () => undefined, | ||
onClose: () => undefined, | ||
onDelete: () => undefined, | ||
onCreate: () => undefined, | ||
onUpdate: () => undefined, | ||
onReset: () => undefined, | ||
onInviteCollaboratorsClose: () => undefined, | ||
InviteCollaborators: null, | ||
inviteCollaboratorsOpen: false, | ||
onEmailPasswordResetClose: () => undefined, | ||
EmailPasswordReset: null, | ||
showMauInfo: false, | ||
onDismissUsersMauNotice: () => null, | ||
usersAcl: defaultAcl, | ||
}; | ||
}); | ||
|
||
test('displays a disabled Create Users button if lacking permissions', async () => { | ||
const testProps = { | ||
...props, | ||
usersAcl: { | ||
...defaultAcl, | ||
edit: false, | ||
}, | ||
}; | ||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Users {...testProps} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(screen.getByTestId('create_new_users_button')).toBeDisabled(); | ||
}); | ||
|
||
test('edit and reset options not available in the menu', async () => { | ||
const testProps = { | ||
...props, | ||
usersAcl: { | ||
...defaultAcl, | ||
edit: false, | ||
}, | ||
}; | ||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Users {...testProps} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
const optionsButton = screen.getByRole('button', { name: /options/i }); | ||
fireEvent.click(optionsButton); | ||
const menuItems = screen.queryAllByRole('menuitem'); | ||
expect(menuItems).toHaveLength(1); | ||
expect(menuItems.some(item => item.textContent.includes('Delete'))).toBe( | ||
true | ||
); | ||
}); | ||
|
||
test('all options are available in the menu', async () => { | ||
const testProps = { | ||
...props, | ||
usersAcl: { | ||
read: true, | ||
list: true, | ||
edit: true, | ||
create: true, | ||
remove: true, | ||
}, | ||
}; | ||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Users {...testProps} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(screen.getByText('tester')).toBeInTheDocument(); | ||
const optionsButton = screen.getByRole('button', { name: /options/i }); | ||
fireEvent.click(optionsButton); | ||
const menuItems = screen.queryAllByRole('menuitem'); | ||
expect(menuItems).toHaveLength(3); | ||
expect(menuItems.some(item => item.textContent.includes('Delete'))).toBe( | ||
true | ||
); | ||
expect( | ||
menuItems.some(item => item.textContent.includes('Reset Auth')) | ||
).toBe(true); | ||
expect(menuItems.some(item => item.textContent.includes('Edit'))).toBe( | ||
true | ||
); | ||
}); | ||
|
||
test('delete is not available in menu', async () => { | ||
const testProps = { | ||
...props, | ||
usersAcl: { | ||
read: true, | ||
list: true, | ||
edit: true, | ||
create: true, | ||
remove: false, | ||
}, | ||
}; | ||
render( | ||
<MemoryRouter> | ||
<ContextProvider ctx={ctx}> | ||
<Users {...testProps} /> | ||
</ContextProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(screen.getByText('tester')).toBeInTheDocument(); | ||
const optionsButton = screen.getByRole('button', { name: /options/i }); | ||
fireEvent.click(optionsButton); | ||
const menuItems = screen.queryAllByRole('menuitem'); | ||
expect(menuItems).toHaveLength(2); | ||
expect( | ||
menuItems.every(item => item.textContent.includes('Delete')) | ||
).not.toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.