|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { POST } from './route'; |
| 3 | +import { createMockSupabaseClient } from '@/test/mocks/supabase'; |
| 4 | + |
| 5 | +vi.mock('@/lib/supabase/server', () => ({ |
| 6 | + createClient: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +// Override the global headers mock for this test file |
| 10 | +vi.mock('next/headers', () => ({ |
| 11 | + headers: vi.fn(() => Promise.resolve({ |
| 12 | + get: (name: string) => name === 'origin' ? 'http://localhost:3000' : null, |
| 13 | + })), |
| 14 | +})); |
| 15 | + |
| 16 | +import { createClient } from '@/lib/supabase/server'; |
| 17 | + |
| 18 | +describe('POST /api/auth/forgot-password', () => { |
| 19 | + beforeEach(() => { |
| 20 | + vi.clearAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('sends reset email successfully', async () => { |
| 24 | + const mockSupabase = createMockSupabaseClient({ |
| 25 | + auth: { |
| 26 | + resetPasswordForEmail: vi.fn().mockResolvedValue({ error: null }), |
| 27 | + }, |
| 28 | + }); |
| 29 | + vi.mocked(createClient).mockResolvedValue(mockSupabase as never); |
| 30 | + |
| 31 | + const request = new Request('http://localhost/api/auth/forgot-password', { |
| 32 | + method: 'POST', |
| 33 | + headers: { 'Content-Type': 'application/json' }, |
| 34 | + body: JSON.stringify({ email: 'user@example.com' }), |
| 35 | + }); |
| 36 | + |
| 37 | + const response = await POST(request); |
| 38 | + const body = await response.json(); |
| 39 | + |
| 40 | + expect(response.status).toBe(200); |
| 41 | + expect(body.data.message).toContain('If an account exists'); |
| 42 | + expect(mockSupabase.auth.resetPasswordForEmail).toHaveBeenCalledWith( |
| 43 | + 'user@example.com', |
| 44 | + expect.objectContaining({ |
| 45 | + redirectTo: expect.stringContaining('/reset-password'), |
| 46 | + }) |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns success even for non-existent email (prevents enumeration)', async () => { |
| 51 | + const mockSupabase = createMockSupabaseClient({ |
| 52 | + auth: { |
| 53 | + resetPasswordForEmail: vi.fn().mockResolvedValue({ error: null }), |
| 54 | + }, |
| 55 | + }); |
| 56 | + vi.mocked(createClient).mockResolvedValue(mockSupabase as never); |
| 57 | + |
| 58 | + const request = new Request('http://localhost/api/auth/forgot-password', { |
| 59 | + method: 'POST', |
| 60 | + headers: { 'Content-Type': 'application/json' }, |
| 61 | + body: JSON.stringify({ email: 'nonexistent@example.com' }), |
| 62 | + }); |
| 63 | + |
| 64 | + const response = await POST(request); |
| 65 | + const body = await response.json(); |
| 66 | + |
| 67 | + // Should return success to prevent email enumeration |
| 68 | + expect(response.status).toBe(200); |
| 69 | + expect(body.data.message).toContain('If an account exists'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns 400 for invalid email format', async () => { |
| 73 | + const request = new Request('http://localhost/api/auth/forgot-password', { |
| 74 | + method: 'POST', |
| 75 | + headers: { 'Content-Type': 'application/json' }, |
| 76 | + body: JSON.stringify({ email: 'not-an-email' }), |
| 77 | + }); |
| 78 | + |
| 79 | + const response = await POST(request); |
| 80 | + const body = await response.json(); |
| 81 | + |
| 82 | + expect(response.status).toBe(400); |
| 83 | + expect(body.error).toContain('email'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns 400 when supabase returns error', async () => { |
| 87 | + const mockSupabase = createMockSupabaseClient({ |
| 88 | + auth: { |
| 89 | + resetPasswordForEmail: vi.fn().mockResolvedValue({ |
| 90 | + error: { message: 'Rate limit exceeded' }, |
| 91 | + }), |
| 92 | + }, |
| 93 | + }); |
| 94 | + vi.mocked(createClient).mockResolvedValue(mockSupabase as never); |
| 95 | + |
| 96 | + const request = new Request('http://localhost/api/auth/forgot-password', { |
| 97 | + method: 'POST', |
| 98 | + headers: { 'Content-Type': 'application/json' }, |
| 99 | + body: JSON.stringify({ email: 'user@example.com' }), |
| 100 | + }); |
| 101 | + |
| 102 | + const response = await POST(request); |
| 103 | + const body = await response.json(); |
| 104 | + |
| 105 | + expect(response.status).toBe(400); |
| 106 | + expect(body.error).toBe('Rate limit exceeded'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns 400 for missing email', async () => { |
| 110 | + const request = new Request('http://localhost/api/auth/forgot-password', { |
| 111 | + method: 'POST', |
| 112 | + headers: { 'Content-Type': 'application/json' }, |
| 113 | + body: JSON.stringify({}), |
| 114 | + }); |
| 115 | + |
| 116 | + const response = await POST(request); |
| 117 | + expect(response.status).toBe(400); |
| 118 | + }); |
| 119 | +}); |
0 commit comments