webhook_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. actor: &db.User{IsAdmin: true},
  29. webhook: &db.Webhook{URL: "http://localhost:3306"},
  30. expOK: true,
  31. },
  32. {
  33. name: "local address not allowed",
  34. actor: &db.User{},
  35. webhook: &db.Webhook{URL: "http://localhost:3306"},
  36. expField: "PayloadURL",
  37. expMsg: "repo.settings.webhook.err_cannot_use_local_addresses",
  38. expOK: false,
  39. },
  40. }
  41. for _, test := range tests {
  42. t.Run(test.name, func(t *testing.T) {
  43. field, msg, ok := validateWebhook(test.actor, l, test.webhook)
  44. assert.Equal(t, test.expOK, ok)
  45. assert.Equal(t, test.expMsg, msg)
  46. assert.Equal(t, test.expField, field)
  47. })
  48. }
  49. }