Skip to content

Commit 664d53c

Browse files
author
Edvard Makhlin
committed
add tests
1 parent e2da6e1 commit 664d53c

File tree

3 files changed

+457
-0
lines changed

3 files changed

+457
-0
lines changed

server/events/vcs/bitbucketcloud/client_test.go

+152
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http/httptest"
77
"os"
88
"path/filepath"
9+
"strings"
910
"testing"
1011

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

0 commit comments

Comments
 (0)