-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add input for api token in playground (#5130)
Adds a token input in playground. In the case of tokens that span multiple projects ie `[]:development.etc` it will look into the token definitions to find the token and get the projects Otherwise it will try to parse the project and environment from the token itself (without checking for it being a valid token) Also, does not support admin tokens `*:*.etc` Closes # [1-1507](https://linear.app/unleash/issue/1-1507/create-a-token-input-in-the-playground-form) <img width="1661" alt="Screenshot 2023-10-23 at 16 38 11" src="https://github.com/Unleash/unleash/assets/104830839/f2d4fb6e-962f-4cc1-b5e4-817fd2de18ff"> <img width="1673" alt="Screenshot 2023-10-23 at 16 38 33" src="https://github.com/Unleash/unleash/assets/104830839/27645955-d651-41e6-be02-4381c4f00551"> <img width="1377" alt="Screenshot 2023-10-25 at 17 06 43" src="https://github.com/Unleash/unleash/assets/104830839/c7638366-3634-4521-af65-4f68a4f3b330"> --------- Signed-off-by: andreas-unleash <[email protected]>
- Loading branch information
1 parent
de540e0
commit 8e3863a
Showing
6 changed files
with
411 additions
and
45 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
150 changes: 150 additions & 0 deletions
150
...yground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.test.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,150 @@ | ||
import { testServerRoute, testServerSetup } from 'utils/testServer'; | ||
import { render } from 'utils/testRenderer'; | ||
import { fireEvent, screen, within } from '@testing-library/react'; | ||
import { PlaygroundConnectionFieldset } from './PlaygroundConnectionFieldset'; | ||
import { useState } from 'react'; | ||
|
||
const server = testServerSetup(); | ||
|
||
beforeEach(() => { | ||
testServerRoute(server, '/api/admin/ui-config', { | ||
versionInfo: { | ||
current: { oss: 'version', enterprise: 'version' }, | ||
}, | ||
flags: { | ||
playgroundImprovements: true, | ||
}, | ||
}); | ||
testServerRoute( | ||
server, | ||
'/api/admin/projects', | ||
{ | ||
projects: [ | ||
{ | ||
id: 'default', | ||
name: 'Default', | ||
}, | ||
{ | ||
id: 'MyProject', | ||
name: 'MyProject', | ||
}, | ||
], | ||
}, | ||
'get', | ||
200, | ||
); | ||
testServerRoute( | ||
server, | ||
'/api/admin/api-tokens', | ||
{ | ||
tokens: [ | ||
{ | ||
secret: '[]:development.964a287e1b728cb5f4f3e0120df92cb5', | ||
projects: ['default', 'MyProject'], | ||
}, | ||
], | ||
}, | ||
'get', | ||
200, | ||
); | ||
}); | ||
|
||
const Component = () => { | ||
const [environments, setEnvironments] = useState<string[]>([]); | ||
const [projects, setProjects] = useState<string[]>([]); | ||
const [token, setToken] = useState<string>(); | ||
|
||
const availableEnvironments = ['development', 'production']; | ||
|
||
return ( | ||
<PlaygroundConnectionFieldset | ||
environments={environments} | ||
projects={projects} | ||
token={token} | ||
setToken={setToken} | ||
setEnvironments={setEnvironments} | ||
setProjects={setProjects} | ||
availableEnvironments={availableEnvironments} | ||
/> | ||
); | ||
}; | ||
|
||
test('should parse project and environment from token input', async () => { | ||
render(<Component />); | ||
|
||
const tokenInput = await screen.findByLabelText('Api token'); | ||
fireEvent.change(tokenInput, { | ||
target: { | ||
value: 'default:development.964a287e1b728cb5f4f3e0120df92cb5', | ||
}, | ||
}); | ||
|
||
const projectAutocomplete = await screen.findByTestId( | ||
'PLAYGROUND_PROJECT_SELECT', | ||
); | ||
const projectInput = within(projectAutocomplete).getByRole('combobox'); | ||
|
||
const environmentAutocomplete = await screen.findByTestId( | ||
'PLAYGROUND_ENVIRONMENT_SELECT', | ||
); | ||
const environmentInput = within(environmentAutocomplete).getByRole( | ||
'combobox', | ||
); | ||
|
||
expect(projectInput).toBeDisabled(); | ||
expect(environmentInput).toBeDisabled(); | ||
await within(projectAutocomplete).findByText('Default'); | ||
await within(environmentAutocomplete).findByText('development'); | ||
}); | ||
|
||
test('should load projects from token definition if project is []', async () => { | ||
render(<Component />); | ||
|
||
const tokenInput = await screen.findByLabelText('Api token'); | ||
fireEvent.change(tokenInput, { | ||
target: { value: '[]:development.964a287e1b728cb5f4f3e0120df92cb5' }, | ||
}); | ||
|
||
const projectAutocomplete = await screen.findByTestId( | ||
'PLAYGROUND_PROJECT_SELECT', | ||
); | ||
const projectInput = within(projectAutocomplete).getByRole('combobox'); | ||
|
||
const environmentAutocomplete = await screen.findByTestId( | ||
'PLAYGROUND_ENVIRONMENT_SELECT', | ||
); | ||
const environmentInput = within(environmentAutocomplete).getByRole( | ||
'combobox', | ||
); | ||
|
||
expect(projectInput).toBeDisabled(); | ||
expect(environmentInput).toBeDisabled(); | ||
await within(projectAutocomplete).findByText('Default'); | ||
await within(projectAutocomplete).findByText('MyProject'); | ||
await within(environmentAutocomplete).findByText('development'); | ||
}); | ||
|
||
test('should show an error when admin token', async () => { | ||
render(<Component />); | ||
|
||
const tokenInput = await screen.findByLabelText('Api token'); | ||
fireEvent.change(tokenInput, { | ||
target: { value: '*:*.964a287e1b728cb5f4f3e0120df92cb5' }, | ||
}); | ||
|
||
const projectAutocomplete = await screen.findByTestId( | ||
'PLAYGROUND_PROJECT_SELECT', | ||
); | ||
const projectInput = within(projectAutocomplete).getByRole('combobox'); | ||
|
||
const environmentAutocomplete = await screen.findByTestId( | ||
'PLAYGROUND_ENVIRONMENT_SELECT', | ||
); | ||
const environmentInput = within(environmentAutocomplete).getByRole( | ||
'combobox', | ||
); | ||
|
||
expect(projectInput).toBeDisabled(); | ||
expect(environmentInput).toBeDisabled(); | ||
await screen.findByText('Admin tokens are not supported in the playground'); | ||
}); |
Oops, something went wrong.