|  | 
|  | 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 | +}); | 
0 commit comments