Skip to content

Commit 43e669a

Browse files
test(kms): add tests for download public ca buttons
ref: #MANAGER-17309 Signed-off-by: Mathieu Mousnier <[email protected]>
1 parent 578002a commit 43e669a

File tree

3 files changed

+191
-2
lines changed

3 files changed

+191
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import React from 'react';
2+
import { describe, vi, expect, test, beforeEach, afterEach } from 'vitest';
3+
import { render, waitFor } from '@testing-library/react';
4+
import userEvent from '@testing-library/user-event';
5+
import { getOdsButtonByLabel } from '@ovh-ux/manager-core-test-utils';
6+
import {
7+
CertificateType,
8+
DownloadKmsPublicCaLink,
9+
} from './DownloadKmsPublicCaLink';
10+
import * as api from '@/data/api/okms';
11+
import { initiateTextFileDownload } from '@/utils/dom/download';
12+
import { OKMS } from '@/types/okms.type';
13+
14+
const addErrorMock = vi.fn();
15+
vi.mock('@ovh-ux/manager-react-components', () => ({
16+
useNotifications: () => ({
17+
addError: addErrorMock,
18+
}),
19+
}));
20+
21+
vi.mock('react-i18next', () => ({
22+
useTranslation: () => ({
23+
t: (translationKey: string) => translationKey,
24+
}),
25+
}));
26+
27+
vi.mock('@/utils/dom/download', () => ({
28+
initiateTextFileDownload: vi.fn(),
29+
}));
30+
31+
const mockOkms = {
32+
id: 'test-okms-id',
33+
region: 'test-region',
34+
} as OKMS;
35+
36+
const mockCertificates = {
37+
publicCA:
38+
'-----BEGIN CERTIFICATE-----\nMIIDDummyCertificate\n-----END CERTIFICATE-----',
39+
publicRsaCA:
40+
'-----BEGIN CERTIFICATE-----\nMIIDummyRsaCertificate\n-----END CERTIFICATE-----',
41+
};
42+
43+
const renderComponentAndGetLink = async ({
44+
type,
45+
label,
46+
}: {
47+
type: CertificateType;
48+
label: string;
49+
}) => {
50+
const { container } = render(
51+
<DownloadKmsPublicCaLink okms={mockOkms} type={type} />,
52+
);
53+
54+
const downloadLink = await getOdsButtonByLabel({
55+
container,
56+
label,
57+
isLink: true,
58+
});
59+
60+
return { downloadLink };
61+
};
62+
63+
describe('DownloadKmsPublicCaLink component tests suite', () => {
64+
beforeEach(() => {
65+
vi.spyOn(api, 'getOkmsPublicCa').mockResolvedValue(mockCertificates);
66+
});
67+
68+
afterEach(() => {
69+
vi.clearAllMocks();
70+
});
71+
72+
test('should render publicCa download link correctly', async () => {
73+
const { downloadLink } = await renderComponentAndGetLink({
74+
type: 'publicCa',
75+
label: 'key_management_service_dashboard_button_label_download_ca',
76+
});
77+
expect(downloadLink).toBeInTheDocument();
78+
});
79+
80+
test('should render publicRsaCa download link correctly', async () => {
81+
const { downloadLink } = await renderComponentAndGetLink({
82+
type: 'publicRsaCa',
83+
label: 'key_management_service_dashboard_button_label_download_rsa_ca',
84+
});
85+
expect(downloadLink).toBeInTheDocument();
86+
});
87+
88+
test('should download publicCa certificate when clicked', async () => {
89+
const { downloadLink } = await renderComponentAndGetLink({
90+
type: 'publicCa',
91+
label: 'key_management_service_dashboard_button_label_download_ca',
92+
});
93+
94+
const user = userEvent.setup();
95+
await waitFor(() => user.click(downloadLink));
96+
97+
await waitFor(() => {
98+
expect(api.getOkmsPublicCa).toHaveBeenCalledWith(mockOkms.id);
99+
});
100+
101+
await waitFor(() => {
102+
expect(initiateTextFileDownload).toHaveBeenCalledWith({
103+
text: mockCertificates.publicCA,
104+
filename: 'okms_test-region_public_ca.pem',
105+
});
106+
});
107+
});
108+
109+
test('should download publicRsaCa certificate when clicked', async () => {
110+
const { downloadLink } = await renderComponentAndGetLink({
111+
type: 'publicRsaCa',
112+
label: 'key_management_service_dashboard_button_label_download_rsa_ca',
113+
});
114+
115+
const user = userEvent.setup();
116+
await waitFor(() => user.click(downloadLink));
117+
118+
await waitFor(() => {
119+
expect(api.getOkmsPublicCa).toHaveBeenCalledWith(mockOkms.id);
120+
});
121+
122+
await waitFor(() => {
123+
expect(initiateTextFileDownload).toHaveBeenCalledWith({
124+
text: mockCertificates.publicRsaCA,
125+
filename: 'okms_test-region_public_rsa_ca.pem',
126+
});
127+
});
128+
});
129+
130+
test('should show error notification when download fails', async () => {
131+
// Override the successful mock with an error
132+
vi.spyOn(api, 'getOkmsPublicCa').mockRejectedValueOnce(
133+
new Error('API Error'),
134+
);
135+
136+
const { downloadLink } = await renderComponentAndGetLink({
137+
type: 'publicCa',
138+
label: 'key_management_service_dashboard_button_label_download_ca',
139+
});
140+
141+
const user = userEvent.setup();
142+
await waitFor(() => user.click(downloadLink));
143+
144+
await waitFor(() => {
145+
// Error notification should be shown
146+
expect(addErrorMock).toHaveBeenCalledWith(
147+
'key_management_service_dashboard_error_download_ca',
148+
);
149+
});
150+
});
151+
});

