|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { screen, waitFor } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { renderWithProviders } from '../test-utils'; |
| 5 | +import ApiKeys from '../../../platform/settings_api_keys/ApiKeys'; |
| 6 | + |
| 7 | +vi.mock('../../render.jsx'); |
| 8 | + |
| 9 | +vi.mock('@mui/material', async () => ({ |
| 10 | + ...(await vi.importActual('@mui/material')), |
| 11 | + useMediaQuery: vi.fn(() => true), |
| 12 | +})); |
| 13 | + |
| 14 | +function setupFetch(apiKeys = []) { |
| 15 | + global.fetch.mockImplementation((url) => { |
| 16 | + if (url.includes('/api_keys/')) { |
| 17 | + return Promise.resolve({ |
| 18 | + ok: true, |
| 19 | + json: () => Promise.resolve({ api_keys: apiKeys }), |
| 20 | + }); |
| 21 | + } |
| 22 | + if (url.includes('/status/jobs/')) { |
| 23 | + return Promise.resolve({ |
| 24 | + ok: true, |
| 25 | + json: () => Promise.resolve({ jobs: { deliver_contents: null } }), |
| 26 | + }); |
| 27 | + } |
| 28 | + if (url.includes('/organizations/')) { |
| 29 | + return Promise.resolve({ |
| 30 | + ok: true, |
| 31 | + json: () => Promise.resolve({ organizations: [] }), |
| 32 | + }); |
| 33 | + } |
| 34 | + return Promise.resolve({ ok: true, json: () => Promise.resolve({}) }); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +const localeMessages = { |
| 39 | + api_keys: 'API Keys', |
| 40 | + api_key_intro: 'Use API keys to access the API.', |
| 41 | + add_api_key: 'Add API Key', |
| 42 | + key: 'Key', |
| 43 | + created_by: 'Created By', |
| 44 | + created_at: 'Created At', |
| 45 | + actions: 'Actions', |
| 46 | + copy: 'Copy', |
| 47 | + confirm_deletion: 'Confirm Deletion', |
| 48 | + are_you_sure_delete_key: 'Are you sure you want to delete this API key?', |
| 49 | + cancel: 'Cancel', |
| 50 | + delete: 'Delete', |
| 51 | + organizations: 'Organizations', |
| 52 | + course_management: 'Courses', |
| 53 | + learners: 'Learners', |
| 54 | + content_delivery_tooltip: 'Content delivery', |
| 55 | + content_delivery_job: 'Content delivery', |
| 56 | + last_run: 'Last run:', |
| 57 | + never_run: 'Never run', |
| 58 | + upload_button_label: 'Upload', |
| 59 | + uploaded_image_alt: 'Uploaded image', |
| 60 | + remove_image: 'Remove', |
| 61 | +}; |
| 62 | + |
| 63 | +describe('ApiKeys', () => { |
| 64 | + beforeEach(() => { |
| 65 | + setupFetch(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('renders the intro text', async () => { |
| 69 | + renderWithProviders(<ApiKeys />, { |
| 70 | + appContext: { localeMessages, isPlatformAdmin: true }, |
| 71 | + }); |
| 72 | + await waitFor(() => |
| 73 | + expect(screen.getByText('Use API keys to access the API.')).toBeInTheDocument() |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders the Add API Key button', async () => { |
| 78 | + renderWithProviders(<ApiKeys />, { |
| 79 | + appContext: { localeMessages, isPlatformAdmin: true }, |
| 80 | + }); |
| 81 | + await waitFor(() => |
| 82 | + expect(screen.getByRole('button', { name: /Add API Key/ })).toBeInTheDocument() |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('shows the key table after loading keys', async () => { |
| 87 | + setupFetch([ |
| 88 | + { id: '1', key: 'abc123', created_by: 'admin', created_at: '2024-01-01', visible: false }, |
| 89 | + ]); |
| 90 | + renderWithProviders(<ApiKeys />, { |
| 91 | + appContext: { localeMessages, isPlatformAdmin: true }, |
| 92 | + }); |
| 93 | + await waitFor(() => expect(screen.getByText('admin')).toBeInTheDocument()); |
| 94 | + expect(screen.getByText('2024-01-01')).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('adds a new API key when Add API Key is clicked', async () => { |
| 98 | + const user = userEvent.setup(); |
| 99 | + global.fetch.mockImplementation((url, options) => { |
| 100 | + if (url.includes('/api_keys/') && options?.method === 'POST') { |
| 101 | + return Promise.resolve({ |
| 102 | + ok: true, |
| 103 | + json: () => Promise.resolve({ |
| 104 | + id: '2', key: 'new-key-xyz', created_by: 'admin', created_at: '2024-06-01', visible: false, |
| 105 | + }), |
| 106 | + }); |
| 107 | + } |
| 108 | + if (url.includes('/api_keys/')) { |
| 109 | + return Promise.resolve({ |
| 110 | + ok: true, |
| 111 | + json: () => Promise.resolve({ api_keys: [] }), |
| 112 | + }); |
| 113 | + } |
| 114 | + if (url.includes('/status/jobs/')) { |
| 115 | + return Promise.resolve({ ok: true, json: () => Promise.resolve({ jobs: { deliver_contents: null } }) }); |
| 116 | + } |
| 117 | + return Promise.resolve({ ok: true, json: () => Promise.resolve({ organizations: [] }) }); |
| 118 | + }); |
| 119 | + renderWithProviders(<ApiKeys />, { |
| 120 | + appContext: { localeMessages, isPlatformAdmin: true }, |
| 121 | + }); |
| 122 | + await waitFor(() => |
| 123 | + expect(screen.getByRole('button', { name: /Add API Key/ })).toBeInTheDocument() |
| 124 | + ); |
| 125 | + await user.click(screen.getByRole('button', { name: /Add API Key/ })); |
| 126 | + await waitFor(() => expect(screen.getByText('2024-06-01')).toBeInTheDocument()); |
| 127 | + }); |
| 128 | + |
| 129 | + it('shows delete confirmation dialog when delete icon is clicked', async () => { |
| 130 | + setupFetch([ |
| 131 | + { id: '1', key: 'abc123', created_by: 'admin', created_at: '2024-01-01', visible: false }, |
| 132 | + ]); |
| 133 | + const user = userEvent.setup(); |
| 134 | + renderWithProviders(<ApiKeys />, { |
| 135 | + appContext: { localeMessages, isPlatformAdmin: true }, |
| 136 | + }); |
| 137 | + await waitFor(() => expect(screen.getByText('admin')).toBeInTheDocument()); |
| 138 | + // Find the button containing the DeleteIcon svg |
| 139 | + const deleteButton = screen.getAllByRole('button').find( |
| 140 | + (btn) => btn.querySelector('[data-testid="DeleteIcon"]') |
| 141 | + ); |
| 142 | + await user.click(deleteButton); |
| 143 | + await waitFor(() => |
| 144 | + expect(screen.getByText('Confirm Deletion')).toBeInTheDocument() |
| 145 | + ); |
| 146 | + }); |
| 147 | +}); |
0 commit comments