settings.test.ts129 lines · main
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2
3import { getProjectSettings } from './settings'
4
5vi.mock('./util', () => ({
6 assertSelfHosted: vi.fn(),
7}))
8
9vi.mock('@/lib/constants/api', () => ({
10 PROJECT_ENDPOINT: 'localhost:8000',
11 PROJECT_ENDPOINT_PROTOCOL: 'http',
12}))
13
14describe('api/self-hosted/settings', () => {
15 let mockAssertSelfHosted: ReturnType<typeof vi.fn>
16
17 beforeEach(async () => {
18 vi.clearAllMocks()
19 vi.resetModules()
20
21 const util = await import('./util')
22 mockAssertSelfHosted = vi.mocked(util.assertSelfHosted)
23 })
24
25 describe('getProjectSettings', () => {
26 it('should call assertSelfHosted', () => {
27 getProjectSettings()
28
29 expect(mockAssertSelfHosted).toHaveBeenCalled()
30 })
31
32 it('should return project settings with correct structure', () => {
33 const settings = getProjectSettings()
34
35 expect(settings).toHaveProperty('app_config')
36 expect(settings).toHaveProperty('cloud_provider')
37 expect(settings).toHaveProperty('db_dns_name')
38 expect(settings).toHaveProperty('db_host')
39 expect(settings).toHaveProperty('db_name')
40 expect(settings).toHaveProperty('jwt_secret')
41 expect(settings).toHaveProperty('service_api_keys')
42 })
43
44 it('should return correct default values', () => {
45 const settings = getProjectSettings()
46
47 expect(settings.cloud_provider).toBe('AWS')
48 expect(settings.db_host).toBe('localhost')
49 expect(settings.db_name).toBe('postgres')
50 expect(settings.db_port).toBe(5432)
51 expect(settings.db_user).toBe('postgres')
52 expect(settings.ref).toBe('default')
53 expect(settings.region).toBe('ap-southeast-1')
54 expect(settings.status).toBe('ACTIVE_HEALTHY')
55 expect(settings.ssl_enforced).toBe(false)
56 })
57
58 it('should include app_config with endpoint and protocol', () => {
59 const settings = getProjectSettings()
60
61 expect(settings.app_config).toEqual({
62 db_schema: 'public',
63 endpoint: 'localhost:8000',
64 storage_endpoint: 'localhost:8000',
65 protocol: 'http',
66 })
67 })
68
69 it('should include service_api_keys array', () => {
70 const settings = getProjectSettings()
71
72 expect(settings.service_api_keys).toHaveLength(2)
73 expect(settings.service_api_keys[0].name).toBe('service_role key')
74 expect(settings.service_api_keys[0].tags).toBe('service_role')
75 expect(settings.service_api_keys[1].name).toBe('anon key')
76 expect(settings.service_api_keys[1].tags).toBe('anon')
77 })
78
79 it('should use environment variables when set', async () => {
80 vi.stubEnv('AUTH_JWT_SECRET', 'custom-jwt-secret-with-at-least-32-chars')
81 vi.stubEnv('DEFAULT_PROJECT_NAME', 'My Custom Project')
82 vi.stubEnv('BRIVEN_SERVICE_KEY', 'custom-service-key')
83 vi.stubEnv('BRIVEN_ANON_KEY', 'custom-anon-key')
84
85 // Need to re-import to pick up new env vars
86 vi.resetModules()
87
88 const { getProjectSettings: getSettings } = await import('./settings')
89 const settings = getSettings()
90
91 expect(settings.jwt_secret).toBe('custom-jwt-secret-with-at-least-32-chars')
92 expect(settings.name).toBe('My Custom Project')
93 expect(settings.service_api_keys[0].api_key).toBe('custom-service-key')
94 expect(settings.service_api_keys[1].api_key).toBe('custom-anon-key')
95 })
96
97 it('should use default JWT secret when not set', async () => {
98 vi.unstubAllEnvs()
99
100 vi.resetModules()
101 const { getProjectSettings: getSettings } = await import('./settings')
102 const settings = getSettings()
103
104 expect(settings.jwt_secret).toBe('super-secret-jwt-token-with-at-least-32-characters-long')
105 })
106
107 it('should use default project name when not set', async () => {
108 vi.unstubAllEnvs()
109
110 vi.resetModules()
111 const { getProjectSettings: getSettings } = await import('./settings')
112 const settings = getSettings()
113
114 expect(settings.name).toBe('Default Project')
115 })
116
117 it('should have correct db_ip_addr_config', () => {
118 const settings = getProjectSettings()
119
120 expect(settings.db_ip_addr_config).toBe('legacy')
121 })
122
123 it('should have correct inserted_at timestamp', () => {
124 const settings = getProjectSettings()
125
126 expect(settings.inserted_at).toBe('2021-08-02T06:40:40.646Z')
127 })
128 })
129})