github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/validation/refname_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package validation
     7  
     8  import (
     9  	"testing"
    10  
    11  	"gitea.com/go-chi/binding"
    12  )
    13  
    14  var gitRefNameValidationTestCases = []validationTestCase{
    15  	{
    16  		description: "Reference name contains only characters",
    17  		data: TestForm{
    18  			BranchName: "test",
    19  		},
    20  		expectedErrors: binding.Errors{},
    21  	},
    22  	{
    23  		description: "Reference name contains single slash",
    24  		data: TestForm{
    25  			BranchName: "feature/test",
    26  		},
    27  		expectedErrors: binding.Errors{},
    28  	},
    29  	{
    30  		description: "Reference name has allowed special characters",
    31  		data: TestForm{
    32  			BranchName: "debian/1%1.6.0-2",
    33  		},
    34  		expectedErrors: binding.Errors{},
    35  	},
    36  	{
    37  		description: "Reference name contains backslash",
    38  		data: TestForm{
    39  			BranchName: "feature\\test",
    40  		},
    41  		expectedErrors: binding.Errors{
    42  			binding.Error{
    43  				FieldNames:     []string{"BranchName"},
    44  				Classification: ErrGitRefName,
    45  				Message:        "GitRefName",
    46  			},
    47  		},
    48  	},
    49  	{
    50  		description: "Reference name starts with dot",
    51  		data: TestForm{
    52  			BranchName: ".test",
    53  		},
    54  		expectedErrors: binding.Errors{
    55  			binding.Error{
    56  				FieldNames:     []string{"BranchName"},
    57  				Classification: ErrGitRefName,
    58  				Message:        "GitRefName",
    59  			},
    60  		},
    61  	},
    62  	{
    63  		description: "Reference name ends with dot",
    64  		data: TestForm{
    65  			BranchName: "test.",
    66  		},
    67  		expectedErrors: binding.Errors{
    68  			binding.Error{
    69  				FieldNames:     []string{"BranchName"},
    70  				Classification: ErrGitRefName,
    71  				Message:        "GitRefName",
    72  			},
    73  		},
    74  	},
    75  	{
    76  		description: "Reference name starts with slash",
    77  		data: TestForm{
    78  			BranchName: "/test",
    79  		},
    80  		expectedErrors: binding.Errors{
    81  			binding.Error{
    82  				FieldNames:     []string{"BranchName"},
    83  				Classification: ErrGitRefName,
    84  				Message:        "GitRefName",
    85  			},
    86  		},
    87  	},
    88  	{
    89  		description: "Reference name ends with slash",
    90  		data: TestForm{
    91  			BranchName: "test/",
    92  		},
    93  		expectedErrors: binding.Errors{
    94  			binding.Error{
    95  				FieldNames:     []string{"BranchName"},
    96  				Classification: ErrGitRefName,
    97  				Message:        "GitRefName",
    98  			},
    99  		},
   100  	},
   101  	{
   102  		description: "Reference name ends with .lock",
   103  		data: TestForm{
   104  			BranchName: "test.lock",
   105  		},
   106  		expectedErrors: binding.Errors{
   107  			binding.Error{
   108  				FieldNames:     []string{"BranchName"},
   109  				Classification: ErrGitRefName,
   110  				Message:        "GitRefName",
   111  			},
   112  		},
   113  	},
   114  	{
   115  		description: "Reference name contains multiple consecutive dots",
   116  		data: TestForm{
   117  			BranchName: "te..st",
   118  		},
   119  		expectedErrors: binding.Errors{
   120  			binding.Error{
   121  				FieldNames:     []string{"BranchName"},
   122  				Classification: ErrGitRefName,
   123  				Message:        "GitRefName",
   124  			},
   125  		},
   126  	},
   127  	{
   128  		description: "Reference name contains multiple consecutive slashes",
   129  		data: TestForm{
   130  			BranchName: "te//st",
   131  		},
   132  		expectedErrors: binding.Errors{
   133  			binding.Error{
   134  				FieldNames:     []string{"BranchName"},
   135  				Classification: ErrGitRefName,
   136  				Message:        "GitRefName",
   137  			},
   138  		},
   139  	},
   140  	{
   141  		description: "Reference name is single @",
   142  		data: TestForm{
   143  			BranchName: "@",
   144  		},
   145  		expectedErrors: binding.Errors{
   146  			binding.Error{
   147  				FieldNames:     []string{"BranchName"},
   148  				Classification: ErrGitRefName,
   149  				Message:        "GitRefName",
   150  			},
   151  		},
   152  	},
   153  	{
   154  		description: "Reference name has @{",
   155  		data: TestForm{
   156  			BranchName: "branch@{",
   157  		},
   158  		expectedErrors: binding.Errors{
   159  			binding.Error{
   160  				FieldNames:     []string{"BranchName"},
   161  				Classification: ErrGitRefName,
   162  				Message:        "GitRefName",
   163  			},
   164  		},
   165  	},
   166  	{
   167  		description: "Reference name has unallowed special character ~",
   168  		data: TestForm{
   169  			BranchName: "~debian/1%1.6.0-2",
   170  		},
   171  		expectedErrors: binding.Errors{
   172  			binding.Error{
   173  				FieldNames:     []string{"BranchName"},
   174  				Classification: ErrGitRefName,
   175  				Message:        "GitRefName",
   176  			},
   177  		},
   178  	},
   179  	{
   180  		description: "Reference name has unallowed special character *",
   181  		data: TestForm{
   182  			BranchName: "*debian/1%1.6.0-2",
   183  		},
   184  		expectedErrors: binding.Errors{
   185  			binding.Error{
   186  				FieldNames:     []string{"BranchName"},
   187  				Classification: ErrGitRefName,
   188  				Message:        "GitRefName",
   189  			},
   190  		},
   191  	},
   192  	{
   193  		description: "Reference name has unallowed special character ?",
   194  		data: TestForm{
   195  			BranchName: "?debian/1%1.6.0-2",
   196  		},
   197  		expectedErrors: binding.Errors{
   198  			binding.Error{
   199  				FieldNames:     []string{"BranchName"},
   200  				Classification: ErrGitRefName,
   201  				Message:        "GitRefName",
   202  			},
   203  		},
   204  	},
   205  	{
   206  		description: "Reference name has unallowed special character ^",
   207  		data: TestForm{
   208  			BranchName: "^debian/1%1.6.0-2",
   209  		},
   210  		expectedErrors: binding.Errors{
   211  			binding.Error{
   212  				FieldNames:     []string{"BranchName"},
   213  				Classification: ErrGitRefName,
   214  				Message:        "GitRefName",
   215  			},
   216  		},
   217  	},
   218  	{
   219  		description: "Reference name has unallowed special character :",
   220  		data: TestForm{
   221  			BranchName: "debian:jessie",
   222  		},
   223  		expectedErrors: binding.Errors{
   224  			binding.Error{
   225  				FieldNames:     []string{"BranchName"},
   226  				Classification: ErrGitRefName,
   227  				Message:        "GitRefName",
   228  			},
   229  		},
   230  	},
   231  	{
   232  		description: "Reference name has unallowed special character (whitespace)",
   233  		data: TestForm{
   234  			BranchName: "debian jessie",
   235  		},
   236  		expectedErrors: binding.Errors{
   237  			binding.Error{
   238  				FieldNames:     []string{"BranchName"},
   239  				Classification: ErrGitRefName,
   240  				Message:        "GitRefName",
   241  			},
   242  		},
   243  	},
   244  	{
   245  		description: "Reference name has unallowed special character [",
   246  		data: TestForm{
   247  			BranchName: "debian[jessie",
   248  		},
   249  		expectedErrors: binding.Errors{
   250  			binding.Error{
   251  				FieldNames:     []string{"BranchName"},
   252  				Classification: ErrGitRefName,
   253  				Message:        "GitRefName",
   254  			},
   255  		},
   256  	},
   257  }
   258  
   259  func Test_GitRefNameValidation(t *testing.T) {
   260  	AddBindingRules()
   261  
   262  	for _, testCase := range gitRefNameValidationTestCases {
   263  		t.Run(testCase.description, func(t *testing.T) {
   264  			performValidationTest(t, testCase)
   265  		})
   266  	}
   267  }