webhook_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_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 := &mocks.Locale{
  30. MockLang: "en",
  31. MockTr: func(s string, _ ...interface{}) string {
  32. return s
  33. },
  34. }
  35. tests := []struct {
  36. name string
  37. actor *db.User
  38. webhook *db.Webhook
  39. expField string
  40. expMsg string
  41. expOK bool
  42. }{
  43. {
  44. name: "admin bypass local address check",
  45. actor: &db.User{IsAdmin: true},
  46. webhook: &db.Webhook{URL: "http://localhost:3306"},
  47. expOK: true,
  48. },
  49. {
  50. name: "local address not allowed",
  51. actor: &db.User{},
  52. webhook: &db.Webhook{URL: "http://localhost:3306"},
  53. expField: "PayloadURL",
  54. expMsg: "repo.settings.webhook.err_cannot_use_local_addresses",
  55. expOK: false,
  56. },
  57. }
  58. for _, test := range tests {
  59. t.Run(test.name, func(t *testing.T) {
  60. field, msg, ok := validateWebhook(test.actor, l, test.webhook)
  61. assert.Equal(t, test.expOK, ok)
  62. assert.Equal(t, test.expMsg, msg)
  63. assert.Equal(t, test.expField, field)
  64. })
  65. }
  66. }