You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

  1. // Copyright 2017 The Gitea 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 utils
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestRemoveUsernameParameterSuffix(t *testing.T) {
  11. assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar (Foo Bar)"))
  12. assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar"))
  13. assert.Equal(t, "", RemoveUsernameParameterSuffix(""))
  14. }
  15. func TestIsValidSlackChannel(t *testing.T) {
  16. tt := []struct {
  17. channelName string
  18. expected bool
  19. }{
  20. {"gitea", true},
  21. {" ", false},
  22. {"#", false},
  23. {"gitea ", true},
  24. {" gitea", true},
  25. }
  26. for _, v := range tt {
  27. assert.Equal(t, v.expected, IsValidSlackChannel(v.channelName))
  28. }
  29. }
  30. func TestIsExternalURL(t *testing.T) {
  31. setting.AppURL = "https://try.gitea.io"
  32. type test struct {
  33. Expected bool
  34. RawURL string
  35. }
  36. newTest := func(expected bool, rawURL string) test {
  37. return test{Expected: expected, RawURL: rawURL}
  38. }
  39. for _, test := range []test{
  40. newTest(false,
  41. "https://try.gitea.io"),
  42. newTest(true,
  43. "https://example.com/"),
  44. newTest(true,
  45. "//example.com"),
  46. newTest(true,
  47. "http://example.com"),
  48. newTest(false,
  49. "a/"),
  50. newTest(false,
  51. "https://try.gitea.io/test?param=false"),
  52. newTest(false,
  53. "test?param=false"),
  54. newTest(false,
  55. "//try.gitea.io/test?param=false"),
  56. newTest(false,
  57. "/hey/hey/hey#3244"),
  58. } {
  59. assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
  60. }
  61. }