Skip to content

Commit

Permalink
test(kms): kms dashboard tests (#13797)
Browse files Browse the repository at this point in the history
ref: MANAGER-15108

Signed-off-by: Romain Jamet <[email protected]>
  • Loading branch information
rjamet-ovh authored Nov 7, 2024
1 parent 20ed8b3 commit 47b3a75
Show file tree
Hide file tree
Showing 37 changed files with 596 additions and 225 deletions.
1 change: 1 addition & 0 deletions packages/manager/apps/key-management-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
1 change: 1 addition & 0 deletions packages/manager/apps/key-management-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"start:dev": "lerna exec --stream --scope='@ovh-ux/manager-key-management-service-app' --include-dependencies -- npm run dev --if-present",
"start:watch": "lerna exec --stream --parallel --scope='@ovh-ux/manager-key-management-service-app' --include-dependencies -- npm run dev:watch --if-present",
"test": "vitest run --reporter=verbose",
"test:coverage": "vitest run --coverage",
"test:e2e": "tsc && npx playwright test --headed",
"test:e2e:cii": "tsc && npx playwright test",
"test:e2e:script": "tsc && node ../../../../scripts/run-playwright.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Clipboard,
DataGridTextCell,
Links,
useServiceDetails,
} from '@ovh-ux/manager-react-components';
import {
ButtonType,
Expand All @@ -22,7 +23,6 @@ import { ServiceKeyStatus } from '../serviceKey/serviceKeyStatus/serviceKeyStatu
import useServiceKeyActionsList from '@/hooks/serviceKey/useServiceKeyActionsList';
import { useOkmsServiceKeyById } from '@/data/hooks/useOkmsServiceKeys';
import { useFormattedDate } from '@/hooks/useFormattedDate';
import { useKMSServiceInfos } from '@/data/hooks/useKMSServiceInfos';
import { OkmsServiceState } from '../layout-helpers/Dashboard/okmsServiceState/OkmsServiceState.component';
import { OkmsContext } from '@/pages/dashboard';

Expand Down Expand Up @@ -61,10 +61,10 @@ export const DatagridCellRegion = (props: OKMS) => {
);
};

