|
| 1 | +/** |
| 2 | + * #3238: autofill/copy-paste smuggle leading/trailing whitespace into the |
| 3 | + * email field; the server then rejects it ("Invalid email"). RegisterForm and |
| 4 | + * ForgotPasswordForm must trim the email before sending, matching LoginForm |
| 5 | + * (whose trim tests live in LoginForm.test.tsx). |
| 6 | + */ |
| 7 | + |
| 8 | +import React from 'react'; |
| 9 | +import { describe, it, expect, vi } from 'vitest'; |
| 10 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 11 | +import { AuthProvider } from '../AuthProvider'; |
| 12 | +import { RegisterForm } from '../RegisterForm'; |
| 13 | +import { ForgotPasswordForm } from '../ForgotPasswordForm'; |
| 14 | +import type { AuthClient, AuthPublicConfig } from '../types'; |
| 15 | + |
| 16 | +function createMockClient( |
| 17 | + config: AuthPublicConfig, |
| 18 | + overrides: Partial<AuthClient> = {}, |
| 19 | +): AuthClient { |
| 20 | + return { |
| 21 | + signIn: vi.fn().mockResolvedValue({ user: { id: '1' }, session: { token: 't' } }), |
| 22 | + signUp: vi.fn().mockResolvedValue({ user: { id: '2' }, session: null, requiresVerification: false }), |
| 23 | + signOut: vi.fn().mockResolvedValue(undefined), |
| 24 | + getSession: vi.fn().mockResolvedValue(null), |
| 25 | + forgotPassword: vi.fn().mockResolvedValue(undefined), |
| 26 | + resetPassword: vi.fn().mockResolvedValue(undefined), |
| 27 | + getConfig: vi.fn().mockResolvedValue(config), |
| 28 | + ...overrides, |
| 29 | + } as unknown as AuthClient; |
| 30 | +} |
| 31 | + |
| 32 | +function renderWithAuth(client: AuthClient, ui: React.ReactElement) { |
| 33 | + return render( |
| 34 | + <AuthProvider authUrl="/api/auth" client={client}> |
| 35 | + {ui} |
| 36 | + </AuthProvider>, |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +describe('RegisterForm — email whitespace trimming (#3238)', () => { |
| 41 | + it('trims whitespace around the email before signUp', async () => { |
| 42 | + const signUp = vi |
| 43 | + .fn() |
| 44 | + .mockResolvedValue({ user: { id: '2' }, session: null, requiresVerification: false }); |
| 45 | + renderWithAuth(createMockClient({}, { signUp }), <RegisterForm />); |
| 46 | + |
| 47 | + fireEvent.change(await screen.findByLabelText('Name'), { target: { value: 'Test User' } }); |
| 48 | + fireEvent.change(screen.getByLabelText('Email'), { |
| 49 | + target: { value: ' new@objectos.ai ' }, |
| 50 | + }); |
| 51 | + fireEvent.change(screen.getByLabelText('Password'), { target: { value: 'password123' } }); |
| 52 | + fireEvent.change(screen.getByLabelText('Confirm Password'), { |
| 53 | + target: { value: 'password123' }, |
| 54 | + }); |
| 55 | + fireEvent.click(screen.getByRole('button', { name: 'Create Account' })); |
| 56 | + |
| 57 | + await waitFor(() => |
| 58 | + expect(signUp).toHaveBeenCalledWith({ |
| 59 | + name: 'Test User', |
| 60 | + email: 'new@objectos.ai', |
| 61 | + password: 'password123', |
| 62 | + }), |
| 63 | + ); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('ForgotPasswordForm — email whitespace trimming (#3238)', () => { |
| 68 | + it('trims whitespace around the email before requesting the reset link', async () => { |
| 69 | + const forgotPassword = vi.fn().mockResolvedValue(undefined); |
| 70 | + renderWithAuth(createMockClient({}, { forgotPassword }), <ForgotPasswordForm />); |
| 71 | + |
| 72 | + fireEvent.change(await screen.findByLabelText('Email'), { |
| 73 | + target: { value: ' reset@objectos.ai ' }, |
| 74 | + }); |
| 75 | + fireEvent.click(screen.getByRole('button', { name: 'Send Reset Link' })); |
| 76 | + |
| 77 | + await waitFor(() => expect(forgotPassword).toHaveBeenCalledWith('reset@objectos.ai')); |
| 78 | + }); |
| 79 | +}); |
0 commit comments