From 1255b156aedb7dfe7517cd5bdba7e68191b7cbdf Mon Sep 17 00:00:00 2001 From: Hitesh Aringa Date: Tue, 16 Apr 2024 18:09:04 -0700 Subject: [PATCH] feat: fixed harness UTs based on new API convention (#303) --- scm/driver/harness/content_test.go | 30 ++++++++++++++++++++----- scm/driver/harness/git_test.go | 36 +++++++++++++++++++++++++----- scm/driver/harness/pr_test.go | 21 +++++++++++++---- scm/driver/harness/repo.go | 4 +--- scm/driver/harness/repo_test.go | 36 +++++++++++++++++++++++++----- 5 files changed, 103 insertions(+), 24 deletions(-) diff --git a/scm/driver/harness/content_test.go b/scm/driver/harness/content_test.go index 6a84ec797..f59776f2c 100644 --- a/scm/driver/harness/content_test.go +++ b/scm/driver/harness/content_test.go @@ -32,7 +32,11 @@ func TestContentFind(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/content/README.md"). + Get("/gateway/code/api/v1/repos/thomas/content/README.md"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("plain/text"). File("testdata/content.json") @@ -68,7 +72,11 @@ func TestContentCreate(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/commits"). + Post("/gateway/code/api/v1/repos/thomas/commits"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("plain/text"). BodyString("{\"commit_id\":\"20ecde1f8c277da0e91750bef9f3b88f228d86db\"}") @@ -105,7 +113,11 @@ func TestContentUpdate(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/commits"). + Post("/gateway/code/api/v1/repos/thomas/commits"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("plain/text"). BodyString("{\"commit_id\":\"20ecde1f8c277da0e91750bef9f3b88f228d86db\"}") @@ -143,7 +155,11 @@ func TestContentDelete(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/commits"). + Post("/gateway/code/api/v1/repos/thomas/commits"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("plain/text"). BodyString("{\"commit_id\":\"20ecde1f8c277da0e91750bef9f3b88f228d86db\"}") @@ -178,7 +194,11 @@ func TestContentList(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/content/docker"). + Get("/gateway/code/api/v1/repos/thomas/content/docker"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/content_list.json") diff --git a/scm/driver/harness/git_test.go b/scm/driver/harness/git_test.go index 515705068..dfbd5a99e 100644 --- a/scm/driver/harness/git_test.go +++ b/scm/driver/harness/git_test.go @@ -24,7 +24,11 @@ func TestListCommits(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/commits"). + Get("/gateway/code/api/v1/repos/thomas/commits"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/commits.json") @@ -66,7 +70,11 @@ func TestFindCommit(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/commits/1d640265d8bdd818175fa736f0fcbad2c9b716c9"). + Get("/gateway/code/api/v1/repos/thomas/commits/1d640265d8bdd818175fa736f0fcbad2c9b716c9"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/commit.json") @@ -104,7 +112,11 @@ func TestFindBranch(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/branches/main"). + Get("/gateway/code/api/v1/repos/thomas/branches/main"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/branch.json") @@ -142,7 +154,11 @@ func TestListBranches(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/branches"). + Get("/gateway/code/api/v1/repos/thomas/branches"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/branches.json") @@ -180,7 +196,11 @@ func TestCreateBranch(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/branches"). + Post("/gateway/code/api/v1/repos/thomas/branches"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/branch.json") @@ -215,7 +235,11 @@ func TestCompareChanges(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get(fmt.Sprintf("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/diff/%s...%s", source, target)). + Get(fmt.Sprintf("/gateway/code/api/v1/repos/thomas/diff/%s...%s", source, target)). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/gitdiff.json") diff --git a/scm/driver/harness/pr_test.go b/scm/driver/harness/pr_test.go index 951690164..fa26ebb4c 100644 --- a/scm/driver/harness/pr_test.go +++ b/scm/driver/harness/pr_test.go @@ -8,11 +8,12 @@ import ( "context" "encoding/json" "fmt" - "github.com/google/go-cmp/cmp/cmpopts" "io/ioutil" "net/http" "testing" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/drone/go-scm/scm" "github.com/drone/go-scm/scm/transport" "github.com/google/go-cmp/cmp" @@ -24,7 +25,11 @@ func TestPRFind(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/pullreq/1"). + Get("/gateway/code/api/v1/repos/thomas/pullreq/1"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("plain/text"). File("testdata/pr.json") @@ -63,7 +68,11 @@ func TestPRCommits(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/pullreq/1/commits"). + Get("/gateway/code/api/v1/repos/thomas/pullreq/1/commits"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("plain/text"). File("testdata/pr_commits.json") @@ -100,7 +109,11 @@ func TestPRCommits(t *testing.T) { func TestPullCreate(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/pullreq"). + Post("/gateway/code/api/v1/repos/thomas/pullreq"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("plain/text"). File("testdata/pr.json") diff --git a/scm/driver/harness/repo.go b/scm/driver/harness/repo.go index dc489a267..cef50eff3 100644 --- a/scm/driver/harness/repo.go +++ b/scm/driver/harness/repo.go @@ -97,9 +97,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input * in.Secret = input.Secret in.Insecure = input.SkipVerify in.URL = input.Target - in.Triggers = append( - input.NativeEvents, - ) + in.Triggers = input.NativeEvents out := new(hook) res, err := s.client.do(ctx, "POST", path, in, out) return convertHook(out), res, err diff --git a/scm/driver/harness/repo_test.go b/scm/driver/harness/repo_test.go index d482d4bcb..1fc861084 100644 --- a/scm/driver/harness/repo_test.go +++ b/scm/driver/harness/repo_test.go @@ -24,7 +24,11 @@ func TestRepositoryFind(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/demo/+"). + Get("/gateway/code/api/v1/repos/demo"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/repo.json") @@ -58,11 +62,15 @@ func TestRepositoryList(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/spaces/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/+/repos"). + Get("/gateway/code/api/v1/repos"). MatchParam("page", "1"). MatchParam("limit", "20"). MatchParam("sort", "path"). MatchParam("order", "asc"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/repos.json") @@ -100,7 +108,11 @@ func TestRepositoryHookList(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks"). + Get("/gateway/code/api/v1/repos/thomas/webhooks"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). MatchParam("page", "1"). MatchParam("limit", "30"). MatchParam("sort", "display_name"). @@ -138,7 +150,11 @@ func TestRepositoryFindHook(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks/6"). + Get("/gateway/code/api/v1/repos/thomas/webhooks/6"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/hook.json") @@ -172,7 +188,11 @@ func TestRepositoryHookCreateDelete(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks"). + Post("/gateway/code/api/v1/repos/thomas/webhooks"). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(200). Type("application/json"). File("testdata/hook_create.json") @@ -210,7 +230,11 @@ func TestRepositoryHookCreateDelete(t *testing.T) { defer gock.Off() gock.New(gockOrigin). - Delete(fmt.Sprintf("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks/%s", got.ID)). + Delete(fmt.Sprintf("/gateway/code/api/v1/repos/thomas/webhooks/%s", got.ID)). + MatchParam("accountIdentifier", "px7xd_BFRCi-pfWPYXVjvw"). + MatchParam("orgIdentifier", "default"). + MatchParam("projectIdentifier", "codeciintegration"). + MatchParam("routingId", "px7xd_BFRCi-pfWPYXVjvw"). Reply(204) } client, _ = New(gockOrigin, harnessOrg, harnessAccount, harnessProject)