github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/validation/helpers_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  	"github.com/gitbundle/modules/setting"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func Test_IsValidURL(t *testing.T) {
    17  	cases := []struct {
    18  		description string
    19  		url         string
    20  		valid       bool
    21  	}{
    22  		{
    23  			description: "Empty URL",
    24  			url:         "",
    25  			valid:       false,
    26  		},
    27  		{
    28  			description: "Loopback IPv4 URL",
    29  			url:         "http://127.0.1.1:5678/",
    30  			valid:       true,
    31  		},
    32  		{
    33  			description: "Loopback IPv6 URL",
    34  			url:         "https://[::1]/",
    35  			valid:       true,
    36  		},
    37  		{
    38  			description: "Missing semicolon after schema",
    39  			url:         "http//meh/",
    40  			valid:       false,
    41  		},
    42  	}
    43  
    44  	for _, testCase := range cases {
    45  		t.Run(testCase.description, func(t *testing.T) {
    46  			assert.Equal(t, testCase.valid, IsValidURL(testCase.url))
    47  		})
    48  	}
    49  }
    50  
    51  func Test_IsValidExternalURL(t *testing.T) {
    52  	setting.AppURL = "https://try.gitea.io/"
    53  
    54  	cases := []struct {
    55  		description string
    56  		url         string
    57  		valid       bool
    58  	}{
    59  		{
    60  			description: "Current instance URL",
    61  			url:         "https://try.gitea.io/test",
    62  			valid:       true,
    63  		},
    64  		{
    65  			description: "Loopback IPv4 URL",
    66  			url:         "http://127.0.1.1:5678/",
    67  			valid:       false,
    68  		},
    69  		{
    70  			description: "Current instance API URL",
    71  			url:         "https://try.gitea.io/api/v1/user/follow",
    72  			valid:       false,
    73  		},
    74  		{
    75  			description: "Local network URL",
    76  			url:         "http://192.168.1.2/api/v1/user/follow",
    77  			valid:       true,
    78  		},
    79  		{
    80  			description: "Local URL",
    81  			url:         "http://LOCALHOST:1234/whatever",
    82  			valid:       false,
    83  		},
    84  	}
    85  
    86  	for _, testCase := range cases {
    87  		t.Run(testCase.description, func(t *testing.T) {
    88  			assert.Equal(t, testCase.valid, IsValidExternalURL(testCase.url))
    89  		})
    90  	}
    91  }
    92  
    93  func Test_IsValidExternalTrackerURLFormat(t *testing.T) {
    94  	setting.AppURL = "https://try.gitea.io/"
    95  
    96  	cases := []struct {
    97  		description string
    98  		url         string
    99  		valid       bool
   100  	}{
   101  		{
   102  			description: "Correct external tracker URL with all placeholders",
   103  			url:         "https://github.com/{user}/{repo}/issues/{index}",
   104  			valid:       true,
   105  		},
   106  		{
   107  			description: "Local external tracker URL with all placeholders",
   108  			url:         "https://127.0.0.1/{user}/{repo}/issues/{index}",
   109  			valid:       false,
   110  		},
   111  		{
   112  			description: "External tracker URL with typo placeholder",
   113  			url:         "https://github.com/{user}/{repo/issues/{index}",
   114  			valid:       false,
   115  		},
   116  		{
   117  			description: "External tracker URL with typo placeholder",
   118  			url:         "https://github.com/[user}/{repo/issues/{index}",
   119  			valid:       false,
   120  		},
   121  		{
   122  			description: "External tracker URL with typo placeholder",
   123  			url:         "https://github.com/{user}/repo}/issues/{index}",
   124  			valid:       false,
   125  		},
   126  		{
   127  			description: "External tracker URL missing optional placeholder",
   128  			url:         "https://github.com/{user}/issues/{index}",
   129  			valid:       true,
   130  		},
   131  		{
   132  			description: "External tracker URL missing optional placeholder",
   133  			url:         "https://github.com/{repo}/issues/{index}",
   134  			valid:       true,
   135  		},
   136  		{
   137  			description: "External tracker URL missing optional placeholder",
   138  			url:         "https://github.com/issues/{index}",
   139  			valid:       true,
   140  		},
   141  		{
   142  			description: "External tracker URL missing optional placeholder",
   143  			url:         "https://github.com/issues/{user}",
   144  			valid:       true,
   145  		},
   146  		{
   147  			description: "External tracker URL with similar placeholder names test",
   148  			url:         "https://github.com/user/repo/issues/{index}",
   149  			valid:       true,
   150  		},
   151  	}
   152  
   153  	for _, testCase := range cases {
   154  		t.Run(testCase.description, func(t *testing.T) {
   155  			assert.Equal(t, testCase.valid, IsValidExternalTrackerURLFormat(testCase.url))
   156  		})
   157  	}
   158  }