webhook_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package repo
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "gogs.io/gogs/internal/database"
  7. "gogs.io/gogs/internal/mocks"
  8. )
  9. func TestValidateWebhook(t *testing.T) {
  10. l := &mocks.Locale{
  11. MockLang: "en",
  12. MockTr: func(s string, _ ...any) string {
  13. return s
  14. },
  15. }
  16. tests := []struct {
  17. name string
  18. actor *database.User
  19. webhook *database.Webhook
  20. wantField string
  21. wantMsg string
  22. wantStatus int
  23. }{
  24. {
  25. name: "admin bypass local address check",
  26. webhook: &database.Webhook{URL: "https://www.google.com"},
  27. wantStatus: http.StatusOK,
  28. },
  29. {
  30. name: "local address not allowed",
  31. webhook: &database.Webhook{URL: "http://localhost:3306"},
  32. wantField: "PayloadURL",
  33. wantMsg: "repo.settings.webhook.url_resolved_to_blocked_local_address",
  34. wantStatus: http.StatusForbidden,
  35. },
  36. }
  37. for _, test := range tests {
  38. t.Run(test.name, func(t *testing.T) {
  39. field, msg, status := validateWebhook(l, test.webhook)
  40. assert.Equal(t, test.wantStatus, status)
  41. assert.Equal(t, test.wantMsg, msg)
  42. assert.Equal(t, test.wantField, field)
  43. })
  44. }
  45. }