@@ -55,9 +55,9 @@ func TestGetTektonDir(t *testing.T) {
5555 observer , _ := zapobserver .New (zap .InfoLevel )
5656 logger := zap .New (observer ).Sugar ()
5757 ctx , _ := rtesting .SetupFakeContext (t )
58- client , mux , tearDown := bbtest .SetupBBServerClient (ctx )
58+ client , mux , tearDown , tURL := bbtest .SetupBBServerClient (ctx )
5959 defer tearDown ()
60- v := & Provider {Logger : logger , Client : client , projectKey : tt .event .Organization }
60+ v := & Provider {Logger : logger , baseURL : tURL , Client : client , projectKey : tt .event .Organization }
6161 bbtest .MuxDirContent (t , mux , tt .event , tt .testDirPath , tt .path )
6262 content , err := v .GetTektonDir (ctx , tt .event , tt .path , "" )
6363 if tt .wantErr {
@@ -168,7 +168,7 @@ func TestCreateStatus(t *testing.T) {
168168 for _ , tt := range tests {
169169 t .Run (tt .name , func (t * testing.T ) {
170170 ctx , _ := rtesting .SetupFakeContext (t )
171- client , mux , tearDown := bbtest .SetupBBServerClient (ctx )
171+ client , mux , tearDown , tURL := bbtest .SetupBBServerClient (ctx )
172172 defer tearDown ()
173173 if tt .nilClient {
174174 client = nil
@@ -177,6 +177,7 @@ func TestCreateStatus(t *testing.T) {
177177 event .EventType = "pull_request"
178178 event .Provider .Token = "token"
179179 v := & Provider {
180+ baseURL : tURL ,
180181 Client : client ,
181182 pullRequestNumber : pullRequestNumber ,
182183 projectKey : event .Organization ,
@@ -229,9 +230,9 @@ func TestGetFileInsideRepo(t *testing.T) {
229230 for _ , tt := range tests {
230231 t .Run (tt .name , func (t * testing.T ) {
231232 ctx , _ := rtesting .SetupFakeContext (t )
232- client , mux , tearDown := bbtest .SetupBBServerClient (ctx )
233+ client , mux , tearDown , tURL := bbtest .SetupBBServerClient (ctx )
233234 defer tearDown ()
234- v := & Provider {Client : client , defaultBranchLatestCommit : "1234" , projectKey : tt .event .Organization }
235+ v := & Provider {Client : client , baseURL : tURL , defaultBranchLatestCommit : "1234" , projectKey : tt .event .Organization }
235236 bbtest .MuxFiles (t , mux , tt .event , tt .targetbranch , filepath .Dir (tt .path ), tt .filescontents )
236237 fc , err := v .GetFileInsideRepo (ctx , tt .event , tt .path , tt .targetbranch )
237238 assert .NilError (t , err )
@@ -246,11 +247,12 @@ func TestSetClient(t *testing.T) {
246247 apiURL string
247248 opts * info.Event
248249 wantErrSubstr string
250+ muxUser func (w http.ResponseWriter , r * http.Request )
249251 }{
250252 {
251253 name : "bad/no username" ,
252254 opts : info .NewEvent (),
253- wantErrSubstr : "no provider .user" ,
255+ wantErrSubstr : "no spec.git_provider .user" ,
254256 },
255257 {
256258 name : "bad/no secret" ,
@@ -259,7 +261,7 @@ func TestSetClient(t *testing.T) {
259261 User : "foo" ,
260262 },
261263 },
262- wantErrSubstr : "no provider .secret" ,
264+ wantErrSubstr : "no spec.git_provider .secret" ,
263265 },
264266 {
265267 name : "bad/no url" ,
@@ -269,7 +271,39 @@ func TestSetClient(t *testing.T) {
269271 Token : "bar" ,
270272 },
271273 },
272- wantErrSubstr : "no provider.url" ,
274+ wantErrSubstr : "no spec.git_provider.url" ,
275+ },
276+ {
277+ name : "bad/invalid user" ,
278+ opts : & info.Event {
279+ Provider : & info.Provider {
280+ User : "foo" ,
281+ Token : "bar" ,
282+ URL : "https://foo.bar" ,
283+ },
284+ },
285+ muxUser : func (w http.ResponseWriter , _ * http.Request ) {
286+ w .WriteHeader (http .StatusUnauthorized )
287+ _ , _ = w .Write ([]byte (`{"errors": [{"message": "Unauthorized"}]}` ))
288+ },
289+ apiURL : "https://foo.bar/rest" ,
290+ wantErrSubstr : "cannot get user foo with token" ,
291+ },
292+ {
293+ name : "bad/unknown error" ,
294+ opts : & info.Event {
295+ Provider : & info.Provider {
296+ User : "foo" ,
297+ Token : "bar" ,
298+ URL : "https://foo.bar" ,
299+ },
300+ },
301+ muxUser : func (w http.ResponseWriter , _ * http.Request ) {
302+ w .WriteHeader (http .StatusInternalServerError )
303+ _ , _ = w .Write ([]byte (`{"errors": [{"message": "Internal Server Error"}]}` ))
304+ },
305+ apiURL : "https://foo.bar/rest" ,
306+ wantErrSubstr : "cannot get user foo: Status: 500" ,
273307 },
274308 {
275309 name : "good/url append /rest" ,
@@ -280,13 +314,21 @@ func TestSetClient(t *testing.T) {
280314 URL : "https://foo.bar" ,
281315 },
282316 },
317+ muxUser : func (w http.ResponseWriter , _ * http.Request ) {
318+ fmt .Fprint (w , `{"name": "foo"}` )
319+ },
283320 apiURL : "https://foo.bar/rest" ,
284321 },
285322 }
286323 for _ , tt := range tests {
287324 t .Run (tt .name , func (t * testing.T ) {
288325 ctx , _ := rtesting .SetupFakeContext (t )
289- v := & Provider {}
326+ bbclient , mux , tearDown , tURL := bbtest .SetupBBServerClient (ctx )
327+ defer tearDown ()
328+ if tt .muxUser != nil {
329+ mux .HandleFunc ("/users/foo" , tt .muxUser )
330+ }
331+ v := & Provider {Client : bbclient , baseURL : tURL }
290332 err := v .SetClient (ctx , nil , tt .opts , nil , nil )
291333 if tt .wantErrSubstr != "" {
292334 assert .ErrorContains (t , err , tt .wantErrSubstr )
@@ -299,7 +341,6 @@ func TestSetClient(t *testing.T) {
299341}
300342
301343func TestGetCommitInfo (t * testing.T ) {
302- defaultBaseURL := "https://base"
303344 tests := []struct {
304345 name string
305346 event * info.Event
@@ -325,11 +366,11 @@ func TestGetCommitInfo(t *testing.T) {
325366 for _ , tt := range tests {
326367 t .Run (tt .name , func (t * testing.T ) {
327368 ctx , _ := rtesting .SetupFakeContext (t )
328- bbclient , mux , tearDown := bbtest .SetupBBServerClient (ctx )
369+ bbclient , mux , tearDown , tURL := bbtest .SetupBBServerClient (ctx )
329370 bbtest .MuxCommitInfo (t , mux , tt .event , tt .commit )
330371 bbtest .MuxDefaultBranch (t , mux , tt .event , tt .defaultBranch , tt .latestCommit )
331372 defer tearDown ()
332- v := & Provider {Client : bbclient , baseURL : defaultBaseURL , projectKey : tt .event .Organization }
373+ v := & Provider {Client : bbclient , baseURL : tURL , projectKey : tt .event .Organization }
333374 err := v .GetCommitInfo (ctx , tt .event )
334375 assert .NilError (t , err )
335376 assert .Equal (t , tt .defaultBranch , tt .event .DefaultBranch )
0 commit comments