|
| 1 | +/** |
| 2 | + * Tests for forget password API route |
| 3 | + * |
| 4 | + * @vitest-environment node |
| 5 | + */ |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { createMockRequest } from '@/app/api/__test-utils__/utils' |
| 8 | + |
| 9 | +describe('Forget Password API Route', () => { |
| 10 | + const mockAuth = { |
| 11 | + api: { |
| 12 | + forgetPassword: vi.fn(), |
| 13 | + }, |
| 14 | + } |
| 15 | + const mockLogger = { |
| 16 | + info: vi.fn(), |
| 17 | + warn: vi.fn(), |
| 18 | + error: vi.fn(), |
| 19 | + debug: vi.fn(), |
| 20 | + } |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + vi.resetModules() |
| 24 | + |
| 25 | + vi.doMock('@/lib/auth', () => ({ |
| 26 | + auth: mockAuth, |
| 27 | + })) |
| 28 | + |
| 29 | + vi.doMock('@/lib/logs/console-logger', () => ({ |
| 30 | + createLogger: vi.fn().mockReturnValue(mockLogger), |
| 31 | + })) |
| 32 | + }) |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + vi.clearAllMocks() |
| 36 | + }) |
| 37 | + |
| 38 | + it('should send password reset email successfully', async () => { |
| 39 | + mockAuth.api.forgetPassword.mockResolvedValueOnce(undefined) |
| 40 | + |
| 41 | + const req = createMockRequest('POST', { |
| 42 | + email: 'test@example.com', |
| 43 | + redirectTo: 'https://example.com/reset', |
| 44 | + }) |
| 45 | + |
| 46 | + const { POST } = await import('./route') |
| 47 | + |
| 48 | + const response = await POST(req) |
| 49 | + const data = await response.json() |
| 50 | + |
| 51 | + expect(response.status).toBe(200) |
| 52 | + expect(data.success).toBe(true) |
| 53 | + expect(mockAuth.api.forgetPassword).toHaveBeenCalledWith({ |
| 54 | + body: { |
| 55 | + email: 'test@example.com', |
| 56 | + redirectTo: 'https://example.com/reset', |
| 57 | + }, |
| 58 | + method: 'POST', |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should send password reset email without redirectTo', async () => { |
| 63 | + mockAuth.api.forgetPassword.mockResolvedValueOnce(undefined) |
| 64 | + |
| 65 | + const req = createMockRequest('POST', { |
| 66 | + email: 'test@example.com', |
| 67 | + }) |
| 68 | + |
| 69 | + const { POST } = await import('./route') |
| 70 | + |
| 71 | + const response = await POST(req) |
| 72 | + const data = await response.json() |
| 73 | + |
| 74 | + expect(response.status).toBe(200) |
| 75 | + expect(data.success).toBe(true) |
| 76 | + expect(mockAuth.api.forgetPassword).toHaveBeenCalledWith({ |
| 77 | + body: { |
| 78 | + email: 'test@example.com', |
| 79 | + redirectTo: undefined, |
| 80 | + }, |
| 81 | + method: 'POST', |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should handle missing email', async () => { |
| 86 | + const req = createMockRequest('POST', {}) |
| 87 | + |
| 88 | + const { POST } = await import('./route') |
| 89 | + |
| 90 | + const response = await POST(req) |
| 91 | + const data = await response.json() |
| 92 | + |
| 93 | + expect(response.status).toBe(400) |
| 94 | + expect(data.message).toBe('Email is required') |
| 95 | + expect(mockAuth.api.forgetPassword).not.toHaveBeenCalled() |
| 96 | + }) |
| 97 | + |
| 98 | + it('should handle empty email', async () => { |
| 99 | + const req = createMockRequest('POST', { |
| 100 | + email: '', |
| 101 | + }) |
| 102 | + |
| 103 | + const { POST } = await import('./route') |
| 104 | + |
| 105 | + const response = await POST(req) |
| 106 | + const data = await response.json() |
| 107 | + |
| 108 | + expect(response.status).toBe(400) |
| 109 | + expect(data.message).toBe('Email is required') |
| 110 | + expect(mockAuth.api.forgetPassword).not.toHaveBeenCalled() |
| 111 | + }) |
| 112 | + |
| 113 | + it('should handle auth service error with message', async () => { |
| 114 | + const errorMessage = 'User not found' |
| 115 | + mockAuth.api.forgetPassword.mockRejectedValueOnce(new Error(errorMessage)) |
| 116 | + |
| 117 | + const req = createMockRequest('POST', { |
| 118 | + email: 'nonexistent@example.com', |
| 119 | + }) |
| 120 | + |
| 121 | + const { POST } = await import('./route') |
| 122 | + |
| 123 | + const response = await POST(req) |
| 124 | + const data = await response.json() |
| 125 | + |
| 126 | + expect(response.status).toBe(500) |
| 127 | + expect(data.message).toBe(errorMessage) |
| 128 | + expect(mockLogger.error).toHaveBeenCalledWith('Error requesting password reset:', { |
| 129 | + error: expect.any(Error), |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should handle unknown error', async () => { |
| 134 | + mockAuth.api.forgetPassword.mockRejectedValueOnce('Unknown error') |
| 135 | + |
| 136 | + const req = createMockRequest('POST', { |
| 137 | + email: 'test@example.com', |
| 138 | + }) |
| 139 | + |
| 140 | + const { POST } = await import('./route') |
| 141 | + |
| 142 | + const response = await POST(req) |
| 143 | + const data = await response.json() |
| 144 | + |
| 145 | + expect(response.status).toBe(500) |
| 146 | + expect(data.message).toBe('Failed to send password reset email. Please try again later.') |
| 147 | + expect(mockLogger.error).toHaveBeenCalled() |
| 148 | + }) |
| 149 | +}) |
0 commit comments