DefaultEdgeFunctionSecrets.utils.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. DEFAULT_EDGE_FUNCTION_SECRETS,
  4. getVisibleDefaultEdgeFunctionSecrets,
  5. isInternalEdgeFunctionSecret,
  6. } from './DefaultEdgeFunctionSecrets.utils'
  7. describe('isInternalEdgeFunctionSecret', () => {
  8. it.each(['BRIVEN_URL', 'BRIVEN_ANON_KEY', 'BRIVEN_THIS_DOES_NOT_EXIST_YET'])(
  9. 'treats BRIVEN_-prefixed names as internal (%s)',
  10. (name) => {
  11. expect(isInternalEdgeFunctionSecret(name)).toBe(true)
  12. }
  13. )
  14. it.each(['SB_REGION', 'SB_EXECUTION_ID', 'DENO_DEPLOYMENT_ID'])(
  15. 'treats hardcoded default name %s as internal',
  16. (name) => {
  17. expect(isInternalEdgeFunctionSecret(name)).toBe(true)
  18. }
  19. )
  20. it.each(['MY_API_KEY', 'STRIPE_SECRET', 'sb_region', 'DENO_OTHER_VAR'])(
  21. 'treats user-defined name %s as not internal',
  22. (name) => {
  23. expect(isInternalEdgeFunctionSecret(name)).toBe(false)
  24. }
  25. )
  26. })
  27. describe('getVisibleDefaultEdgeFunctionSecrets', () => {
  28. const runtimeNames = DEFAULT_EDGE_FUNCTION_SECRETS.filter((secret) => secret.isRuntime).map(
  29. (secret) => secret.name
  30. )
  31. const staticNames = DEFAULT_EDGE_FUNCTION_SECRETS.filter((secret) => !secret.isRuntime).map(
  32. (secret) => secret.name
  33. )
  34. it('always includes runtime secrets', () => {
  35. const result = getVisibleDefaultEdgeFunctionSecrets(new Set())
  36. for (const name of runtimeNames) {
  37. expect(result.map((secret) => secret.name)).toContain(name)
  38. }
  39. })
  40. it('falls back to the full hardcoded list when API returned no static defaults', () => {
  41. const result = getVisibleDefaultEdgeFunctionSecrets(new Set())
  42. expect(result.map((secret) => secret.name)).toEqual([...staticNames, ...runtimeNames])
  43. })
  44. it('shows only the static defaults present in the API response', () => {
  45. const apiNames = new Set(['BRIVEN_URL', 'BRIVEN_ANON_KEY', 'MY_USER_SECRET'])
  46. const result = getVisibleDefaultEdgeFunctionSecrets(apiNames)
  47. expect(result.map((secret) => secret.name)).toEqual([
  48. 'BRIVEN_URL',
  49. 'BRIVEN_ANON_KEY',
  50. ...runtimeNames,
  51. ])
  52. })
  53. it('does not surface user-defined secret names from the API set', () => {
  54. const apiNames = new Set(['BRIVEN_URL', 'MY_USER_SECRET'])
  55. const result = getVisibleDefaultEdgeFunctionSecrets(apiNames)
  56. expect(result.map((secret) => secret.name)).not.toContain('MY_USER_SECRET')
  57. })
  58. })