justification_compat_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package config
  2. import (
  3. "testing"
  4. "github.com/knadh/koanf/parsers/yaml"
  5. "github.com/knadh/koanf/providers/rawbytes"
  6. "github.com/knadh/koanf/v2"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestJustificationDecodeHookMigratesLegacyBooleanFalse(t *testing.T) {
  11. cfg := loadJustificationCompatConfig(t, `
  12. actions:
  13. - title: Legacy disabled
  14. shell: echo hi
  15. justification: false
  16. `)
  17. require.Len(t, cfg.Actions, 1)
  18. assert.Empty(t, cfg.Actions[0].Justification)
  19. }
  20. func TestJustificationDecodeHookMigratesLegacyBooleanTrue(t *testing.T) {
  21. cfg := loadJustificationCompatConfig(t, `
  22. actions:
  23. - title: Legacy required
  24. shell: echo hi
  25. justification: true
  26. `)
  27. require.Len(t, cfg.Actions, 1)
  28. assert.Equal(t, JustificationRequiredNoTemplate, cfg.Actions[0].Justification)
  29. }
  30. func TestSanitizeJustificationMigratesWeaklyTypedLegacyStrings(t *testing.T) {
  31. action := &Action{Justification: "false"}
  32. action.sanitizeJustification()
  33. assert.Empty(t, action.Justification)
  34. action.Justification = "true"
  35. action.sanitizeJustification()
  36. assert.Equal(t, JustificationRequiredNoTemplate, action.Justification)
  37. }
  38. func loadJustificationCompatConfig(t *testing.T, yamlBody string) *Config {
  39. t.Helper()
  40. k := koanf.New(".")
  41. require.NoError(t, k.Load(rawbytes.Provider([]byte(yamlBody)), yaml.Parser()))
  42. cfg := DefaultConfig()
  43. require.True(t, unmarshalRoot(k, cfg))
  44. cfg.Sanitize()
  45. return cfg
  46. }