webhook_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/mock"
  10. )
  11. func Test_isLocalHostname(t *testing.T) {
  12. tests := []struct {
  13. hostname string
  14. want bool
  15. }{
  16. {hostname: "localhost", want: true},
  17. {hostname: "127.0.0.1", want: true},
  18. {hostname: "::1", want: true},
  19. {hostname: "0:0:0:0:0:0:0:1", want: true},
  20. {hostname: "gogs.io", want: false},
  21. }
  22. for _, test := range tests {
  23. t.Run("", func(t *testing.T) {
  24. assert.Equal(t, test.want, isLocalHostname(test.hostname))
  25. })
  26. }
  27. }
  28. func Test_validateWebhook(t *testing.T) {
  29. l := mock.NewLocale("en", func(s string, _ ...interface{}) string {
  30. return s
  31. })
  32. tests := []struct {
  33. name string
  34. actor *db.User
  35. webhook *db.Webhook
  36. expField string
  37. expMsg string
  38. expOK bool
  39. }{
  40. {
  41. name: "admin bypass local address check",
  42. actor: &db.User{IsAdmin: true},
  43. webhook: &db.Webhook{URL: "http://localhost:3306"},
  44. expOK: true,
  45. },
  46. {
  47. name: "local address not allowed",
  48. actor: &db.User{},
  49. webhook: &db.Webhook{URL: "http://localhost:3306"},
  50. expField: "PayloadURL",
  51. expMsg: "repo.settings.webhook.err_cannot_use_local_addresses",
  52. expOK: false,
  53. },
  54. }
  55. for _, test := range tests {
  56. t.Run(test.name, func(t *testing.T) {
  57. field, msg, ok := validateWebhook(test.actor, l, test.webhook)
  58. assert.Equal(t, test.expOK, ok)
  59. assert.Equal(t, test.expMsg, msg)
  60. assert.Equal(t, test.expField, field)
  61. })
  62. }
  63. }