code.gitea.io/gitea@v1.19.3/modules/validation/helpers_test.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package validation
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/modules/setting"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func Test_IsValidURL(t *testing.T) {
    15  	cases := []struct {
    16  		description string
    17  		url         string
    18  		valid       bool
    19  	}{
    20  		{
    21  			description: "Empty URL",
    22  			url:         "",
    23  			valid:       false,
    24  		},
    25  		{
    26  			description: "Loopback IPv4 URL",
    27  			url:         "http://127.0.1.1:5678/",
    28  			valid:       true,
    29  		},
    30  		{
    31  			description: "Loopback IPv6 URL",
    32  			url:         "https://[::1]/",
    33  			valid:       true,
    34  		},
    35  		{
    36  			description: "Missing semicolon after schema",
    37  			url:         "http//meh/",
    38  			valid:       false,
    39  		},
    40  	}
    41  
    42  	for _, testCase := range cases {
    43  		t.Run(testCase.description, func(t *testing.T) {
    44  			assert.Equal(t, testCase.valid, IsValidURL(testCase.url))
    45  		})
    46  	}
    47  }
    48  
    49  func Test_IsValidExternalURL(t *testing.T) {
    50  	setting.AppURL = "https://try.gitea.io/"
    51  
    52  	cases := []struct {
    53  		description string
    54  		url         string
    55  		valid       bool
    56  	}{
    57  		{
    58  			description: "Current instance URL",
    59  			url:         "https://try.gitea.io/test",
    60  			valid:       true,
    61  		},
    62  		{
    63  			description: "Loopback IPv4 URL",
    64  			url:         "http://127.0.1.1:5678/",
    65  			valid:       false,
    66  		},
    67  		{
    68  			description: "Current instance API URL",
    69  			url:         "https://try.gitea.io/api/v1/user/follow",
    70  			valid:       false,
    71  		},
    72  		{
    73  			description: "Local network URL",
    74  			url:         "http://192.168.1.2/api/v1/user/follow",
    75  			valid:       true,
    76  		},
    77  		{
    78  			description: "Local URL",
    79  			url:         "http://LOCALHOST:1234/whatever",
    80  			valid:       false,
    81  		},
    82  	}
    83  
    84  	for _, testCase := range cases {
    85  		t.Run(testCase.description, func(t *testing.T) {
    86  			assert.Equal(t, testCase.valid, IsValidExternalURL(testCase.url))
    87  		})
    88  	}
    89  }
    90  
    91  func Test_IsValidExternalTrackerURLFormat(t *testing.T) {
    92  	setting.AppURL = "https://try.gitea.io/"
    93  
    94  	cases := []struct {
    95  		description string
    96  		url         string
    97  		valid       bool
    98  	}{
    99  		{
   100  			description: "Correct external tracker URL with all placeholders",
   101  			url:         "https://github.com/{user}/{repo}/issues/{index}",
   102  			valid:       true,
   103  		},
   104  		{
   105  			description: "Local external tracker URL with all placeholders",
   106  			url:         "https://127.0.0.1/{user}/{repo}/issues/{index}",
   107  			valid:       false,
   108  		},
   109  		{
   110  			description: "External tracker URL with typo placeholder",
   111  			url:         "https://github.com/{user}/{repo/issues/{index}",
   112  			valid:       false,
   113  		},
   114  		{
   115  			description: "External tracker URL with typo placeholder",
   116  			url:         "https://github.com/[user}/{repo/issues/{index}",
   117  			valid:       false,
   118  		},
   119  		{
   120  			description: "External tracker URL with typo placeholder",
   121  			url:         "https://github.com/{user}/repo}/issues/{index}",
   122  			valid:       false,
   123  		},
   124  		{
   125  			description: "External tracker URL missing optional placeholder",
   126  			url:         "https://github.com/{user}/issues/{index}",
   127  			valid:       true,
   128  		},
   129  		{
   130  			description: "External tracker URL missing optional placeholder",
   131  			url:         "https://github.com/{repo}/issues/{index}",
   132  			valid:       true,
   133  		},
   134  		{
   135  			description: "External tracker URL missing optional placeholder",
   136  			url:         "https://github.com/issues/{index}",
   137  			valid:       true,
   138  		},
   139  		{
   140  			description: "External tracker URL missing optional placeholder",
   141  			url:         "https://github.com/issues/{user}",
   142  			valid:       true,
   143  		},
   144  		{
   145  			description: "External tracker URL with similar placeholder names test",
   146  			url:         "https://github.com/user/repo/issues/{index}",
   147  			valid:       true,
   148  		},
   149  	}
   150  
   151  	for _, testCase := range cases {
   152  		t.Run(testCase.description, func(t *testing.T) {
   153  			assert.Equal(t, testCase.valid, IsValidExternalTrackerURLFormat(testCase.url))
   154  		})
   155  	}
   156  }
   157  
   158  func TestIsValidUsername(t *testing.T) {
   159  	tests := []struct {
   160  		arg  string
   161  		want bool
   162  	}{
   163  		{arg: "a", want: true},
   164  		{arg: "abc", want: true},
   165  		{arg: "0.b-c", want: true},
   166  		{arg: "a.b-c_d", want: true},
   167  		{arg: "", want: false},
   168  		{arg: ".abc", want: false},
   169  		{arg: "abc.", want: false},
   170  		{arg: "a..bc", want: false},
   171  		{arg: "a...bc", want: false},
   172  		{arg: "a.-bc", want: false},
   173  		{arg: "a._bc", want: false},
   174  		{arg: "a_-bc", want: false},
   175  		{arg: "a/bc", want: false},
   176  		{arg: "☁️", want: false},
   177  		{arg: "-", want: false},
   178  		{arg: "--diff", want: false},
   179  		{arg: "-im-here", want: false},
   180  		{arg: "a space", want: false},
   181  	}
   182  	for _, tt := range tests {
   183  		t.Run(tt.arg, func(t *testing.T) {
   184  			assert.Equalf(t, tt.want, IsValidUsername(tt.arg), "IsValidUsername(%v)", tt.arg)
   185  		})
   186  	}
   187  }