|
| 1 | +/** |
| 2 | + * Copyright 2022 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +// biome-ignore-all lint/style/noNamespaceImport: test file |
| 13 | + |
| 14 | +import { render, screen } from '@testing-library/react'; |
| 15 | +import { UserInformationCard } from 'components/pages/roles/user-information-card'; |
| 16 | +import { isServerless } from 'config'; |
| 17 | + |
| 18 | +vi.mock('config', async (importOriginal) => { |
| 19 | + const actual = await importOriginal<typeof import('config')>(); |
| 20 | + return { |
| 21 | + ...actual, |
| 22 | + isServerless: vi.fn(() => false), |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +const mockedIsServerless = vi.mocked(isServerless); |
| 27 | + |
| 28 | +/** |
| 29 | + * These tests verify the serverless guard on the password change UI (UX-963). |
| 30 | + * |
| 31 | + * In both user-details.tsx and acl-list.tsx, the password change controls are |
| 32 | + * gated by `api.isAdminApiConfigured && !isServerless()`. When isServerless() |
| 33 | + * returns true, onEditPassword is undefined and the UI is hidden. |
| 34 | + * |
| 35 | + * We test the UserInformationCard component directly, which renders the "Edit" |
| 36 | + * password button only when the onEditPassword callback is provided. This |
| 37 | + * mirrors the guard logic in the parent components. |
| 38 | + */ |
| 39 | +describe('UX-963: password change hidden in serverless mode', () => { |
| 40 | + afterEach(() => { |
| 41 | + vi.restoreAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('shows the Edit password button when not in serverless mode (onEditPassword provided)', () => { |
| 45 | + mockedIsServerless.mockReturnValue(false); |
| 46 | + |
| 47 | + const isAdminApiConfigured = true; |
| 48 | + const onEditPassword = isAdminApiConfigured && !isServerless() ? vi.fn() : undefined; |
| 49 | + |
| 50 | + render(<UserInformationCard onEditPassword={onEditPassword} username="test-user" />); |
| 51 | + |
| 52 | + expect(screen.getByRole('button', { name: 'Edit' })).toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('hides the Edit password button when in serverless mode (onEditPassword is undefined)', () => { |
| 56 | + mockedIsServerless.mockReturnValue(true); |
| 57 | + |
| 58 | + const isAdminApiConfigured = true; |
| 59 | + const onEditPassword = isAdminApiConfigured && !isServerless() ? vi.fn() : undefined; |
| 60 | + |
| 61 | + render(<UserInformationCard onEditPassword={onEditPassword} username="test-user" />); |
| 62 | + |
| 63 | + expect(screen.queryByRole('button', { name: 'Edit' })).not.toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('hides the Edit password button when admin API is not configured', () => { |
| 67 | + mockedIsServerless.mockReturnValue(false); |
| 68 | + |
| 69 | + const isAdminApiConfigured = false; |
| 70 | + const onEditPassword = isAdminApiConfigured && !isServerless() ? vi.fn() : undefined; |
| 71 | + |
| 72 | + render(<UserInformationCard onEditPassword={onEditPassword} username="test-user" />); |
| 73 | + |
| 74 | + expect(screen.queryByRole('button', { name: 'Edit' })).not.toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('evaluates the guard condition correctly for all combinations', () => { |
| 78 | + // This directly tests the boolean logic used in user-details.tsx and acl-list.tsx: |
| 79 | + // api.isAdminApiConfigured && !isServerless() |
| 80 | + const cases = [ |
| 81 | + { adminApi: true, serverless: false, expected: true }, |
| 82 | + { adminApi: true, serverless: true, expected: false }, |
| 83 | + { adminApi: false, serverless: false, expected: false }, |
| 84 | + { adminApi: false, serverless: true, expected: false }, |
| 85 | + ]; |
| 86 | + |
| 87 | + for (const { adminApi, serverless, expected } of cases) { |
| 88 | + mockedIsServerless.mockReturnValue(serverless); |
| 89 | + const result = adminApi && !isServerless(); |
| 90 | + expect(result).toBe(expected); |
| 91 | + } |
| 92 | + }); |
| 93 | +}); |
0 commit comments