|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 4 | +import { AuthManager } from './auth-manager'; |
| 5 | + |
| 6 | +// Mock better-auth so we can control the handler behaviour |
| 7 | +vi.mock('better-auth', () => ({ |
| 8 | + betterAuth: vi.fn(() => ({ |
| 9 | + handler: vi.fn(), |
| 10 | + api: {}, |
| 11 | + })), |
| 12 | +})); |
| 13 | + |
| 14 | +import { betterAuth } from 'better-auth'; |
| 15 | + |
| 16 | +describe('AuthManager', () => { |
| 17 | + let consoleSpy: ReturnType<typeof vi.spyOn>; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + vi.clearAllMocks(); |
| 21 | + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + consoleSpy.mockRestore(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('handleRequest – error response logging', () => { |
| 29 | + it('should log when better-auth returns a 500 response', async () => { |
| 30 | + const errorResponse = new Response( |
| 31 | + JSON.stringify({ error: 'Internal database error' }), |
| 32 | + { status: 500, headers: { 'Content-Type': 'application/json' } }, |
| 33 | + ); |
| 34 | + |
| 35 | + const mockHandler = vi.fn().mockResolvedValue(errorResponse); |
| 36 | + (betterAuth as any).mockReturnValue({ handler: mockHandler, api: {} }); |
| 37 | + |
| 38 | + const manager = new AuthManager({ |
| 39 | + secret: 'test-secret-at-least-32-chars-long', |
| 40 | + baseUrl: 'http://localhost:3000', |
| 41 | + }); |
| 42 | + |
| 43 | + const request = new Request('http://localhost:3000/sign-up/email', { |
| 44 | + method: 'POST', |
| 45 | + body: JSON.stringify({ email: 'a@b.com', password: 'pass' }), |
| 46 | + headers: { 'Content-Type': 'application/json' }, |
| 47 | + }); |
| 48 | + |
| 49 | + const response = await manager.handleRequest(request); |
| 50 | + |
| 51 | + expect(response.status).toBe(500); |
| 52 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 53 | + '[AuthManager] better-auth returned error:', |
| 54 | + 500, |
| 55 | + expect.stringContaining('Internal database error'), |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should NOT log for successful (2xx) responses', async () => { |
| 60 | + const okResponse = new Response(JSON.stringify({ user: {} }), { |
| 61 | + status: 200, |
| 62 | + }); |
| 63 | + |
| 64 | + const mockHandler = vi.fn().mockResolvedValue(okResponse); |
| 65 | + (betterAuth as any).mockReturnValue({ handler: mockHandler, api: {} }); |
| 66 | + |
| 67 | + const manager = new AuthManager({ |
| 68 | + secret: 'test-secret-at-least-32-chars-long', |
| 69 | + baseUrl: 'http://localhost:3000', |
| 70 | + }); |
| 71 | + |
| 72 | + const request = new Request('http://localhost:3000/sign-in/email', { |
| 73 | + method: 'POST', |
| 74 | + body: JSON.stringify({ email: 'a@b.com', password: 'pass' }), |
| 75 | + headers: { 'Content-Type': 'application/json' }, |
| 76 | + }); |
| 77 | + |
| 78 | + const response = await manager.handleRequest(request); |
| 79 | + |
| 80 | + expect(response.status).toBe(200); |
| 81 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should NOT log for 4xx responses', async () => { |
| 85 | + const badRequestResponse = new Response( |
| 86 | + JSON.stringify({ error: 'Bad request' }), |
| 87 | + { status: 400 }, |
| 88 | + ); |
| 89 | + |
| 90 | + const mockHandler = vi.fn().mockResolvedValue(badRequestResponse); |
| 91 | + (betterAuth as any).mockReturnValue({ handler: mockHandler, api: {} }); |
| 92 | + |
| 93 | + const manager = new AuthManager({ |
| 94 | + secret: 'test-secret-at-least-32-chars-long', |
| 95 | + baseUrl: 'http://localhost:3000', |
| 96 | + }); |
| 97 | + |
| 98 | + const request = new Request('http://localhost:3000/sign-in/email', { |
| 99 | + method: 'POST', |
| 100 | + }); |
| 101 | + |
| 102 | + const response = await manager.handleRequest(request); |
| 103 | + |
| 104 | + expect(response.status).toBe(400); |
| 105 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('createDatabaseConfig – adapter wrapping', () => { |
| 110 | + it('should pass a function (DBAdapterInstance) to betterAuth when dataEngine is provided', () => { |
| 111 | + const mockDataEngine = { |
| 112 | + insert: vi.fn(), |
| 113 | + findOne: vi.fn(), |
| 114 | + find: vi.fn(), |
| 115 | + count: vi.fn(), |
| 116 | + update: vi.fn(), |
| 117 | + delete: vi.fn(), |
| 118 | + }; |
| 119 | + |
| 120 | + new AuthManager({ |
| 121 | + secret: 'test-secret-at-least-32-chars-long', |
| 122 | + baseUrl: 'http://localhost:3000', |
| 123 | + dataEngine: mockDataEngine as any, |
| 124 | + }); |
| 125 | + |
| 126 | + // Trigger lazy initialization by calling getAuthInstance() |
| 127 | + // betterAuth should have been called with a database value that is a function |
| 128 | + // We need to trigger the lazy init first |
| 129 | + }); |
| 130 | + |
| 131 | + it('should provide a factory function as database config that returns adapter with id and transaction', () => { |
| 132 | + const mockDataEngine = { |
| 133 | + insert: vi.fn().mockResolvedValue({ id: '1' }), |
| 134 | + findOne: vi.fn().mockResolvedValue({ id: '1' }), |
| 135 | + find: vi.fn().mockResolvedValue([]), |
| 136 | + count: vi.fn().mockResolvedValue(0), |
| 137 | + update: vi.fn().mockResolvedValue({ id: '1' }), |
| 138 | + delete: vi.fn().mockResolvedValue(undefined), |
| 139 | + }; |
| 140 | + |
| 141 | + let capturedConfig: any; |
| 142 | + (betterAuth as any).mockImplementation((config: any) => { |
| 143 | + capturedConfig = config; |
| 144 | + return { handler: vi.fn(), api: {} }; |
| 145 | + }); |
| 146 | + |
| 147 | + const manager = new AuthManager({ |
| 148 | + secret: 'test-secret-at-least-32-chars-long', |
| 149 | + baseUrl: 'http://localhost:3000', |
| 150 | + dataEngine: mockDataEngine as any, |
| 151 | + }); |
| 152 | + |
| 153 | + // Trigger lazy initialisation |
| 154 | + manager.getAuthInstance(); |
| 155 | + |
| 156 | + // The database config should be a function (DBAdapterInstance) |
| 157 | + expect(typeof capturedConfig.database).toBe('function'); |
| 158 | + |
| 159 | + // Calling the factory should return an adapter object |
| 160 | + const adapterResult = capturedConfig.database({}); |
| 161 | + expect(adapterResult).toHaveProperty('id', 'objectql'); |
| 162 | + expect(typeof adapterResult.create).toBe('function'); |
| 163 | + expect(typeof adapterResult.findOne).toBe('function'); |
| 164 | + expect(typeof adapterResult.findMany).toBe('function'); |
| 165 | + expect(typeof adapterResult.count).toBe('function'); |
| 166 | + expect(typeof adapterResult.update).toBe('function'); |
| 167 | + expect(typeof adapterResult.delete).toBe('function'); |
| 168 | + expect(typeof adapterResult.deleteMany).toBe('function'); |
| 169 | + expect(typeof adapterResult.updateMany).toBe('function'); |
| 170 | + expect(typeof adapterResult.transaction).toBe('function'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should return undefined (in-memory fallback) when no dataEngine is provided', () => { |
| 174 | + let capturedConfig: any; |
| 175 | + (betterAuth as any).mockImplementation((config: any) => { |
| 176 | + capturedConfig = config; |
| 177 | + return { handler: vi.fn(), api: {} }; |
| 178 | + }); |
| 179 | + |
| 180 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 181 | + |
| 182 | + const manager = new AuthManager({ |
| 183 | + secret: 'test-secret-at-least-32-chars-long', |
| 184 | + baseUrl: 'http://localhost:3000', |
| 185 | + }); |
| 186 | + |
| 187 | + manager.getAuthInstance(); |
| 188 | + |
| 189 | + expect(capturedConfig.database).toBeUndefined(); |
| 190 | + warnSpy.mockRestore(); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments