util.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { assertSelfHosted, encryptString } from './util'
  3. vi.mock('@/lib/constants', () => ({
  4. IS_PLATFORM: false,
  5. }))
  6. vi.mock('crypto-js', () => {
  7. const mockEncrypt = vi.fn()
  8. return {
  9. default: {
  10. AES: {
  11. encrypt: mockEncrypt,
  12. },
  13. },
  14. AES: {
  15. encrypt: mockEncrypt,
  16. },
  17. }
  18. })
  19. describe('api/self-hosted/util', () => {
  20. beforeEach(() => {
  21. vi.clearAllMocks()
  22. })
  23. describe('assertSelfHosted', () => {
  24. it('should not throw when IS_PLATFORM is false', async () => {
  25. const constants = await import('@/lib/constants')
  26. vi.spyOn(constants, 'IS_PLATFORM', 'get').mockReturnValue(false)
  27. expect(() => assertSelfHosted()).not.toThrow()
  28. })
  29. it('should throw error when IS_PLATFORM is true', async () => {
  30. const constants = await import('@/lib/constants')
  31. vi.spyOn(constants, 'IS_PLATFORM', 'get').mockReturnValue(true)
  32. expect(() => assertSelfHosted()).toThrow(
  33. 'This function can only be called in self-hosted environments'
  34. )
  35. })
  36. })
  37. describe('encryptString', () => {
  38. it('should encrypt string using AES', async () => {
  39. const crypto = await import('crypto-js')
  40. const mockEncrypted = 'encrypted-string-123'
  41. vi.mocked(crypto.default.AES.encrypt).mockReturnValue({
  42. toString: () => mockEncrypted,
  43. } as any)
  44. const result = encryptString('my-secret-data')
  45. expect(crypto.default.AES.encrypt).toHaveBeenCalledWith('my-secret-data', expect.any(String))
  46. expect(result).toBe(mockEncrypted)
  47. })
  48. it('should return encrypted string as string', async () => {
  49. const crypto = await import('crypto-js')
  50. vi.mocked(crypto.default.AES.encrypt).mockReturnValue({
  51. toString: () => 'U2FsdGVkX1+abc123',
  52. } as any)
  53. const result = encryptString('test')
  54. expect(typeof result).toBe('string')
  55. expect(result).toBe('U2FsdGVkX1+abc123')
  56. })
  57. })
  58. describe('getConnectionString', () => {
  59. beforeEach(() => {
  60. vi.resetModules()
  61. })
  62. it('should build connection string with read-write user', async () => {
  63. vi.stubEnv('POSTGRES_HOST', 'localhost')
  64. vi.stubEnv('POSTGRES_PORT', '5432')
  65. vi.stubEnv('POSTGRES_DB', 'testdb')
  66. vi.stubEnv('POSTGRES_PASSWORD', 'testpass')
  67. vi.stubEnv('POSTGRES_USER_READ_WRITE', 'admin_user')
  68. // Re-import to get updated env values
  69. const { getConnectionString } = await import('./util')
  70. const result = getConnectionString({ readOnly: false })
  71. expect(result).toBe('postgresql://admin_user:testpass@localhost:5432/testdb')
  72. })
  73. it('should build connection string with read-only user', async () => {
  74. vi.stubEnv('POSTGRES_HOST', 'db.example.com')
  75. vi.stubEnv('POSTGRES_PORT', '5433')
  76. vi.stubEnv('POSTGRES_DB', 'mydb')
  77. vi.stubEnv('POSTGRES_PASSWORD', 'secret')
  78. vi.stubEnv('POSTGRES_USER_READ_ONLY', 'readonly_user')
  79. const { getConnectionString } = await import('./util')
  80. const result = getConnectionString({ readOnly: true })
  81. expect(result).toBe('postgresql://readonly_user:secret@db.example.com:5433/mydb')
  82. })
  83. it('should use default values when env vars not set', async () => {
  84. vi.stubEnv('POSTGRES_HOST', '')
  85. vi.stubEnv('POSTGRES_PORT', '')
  86. vi.stubEnv('POSTGRES_DB', '')
  87. vi.stubEnv('POSTGRES_PASSWORD', '')
  88. vi.stubEnv('POSTGRES_USER_READ_WRITE', '')
  89. vi.stubEnv('POSTGRES_USER_READ_ONLY', '')
  90. const { getConnectionString } = await import('./util')
  91. const resultReadWrite = getConnectionString({ readOnly: false })
  92. const resultReadOnly = getConnectionString({ readOnly: true })
  93. expect(resultReadWrite).toBe('postgresql://briven_admin:postgres@db:5432/postgres')
  94. expect(resultReadOnly).toBe('postgresql://briven_read_only_user:postgres@db:5432/postgres')
  95. })
  96. })
  97. })