1
0

webhook_test.go 1.0 KB

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