|
6 | 6 | "net/http/httptest"
|
7 | 7 | "os"
|
8 | 8 | "path/filepath"
|
| 9 | + "strings" |
9 | 10 | "testing"
|
10 | 11 |
|
11 | 12 | "github.com/runatlantis/atlantis/server/events/models"
|
@@ -367,3 +368,159 @@ func TestClient_MarkdownPullLink(t *testing.T) {
|
367 | 368 | exp := "#1"
|
368 | 369 | Equals(t, exp, s)
|
369 | 370 | }
|
| 371 | + |
| 372 | +func TestClient_GetMyUUID(t *testing.T) { |
| 373 | + json, err := os.ReadFile(filepath.Join("testdata", "user.json")) |
| 374 | + Ok(t, err) |
| 375 | + |
| 376 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 377 | + switch r.RequestURI { |
| 378 | + case "/2.0/user": |
| 379 | + w.Write(json) // nolint: errcheck |
| 380 | + return |
| 381 | + default: |
| 382 | + t.Errorf("got unexpected request at %q", r.RequestURI) |
| 383 | + http.Error(w, "not found", http.StatusNotFound) |
| 384 | + return |
| 385 | + } |
| 386 | + })) |
| 387 | + defer testServer.Close() |
| 388 | + |
| 389 | + client := bitbucketcloud.NewClient(http.DefaultClient, "user", "pass", "runatlantis.io") |
| 390 | + client.BaseURL = testServer.URL |
| 391 | + v, _ := client.GetMyUUID() |
| 392 | + Equals(t, v, "{00000000-0000-0000-0000-000000000001}") |
| 393 | +} |
| 394 | + |
| 395 | +func TestClient_GetComment(t *testing.T) { |
| 396 | + json, err := os.ReadFile(filepath.Join("testdata", "comments.json")) |
| 397 | + Ok(t, err) |
| 398 | + |
| 399 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 400 | + switch r.RequestURI { |
| 401 | + case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments": |
| 402 | + w.Write(json) // nolint: errcheck |
| 403 | + return |
| 404 | + default: |
| 405 | + t.Errorf("got unexpected request at %q", r.RequestURI) |
| 406 | + http.Error(w, "not found", http.StatusNotFound) |
| 407 | + return |
| 408 | + } |
| 409 | + })) |
| 410 | + defer testServer.Close() |
| 411 | + |
| 412 | + client := bitbucketcloud.NewClient(http.DefaultClient, "user", "pass", "runatlantis.io") |
| 413 | + client.BaseURL = testServer.URL |
| 414 | + v, _ := client.GetPullRequestComments( |
| 415 | + models.Repo{ |
| 416 | + FullName: "myorg/myrepo", |
| 417 | + Owner: "owner", |
| 418 | + Name: "myrepo", |
| 419 | + CloneURL: "", |
| 420 | + SanitizedCloneURL: "", |
| 421 | + VCSHost: models.VCSHost{ |
| 422 | + Type: models.BitbucketCloud, |
| 423 | + Hostname: "bitbucket.org", |
| 424 | + }, |
| 425 | + }, 5) |
| 426 | + |
| 427 | + Equals(t, len(v), 5) |
| 428 | + exp := "Plan" |
| 429 | + Assert(t, strings.Contains(v[1].Content.Raw, exp), "Comment should contain word \"%s\", has \"%s\"", exp, v[1].Content.Raw) |
| 430 | +} |
| 431 | + |
| 432 | +func TestClient_DeleteComment(t *testing.T) { |
| 433 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 434 | + switch r.RequestURI { |
| 435 | + case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments/1": |
| 436 | + if r.Method == "DELETE" { |
| 437 | + w.WriteHeader(http.StatusNoContent) |
| 438 | + } |
| 439 | + return |
| 440 | + default: |
| 441 | + t.Errorf("got unexpected request at %q", r.RequestURI) |
| 442 | + http.Error(w, "not found", http.StatusNotFound) |
| 443 | + return |
| 444 | + } |
| 445 | + })) |
| 446 | + defer testServer.Close() |
| 447 | + |
| 448 | + client := bitbucketcloud.NewClient(http.DefaultClient, "user", "pass", "runatlantis.io") |
| 449 | + client.BaseURL = testServer.URL |
| 450 | + err := client.DeletePullRequestComment( |
| 451 | + models.Repo{ |
| 452 | + FullName: "myorg/myrepo", |
| 453 | + Owner: "owner", |
| 454 | + Name: "myrepo", |
| 455 | + CloneURL: "", |
| 456 | + SanitizedCloneURL: "", |
| 457 | + VCSHost: models.VCSHost{ |
| 458 | + Type: models.BitbucketCloud, |
| 459 | + Hostname: "bitbucket.org", |
| 460 | + }, |
| 461 | + }, 5, 1) |
| 462 | + Ok(t, err) |
| 463 | +} |
| 464 | + |
| 465 | +func TestClient_HidePRComments(t *testing.T) { |
| 466 | + logger := logging.NewNoopLogger(t) |
| 467 | + comments, err := os.ReadFile(filepath.Join("testdata", "comments.json")) |
| 468 | + Ok(t, err) |
| 469 | + json, err := os.ReadFile(filepath.Join("testdata", "user.json")) |
| 470 | + Ok(t, err) |
| 471 | + |
| 472 | + called := 0 |
| 473 | + |
| 474 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 475 | + switch r.RequestURI { |
| 476 | + // we have two comments in the test file |
| 477 | + // The code is going to delete them all and then create a new one |
| 478 | + case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments/498931882": |
| 479 | + if r.Method == "DELETE" { |
| 480 | + w.WriteHeader(http.StatusNoContent) |
| 481 | + } |
| 482 | + w.Write([]byte("")) // nolint: errcheck |
| 483 | + called += 1 |
| 484 | + return |
| 485 | + // This is the second one |
| 486 | + case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments/498931784": |
| 487 | + if r.Method == "DELETE" { |
| 488 | + http.Error(w, "", http.StatusNoContent) |
| 489 | + } |
| 490 | + w.Write([]byte("")) // nolint: errcheck |
| 491 | + called += 1 |
| 492 | + return |
| 493 | + case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments/49893111": |
| 494 | + Assert(t, r.Method != "DELETE", "Shouldn't delete this one") |
| 495 | + return |
| 496 | + case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments": |
| 497 | + w.Write(comments) // nolint: errcheck |
| 498 | + return |
| 499 | + case "/2.0/user": |
| 500 | + w.Write(json) // nolint: errcheck |
| 501 | + return |
| 502 | + default: |
| 503 | + t.Errorf("got unexpected request at %q", r.RequestURI) |
| 504 | + http.Error(w, "not found", http.StatusNotFound) |
| 505 | + return |
| 506 | + } |
| 507 | + })) |
| 508 | + defer testServer.Close() |
| 509 | + |
| 510 | + client := bitbucketcloud.NewClient(http.DefaultClient, "user", "pass", "runatlantis.io") |
| 511 | + client.BaseURL = testServer.URL |
| 512 | + err = client.HidePrevCommandComments(logger, |
| 513 | + models.Repo{ |
| 514 | + FullName: "myorg/myrepo", |
| 515 | + Owner: "owner", |
| 516 | + Name: "myrepo", |
| 517 | + CloneURL: "", |
| 518 | + SanitizedCloneURL: "", |
| 519 | + VCSHost: models.VCSHost{ |
| 520 | + Type: models.BitbucketCloud, |
| 521 | + Hostname: "bitbucket.org", |
| 522 | + }, |
| 523 | + }, 5, "plan", "") |
| 524 | + Ok(t, err) |
| 525 | + Equals(t, 2, called) |
| 526 | +} |
0 commit comments