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.

refname_test.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 validation
  5. import (
  6. "testing"
  7. "github.com/go-macaron/binding"
  8. )
  9. var gitRefNameValidationTestCases = []validationTestCase{
  10. {
  11. description: "Referece contains only characters",
  12. data: TestForm{
  13. BranchName: "test",
  14. },
  15. expectedErrors: binding.Errors{},
  16. },
  17. {
  18. description: "Reference name contains single slash",
  19. data: TestForm{
  20. BranchName: "feature/test",
  21. },
  22. expectedErrors: binding.Errors{},
  23. },
  24. {
  25. description: "Reference name contains backslash",
  26. data: TestForm{
  27. BranchName: "feature\\test",
  28. },
  29. expectedErrors: binding.Errors{
  30. binding.Error{
  31. FieldNames: []string{"BranchName"},
  32. Classification: ErrGitRefName,
  33. Message: "GitRefName",
  34. },
  35. },
  36. },
  37. {
  38. description: "Reference name starts with dot",
  39. data: TestForm{
  40. BranchName: ".test",
  41. },
  42. expectedErrors: binding.Errors{
  43. binding.Error{
  44. FieldNames: []string{"BranchName"},
  45. Classification: ErrGitRefName,
  46. Message: "GitRefName",
  47. },
  48. },
  49. },
  50. {
  51. description: "Reference name ends with dot",
  52. data: TestForm{
  53. BranchName: "test.",
  54. },
  55. expectedErrors: binding.Errors{
  56. binding.Error{
  57. FieldNames: []string{"BranchName"},
  58. Classification: ErrGitRefName,
  59. Message: "GitRefName",
  60. },
  61. },
  62. },
  63. {
  64. description: "Reference name starts with slash",
  65. data: TestForm{
  66. BranchName: "/test",
  67. },
  68. expectedErrors: binding.Errors{
  69. binding.Error{
  70. FieldNames: []string{"BranchName"},
  71. Classification: ErrGitRefName,
  72. Message: "GitRefName",
  73. },
  74. },
  75. },
  76. {
  77. description: "Reference name ends with slash",
  78. data: TestForm{
  79. BranchName: "test/",
  80. },
  81. expectedErrors: binding.Errors{
  82. binding.Error{
  83. FieldNames: []string{"BranchName"},
  84. Classification: ErrGitRefName,
  85. Message: "GitRefName",
  86. },
  87. },
  88. },
  89. {
  90. description: "Reference name ends with .lock",
  91. data: TestForm{
  92. BranchName: "test.lock",
  93. },
  94. expectedErrors: binding.Errors{
  95. binding.Error{
  96. FieldNames: []string{"BranchName"},
  97. Classification: ErrGitRefName,
  98. Message: "GitRefName",
  99. },
  100. },
  101. },
  102. {
  103. description: "Reference name contains multiple consecutive dots",
  104. data: TestForm{
  105. BranchName: "te..st",
  106. },
  107. expectedErrors: binding.Errors{
  108. binding.Error{
  109. FieldNames: []string{"BranchName"},
  110. Classification: ErrGitRefName,
  111. Message: "GitRefName",
  112. },
  113. },
  114. },
  115. {
  116. description: "Reference name contains multiple consecutive slashes",
  117. data: TestForm{
  118. BranchName: "te//st",
  119. },
  120. expectedErrors: binding.Errors{
  121. binding.Error{
  122. FieldNames: []string{"BranchName"},
  123. Classification: ErrGitRefName,
  124. Message: "GitRefName",
  125. },
  126. },
  127. },
  128. }
  129. func Test_GitRefNameValidation(t *testing.T) {
  130. AddBindingRules()
  131. for _, testCase := range gitRefNameValidationTestCases {
  132. t.Run(testCase.description, func(t *testing.T) {
  133. performValidationTest(t, testCase)
  134. })
  135. }
  136. }