|
| 1 | +/** |
| 2 | + * Tests for LoginForm — specifically the server-gated "Sign in with SSO" |
| 3 | + * button. The button must only render when the server's `/auth/config` |
| 4 | + * reports `features.sso`, mirroring how social providers are gated. Otherwise |
| 5 | + * a self-hosted / local deployment (where `@better-auth/sso` isn't wired) |
| 6 | + * shows a button whose `/sign-in/sso` route 404s, surfacing the misleading |
| 7 | + * "No SSO provider is configured for this email domain." only at click time. |
| 8 | + */ |
| 9 | + |
| 10 | +import React from 'react'; |
| 11 | +import { describe, it, expect, vi } from 'vitest'; |
| 12 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 13 | +import { AuthProvider } from '../AuthProvider'; |
| 14 | +import { LoginForm } from '../LoginForm'; |
| 15 | +import type { AuthClient, AuthPublicConfig } from '../types'; |
| 16 | + |
| 17 | +const SSO_BUTTON = { name: 'Sign in with SSO' } as const; |
| 18 | + |
| 19 | +function createMockClient( |
| 20 | + config: AuthPublicConfig, |
| 21 | + overrides: Partial<AuthClient> = {}, |
| 22 | +): AuthClient { |
| 23 | + return { |
| 24 | + signIn: vi.fn().mockResolvedValue({ |
| 25 | + user: { id: '1', name: 'Test User', email: 'test@test.com' }, |
| 26 | + session: { token: 'tok123' }, |
| 27 | + }), |
| 28 | + signUp: vi.fn().mockResolvedValue({ user: { id: '2' }, session: null, requiresVerification: false }), |
| 29 | + signOut: vi.fn().mockResolvedValue(undefined), |
| 30 | + getSession: vi.fn().mockResolvedValue(null), |
| 31 | + forgotPassword: vi.fn().mockResolvedValue(undefined), |
| 32 | + resetPassword: vi.fn().mockResolvedValue(undefined), |
| 33 | + updateUser: vi.fn().mockResolvedValue({ id: '1', name: 'Updated', email: 'test@test.com' }), |
| 34 | + getConfig: vi.fn().mockResolvedValue(config), |
| 35 | + ...overrides, |
| 36 | + } as unknown as AuthClient; |
| 37 | +} |
| 38 | + |
| 39 | +function renderLogin(client: AuthClient) { |
| 40 | + return render( |
| 41 | + <AuthProvider authUrl="/api/auth" client={client}> |
| 42 | + <LoginForm /> |
| 43 | + </AuthProvider>, |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +describe('LoginForm — server-gated SSO button', () => { |
| 48 | + it('hides the SSO button when the server does not report features.sso', async () => { |
| 49 | + renderLogin(createMockClient({ features: { sso: false } })); |
| 50 | + |
| 51 | + // Email/password form is always present… |
| 52 | + await screen.findByLabelText('Email'); |
| 53 | + // …but the SSO button must not render. |
| 54 | + expect(screen.queryByRole('button', SSO_BUTTON)).toBeNull(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('hides the SSO button when features is absent entirely (older server)', async () => { |
| 58 | + renderLogin(createMockClient({})); |
| 59 | + |
| 60 | + await screen.findByLabelText('Email'); |
| 61 | + expect(screen.queryByRole('button', SSO_BUTTON)).toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('shows the SSO button only when the server reports features.sso = true', async () => { |
| 65 | + renderLogin(createMockClient({ features: { sso: true } })); |
| 66 | + |
| 67 | + await waitFor(() => { |
| 68 | + expect(screen.getByRole('button', SSO_BUTTON)).toBeTruthy(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('keeps the button hidden when the config fetch fails (SSO is an enhancement)', async () => { |
| 73 | + renderLogin( |
| 74 | + createMockClient({}, { getConfig: vi.fn().mockRejectedValue(new Error('boom')) }), |
| 75 | + ); |
| 76 | + |
| 77 | + await screen.findByLabelText('Email'); |
| 78 | + expect(screen.queryByRole('button', SSO_BUTTON)).toBeNull(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments