From 510b801d4adaeb47993c0aa5e4715953bb30aefe Mon Sep 17 00:00:00 2001 From: Gosha Date: Sun, 3 Sep 2023 06:47:29 +0300 Subject: [PATCH] fix: bitbucket list files test Signed-off-by: Gosha --- pkg/git_provider/bitbucket.go | 22 +++++------ pkg/git_provider/bitbucket_test.go | 60 +++++++++++++++++++++++++++++ pkg/git_provider/bitbucket_utils.go | 3 +- pkg/git_provider/test_utils.go | 25 +++++++++--- 4 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 pkg/git_provider/bitbucket_test.go diff --git a/pkg/git_provider/bitbucket.go b/pkg/git_provider/bitbucket.go index 70cc7658..00499495 100644 --- a/pkg/git_provider/bitbucket.go +++ b/pkg/git_provider/bitbucket.go @@ -15,7 +15,7 @@ import ( "strings" ) -type BitbucketServerClientImpl struct { +type BitbucketClientImpl struct { client *bitbucket.Client cfg *conf.GlobalConfig HooksHashTable map[string]int64 @@ -29,14 +29,14 @@ func NewBitbucketServerClient(cfg *conf.GlobalConfig) (Client, error) { return nil, err } - return &BitbucketServerClientImpl{ + return &BitbucketClientImpl{ client: client, cfg: cfg, HooksHashTable: make(map[string]int64), }, err } -func (b BitbucketServerClientImpl) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) { +func (b BitbucketClientImpl) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) { var filesList []string fileOptions := bitbucket.RepositoryFilesOptions{ Owner: b.cfg.GitProviderConfig.OrgName, @@ -58,7 +58,7 @@ func (b BitbucketServerClientImpl) ListFiles(ctx *context.Context, repo string, return filesList, nil } -func (b BitbucketServerClientImpl) GetFile(ctx *context.Context, repo string, branch string, path string) (*CommitFile, error) { +func (b BitbucketClientImpl) GetFile(ctx *context.Context, repo string, branch string, path string) (*CommitFile, error) { fileOptions := bitbucket.RepositoryFilesOptions{ Owner: b.cfg.GitProviderConfig.OrgName, RepoSlug: repo, @@ -78,7 +78,7 @@ func (b BitbucketServerClientImpl) GetFile(ctx *context.Context, repo string, br }, nil } -func (b BitbucketServerClientImpl) GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) { +func (b BitbucketClientImpl) GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) { var commitFiles []*CommitFile for _, path := range paths { file, err := b.GetFile(ctx, repo, branch, path) @@ -94,7 +94,7 @@ func (b BitbucketServerClientImpl) GetFiles(ctx *context.Context, repo string, b return commitFiles, nil } -func (b BitbucketServerClientImpl) SetWebhook(ctx *context.Context, repo *string) (*HookWithStatus, error) { +func (b BitbucketClientImpl) SetWebhook(ctx *context.Context, repo *string) (*HookWithStatus, error) { webhookOptions := &bitbucket.WebhooksOptions{ Owner: b.cfg.GitProviderConfig.OrgName, RepoSlug: *repo, @@ -139,12 +139,12 @@ func (b BitbucketServerClientImpl) SetWebhook(ctx *context.Context, repo *string }, nil } -func (b BitbucketServerClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStatus) error { +func (b BitbucketClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStatus) error { //TODO implement me panic("implement me") } -func (b BitbucketServerClientImpl) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) { +func (b BitbucketClientImpl) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) { var webhookPayload *WebhookPayload var buf bytes.Buffer @@ -192,7 +192,7 @@ func (b BitbucketServerClientImpl) HandlePayload(ctx *context.Context, request * return webhookPayload, nil } -func (b BitbucketServerClientImpl) SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error { +func (b BitbucketClientImpl) SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error { commitOptions := bitbucket.CommitsOptions{ Owner: b.cfg.GitProviderConfig.OrgName, RepoSlug: *repo, @@ -212,12 +212,12 @@ func (b BitbucketServerClientImpl) SetStatus(ctx *context.Context, repo *string, return nil } -func (b BitbucketServerClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus) error { +func (b BitbucketClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus) error { //TODO implement me panic("implement me") } -func (b BitbucketServerClientImpl) isRepoWebhookExists(repo string) (*bitbucket.Webhook, bool) { +func (b BitbucketClientImpl) isRepoWebhookExists(repo string) (*bitbucket.Webhook, bool) { emptyHook := bitbucket.Webhook{} webhookOptions := bitbucket.WebhooksOptions{ diff --git a/pkg/git_provider/bitbucket_test.go b/pkg/git_provider/bitbucket_test.go new file mode 100644 index 00000000..1b6b71a6 --- /dev/null +++ b/pkg/git_provider/bitbucket_test.go @@ -0,0 +1,60 @@ +package git_provider + +import ( + "encoding/json" + "fmt" + "github.com/ktrysmt/go-bitbucket" + "github.com/rookout/piper/pkg/conf" + assertion "github.com/stretchr/testify/assert" + "golang.org/x/net/context" + "net/http" + "testing" +) + +func TestBitbucketListFiles(t *testing.T) { + // Prepare + client, mux, _, teardown := setupBitbucket() + defer teardown() + + repoContent := &bitbucket.RepositoryFile{ + Type: "file", + Path: ".workflows/exit.yaml", + } + + repoContent2 := &bitbucket.RepositoryFile{ + Type: "file", + Path: ".workflows/main.yaml", + } + + data := map[string]interface{}{"values": []bitbucket.RepositoryFile{*repoContent, *repoContent2}} + jsonBytes, _ := json.Marshal(data) + + mux.HandleFunc("/repositories/test/test-repo1/src/branch1/.workflows/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + //testFormValues(t, r, values{}) + + _, _ = fmt.Fprint(w, string(jsonBytes)) + }) + + c := BitbucketClientImpl{ + client: client, + cfg: &conf.GlobalConfig{ + GitProviderConfig: conf.GitProviderConfig{ + OrgLevelWebhook: false, + OrgName: "test", + RepoList: "test-repo1", + }, + }, + } + ctx := context.Background() + + // Execute + actualContent, err := c.ListFiles(&ctx, "test-repo1", "branch1", ".workflows") + expectedContent := []string{"exit.yaml", "main.yaml"} + + // Assert + assert := assertion.New(t) + assert.NotNil(t, err) + assert.Equal(expectedContent, actualContent) + +} diff --git a/pkg/git_provider/bitbucket_utils.go b/pkg/git_provider/bitbucket_utils.go index 659463d2..568725af 100644 --- a/pkg/git_provider/bitbucket_utils.go +++ b/pkg/git_provider/bitbucket_utils.go @@ -36,8 +36,7 @@ func ValidateBitbucketPermissions(client *bitbucket.Client, cfg *conf.GlobalConf func GetBitbucketTokenScopes(client *bitbucket.Client, cfg *conf.GlobalConfig) ([]string, error) { - client.GetApiBaseURL() - req, err := http.NewRequest("GET", fmt.Sprintf("https://api.bitbucket.org/2.0/repositories/%s", cfg.GitProviderConfig.OrgName), nil) + req, err := http.NewRequest("GET", fmt.Sprintf("%s/repositories/%s", client.GetApiBaseURL(), cfg.GitProviderConfig.OrgName), nil) if err != nil { log.Println("Error creating request:", err) return nil, err diff --git a/pkg/git_provider/test_utils.go b/pkg/git_provider/test_utils.go index 9ac9fae0..20c13b5f 100644 --- a/pkg/git_provider/test_utils.go +++ b/pkg/git_provider/test_utils.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/google/go-cmp/cmp" "github.com/google/go-github/v52/github" + "github.com/ktrysmt/go-bitbucket" "net/http" "net/http/httptest" "net/url" @@ -14,16 +15,13 @@ import ( const ( // baseURLPath is a non-empty Client.BaseURL path to use during tests, // to ensure relative URLs are used for all endpoints. See issue #752. - baseURLPath = "/api-v3" + baseURLPath = "/api-v3" + bitbucketBaseURLPath = "/2.0" ) func setup() (client *github.Client, mux *http.ServeMux, serverURL string, teardown func()) { - // mux is the HTTP request multiplexer used with the test server. mux = http.NewServeMux() - // We want to ensure that tests catch mistakes where the endpoint URL is - // specified as absolute rather than relative. It only makes a difference - // when there's a non-empty base URL path. So, use that. See issue #752. apiHandler := http.NewServeMux() apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux)) apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { @@ -73,3 +71,20 @@ func testFormValues(t *testing.T, r *http.Request, values values) { t.Errorf("Request parameters: %v, want %v", got, want) } } + +func setupBitbucket() (client *bitbucket.Client, mux *http.ServeMux, serverURL string, teardown func()) { + mux = http.NewServeMux() + + apiHandler := http.NewServeMux() + apiHandler.Handle(bitbucketBaseURLPath+"/", http.StripPrefix(bitbucketBaseURLPath, mux)) + apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + http.Error(w, "Client.BaseURL path prefix is not preserved in the request URL.", http.StatusInternalServerError) + }) + + server := httptest.NewServer(apiHandler) + url, _ := url.Parse(server.URL + bitbucketBaseURLPath) + client = bitbucket.NewBasicAuth("username", "password") + client.SetApiBaseURL(*url) + + return client, mux, server.URL, server.Close +}