|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { MemoryRouter } from "react-router-dom"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { Login } from "./Login"; |
| 5 | +import { Register } from "./Register"; |
| 6 | + |
| 7 | +const mockUseAuth = vi.fn(); |
| 8 | +const mockNavigate = vi.fn(); |
| 9 | + |
| 10 | +vi.mock("../context/AuthContext", () => ({ |
| 11 | + useAuth: () => mockUseAuth(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../api", () => ({ |
| 15 | + startOidcSignIn: vi.fn(), |
| 16 | + api: { |
| 17 | + post: vi.fn(), |
| 18 | + }, |
| 19 | + isAxiosError: vi.fn(() => false), |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("../components/Logo", () => ({ |
| 23 | + Logo: () => <div data-testid="logo" />, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("react-router-dom", async () => { |
| 27 | + const actual = await vi.importActual<typeof import("react-router-dom")>("react-router-dom"); |
| 28 | + return { |
| 29 | + ...actual, |
| 30 | + useNavigate: () => mockNavigate, |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +const baseAuthState = { |
| 35 | + login: vi.fn(), |
| 36 | + logout: vi.fn(), |
| 37 | + register: vi.fn(), |
| 38 | + authEnabled: true, |
| 39 | + registrationEnabled: true, |
| 40 | + authStatusError: null, |
| 41 | + retryAuthStatus: vi.fn(), |
| 42 | + oidcEnabled: false, |
| 43 | + oidcEnforced: false, |
| 44 | + oidcProvider: "OIDC", |
| 45 | + bootstrapRequired: false, |
| 46 | + authOnboardingRequired: false, |
| 47 | + isAuthenticated: false, |
| 48 | + loading: false, |
| 49 | + user: null, |
| 50 | +}; |
| 51 | + |
| 52 | +describe("auth page registration policy", () => { |
| 53 | + beforeEach(() => { |
| 54 | + vi.clearAllMocks(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("hides the register link on the login page when registration is disabled", () => { |
| 58 | + mockUseAuth.mockReturnValue({ |
| 59 | + ...baseAuthState, |
| 60 | + registrationEnabled: false, |
| 61 | + }); |
| 62 | + |
| 63 | + render( |
| 64 | + <MemoryRouter initialEntries={["/login"]}> |
| 65 | + <Login /> |
| 66 | + </MemoryRouter> |
| 67 | + ); |
| 68 | + |
| 69 | + expect(screen.queryByRole("link", { name: /create a new account/i })).not.toBeInTheDocument(); |
| 70 | + expect(screen.getByText(/sign in with an existing account/i)).toBeInTheDocument(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("redirects away from /register when registration is disabled outside bootstrap flow", () => { |
| 74 | + mockUseAuth.mockReturnValue({ |
| 75 | + ...baseAuthState, |
| 76 | + registrationEnabled: false, |
| 77 | + }); |
| 78 | + |
| 79 | + render( |
| 80 | + <MemoryRouter initialEntries={["/register"]}> |
| 81 | + <Register /> |
| 82 | + </MemoryRouter> |
| 83 | + ); |
| 84 | + |
| 85 | + expect(mockNavigate).toHaveBeenCalledWith("/login", { replace: true }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments