|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('@/database/mongoose', () => ({ |
| 4 | + connectToDatabase: vi.fn().mockResolvedValue({ |
| 5 | + connection: { db: {} }, |
| 6 | + }), |
| 7 | + isWorkersRuntime: () => false, |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('@/lib/nodemailer/reset-password', () => ({ |
| 11 | + sendPasswordResetEmail: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +describe('Better Auth secret resolution and production guard', () => { |
| 15 | + const originalEnv = { ...process.env }; |
| 16 | + const setNodeEnv = (val: string) => { |
| 17 | + (process.env as Record<string, string | undefined>).NODE_ENV = val; |
| 18 | + }; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + vi.resetModules(); |
| 22 | + process.env = { ...originalEnv }; |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + process.env = { ...originalEnv }; |
| 27 | + }); |
| 28 | + |
| 29 | + it('uses BETTER_AUTH_SECRET when only BETTER_AUTH_SECRET is configured', async () => { |
| 30 | + setNodeEnv('production'); |
| 31 | + process.env.BETTER_AUTH_SECRET = 'better-auth-secret-32-chars-long-12345'; |
| 32 | + delete process.env.AUTH_SECRET; |
| 33 | + |
| 34 | + const { getAuth } = await import('@/lib/better-auth/auth'); |
| 35 | + const auth = await getAuth(); |
| 36 | + |
| 37 | + expect(auth.options.secret).toBe('better-auth-secret-32-chars-long-12345'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('falls back to AUTH_SECRET when only AUTH_SECRET is configured', async () => { |
| 41 | + setNodeEnv('production'); |
| 42 | + delete process.env.BETTER_AUTH_SECRET; |
| 43 | + process.env.AUTH_SECRET = 'auth-secret-fallback-32-chars-long-12345'; |
| 44 | + |
| 45 | + const { getAuth } = await import('@/lib/better-auth/auth'); |
| 46 | + const auth = await getAuth(); |
| 47 | + |
| 48 | + expect(auth.options.secret).toBe('auth-secret-fallback-32-chars-long-12345'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('prefers BETTER_AUTH_SECRET over AUTH_SECRET when both are configured', async () => { |
| 52 | + setNodeEnv('production'); |
| 53 | + process.env.BETTER_AUTH_SECRET = 'preferred-better-auth-secret-32-chars'; |
| 54 | + process.env.AUTH_SECRET = 'secondary-auth-secret-32-chars-long'; |
| 55 | + |
| 56 | + const { getAuth } = await import('@/lib/better-auth/auth'); |
| 57 | + const auth = await getAuth(); |
| 58 | + |
| 59 | + expect(auth.options.secret).toBe('preferred-better-auth-secret-32-chars'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('rejects in production when neither secret is configured', async () => { |
| 63 | + setNodeEnv('production'); |
| 64 | + delete process.env.BETTER_AUTH_SECRET; |
| 65 | + delete process.env.AUTH_SECRET; |
| 66 | + |
| 67 | + const { getAuth } = await import('@/lib/better-auth/auth'); |
| 68 | + await expect(getAuth()).rejects.toThrow('BETTER_AUTH_SECRET must be set in production'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('preserves existing behaviour in development without either variable', async () => { |
| 72 | + setNodeEnv('development'); |
| 73 | + delete process.env.BETTER_AUTH_SECRET; |
| 74 | + delete process.env.AUTH_SECRET; |
| 75 | + |
| 76 | + const { getAuth } = await import('@/lib/better-auth/auth'); |
| 77 | + const auth = await getAuth(); |
| 78 | + |
| 79 | + expect(auth).toBeDefined(); |
| 80 | + expect(auth.options.secret).toBeUndefined(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments