matcher_justification_test.go 965 B

123456789101112131415161718192021222324252627282930313233343536
  1. package webhooks
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. config "github.com/OliveTin/OliveTin/internal/config"
  8. )
  9. func TestExtractJustificationFromWebhookBody(t *testing.T) {
  10. body := []byte(`{"message":"deploy production","repo":"my-app"}`)
  11. req, err := http.NewRequest(http.MethodPost, "/webhooks/deploy", nil)
  12. require.NoError(t, err)
  13. matcher := NewWebhookMatcher(config.WebhookConfig{
  14. Justification: "$.message",
  15. }, req, body)
  16. value, err := matcher.ExtractJustification()
  17. require.NoError(t, err)
  18. assert.Equal(t, "deploy production", value)
  19. }
  20. func TestExtractJustificationEmptyWhenNotConfigured(t *testing.T) {
  21. req, err := http.NewRequest(http.MethodPost, "/webhooks/deploy", nil)
  22. require.NoError(t, err)
  23. matcher := NewWebhookMatcher(config.WebhookConfig{}, req, []byte(`{}`))
  24. value, err := matcher.ExtractJustification()
  25. require.NoError(t, err)
  26. assert.Empty(t, value)
  27. }