github.com/cobalt77/jfrog-client-go@v0.14.5/utils/regexputils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/cobalt77/jfrog-client-go/utils/log"
     9  	gofrogio "github.com/jfrog/gofrog/io"
    10  )
    11  
    12  func init() {
    13  	log.SetLogger(log.NewLogger(log.DEBUG, nil))
    14  }
    15  
    16  func TestRemoveCredentialsFromLine(t *testing.T) {
    17  	regExpProtocol, err := GetRegExp(CredentialsInUrlRegexp)
    18  	if err != nil {
    19  		t.Error(err)
    20  	}
    21  
    22  	tests := []struct {
    23  		name         string
    24  		regex        gofrogio.CmdOutputPattern
    25  		expectedLine string
    26  		matched      bool
    27  	}{
    28  		{"http", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line http://***.***@127.0.0.1:8081/artifactory/path/to/repo", true},
    29  		{"https", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://***.***@127.0.0.1:8081/artifactory/path/to/repo", true},
    30  		{"Special characters 1", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://u-s!<e>_r:!p-a&%%s%sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://***.***@127.0.0.1:8081/artifactory/path/to/repo", true},
    31  		{"Special characters 2", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://!user:[p]a(s)sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://***.***@127.0.0.1:8081/artifactory/path/to/repo", true},
    32  		{"No credentials", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", false},
    33  		{"No http", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line"}, "This is an example line", false},
    34  	}
    35  	for _, test := range tests {
    36  		t.Run(test.name, func(t *testing.T) {
    37  			test.regex.MatchedResults = test.regex.RegExp.FindStringSubmatch(test.regex.Line)
    38  			if test.matched && len(test.regex.MatchedResults) > 3 {
    39  				t.Error(fmt.Sprintf("Expected to find 3 results, however, found %d.", len(test.regex.MatchedResults)))
    40  			}
    41  			if test.matched && test.regex.MatchedResults[0] == "" {
    42  				t.Error("Expected to find a match.")
    43  			}
    44  			if test.matched {
    45  				actual := MaskCredentials(test.regex.Line, test.regex.MatchedResults[0])
    46  				if !strings.EqualFold(actual, test.expectedLine) {
    47  					t.Errorf("Expected: %s, The Regex found %s and the masked line: %s", test.expectedLine, test.regex.MatchedResults[0], actual)
    48  				}
    49  			}
    50  			if !test.matched && len(test.regex.MatchedResults) != 0 {
    51  				t.Error("Expected to find zero match, found:", test.regex.MatchedResults[0])
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestReturnErrorOnNotFound(t *testing.T) {
    58  	regExpProtocol, err := GetRegExp(`^go: ([^\/\r\n]+\/[^\r\n\s:]*).*(404( Not Found)?[\s]?)$`)
    59  	if err != nil {
    60  		t.Error(err)
    61  	}
    62  
    63  	tests := []struct {
    64  		name  string
    65  		regex gofrogio.CmdOutputPattern
    66  		error bool
    67  	}{
    68  		{"Without Error", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/cobalt77/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, false},
    69  		{"With Error No Response Message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/cobalt77/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo: 404"}, true},
    70  		{"With Error With response message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/cobalt77/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo: 404 Not Found"}, true},
    71  		{"On Different Message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: finding github.com/elazarl/go-bindata-assetfs v0.0.0-20151224045452-57eb5e1fc594"}, false},
    72  	}
    73  	for _, test := range tests {
    74  		t.Run(test.name, func(t *testing.T) {
    75  			test.regex.MatchedResults = test.regex.RegExp.FindStringSubmatch(test.regex.Line)
    76  			if test.error && len(test.regex.MatchedResults) < 3 {
    77  				t.Error(fmt.Sprintf("Expected to find at least 3 results, however, found %d.", len(test.regex.MatchedResults)))
    78  			}
    79  			if test.error && test.regex.MatchedResults[0] == "" {
    80  				t.Error("Expected to find 404 not found, found nothing.")
    81  			}
    82  			if !test.error && len(test.regex.MatchedResults) != 0 {
    83  				t.Error("Expected regex to return empty result. Got:", test.regex.MatchedResults[0])
    84  			}
    85  		})
    86  	}
    87  }