webhook_auth_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package configcheck_test
  2. import (
  3. "testing"
  4. "github.com/OliveTin/OliveTin/internal/config"
  5. "github.com/OliveTin/OliveTin/internal/configcheck"
  6. "github.com/OliveTin/OliveTin/internal/configissues"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func webhookAuthIssues(webhooks ...config.WebhookConfig) []configissues.Issue {
  11. configissues.BeginConfigLoad()
  12. cfg := config.DefaultConfig()
  13. cfg.Actions = []*config.Action{{ID: "deploy", Title: "Deploy", Exec: []string{"true"}, ExecOnWebhook: webhooks}}
  14. configcheck.Rebuild(cfg)
  15. return issuesWithCode(configissues.List(), configissues.CodeWebhookAuthTypeUnset)
  16. }
  17. func TestRebuildReportsWebhookWithoutAuthType(t *testing.T) {
  18. issues := webhookAuthIssues(config.WebhookConfig{MatchQuery: map[string]string{"demo": "x"}})
  19. require.Len(t, issues, 1)
  20. assert.Equal(t, configissues.SeverityError, issues[0].Severity)
  21. assert.Equal(t, "Deploy", issues[0].ActionTitle)
  22. assert.Contains(t, issues[0].Message, "authType: none")
  23. }
  24. func TestRebuildAcceptsWebhookWithExplicitAuthType(t *testing.T) {
  25. assert.Empty(t, webhookAuthIssues(
  26. config.WebhookConfig{AuthType: config.WebhookAuthTypeNone},
  27. config.WebhookConfig{AuthType: "bearer", Secret: "token"},
  28. ))
  29. }
  30. func TestRebuildAcceptsWebhookTemplateWithoutAuthType(t *testing.T) {
  31. assert.Empty(t, webhookAuthIssues(config.WebhookConfig{Template: "github-push", Secret: "s3cret"}))
  32. }
  33. func TestRebuildReportsEachWebhookWithoutAuthType(t *testing.T) {
  34. issues := webhookAuthIssues(config.WebhookConfig{}, config.WebhookConfig{AuthType: "bearer"}, config.WebhookConfig{})
  35. require.Len(t, issues, 2)
  36. assert.Equal(t, "execOnWebhook[0]", issues[0].Source)
  37. assert.Equal(t, "execOnWebhook[2]", issues[1].Source)
  38. }
  39. func TestRebuildReportsUnknownWebhookTemplateWithoutAuthType(t *testing.T) {
  40. webhook := config.WebhookConfig{Template: "github-pus", Secret: "s3cret"}
  41. assert.Len(t, webhookAuthIssues(webhook), 1, "a misspelt template must not excuse a missing authType")
  42. templateIssues := issuesWithCode(configissues.List(), configissues.CodeWebhookTemplateUnknown)
  43. require.Len(t, templateIssues, 1)
  44. assert.Contains(t, templateIssues[0].Message, "github-pus")
  45. assert.Contains(t, templateIssues[0].Message, "github-push")
  46. assert.Equal(t, "execOnWebhook[0]", templateIssues[0].Source)
  47. }
  48. func TestRebuildReportsUnknownWebhookTemplateEvenWithAuthType(t *testing.T) {
  49. webhook := config.WebhookConfig{Template: "gitlab-push", AuthType: "bearer", Secret: "token"}
  50. assert.Empty(t, webhookAuthIssues(webhook))
  51. assert.Len(t, issuesWithCode(configissues.List(), configissues.CodeWebhookTemplateUnknown), 1)
  52. }
  53. func TestRebuildAcceptsEveryKnownWebhookTemplate(t *testing.T) {
  54. for _, name := range config.WebhookTemplateNames {
  55. assert.Empty(t, webhookAuthIssues(config.WebhookConfig{Template: name, Secret: "s3cret"}), name)
  56. assert.Empty(t, issuesWithCode(configissues.List(), configissues.CodeWebhookTemplateUnknown), name)
  57. }
  58. }