export const DatagridCellStatus = (props: OKMS) => {
const { data: OkmsServiceInfos, isLoading, isError } = useKMSServiceInfos(
props.id,
);
export const DatagridCellStatus = (kms: OKMS) => {
const { data: OkmsServiceInfos, isLoading, isError } = useServiceDetails({
resourceName: kms.id,
});
if (isLoading) {
return <OsdsSpinner inline size={ODS_SPINNER_SIZE.sm} />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CredentialStatus } from '../credentialStatus/CredentialStatus.component
import { getDownloadCredentialParameters } from '@/utils/credential/credentialDownload';
import { OkmsContext } from '@/pages/dashboard';
import { ROUTES_URLS } from '@/routes/routes.constants';
import { kmsIamActions } from '@/utils/iam/iam.constants';

export const DatagridCredentialCellName = (credential: OkmsCredential) => {
const navigate = useNavigate();
Expand Down Expand Up @@ -102,7 +103,7 @@ export const DatagridCredentialCellActions = (credential: OkmsCredential) => {
id: 2,
label: t('key_management_service_credential_delete'),
color: ODS_THEME_COLOR_INTENT.error,
iamActions: ['okms:apiovh:credential/delete'],
iamActions: [kmsIamActions.credentialDelete],
urn: okms.iam.urn,
onClick: () =>
navigate(`${ROUTES_URLS.credentialDelete}/${credential.id}`),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ActionMenu, ActionMenuItem } from '@ovh-ux/manager-react-components';
import {
ActionMenu,
ActionMenuItem,
ServiceDetails,
} from '@ovh-ux/manager-react-components';
import { ODS_THEME_COLOR_INTENT } from '@ovhcloud/ods-common-theming';
import {
ODS_ICON_NAME,
Expand All @@ -18,7 +22,6 @@ import {
import { OKMS } from '@/types/okms.type';
import { useTerminateOKms } from '@/data/hooks/useTerminateOKms';
import { TerminateModal } from '@/components/Modal/terminate/TerminateModal.component';
import { KMSServiceInfos } from '@/types/okmsService.type';
import { OkmsServiceState } from '../okmsServiceState/OkmsServiceState.component';
import { Tile } from '@/components/dashboard/tile/tile.component';
import { TileItem } from '@/components/dashboard/tile-item/tileItem.component';
Expand All @@ -28,7 +31,7 @@ import { TileValueDate } from '@/components/dashboard/tile-value-date/tileValueD

type BillingInformationsTileProps = {
okmsData?: OKMS;
okmsService?: KMSServiceInfos;
okmsService?: ServiceDetails;
};

const BillingInformationsTile = ({
Expand Down Expand Up @@ -152,8 +155,8 @@ const BillingInformationsTile = ({
>
<span>
<OsdsChip color={ODS_TEXT_COLOR_INTENT.error} inline>
{okmsService?.billing.engagement
? okmsService.billing.engagement
{okmsService?.billing.engagement?.endRule?.strategy
? okmsService.billing.engagement.endRule.strategy
: t(
'key_management_service_dashboard_field_label_engagement_none',
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { OsdsButton, OsdsIcon, OsdsLink } from '@ovhcloud/ods-components/react';
import { Clipboard } from '@ovh-ux/manager-react-components';
import { Clipboard, ServiceDetails } from '@ovh-ux/manager-react-components';
import {
ODS_BUTTON_VARIANT,
ODS_ICON_NAME,
Expand All @@ -21,11 +21,10 @@ import { Tile } from '@/components/dashboard/tile/tile.component';
import { TileSeparator } from '@/components/dashboard/tile-separator/tileSeparator';
import { TileValue } from '@/components/dashboard/tile-value/tileValue.component';
import { TileItem } from '@/components/dashboard/tile-item/tileItem.component';
import { KMSServiceInfos } from '@/types/okmsService.type';

type InformationTileProps = {
okmsData?: OKMS;
okmsServiceInfos?: KMSServiceInfos;
okmsServiceInfos?: ServiceDetails;
};

const InformationsTile = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
OdsChipAttribute,
} from '@ovhcloud/ods-components';
import { OsdsChip } from '@ovhcloud/ods-components/react';
import { OkmsState } from '@/types/okmsService.type';
import { ResourceStatus } from '@ovh-ux/manager-react-components';

export type OkmsStateProps = Omit<OdsChipAttribute, 'color'> & {
state: OkmsState | string;
state: ResourceStatus;
};

export const OkmsServiceState = ({ state, ...props }: OkmsStateProps) => {
Expand All @@ -21,37 +21,37 @@ export const OkmsServiceState = ({ state, ...props }: OkmsStateProps) => {
let color: OdsChipAttribute['color'];

switch (state) {
case OkmsState.Active:
case 'active':
label = t(
'key_management_service_dashboard_dashboard_field_state_active',
);
color = ODS_TEXT_COLOR_INTENT.success;
break;
case OkmsState.Deleted:
case 'deleted':
label = t(
'key_management_service_dashboard_dashboard_field_state_deleted',
);
color = ODS_TEXT_COLOR_INTENT.error;
break;
case OkmsState.Suspended:
case 'suspended':
label = t(
'key_management_service_dashboard_dashboard_field_state_suspended',
);
color = ODS_TEXT_COLOR_INTENT.warning;
break;
case OkmsState.ToActivate:
case 'toActivate':
label = t(
'key_management_service_dashboard_dashboard_field_state_toActivate',
);
color = ODS_TEXT_COLOR_INTENT.info;
break;
case OkmsState.ToDelete:
case 'toDelete':
label = t(
'key_management_service_dashboard_dashboard_field_state_toDelete',
);
color = ODS_TEXT_COLOR_INTENT.default;
break;
case OkmsState.ToSuspend:
case 'toSuspend':
label = t(
'key_management_service_dashboard_dashboard_field_state_toSuspend',
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { apiClient } from '@ovh-ux/manager-core-api';
import { KMSServiceInfos } from '@/types/okmsService.type';
import { ServiceDetails } from '@ovh-ux/manager-react-components';

export type UpdateOkmsNameParams = {
serviceId: number;
Expand Down Expand Up @@ -30,7 +30,7 @@ export const getOkmsServiceId = async (okmsId: string) => {

export const getServiceInfos = async (okmsId: string) => {
const serviceId = await getOkmsServiceId(okmsId);
return apiClient.v6.get<KMSServiceInfos>(`/services/${serviceId.data[0]}`);
return apiClient.v6.get<ServiceDetails>(`/services/${serviceId.data[0]}`);
};

export type TerminateKmsParams = {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ApiError, ApiResponse } from '@ovh-ux/manager-core-api';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { useNotifications } from '@ovh-ux/manager-react-components';
import {
getServiceDetailsQueryKey,
useNotifications,
} from '@ovh-ux/manager-react-components';
import {
getOkmsResourceQueryKey,
getOkmsServicesResourceListQueryKey,
Expand All @@ -12,7 +15,6 @@ import {
getOkmsServiceId,
updateOkmsName,
} from '../api/okmsService';
import { getKMSServiceInfosQueryKey } from './useKMSServiceInfos';

export type UpdateOkmsParams = {
okmsId: string;
Expand Down Expand Up @@ -58,7 +60,7 @@ export const useUpdateOkmsName = ({
queryKey: getOkmsServicesResourceListQueryKey,
});
await queryClient.invalidateQueries({
queryKey: getKMSServiceInfosQueryKey(okmsId),
queryKey: getServiceDetailsQueryKey(okmsId),
});
await queryClient.invalidateQueries({
queryKey: getOkmsResourceQueryKey(okmsId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import {
OkmsServiceKeyState,
} from '@/types/okmsServiceKey.type';
import useServiceKeyActionsList from './useServiceKeyActionsList';
import { OKMS } from '@/types/okms.type';
import { okmsMock } from '@/mocks/kms/okms.mock';

vi.mock('react-i18next', () => ({
useTranslation: vi.fn(() => ({ t: vi.fn((key) => key) })),
}));

vi.mock('@ovh-ux/manager-react-components', () => ({
useNotifications: vi.fn(() => ({ addSuccess: vi.fn() })),
}));
vi.mock('@ovh-ux/manager-react-components', async (importOriginal) => {
const mod = await importOriginal<
typeof import('@ovh-ux/manager-react-components')
>();

return {
...mod,
useNotifications: vi.fn().mockReturnValue({
addError: vi.fn(),
addSuccess: vi.fn(),
}),
};
});

vi.mock('react-router-dom', () => ({
useNavigate: vi.fn(() => vi.fn()),
Expand All @@ -36,19 +46,6 @@ vi.mock('@/data/hooks/useUpdateOkmsServiceKey', () => ({
}));

describe('useServiceKeyActionsList', () => {
const okms: OKMS = {
iam: {
displayName: 'kms-1',
id: '1b4e7c8e-d1b8-4b46-a584-52c8b4b0225c',
urn: `urn:v1:eu:resource:okms:1b4e7c8e-d1b8-4b46-a584-52c8b4b0225c`,
},
id: '7f3a82ac-a8d8-4c2a-ab0c-f6e86ddf6a7c',
kmipEndpoint: 'eu-west-rbx.okms.ovh.net:1234',
region: 'EU_WEST_RBX',
restEndpoint: 'https://eu-west-rbx.okms.ovh.net',
swaggerEndpoint: '"https://swagger-eu-west-rbx.okms.ovh.net',
};

const commonKeyProps: Omit<OkmsAllServiceKeys, 'type' | 'state'> = {
id: 'testKeyId',
name: 'testKeyName',
Expand Down Expand Up @@ -188,7 +185,7 @@ describe('useServiceKeyActionsList', () => {
useCases.forEach(({ description, okmsKey, expectedActions }) => {
it(description, () => {
const { result } = renderHook(() =>
useServiceKeyActionsList(okms, okmsKey),
useServiceKeyActionsList(okmsMock[0], okmsKey),
);
expect(result.current).toEqual(
expect.arrayContaining(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
OkmsServiceKeyState,
} from '@/types/okmsServiceKey.type';
import { OKMS } from '@/types/okms.type';
import { kmsIamActions } from '@/utils/iam/iam.constants';

const useServiceKeyActionsList = (
okms: OKMS,
Expand Down Expand Up @@ -77,7 +78,7 @@ const useServiceKeyActionsList = (
`/${okms.id}/${ROUTES_URLS.keys}/${okmsKey?.id}/${ROUTES_URLS.serviceKeyDeactivate}`,
);
},
iamActions: ['okms:apiovh:serviceKey/deactivate'],
iamActions: [kmsIamActions.serviceKeyDeactivate],
urn: okms.iam.urn,
});
}
Expand All @@ -91,7 +92,7 @@ const useServiceKeyActionsList = (
color: ODS_THEME_COLOR_INTENT.primary,
disabled: updateIsPending,
onClick: () => updateKmsServiceKey({ state: OkmsServiceKeyState.active }),
iamActions: ['okms:apiovh:serviceKey/activate'],
iamActions: [kmsIamActions.serviceKeyActivate],
urn: okms.iam.urn,
});
}
Expand All @@ -106,7 +107,7 @@ const useServiceKeyActionsList = (
disabled:
okmsKey?.state === OkmsServiceKeyState.active || deleteIsPending,
onClick: () => deleteKmsServiceKey(),
iamActions: ['okms:apiovh:serviceKey/delete'],
iamActions: [kmsIamActions.serviceKeyDelete],
urn: okms.iam.urn,
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { PathParams } from 'msw';
import { Handler } from '../../../../../../../playwright-helpers';
import { credentialMock } from './credentials.mock';

export type GetCredentialsMockParams = {
isCredentialKO?: boolean;
nbCredential?: number;
};

const findCredentialByID = (params: PathParams) =>
credentialMock.find(({ id }) => id === params.id);

export const getCredentialsMock = ({
isCredentialKO,
nbCredential = credentialMock.length,
}: GetCredentialsMockParams): Handler[] => [
{
url: '/okms/resource/:okmsId/credential',
response: isCredentialKO
? {
status: 500,
data: {
message: 'credentials error',
},
}
: (_: unknown, params: PathParams) => findCredentialByID(params),
status: isCredentialKO ? 500 : 200,
api: 'v2',
},
{
url: '/okms/resource/:okmsId/credential/:credentialId',
response: isCredentialKO
? { message: 'credential error' }
: credentialMock.slice(0, nbCredential),
status: isCredentialKO ? 500 : 200,
api: 'v2',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
OkmsCredential,
OkmsCredentialStatus,
} from '@/types/okmsCredential.type';

export const credentialMock: OkmsCredential[] = [
{
createdAt: '2024-10-23T15:24:23Z',
expiredAt: '2024-10-23T15:24:23Z',
fromCSR: true,
id: '3d8a8d6a-e01a-4b0c-a1f1-f0cf67b4e88c',
identityURNs: ['urn:v1:eu:identity:account:ok1-ovh'],
name: 'credential-name',
status: OkmsCredentialStatus.ready,
description: 'credential description',
},
];
Loading

0 comments on commit 47b3a75

Please sign in to comment.