packages/manager/apps/key-management-service/src/components/layout-helpers/Dashboard/GeneralInformationsTiles/KmipTile.spec.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { describe, vi } from 'vitest';
33
import { screen, render, waitFor } from '@testing-library/react';
4+
import { getOdsButtonByLabel } from '@ovh-ux/manager-core-test-utils';
45
import { OKMS } from '@/types/okms.type';
56
import KmipTile from './KmipTile';
67
import { KMIP_ENPOINT_LABEL, KMIP_RSA_LABEL } from './KmipTile.constants';
@@ -19,6 +20,12 @@ vi.mock('react-router-dom', async () => {
1920
};
2021
});
2122

23+
vi.mock('react-i18next', () => ({
24+
useTranslation: () => ({
25+
t: (translationKey: string) => translationKey,
26+
}),
27+
}));
28+
2229
describe('KmipTile component tests suite', () => {
2330
const kms: OKMS = {
2431
id: 'id',
@@ -48,6 +55,14 @@ describe('KmipTile component tests suite', () => {
4855

4956
expect(screen.queryByText(KMIP_RSA_LABEL)).not.toBeInTheDocument();
5057
});
58+
59+
const downloadLink = await getOdsButtonByLabel({
60+
container,
61+
label: 'key_management_service_dashboard_button_label_download_ca',
62+
isLink: true,
63+
});
64+
65+
expect(downloadLink).toBeVisible();
5166
});
5267

5368
test('Should display KMIP tile with all kms data', async () => {
@@ -56,11 +71,19 @@ describe('KmipTile component tests suite', () => {
5671

5772
const { container } = renderComponent(kmsData);
5873

59-
await waitFor(() => {
74+
await waitFor(async () => {
6075
expect(screen.getByText(KMIP_RSA_LABEL)).toBeVisible();
6176
expect(
6277
container.querySelector(`ods-clipboard[value="${kmipRsaEndpoint}"]`),
6378
).toBeVisible();
79+
80+
const downloadLinkRsa = await getOdsButtonByLabel({
81+
container,
82+
label: 'key_management_service_dashboard_button_label_download_rsa_ca',
83+
isLink: true,
84+
});
85+
86+
expect(downloadLinkRsa).toBeVisible();
6487
});
6588
});
6689
});

packages/manager/apps/key-management-service/src/components/layout-helpers/Dashboard/GeneralInformationsTiles/RestApiTile.spec.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { describe, vi } from 'vitest';
33
import { screen, render, waitFor } from '@testing-library/react';
4+
import { getOdsButtonByLabel } from '@ovh-ux/manager-core-test-utils';
45
import { OKMS } from '@/types/okms.type';
56
import RestApiTile from './RestApiTile';
67
import { REST_ENDPOINT_LABEL, SWAGGER_UI_LABEL } from './RestApiTile.constants';
@@ -19,6 +20,12 @@ vi.mock('react-router-dom', async () => {
1920
};
2021
});
2122

23+
vi.mock('react-i18next', () => ({
24+
useTranslation: () => ({
25+
t: (translationKey: string) => translationKey,
26+
}),
27+
}));
28+
2229
describe('RestApiTile component tests suite', () => {
2330
const kms: OKMS = {
2431
id: 'id',
@@ -41,7 +48,7 @@ describe('RestApiTile component tests suite', () => {
4148
test('Should display REST API tile with only all mandatory data', async () => {
4249
const { container } = renderComponent(kms);
4350

44-
await waitFor(() => {
51+
await waitFor(async () => {
4552
expect(screen.getByText(REST_ENDPOINT_LABEL)).toBeVisible();
4653

4754
expect(
@@ -54,6 +61,14 @@ describe('RestApiTile component tests suite', () => {
5461
`ods-link[href="${kms.swaggerEndpoint}"][label="${kms.swaggerEndpoint}"]`,
5562
),
5663
).toBeVisible();
64+
65+
const downloadLink = await getOdsButtonByLabel({
66+
container,
67+
label: 'key_management_service_dashboard_button_label_download_ca',
68+
isLink: true,
69+
});
70+
71+
expect(downloadLink).toBeVisible();
5772
});
5873
});
5974
});

0 commit comments

Comments
 (0)