@@ -2,11 +2,14 @@ package golang
22
33import (
44 "fmt"
5+ biutils "github.com/jfrog/build-info-go/utils"
56 "github.com/jfrog/jfrog-cli-core/v2/utils/config"
67 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
7- goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang "
8+ "github.com/jfrog/jfrog-client-go/artifactory/auth "
89 testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
910 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
12+ "net/url"
1013 "os"
1114 "path/filepath"
1215 "strings"
@@ -34,7 +37,7 @@ func TestBuildPackageVersionRequest(t *testing.T) {
3437}
3538
3639func TestGetPackageFilesPath (t * testing.T ) {
37- packageCachePath , err := goutils .GetGoModCachePath ()
40+ packageCachePath , err := biutils .GetGoModCachePath ()
3841 assert .NoError (t , err )
3942 packageName := "github.com/golang/mock/mockgen"
4043 version := "v1.4.1"
@@ -61,7 +64,7 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) {
6164 cleanup := testsutils .SetEnvWithCallbackAndAssert (t , "GOPROXY" , "" )
6265 defer cleanup ()
6366
64- assert .NoError (t , SetArtifactoryAsResolutionServer (server , repo , goutils. GoProxyUrlParams {Direct : true }))
67+ assert .NoError (t , SetArtifactoryAsResolutionServer (server , repo , GoProxyUrlParams {Direct : true }))
6568
6669 serverUrlWithoutHttp := strings .TrimPrefix (server .ArtifactoryUrl , "http://" )
6770 expectedGoProxy := fmt .Sprintf ("http://%s:%s@%sapi/go/%s|direct" , server .User , server .Password , serverUrlWithoutHttp , repo )
@@ -70,9 +73,95 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) {
7073 // Verify that the EndpointPrefix value is correctly added to the GOPROXY.
7174 // In this test case, the endpoint prefix is set to api/curation/audit/.
7275 // This parameter allows downloading dependencies from a custom API instead of the default one.
73- assert .NoError (t , SetArtifactoryAsResolutionServer (server , repo , goutils. GoProxyUrlParams {Direct : true , EndpointPrefix : coreutils .CurationPassThroughApi }))
76+ assert .NoError (t , SetArtifactoryAsResolutionServer (server , repo , GoProxyUrlParams {Direct : true , EndpointPrefix : coreutils .CurationPassThroughApi }))
7477
7578 serverUrlWithoutHttp = strings .TrimPrefix (server .ArtifactoryUrl , "http://" )
7679 expectedGoProxy = fmt .Sprintf ("http://%s:%s@%sapi/curation/audit/api/go/%s|direct" , server .User , server .Password , serverUrlWithoutHttp , repo )
7780 assert .Equal (t , expectedGoProxy , os .Getenv ("GOPROXY" ))
7881}
82+
83+ func TestGetArtifactoryRemoteRepoUrl (t * testing.T ) {
84+ server := & config.ServerDetails {
85+ ArtifactoryUrl : "https://server.com/artifactory" ,
86+ AccessToken : "eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA" ,
87+ }
88+ repoName := "test-repo"
89+ repoUrl , err := getArtifactoryRemoteRepoUrl (server , repoName , GoProxyUrlParams {})
90+ assert .NoError (t , err )
91+ assert .Equal (t , "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@server.com/artifactory/api/go/test-repo" , repoUrl )
92+ }
93+
94+ func TestGetArtifactoryApiUrl (t * testing.T ) {
95+ details := auth .NewArtifactoryDetails ()
96+ details .SetUrl ("https://test.com/artifactory/" )
97+
98+ // Test username and password
99+ details .SetUser ("frog" )
100+ details .SetPassword ("passfrog" )
101+ url , err := getArtifactoryApiUrl ("test-repo" , details , GoProxyUrlParams {})
102+ assert .NoError (t , err )
103+ assert .
Equal (
t ,
"https://frog:[email protected] /artifactory/api/go/test-repo" ,
url )
104+
105+ // Test username and password with EndpointPrefix and direct
106+ details .SetUser ("frog" )
107+ details .SetPassword ("passfrog" )
108+ url , err = getArtifactoryApiUrl ("test-repo" , details , GoProxyUrlParams {EndpointPrefix : "test" , Direct : true })
109+ assert .NoError (t , err )
110+ assert .
Equal (
t ,
"https://frog:[email protected] /artifactory/test/api/go/test-repo|direct" ,
url )
111+
112+ // Test access token
113+ // Set fake access token with username "test"
114+ details .SetUser ("" )
115+ details .SetAccessToken ("eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA" )
116+ url , err = getArtifactoryApiUrl ("test-repo" , details , GoProxyUrlParams {})
117+ assert .NoError (t , err )
118+ assert .Equal (t , "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@test.com/artifactory/api/go/test-repo" , url )
119+
120+ // Test access token with username
121+ // Set fake access token with username "test"
122+ // Expect username to be "frog"
123+ details .SetUser ("frog" )
124+ details .SetAccessToken ("eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA" )
125+ url , err = getArtifactoryApiUrl ("test-repo" , details , GoProxyUrlParams {})
126+ assert .NoError (t , err )
127+ assert .Equal (t , "https://frog:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@test.com/artifactory/api/go/test-repo" , url )
128+ }
129+
130+ func TestGoProxyUrlParams_BuildUrl (t * testing.T ) {
131+ tests := []struct {
132+ name string
133+ RepoName string
134+ Direct bool
135+ EndpointPrefix string
136+ ExpectedUrl string
137+ }{
138+ {
139+ name : "Url Without direct or Prefix" ,
140+ RepoName : "go" ,
141+ ExpectedUrl : "https://test/api/go/go" ,
142+ },
143+ {
144+ name : "Url With direct" ,
145+ RepoName : "go" ,
146+ Direct : true ,
147+ ExpectedUrl : "https://test/api/go/go|direct" ,
148+ },
149+ {
150+ name : "Url With Prefix" ,
151+ RepoName : "go" ,
152+ EndpointPrefix : "prefix" ,
153+ ExpectedUrl : "https://test/prefix/api/go/go" ,
154+ },
155+ }
156+ for _ , tt := range tests {
157+ t .Run (tt .name , func (t * testing.T ) {
158+ remoteUrl , err := url .Parse ("https://test" )
159+ require .NoError (t , err )
160+ gdu := & GoProxyUrlParams {
161+ Direct : tt .Direct ,
162+ EndpointPrefix : tt .EndpointPrefix ,
163+ }
164+ assert .Equalf (t , tt .ExpectedUrl , gdu .BuildUrl (remoteUrl , tt .RepoName ), "BuildUrl(%v, %v)" , remoteUrl , tt .RepoName )
165+ })
166+ }
167+ }
0 commit comments