constants.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. describe('api/self-hosted/constants', () => {
  3. beforeEach(() => {
  4. vi.resetModules()
  5. })
  6. describe('ENCRYPTION_KEY', () => {
  7. it('should use PG_META_CRYPTO_KEY when set', async () => {
  8. vi.stubEnv('PG_META_CRYPTO_KEY', 'my-secret-key-123')
  9. const { ENCRYPTION_KEY } = await import('./constants')
  10. expect(ENCRYPTION_KEY).toBe('my-secret-key-123')
  11. })
  12. it('should use SAMPLE_KEY as default', async () => {
  13. vi.stubEnv('PG_META_CRYPTO_KEY', '')
  14. const { ENCRYPTION_KEY } = await import('./constants')
  15. expect(ENCRYPTION_KEY).toBe('SAMPLE_KEY')
  16. })
  17. })
  18. describe('POSTGRES_PORT', () => {
  19. it('should use POSTGRES_PORT when set', async () => {
  20. vi.stubEnv('POSTGRES_PORT', '5433')
  21. const { POSTGRES_PORT } = await import('./constants')
  22. expect(POSTGRES_PORT).toBe(5433)
  23. })
  24. it('should default to 5432', async () => {
  25. vi.stubEnv('POSTGRES_PORT', '')
  26. const { POSTGRES_PORT } = await import('./constants')
  27. expect(POSTGRES_PORT).toBe(5432)
  28. })
  29. })
  30. describe('POSTGRES_HOST', () => {
  31. it('should use POSTGRES_HOST when set', async () => {
  32. vi.stubEnv('POSTGRES_HOST', 'my-db-host.example.com')
  33. const { POSTGRES_HOST } = await import('./constants')
  34. expect(POSTGRES_HOST).toBe('my-db-host.example.com')
  35. })
  36. it('should default to db', async () => {
  37. vi.stubEnv('POSTGRES_HOST', '')
  38. const { POSTGRES_HOST } = await import('./constants')
  39. expect(POSTGRES_HOST).toBe('db')
  40. })
  41. })
  42. describe('POSTGRES_DATABASE', () => {
  43. it('should use POSTGRES_DB when set', async () => {
  44. vi.stubEnv('POSTGRES_DB', 'my_database')
  45. const { POSTGRES_DATABASE } = await import('./constants')
  46. expect(POSTGRES_DATABASE).toBe('my_database')
  47. })
  48. it('should default to postgres', async () => {
  49. vi.stubEnv('POSTGRES_DB', '')
  50. const { POSTGRES_DATABASE } = await import('./constants')
  51. expect(POSTGRES_DATABASE).toBe('postgres')
  52. })
  53. })
  54. describe('POSTGRES_PASSWORD', () => {
  55. it('should use POSTGRES_PASSWORD when set', async () => {
  56. vi.stubEnv('POSTGRES_PASSWORD', 'super-secret-password')
  57. const { POSTGRES_PASSWORD } = await import('./constants')
  58. expect(POSTGRES_PASSWORD).toBe('super-secret-password')
  59. })
  60. it('should default to postgres', async () => {
  61. vi.stubEnv('POSTGRES_PASSWORD', '')
  62. const { POSTGRES_PASSWORD } = await import('./constants')
  63. expect(POSTGRES_PASSWORD).toBe('postgres')
  64. })
  65. })
  66. describe('POSTGRES_USER_READ_WRITE', () => {
  67. it('should use POSTGRES_USER_READ_WRITE when set', async () => {
  68. vi.stubEnv('POSTGRES_USER_READ_WRITE', 'custom_admin')
  69. const { POSTGRES_USER_READ_WRITE } = await import('./constants')
  70. expect(POSTGRES_USER_READ_WRITE).toBe('custom_admin')
  71. })
  72. it('should default to briven_admin', async () => {
  73. vi.stubEnv('POSTGRES_USER_READ_WRITE', '')
  74. const { POSTGRES_USER_READ_WRITE } = await import('./constants')
  75. expect(POSTGRES_USER_READ_WRITE).toBe('briven_admin')
  76. })
  77. })
  78. describe('POSTGRES_USER_READ_ONLY', () => {
  79. it('should use POSTGRES_USER_READ_ONLY when set', async () => {
  80. vi.stubEnv('POSTGRES_USER_READ_ONLY', 'custom_readonly')
  81. const { POSTGRES_USER_READ_ONLY } = await import('./constants')
  82. expect(POSTGRES_USER_READ_ONLY).toBe('custom_readonly')
  83. })
  84. it('should default to briven_read_only_user', async () => {
  85. vi.stubEnv('POSTGRES_USER_READ_ONLY', '')
  86. const { POSTGRES_USER_READ_ONLY } = await import('./constants')
  87. expect(POSTGRES_USER_READ_ONLY).toBe('briven_read_only_user')
  88. })
  89. })
  90. })