-
Couldn't load subscription status.
- Fork 110
feat(kms): add buttons for downloading public CA certificates #16410
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
399d83f
refactor(kms): cleanup okms fetch files from unused code
mathieumousnier 8a84aa9
refactor(kms): rename useOKMS & useOrderCatalogOKMS
mathieumousnier 6cc20d1
feat(kms): add download PEM buttons
mathieumousnier bf085db
test(kms): add tests for download public ca buttons
mathieumousnier bd7ca4c
fix(i18n): add missing translations [CDS 3924]
mathieumousnier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
151 changes: 151 additions & 0 deletions
151
...service/src/components/dashboard/downloadKmsPublicCaLink/DownloadKmsPublicCaLink.spec.tsx
This file contains hidden or 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,151 @@ | ||
| import React from 'react'; | ||
| import { describe, vi, expect, test, beforeEach, afterEach } from 'vitest'; | ||
| import { render, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { getOdsButtonByLabel } from '@ovh-ux/manager-core-test-utils'; | ||
| import { | ||
| CertificateType, | ||
| DownloadKmsPublicCaLink, | ||
| } from './DownloadKmsPublicCaLink'; | ||
| import * as api from '@/data/api/okms'; | ||
| import { initiateTextFileDownload } from '@/utils/dom/download'; | ||
| import { OKMS } from '@/types/okms.type'; | ||
|
|
||
| const addErrorMock = vi.fn(); | ||
| vi.mock('@ovh-ux/manager-react-components', () => ({ | ||
| useNotifications: () => ({ | ||
| addError: addErrorMock, | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock('react-i18next', () => ({ | ||
| useTranslation: () => ({ | ||
| t: (translationKey: string) => translationKey, | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock('@/utils/dom/download', () => ({ | ||
| initiateTextFileDownload: vi.fn(), | ||
| })); | ||
|
|
||
| const mockOkms = { | ||
| id: 'test-okms-id', | ||
| region: 'test-region', | ||
| } as OKMS; | ||
|
|
||
| const mockCertificates = { | ||
| publicCA: | ||
| '-----BEGIN CERTIFICATE-----\nMIIDDummyCertificate\n-----END CERTIFICATE-----', | ||
| publicRsaCA: | ||
| '-----BEGIN CERTIFICATE-----\nMIIDummyRsaCertificate\n-----END CERTIFICATE-----', | ||
| }; | ||
|
|
||
| const renderComponentAndGetLink = async ({ | ||
| type, | ||
| label, | ||
| }: { | ||
| type: CertificateType; | ||
| label: string; | ||
| }) => { | ||
| const { container } = render( | ||
| <DownloadKmsPublicCaLink okms={mockOkms} type={type} />, | ||
| ); | ||
|
|
||
| const downloadLink = await getOdsButtonByLabel({ | ||
| container, | ||
| label, | ||
| isLink: true, | ||
| }); | ||
|
|
||
| return { downloadLink }; | ||
| }; | ||
|
|
||
| describe('DownloadKmsPublicCaLink component tests suite', () => { | ||
| beforeEach(() => { | ||
| vi.spyOn(api, 'getOkmsPublicCa').mockResolvedValue(mockCertificates); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('should render publicCa download link correctly', async () => { | ||
| const { downloadLink } = await renderComponentAndGetLink({ | ||
| type: 'publicCa', | ||
| label: 'key_management_service_dashboard_button_label_download_ca', | ||
| }); | ||
| expect(downloadLink).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('should render publicRsaCa download link correctly', async () => { | ||
| const { downloadLink } = await renderComponentAndGetLink({ | ||
| type: 'publicRsaCa', | ||
| label: 'key_management_service_dashboard_button_label_download_rsa_ca', | ||
| }); | ||
| expect(downloadLink).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('should download publicCa certificate when clicked', async () => { | ||
| const { downloadLink } = await renderComponentAndGetLink({ | ||
| type: 'publicCa', | ||
| label: 'key_management_service_dashboard_button_label_download_ca', | ||
| }); | ||
|
|
||
| const user = userEvent.setup(); | ||
| await waitFor(() => user.click(downloadLink)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(api.getOkmsPublicCa).toHaveBeenCalledWith(mockOkms.id); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(initiateTextFileDownload).toHaveBeenCalledWith({ | ||
| text: mockCertificates.publicCA, | ||
| filename: 'okms_test-region_public_ca.pem', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test('should download publicRsaCa certificate when clicked', async () => { | ||
| const { downloadLink } = await renderComponentAndGetLink({ | ||
| type: 'publicRsaCa', | ||
| label: 'key_management_service_dashboard_button_label_download_rsa_ca', | ||
| }); | ||
|
|
||
| const user = userEvent.setup(); | ||
| await waitFor(() => user.click(downloadLink)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(api.getOkmsPublicCa).toHaveBeenCalledWith(mockOkms.id); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(initiateTextFileDownload).toHaveBeenCalledWith({ | ||
| text: mockCertificates.publicRsaCA, | ||
| filename: 'okms_test-region_public_rsa_ca.pem', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test('should show error notification when download fails', async () => { | ||
| // Override the successful mock with an error | ||
| vi.spyOn(api, 'getOkmsPublicCa').mockRejectedValueOnce( | ||
| new Error('API Error'), | ||
| ); | ||
|
|
||
| const { downloadLink } = await renderComponentAndGetLink({ | ||
| type: 'publicCa', | ||
| label: 'key_management_service_dashboard_button_label_download_ca', | ||
| }); | ||
|
|
||
| const user = userEvent.setup(); | ||
| await waitFor(() => user.click(downloadLink)); | ||
|
|
||
| await waitFor(() => { | ||
| // Error notification should be shown | ||
| expect(addErrorMock).toHaveBeenCalledWith( | ||
| 'key_management_service_dashboard_error_download_ca', | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
82 changes: 82 additions & 0 deletions
82
...ment-service/src/components/dashboard/downloadKmsPublicCaLink/DownloadKmsPublicCaLink.tsx
This file contains hidden or 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,82 @@ | ||
| import React, { useMemo, useState } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { useNotifications } from '@ovh-ux/manager-react-components'; | ||
| import { OdsLink, OdsSpinner } from '@ovhcloud/ods-components/react'; | ||
| import { ODS_BUTTON_COLOR } from '@ovhcloud/ods-components'; | ||
| import { getOkmsPublicCa } from '@/data/api/okms'; | ||
| import { initiateTextFileDownload } from '@/utils/dom/download'; | ||
| import { | ||
| PUBLIC_CA_FILENAME, | ||
| PUBLIC_RSA_CA_FILENAME, | ||
| } from './downloadKmsPublicCaLink.constants'; | ||
| import { OKMS } from '@/types/okms.type'; | ||
|
|
||
| export type CertificateType = 'publicCa' | 'publicRsaCa'; | ||
|
|
||
| type DownloadCaButtonProps = { | ||
| okms: OKMS; | ||
| type: CertificateType; | ||
| }; | ||
|
|
||
| export const DownloadKmsPublicCaLink = ({ | ||
| okms, | ||
| type, | ||
| }: DownloadCaButtonProps) => { | ||
| const { t } = useTranslation('key-management-service/dashboard'); | ||
| const [loading, setLoading] = useState(false); | ||
| const { addError } = useNotifications(); | ||
|
|
||
| const resources = useMemo( | ||
| () => ({ | ||
| publicCa: { | ||
| label: t('key_management_service_dashboard_button_label_download_ca'), | ||
| filename: PUBLIC_CA_FILENAME, | ||
| }, | ||
| publicRsaCa: { | ||
| label: t( | ||
| 'key_management_service_dashboard_button_label_download_rsa_ca', | ||
| ), | ||
| filename: PUBLIC_RSA_CA_FILENAME, | ||
| }, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| const handleDownloadCa = async ( | ||
mathieumousnier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| event: React.MouseEvent<HTMLOdsLinkElement, MouseEvent>, | ||
| ) => { | ||
| event.preventDefault(); | ||
|
|
||
| try { | ||
mathieumousnier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| setLoading(true); | ||
| const certificates = await getOkmsPublicCa(okms.id); | ||
|
|
||
| const content: Record<CertificateType, string> = { | ||
| publicCa: certificates.publicCA, | ||
| publicRsaCa: certificates.publicRsaCA, | ||
| }; | ||
|
|
||
| initiateTextFileDownload({ | ||
| text: content[type], | ||
| filename: resources[type].filename.replace('{region}', okms.region), | ||
| }); | ||
| } catch { | ||
| addError(t('key_management_service_dashboard_error_download_ca')); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex items-center gap-2"> | ||
| <OdsLink | ||
| href="#" | ||
mathieumousnier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| color={ODS_BUTTON_COLOR.primary} | ||
| onClick={handleDownloadCa} | ||
| label={resources[type].label} | ||
| isDisabled={loading} | ||
| /> | ||
| {loading && <OdsSpinner size="xs" />} | ||
| </div> | ||
| ); | ||
| }; | ||
2 changes: 2 additions & 0 deletions
2
...ce/src/components/dashboard/downloadKmsPublicCaLink/downloadKmsPublicCaLink.constants.tsx
This file contains hidden or 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,2 @@ | ||
| export const PUBLIC_CA_FILENAME = 'okms_{region}_public_ca.pem'; | ||
| export const PUBLIC_RSA_CA_FILENAME = 'okms_{region}_public_rsa_ca.pem'; |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.