Skip to content

Commit

Permalink
fix: make api tokens ui consistent and remove check for deprecated en…
Browse files Browse the repository at this point in the history
…vs. (#3410)

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

This PR removes the check for deprecated environments when validating
api token environment.

Unifies global and project level tokens allow selection of deprecated
environments when creating an api token

Adds 'deprecated' to the EnvironmentSelector when appropriate

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
  • Loading branch information
andreas-unleash committed Mar 29, 2023
1 parent 0a6f1ec commit 9b6f5cd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 44 deletions.
Expand Up @@ -5,38 +5,29 @@ import {
StyledInputDescription,
StyledSelectInput,
} from '../ApiTokenForm.styles';
import {
IEnvironment,
IProjectEnvironment,
} from '../../../../../interfaces/environments';
import { useEnvironments } from 'hooks/api/getters/useEnvironments/useEnvironments';

interface IEnvironmentSelectorProps {
type: string;
environment?: string;
environments: IProjectEnvironment[] | IEnvironment[];
setEnvironment: React.Dispatch<React.SetStateAction<string | undefined>>;
}
export const EnvironmentSelector = ({
type,
environment,
setEnvironment,
environments,
}: IEnvironmentSelectorProps) => {
const isProjectEnv = (
environment: IEnvironment | IProjectEnvironment
): environment is IProjectEnvironment => {
return 'projectVisible' in environment;
};
const { environments } = useEnvironments();
const selectableEnvs =
type === TokenType.ADMIN
? [{ key: '*', label: 'ALL' }]
: environments.map(environment => ({
key: environment.name,
label: environment.name,
label: `${environment.name.concat(
!environment.enabled ? ' - deprecated' : ''
)}`,
title: environment.name,
disabled: isProjectEnv(environment)
? !environment.projectVisible
: !environment.enabled,
disabled: false,
}));

return (
Expand Down
Expand Up @@ -18,7 +18,6 @@ import { TokenInfo } from '../ApiTokenForm/TokenInfo/TokenInfo';
import { TokenTypeSelector } from '../ApiTokenForm/TokenTypeSelector/TokenTypeSelector';
import { ProjectSelector } from '../ApiTokenForm/ProjectSelector/ProjectSelector';
import { EnvironmentSelector } from '../ApiTokenForm/EnvironmentSelector/EnvironmentSelector';
import { useEnvironments } from '../../../../hooks/api/getters/useEnvironments/useEnvironments';

const pageTitle = 'Create API token';
interface ICreateApiTokenProps {
Expand All @@ -30,7 +29,6 @@ export const CreateApiToken = ({ modal = false }: ICreateApiTokenProps) => {
const navigate = useNavigate();
const [showConfirm, setShowConfirm] = useState(false);
const [token, setToken] = useState('');
const { environments } = useEnvironments();

const {
getApiTokenPayload,
Expand Down Expand Up @@ -127,7 +125,6 @@ export const CreateApiToken = ({ modal = false }: ICreateApiTokenProps) => {
/>
<EnvironmentSelector
type={type}
environments={environments}
environment={environment}
setEnvironment={setEnvironment}
/>
Expand Down
@@ -1,4 +1,4 @@
import { useMemo, useState } from 'react';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import FormTemplate from 'component/common/FormTemplate/FormTemplate';

Expand All @@ -21,9 +21,6 @@ import { TokenTypeSelector } from 'component/admin/apiToken/ApiTokenForm/TokenTy
import { ConfirmToken } from 'component/admin/apiToken/ConfirmToken/ConfirmToken';
import { useProjectApiTokens } from 'hooks/api/getters/useProjectApiTokens/useProjectApiTokens';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
import { useProjectEnvironments } from '../../../../../hooks/api/getters/useProjectEnvironments/useProjectEnvironments';
import { IProjectEnvironment } from '../../../../../interfaces/environments';
import useProject from '../../../../../hooks/api/getters/useProject/useProject';

const pageTitle = 'Create project API token';

Expand Down Expand Up @@ -52,19 +49,7 @@ export const CreateProjectApiTokenForm = () => {
useProjectApiTokensApi();
const { refetch: refetchProjectTokens } = useProjectApiTokens(projectId);
const { trackEvent } = usePlausibleTracker();
const { environments } = useProjectEnvironments(projectId);
const { project } = useProject(projectId);

const projectEnvironments = useMemo<IProjectEnvironment[]>(
() =>
environments.map(environment => ({
...environment,
projectVisible: project?.environments.includes(
environment.name
),
})),
[environments, project?.environments]
);
usePageTitle(pageTitle);

const PATH = `api/admin/project/${projectId}/api-tokens`;
Expand Down Expand Up @@ -146,7 +131,6 @@ export const CreateProjectApiTokenForm = () => {
type={type}
environment={environment}
setEnvironment={setEnvironment}
environments={projectEnvironments}
/>
</ApiTokenForm>
<ConfirmToken
Expand Down
6 changes: 0 additions & 6 deletions src/lib/types/models/api-token.ts
Expand Up @@ -128,10 +128,4 @@ export const validateApiTokenEnvironment = (
if (!selectedEnvironment) {
throw new BadDataError(`Environment=${environment} does not exist`);
}

if (!selectedEnvironment.enabled) {
throw new BadDataError(
'Client token cannot be scoped to disabled environments',
);
}
};
6 changes: 3 additions & 3 deletions src/test/e2e/api/admin/api-token.e2e.test.ts
Expand Up @@ -2,7 +2,7 @@ import { setupApp } from '../../helpers/test-helper';
import dbInit from '../../helpers/database-init';
import getLogger from '../../../fixtures/no-logger';
import { ALL, ApiTokenType } from '../../../../lib/types/models/api-token';
import { DEFAULT_ENV } from '../../../../lib/util/constants';
import { DEFAULT_ENV } from '../../../../lib/util';

let db;
let app;
Expand Down Expand Up @@ -332,7 +332,7 @@ test('client tokens cannot span all environments', async () => {
.expect(400);
});

test('should not create token for disabled environment', async () => {
test('should create token for disabled environment', async () => {
await db.stores.environmentStore.create({
name: 'disabledEnvironment',
type: 'production',
Expand All @@ -347,5 +347,5 @@ test('should not create token for disabled environment', async () => {
environment: 'disabledEnvironment',
})
.set('Content-Type', 'application/json')
.expect(400);
.expect(201);
});

0 comments on commit 9b6f5cd

Please sign in to comment.