-
Notifications
You must be signed in to change notification settings - Fork 407
upcoming: [M3-7879] - Linode Create Refactor - Part 4 - Access #10308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@linode/manager": Upcoming Features | ||
| --- | ||
|
|
||
| Linode Create Refactor - Part 4 - Access ([#10308](https://github.com/linode/manager/pull/10308)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { waitFor } from '@testing-library/react'; | ||
| import React from 'react'; | ||
|
|
||
| import { profileFactory, sshKeyFactory } from 'src/factories'; | ||
| import { grantsFactory } from 'src/factories/grants'; | ||
| import { makeResourcePage } from 'src/mocks/serverHandlers'; | ||
| import { HttpResponse, http, server } from 'src/mocks/testServer'; | ||
| import { renderWithThemeAndHookFormContext } from 'src/utilities/testHelpers'; | ||
|
|
||
| import { Access } from './Access'; | ||
|
|
||
| describe('Access', () => { | ||
| it('should render a root password input', async () => { | ||
| const { findByLabelText } = renderWithThemeAndHookFormContext({ | ||
| component: <Access />, | ||
| }); | ||
|
|
||
| const rootPasswordInput = await findByLabelText('Root Password'); | ||
|
|
||
| expect(rootPasswordInput).toBeVisible(); | ||
| expect(rootPasswordInput).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('should render a SSH Keys heading', async () => { | ||
| const { getAllByText } = renderWithThemeAndHookFormContext({ | ||
| component: <Access />, | ||
| }); | ||
|
|
||
| const heading = getAllByText('SSH Keys')[0]; | ||
|
|
||
| expect(heading).toBeVisible(); | ||
| expect(heading.tagName).toBe('H2'); | ||
| }); | ||
|
|
||
| it('should render an "Add An SSH Key" button', async () => { | ||
| const { getByText } = renderWithThemeAndHookFormContext({ | ||
| component: <Access />, | ||
| }); | ||
|
|
||
| const addSSHKeyButton = getByText('Add an SSH Key'); | ||
|
|
||
| expect(addSSHKeyButton).toBeVisible(); | ||
| expect(addSSHKeyButton).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('should disable the password input if the user does not have permission to create linodes', async () => { | ||
| server.use( | ||
| http.get('*/v4/profile', () => { | ||
| return HttpResponse.json(profileFactory.build({ restricted: true })); | ||
| }), | ||
| http.get('*/v4/profile/sshkeys', () => { | ||
| return HttpResponse.json(makeResourcePage([])); | ||
| }), | ||
| http.get('*/v4/profile/grants', () => { | ||
| return HttpResponse.json( | ||
| grantsFactory.build({ global: { add_linodes: false } }) | ||
| ); | ||
| }) | ||
| ); | ||
|
|
||
| const { findByLabelText } = renderWithThemeAndHookFormContext({ | ||
| component: <Access />, | ||
| }); | ||
|
|
||
| const rootPasswordInput = await findByLabelText('Root Password'); | ||
|
|
||
| await waitFor(() => { | ||
| expect(rootPasswordInput).toBeDisabled(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should disable ssh key selection if the user does not have permission to create linodes', async () => { | ||
| const sshKeys = sshKeyFactory.buildList(3); | ||
| server.use( | ||
| http.get('*/v4/profile', () => { | ||
| return HttpResponse.json(profileFactory.build({ restricted: true })); | ||
| }), | ||
| http.get('*/v4/profile/sshkeys', () => { | ||
| return HttpResponse.json(makeResourcePage(sshKeys)); | ||
| }), | ||
| http.get('*/v4/profile/grants', () => { | ||
| return HttpResponse.json( | ||
| grantsFactory.build({ global: { add_linodes: false } }) | ||
| ); | ||
| }) | ||
| ); | ||
|
|
||
| const { findByText, getByRole } = renderWithThemeAndHookFormContext({ | ||
| component: <Access />, | ||
| }); | ||
|
|
||
| // Make sure the restricted user's SSH keys are loaded | ||
| for (const sshKey of sshKeys) { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| expect(await findByText(sshKey.label, { exact: false })).toBeVisible(); | ||
| } | ||
|
|
||
| await waitFor(() => { | ||
| expect(getByRole('checkbox')).toBeDisabled(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import React from 'react'; | ||
| import { Controller, useFormContext } from 'react-hook-form'; | ||
|
|
||
| import UserSSHKeyPanel from 'src/components/AccessPanel/UserSSHKeyPanel'; | ||
| import { Divider } from 'src/components/Divider'; | ||
| import { Paper } from 'src/components/Paper'; | ||
| import { Skeleton } from 'src/components/Skeleton'; | ||
| import { inputMaxWidth } from 'src/foundations/themes/light'; | ||
| import { useRestrictedGlobalGrantCheck } from 'src/hooks/useRestrictedGlobalGrantCheck'; | ||
|
|
||
| import type { CreateLinodeRequest } from '@linode/api-v4'; | ||
|
|
||
| const PasswordInput = React.lazy( | ||
| () => import('src/components/PasswordInput/PasswordInput') | ||
| ); | ||
|
|
||
| export const Access = () => { | ||
| const { control } = useFormContext<CreateLinodeRequest>(); | ||
|
|
||
| const isLinodeCreateRestricted = useRestrictedGlobalGrantCheck({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: We typically have been calling the variable for this hook
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could force that naming convention through the hook |
||
| globalGrantType: 'add_linodes', | ||
| }); | ||
|
|
||
| return ( | ||
| <Paper> | ||
| <React.Suspense | ||
| fallback={<Skeleton sx={{ height: '89px', maxWidth: inputMaxWidth }} />} | ||
| > | ||
| <Controller | ||
| render={({ field, fieldState }) => ( | ||
| <PasswordInput | ||
| autoComplete="off" | ||
| disabled={isLinodeCreateRestricted} | ||
| errorText={fieldState.error?.message} | ||
| label="Root Password" | ||
| name="password" | ||
| noMarginTop | ||
| onChange={field.onChange} | ||
| placeholder="Enter a password." | ||
| value={field.value ?? ''} | ||
| /> | ||
| )} | ||
| control={control} | ||
| name="root_pass" | ||
| /> | ||
| </React.Suspense> | ||
| <Divider spacingBottom={20} spacingTop={24} /> | ||
| <Controller | ||
| render={({ field }) => ( | ||
| <UserSSHKeyPanel | ||
| authorizedUsers={field.value ?? []} | ||
| disabled={isLinodeCreateRestricted} | ||
| setAuthorizedUsers={field.onChange} | ||
| /> | ||
| )} | ||
| control={control} | ||
| name="authorized_users" | ||
| /> | ||
| </Paper> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a newer, cleaner, simpler version of
packages/manager/src/components/AccessPanel/AccessPanel.tsx