code.gitea.io/gitea@v1.19.3/modules/util/sanitize_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package util
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestSanitizeErrorCredentialURLs(t *testing.T) {
    14  	err := errors.New("error with https://a@b.com")
    15  	se := SanitizeErrorCredentialURLs(err)
    16  	assert.Equal(t, "error with https://"+userPlaceholder+"@b.com", se.Error())
    17  }
    18  
    19  func TestSanitizeCredentialURLs(t *testing.T) {
    20  	cases := []struct {
    21  		input    string
    22  		expected string
    23  	}{
    24  		{
    25  			"https://github.com/go-gitea/test_repo.git",
    26  			"https://github.com/go-gitea/test_repo.git",
    27  		},
    28  		{
    29  			"https://mytoken@github.com/go-gitea/test_repo.git",
    30  			"https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
    31  		},
    32  		{
    33  			"https://user:password@github.com/go-gitea/test_repo.git",
    34  			"https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
    35  		},
    36  		{
    37  			"ftp://x@",
    38  			"ftp://" + userPlaceholder + "@",
    39  		},
    40  		{
    41  			"ftp://x/@",
    42  			"ftp://x/@",
    43  		},
    44  		{
    45  			"ftp://u@x/@", // test multiple @ chars
    46  			"ftp://" + userPlaceholder + "@x/@",
    47  		},
    48  		{
    49  			"😊ftp://u@x😊", // test unicode
    50  			"😊ftp://" + userPlaceholder + "@x😊",
    51  		},
    52  		{
    53  			"://@",
    54  			"://@",
    55  		},
    56  		{
    57  			"//u:p@h", // do not process URLs without explicit scheme, they are not treated as "valid" URLs because there is no scheme context in string
    58  			"//u:p@h",
    59  		},
    60  		{
    61  			"s://u@h", // the minimal pattern to be sanitized
    62  			"s://" + userPlaceholder + "@h",
    63  		},
    64  		{
    65  			"URLs in log https://u:b@h and https://u:b@h:80/, with https://h.com and u@h.com",
    66  			"URLs in log https://" + userPlaceholder + "@h and https://" + userPlaceholder + "@h:80/, with https://h.com and u@h.com",
    67  		},
    68  	}
    69  
    70  	for n, c := range cases {
    71  		result := SanitizeCredentialURLs(c.input)
    72  		assert.Equal(t, c.expected, result, "case %d: error should match", n)
    73  	}
    74  }