settings.test.ts 4.2 KB

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