github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/services/scm_provider/bitbucket_cloud_test.go (about) 1 package scm_provider 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 13 ) 14 15 func TestBitbucketHasRepo(t *testing.T) { 16 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 17 if req.URL.Path == "/repositories/test-owner/testmike/src/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/.gitignore2" { 18 res.WriteHeader(http.StatusNotFound) 19 _, err := res.Write([]byte("")) 20 if err != nil { 21 require.NoError(t, fmt.Errorf("Error in mock response %w", err)) 22 } 23 } 24 if req.URL.Path == "/repositories/test-owner/testmike/src/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/.gitignore" { 25 res.WriteHeader(http.StatusOK) 26 _, err := res.Write([]byte(`{ 27 "mimetype": null, 28 "links": { 29 "self": { 30 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/src/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/.gitignore" 31 }, 32 "meta": { 33 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/src/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/.gitignore?format=meta" 34 }, 35 "history": { 36 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/filehistory/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/.gitignore" 37 } 38 }, 39 "escaped_path": ".gitignore", 40 "path": ".gitignore", 41 "commit": { 42 "type": "commit", 43 "hash": "dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798", 44 "links": { 45 "self": { 46 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 47 }, 48 "html": { 49 "href": "https://bitbucket.org/test-owner/testmike/commits/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 50 } 51 } 52 }, 53 "attributes": [], 54 "type": "commit_file", 55 "size": 624 56 }`)) 57 if err != nil { 58 require.NoError(t, fmt.Errorf("Error in mock response %w", err)) 59 } 60 } 61 })) 62 defer func() { testServer.Close() }() 63 64 t.Setenv("BITBUCKET_API_BASE_URL", testServer.URL) 65 cases := []struct { 66 name, path, repo, owner, sha string 67 status int 68 }{ 69 { 70 name: "exists", 71 owner: "test-owner", 72 repo: "testmike", 73 path: ".gitignore", 74 sha: "dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798", 75 status: http.StatusOK, 76 }, 77 { 78 name: "not exists", 79 owner: "test-owner", 80 repo: "testmike", 81 path: ".gitignore2", 82 sha: "dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798", 83 status: http.StatusNotFound, 84 }, 85 } 86 87 for _, c := range cases { 88 t.Run(c.name, func(t *testing.T) { 89 provider, _ := NewBitBucketCloudProvider(c.owner, "user", "password", false) 90 repo := &Repository{ 91 Organization: c.owner, 92 Repository: c.repo, 93 SHA: c.sha, 94 Branch: "main", 95 } 96 hasPath, err := provider.RepoHasPath(t.Context(), repo, c.path) 97 if err != nil { 98 require.Error(t, fmt.Errorf("Error in test %w", err)) 99 } 100 if c.status != http.StatusOK { 101 assert.False(t, hasPath) 102 } else { 103 assert.True(t, hasPath) 104 } 105 }) 106 } 107 } 108 109 func TestBitbucketListRepos(t *testing.T) { 110 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 111 res.WriteHeader(http.StatusOK) 112 if req.URL.Path == "/repositories/test-owner/testmike/refs/branches" { 113 _, err := res.Write([]byte(`{ 114 "pagelen": 10, 115 "values": [ 116 { 117 "name": "main", 118 "links": { 119 "commits": { 120 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commits/main" 121 }, 122 "self": { 123 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/refs/branches/main" 124 }, 125 "html": { 126 "href": "https://bitbucket.org/test-owner/testmike/branch/main" 127 } 128 }, 129 "default_merge_strategy": "merge_commit", 130 "merge_strategies": [ 131 "merge_commit", 132 "squash", 133 "fast_forward" 134 ], 135 "type": "branch", 136 "target": { 137 "hash": "dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798", 138 "repository": { 139 "links": { 140 "self": { 141 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike" 142 }, 143 "html": { 144 "href": "https://bitbucket.org/test-owner/testmike" 145 }, 146 "avatar": { 147 "href": "https://bytebucket.org/ravatar/%7B76606e75-8aeb-4a87-9396-4abee652ec63%7D?ts=default" 148 } 149 }, 150 "type": "repository", 151 "name": "testMike", 152 "full_name": "test-owner/testmike", 153 "uuid": "{76606e75-8aeb-4a87-9396-4abee652ec63}" 154 }, 155 "links": { 156 "self": { 157 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 158 }, 159 "comments": { 160 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/comments" 161 }, 162 "patch": { 163 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/patch/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 164 }, 165 "html": { 166 "href": "https://bitbucket.org/test-owner/testmike/commits/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 167 }, 168 "diff": { 169 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/diff/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 170 }, 171 "approve": { 172 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/approve" 173 }, 174 "statuses": { 175 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/statuses" 176 } 177 }, 178 "author": { 179 "raw": "Mike Tester <tester@gmail.com>", 180 "type": "author", 181 "user": { 182 "display_name": "Mike Tester", 183 "uuid": "{ca84788f-050b-456b-5cac-93fb4484a686}", 184 "links": { 185 "self": { 186 "href": "https://api.bitbucket.org/2.0/users/%7Bca84788f-050b-456b-5cac-93fb4484a686%7D" 187 }, 188 "html": { 189 "href": "https://bitbucket.org/%7Bca84788f-050b-456b-5cac-93fb4484a686%7D/" 190 }, 191 "avatar": { 192 "href": "https://secure.gravatar.com/avatar/03450fe11788d0dbb39b804110c07b9f?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMM-4.png" 193 } 194 }, 195 "type": "user", 196 "nickname": "Mike Tester", 197 "account_id": "61ec57859d174000690f702b" 198 } 199 }, 200 "parents": [], 201 "date": "2022-03-07T19:37:58+00:00", 202 "message": "Initial commit", 203 "type": "commit" 204 } 205 } 206 ], 207 "page": 1, 208 "size": 1 209 }`)) 210 if err != nil { 211 require.NoError(t, fmt.Errorf("Error in mock response %w", err)) 212 } 213 } 214 if req.URL.Path == "/repositories/test-owner/testmike/refs/branches/main" { 215 _, err := res.Write([]byte(`{ 216 "name": "main", 217 "links": { 218 "commits": { 219 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commits/main" 220 }, 221 "self": { 222 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/refs/branches/main" 223 }, 224 "html": { 225 "href": "https://bitbucket.org/test-owner/testmike/branch/main" 226 } 227 }, 228 "default_merge_strategy": "merge_commit", 229 "merge_strategies": [ 230 "merge_commit", 231 "squash", 232 "fast_forward" 233 ], 234 "type": "branch", 235 "target": { 236 "hash": "dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798", 237 "repository": { 238 "links": { 239 "self": { 240 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike" 241 }, 242 "html": { 243 "href": "https://bitbucket.org/test-owner/testmike" 244 }, 245 "avatar": { 246 "href": "https://bytebucket.org/ravatar/%7B76606e75-8aeb-4a87-9396-4abee652ec63%7D?ts=default" 247 } 248 }, 249 "type": "repository", 250 "name": "testMike", 251 "full_name": "test-owner/testmike", 252 "uuid": "{76606e75-8aeb-4a87-9396-4abee652ec63}" 253 }, 254 "links": { 255 "self": { 256 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 257 }, 258 "comments": { 259 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/comments" 260 }, 261 "patch": { 262 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/patch/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 263 }, 264 "html": { 265 "href": "https://bitbucket.org/test-owner/testmike/commits/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 266 }, 267 "diff": { 268 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/diff/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798" 269 }, 270 "approve": { 271 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/approve" 272 }, 273 "statuses": { 274 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commit/dc1edb6c7d650d8ba67719ddf7b662ad8f8fb798/statuses" 275 } 276 }, 277 "author": { 278 "raw": "Mike Tester <tester@gmail.com>", 279 "type": "author", 280 "user": { 281 "display_name": "Mike Tester", 282 "uuid": "{ca84788f-050b-456b-5cac-93fb4484a686}", 283 "links": { 284 "self": { 285 "href": "https://api.bitbucket.org/2.0/users/%7Bca84788f-050b-456b-5cac-93fb4484a686%7D" 286 }, 287 "html": { 288 "href": "https://bitbucket.org/%7Bca84788f-050b-456b-5cac-93fb4484a686%7D/" 289 }, 290 "avatar": { 291 "href": "https://secure.gravatar.com/avatar/03450fe11788d0dbb39b804110c07b9f?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMM-4.png" 292 } 293 }, 294 "type": "user", 295 "nickname": "Mike Tester", 296 "account_id": "61ec57859d174000690f702b" 297 } 298 }, 299 "parents": [], 300 "date": "2022-03-07T19:37:58+00:00", 301 "message": "Initial commit", 302 "type": "commit" 303 } 304 }`)) 305 if err != nil { 306 require.NoError(t, fmt.Errorf("Error in mock response %w", err)) 307 } 308 } 309 if req.URL.Path == "/repositories/test-owner" { 310 _, err := res.Write([]byte(`{ 311 "pagelen": 10, 312 "values": [ 313 { 314 "scm": "git", 315 "has_wiki": false, 316 "links": { 317 "watchers": { 318 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/watchers" 319 }, 320 "branches": { 321 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/refs/branches" 322 }, 323 "tags": { 324 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/refs/tags" 325 }, 326 "commits": { 327 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/commits" 328 }, 329 "clone": [ 330 { 331 "href": "https://test-owner@bitbucket.org/test-owner/testmike.git", 332 "name": "https" 333 }, 334 { 335 "href": "git@bitbucket.org:test-owner/testmike.git", 336 "name": "ssh" 337 } 338 ], 339 "self": { 340 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike" 341 }, 342 "source": { 343 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/src" 344 }, 345 "html": { 346 "href": "https://bitbucket.org/test-owner/testmike" 347 }, 348 "avatar": { 349 "href": "https://bytebucket.org/ravatar/%7B76606e75-8aeb-4a87-9396-4abee652ec63%7D?ts=default" 350 }, 351 "hooks": { 352 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/hooks" 353 }, 354 "forks": { 355 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/forks" 356 }, 357 "downloads": { 358 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/downloads" 359 }, 360 "pullrequests": { 361 "href": "https://api.bitbucket.org/2.0/repositories/test-owner/testmike/pullrequests" 362 } 363 }, 364 "created_on": "2022-03-07T19:37:58.199968+00:00", 365 "full_name": "test-owner/testmike", 366 "owner": { 367 "display_name": "Mike Tester", 368 "uuid": "{ca84788f-050b-456b-5cac-93fb4484a686}", 369 "links": { 370 "self": { 371 "href": "https://api.bitbucket.org/2.0/users/%7Bca84788f-050b-456b-5cac-93fb4484a686%7D" 372 }, 373 "html": { 374 "href": "https://bitbucket.org/%7Bca84788f-050b-456b-5cac-93fb4484a686%7D/" 375 }, 376 "avatar": { 377 "href": "https://secure.gravatar.com/avatar/03450fe11788d0dbb39b804110c07b9f?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMM-4.png" 378 } 379 }, 380 "type": "user", 381 "nickname": "Mike Tester", 382 "account_id": "61ec57859d174000690f702b" 383 }, 384 "size": 58894, 385 "uuid": "{76606e75-8aeb-4a87-9396-4abee652ec63}", 386 "type": "repository", 387 "website": null, 388 "override_settings": { 389 "branching_model": true, 390 "default_merge_strategy": true, 391 "branch_restrictions": true 392 }, 393 "description": "", 394 "has_issues": false, 395 "slug": "testmike", 396 "is_private": false, 397 "name": "testMike", 398 "language": "", 399 "fork_policy": "allow_forks", 400 "project": { 401 "links": { 402 "self": { 403 "href": "https://api.bitbucket.org/2.0/workspaces/test-owner/projects/TEST" 404 }, 405 "html": { 406 "href": "https://bitbucket.org/test-owner/workspace/projects/TEST" 407 }, 408 "avatar": { 409 "href": "https://bitbucket.org/account/user/test-owner/projects/TEST/avatar/32?ts=1642881431" 410 } 411 }, 412 "type": "project", 413 "name": "test", 414 "key": "TEST", 415 "uuid": "{603a1564-1509-4c97-b2a6-300a3fad2758}" 416 }, 417 "mainbranch": { 418 "type": "branch", 419 "name": "main" 420 }, 421 "workspace": { 422 "slug": "test-owner", 423 "type": "workspace", 424 "name": "Mike Tester", 425 "links": { 426 "self": { 427 "href": "https://api.bitbucket.org/2.0/workspaces/test-owner" 428 }, 429 "html": { 430 "href": "https://bitbucket.org/test-owner/" 431 }, 432 "avatar": { 433 "href": "https://bitbucket.org/workspaces/test-owner/avatar/?ts=1642878863" 434 } 435 }, 436 "uuid": "{ca84788f-050b-456b-5cac-93fb4484a686}" 437 }, 438 "updated_on": "2022-03-07T19:37:59.933133+00:00" 439 } 440 ], 441 "page": 1, 442 "size": 1 443 }`)) 444 if err != nil { 445 require.NoError(t, fmt.Errorf("Error in mock response %w", err)) 446 } 447 } 448 })) 449 defer func() { testServer.Close() }() 450 451 t.Setenv("BITBUCKET_API_BASE_URL", testServer.URL) 452 cases := []struct { 453 name, proto, owner string 454 hasError, allBranches bool 455 branches []string 456 filters []v1alpha1.SCMProviderGeneratorFilter 457 }{ 458 { 459 name: "blank protocol", 460 owner: "test-owner", 461 branches: []string{"main"}, 462 }, 463 { 464 name: "ssh protocol", 465 proto: "ssh", 466 owner: "test-owner", 467 }, 468 { 469 name: "https protocol", 470 proto: "https", 471 owner: "test-owner", 472 }, 473 { 474 name: "other protocol", 475 proto: "other", 476 owner: "test-owner", 477 hasError: true, 478 }, 479 { 480 name: "all branches", 481 allBranches: true, 482 owner: "test-owner", 483 branches: []string{"main"}, 484 }, 485 } 486 487 for _, c := range cases { 488 t.Run(c.name, func(t *testing.T) { 489 provider, _ := NewBitBucketCloudProvider(c.owner, "user", "password", c.allBranches) 490 rawRepos, err := ListRepos(t.Context(), provider, c.filters, c.proto) 491 if c.hasError { 492 require.Error(t, err) 493 } else { 494 require.NoError(t, err) 495 repos := []*Repository{} 496 branches := []string{} 497 for _, r := range rawRepos { 498 if r.Repository == "testmike" { 499 repos = append(repos, r) 500 branches = append(branches, r.Branch) 501 } 502 } 503 assert.NotEmpty(t, repos) 504 for _, b := range c.branches { 505 assert.Contains(t, branches, b) 506 } 507 } 508 }) 509 } 510 }