webhook_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "gogs.io/gogs/internal/db"
  9. "gogs.io/gogs/internal/mocks"
  10. )
  11. func Test_validateWebhook(t *testing.T) {
  12. l := &mocks.Locale{
  13. MockLang: "en",
  14. MockTr: func(s string, _ ...interface{}) string {
  15. return s
  16. },
  17. }
  18. tests := []struct {
  19. name string
  20. actor *db.User
  21. webhook *db.Webhook
  22. expField string
  23. expMsg string
  24. expOK bool
  25. }{
  26. {
  27. name: "admin bypass local address check",
  28. webhook: &db.Webhook{URL: "https://www.google.com"},
  29. expOK: true,
  30. },
  31. {
  32. name: "local address not allowed",
  33. webhook: &db.Webhook{URL: "http://localhost:3306"},
  34. expField: "PayloadURL",
  35. expMsg: "repo.settings.webhook.url_resolved_to_blocked_local_address",
  36. expOK: false,
  37. },
  38. }
  39. for _, test := range tests {
  40. t.Run(test.name, func(t *testing.T) {
  41. field, msg, ok := validateWebhook(l, test.webhook)
  42. assert.Equal(t, test.expOK, ok)
  43. assert.Equal(t, test.expMsg, msg)
  44. assert.Equal(t, test.expField, field)
  45. })
  46. }
  47. }