diff --git a/pkg/util/github/branch_test.go b/pkg/util/github/branch_test.go index cc5f0a46c..7d50c5249 100644 --- a/pkg/util/github/branch_test.go +++ b/pkg/util/github/branch_test.go @@ -7,14 +7,14 @@ import ( ) type newBranchTest struct { - baseTest + BaseTest baseBranch string newBranch string wantErr bool } type delBranchTest struct { - baseTest + BaseTest branch string wantErr bool } @@ -30,20 +30,20 @@ func TestClient_NewBranch(t *testing.T) { "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" } }` - mux, serverUrl, teardown := setup(t) + mux, serverUrl, teardown := Setup() defer teardown() tests := []newBranchTest{ // TODO: Add test cases. { - baseTest{"base", getClientWithOption( + BaseTest{"base", GetClientWithOption( t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl, ), "/repos/or/r/git/ref/heads/b", http.MethodGet, false, "", ""}, "b", "", true, }, { - baseTest{"base set wrong register url for GetRef api in mock server", getClientWithOption( + BaseTest{"base set wrong register url for GetRef api in mock server", GetClientWithOption( t, &Option{Owner: "o", Repo: "r"}, serverUrl, ), "repos", http.MethodGet, false, "", ""}, @@ -64,13 +64,13 @@ func TestClient_NewBranch(t *testing.T) { } func TestClient_DeleteBranch(t *testing.T) { - mux, serverUrl, teardown := setup(t) + mux, serverUrl, teardown := Setup() defer teardown() tests := []delBranchTest{ // TODO: Add test cases. { - baseTest{"base", getClientWithOption( + BaseTest{"base", GetClientWithOption( t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl, ), "/repos/or/r/git/ref/heads/b", http.MethodGet, false, "", ""}, diff --git a/pkg/util/github/commit_test.go b/pkg/util/github/commit_test.go index 936426905..88736fc50 100644 --- a/pkg/util/github/commit_test.go +++ b/pkg/util/github/commit_test.go @@ -10,33 +10,33 @@ import ( ) type commitTest struct { - baseTest + BaseTest want *github.RepositoryCommit wantErr bool } func TestClient_GetLastCommit(t *testing.T) { - mux, serverUrl, teardown := setup(t) + mux, serverUrl, teardown := Setup() defer teardown() sha := "s" tests := []commitTest{ // TODO: Add test cases. { - baseTest{"base 200 ", getClientWithOption( + BaseTest{"base 200 ", GetClientWithOption( t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl, ), "/repos/or/r/commits", http.MethodGet, false, "", `[{"sha": "s"}]`}, &github.RepositoryCommit{SHA: &sha}, false, }, { - baseTest{"base 200 with empty result", getClientWithOption( + BaseTest{"base 200 with empty result", GetClientWithOption( t, &Option{Owner: "o", Repo: "r"}, serverUrl, ), "/repos/o/r/commits", http.MethodGet, false, "", `[]`}, nil, true, }, { - baseTest{"base 404", getClientWithOption( + BaseTest{"base 404", GetClientWithOption( t, &Option{Owner: "o"}, serverUrl, ), "/aaa", http.MethodGet, false, "", ""}, @@ -46,7 +46,7 @@ func TestClient_GetLastCommit(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mux.HandleFunc(tt.registerUrl, func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, tt.wantMethod) + DoTestMethod(t, r, tt.wantMethod) fmt.Fprint(w, tt.respBody) }) got, err := tt.client.GetLastCommit() diff --git a/pkg/util/github/download_test.go b/pkg/util/github/download_test.go new file mode 100644 index 000000000..26c67df52 --- /dev/null +++ b/pkg/util/github/download_test.go @@ -0,0 +1,224 @@ +package github_test + +import ( + "fmt" + "net/http" + "os" + "path/filepath" + + "github.com/argoproj/gitops-engine/pkg/utils/io" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/devstream-io/devstream/pkg/util/github" +) + +var _ = Describe("DownloadAsset", func() { + Context(("do DownloadAsset 200"), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/r/o/releases", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `[{"id":1}]`) + }) + }) + + It("do ListReleases with wrong url ", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Org: "or", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadAsset("t", "a", "f") + Expect(err).NotTo(Succeed()) + }) + + }) + + Context(("do Downloaset step 1"), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/devstream-io/dtm-scaffolding-golang/releases", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `[{"id":1, "tag_name":"t", "name":"n", "assets": [{"id":1}]}]`) + }) + }) + + It("do ListReleases with correct url ", func() { + ghClient, err := github.NewClientWithOption(github.OptNotNeedAuth, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadAsset("t", "a", "f") + Expect(err).NotTo(Succeed()) + }) + + }) + + Context(("do DownloadAsset step 2 "), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/oo/rr/releases", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `[{"id":1, "tag_name":"t", "name":"n", "assets": [{"id":1}]}]`) + }) + }) + + It("do ListReleases with correct url ", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "oo", + Repo: "rr", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadAsset("t", "a", "f") + Expect(err).NotTo(Succeed()) + }) + + }) + + Context(("do DownloadAsset step 2"), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/a/b/releases", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `[{"id":1, "tag_name":"t", "name":"n", "assets":[]}`) + }) + }) + + It("do ListReleases with correct url but empty asset", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "a", + Repo: "b", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadAsset("t", "a", "f") + Expect(err).NotTo(Succeed()) + }) + + }) + + Context(("do DownloadAsset step 3"), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/ooo/rrr/releases", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `[ + { + "id":1, + "tag_name":"t", + "name":"n", + "assets": [{"id":1, "name":"a"}] + }]`) + }) + }) + + It("do get download url without browser_download_url", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "ooo", + Repo: "rrr", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadAsset("t", "a", "f") + Expect(err).NotTo(Succeed()) + }) + + }) + var WorkPath = "./" + Context(("do DownloadAsset step 4"), func() { + BeforeEach(func() { + DeferCleanup(os.RemoveAll, "./"+github.DefaultWorkPath) + mux.HandleFunc("/repos/ow/re/releases", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `[ + { + "id":1, + "tag_name":"t", + "name":"n", + "assets": [{"id":1, "name":"a", "browser_download_url":"u"}] + }]`) + }) + }) + + It("do get download url browser_download_url with unsupported proto scheme", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "ow", + Repo: "re", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadAsset("t", "a", "f") + Expect(err).NotTo(Succeed()) + }) + + }) + + Context(("do DownloadAsset step 4"), func() { + var filename = "f" + var downloadUrl = "/download" + BeforeEach(func() { + mux.HandleFunc("/repos/own/rep/releases", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `[ + { + "id":1, + "tag_name":"t", + "name":"n", + "assets": [{"id":1, "name":"a", "browser_download_url":"%s"}] + }]`, serverURL+github.BaseURLPath+downloadUrl) + }) + mux.HandleFunc(downloadUrl, func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "%s", "download content") + }) + }) + + It("do get download url browser_download_url with supported proto scheme", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "own", + Repo: "rep", + WorkPath: WorkPath, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadAsset("t", "a", filename) + fmt.Println(err) + Expect(err).To(Succeed()) + }) + + AfterEach(func() { + io.DeleteFile(filepath.Join(WorkPath, filename)) + }) + }) +}) + +var _ = Describe("DownloadLatestCodeAsZipFile", func() { + var owner, repo, org, workPath = "owner", "repo", "org", "./" + Context(("do DownloadLatestCodeAsZipFile"), func() { + + It("corrent url ", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + WorkPath: workPath, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadLatestCodeAsZipFile() + Expect(err).To(Succeed()) + }) + + It("produce an error", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + WorkPath: "//", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.DownloadLatestCodeAsZipFile() + Expect(err).NotTo(Succeed()) + }) + + AfterEach(func() { + DeferCleanup(io.DeleteFile, workPath+github.DefaultLatestCodeZipfileName) + }) + + }) + +}) diff --git a/pkg/util/github/file_test.go b/pkg/util/github/file_test.go index e3fd2df0b..1bfd97f1d 100644 --- a/pkg/util/github/file_test.go +++ b/pkg/util/github/file_test.go @@ -7,7 +7,7 @@ import ( ) type fileTest struct { - baseTest + BaseTest content []byte filePath string targetBranch string @@ -15,12 +15,12 @@ type fileTest struct { } func TestClient_CreateFile(t *testing.T) { - mux, serverUrl, teardown := setup(t) + mux, serverUrl, teardown := Setup() defer teardown() tests := []fileTest{ // TODO: Add test cases. { - baseTest{"base ", getClientWithOption( + BaseTest{"base ", GetClientWithOption( t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl, ), "/repos/or/r/contents/a", http.MethodPut, true, `{"message":"Initialize the repository","content":"Yw==","branch":"b"}`, ""}, @@ -30,7 +30,7 @@ func TestClient_CreateFile(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mux.HandleFunc(tt.registerUrl, func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, tt.wantMethod) + DoTestMethod(t, r, tt.wantMethod) fmt.Fprint(w, tt.respBody) }) if err := tt.client.CreateFile(tt.content, tt.filePath, tt.targetBranch); (err != nil) != tt.wantErr { diff --git a/pkg/util/github/github_suite_test.go b/pkg/util/github/github_suite_test.go index aa4cfcb38..a7b16e5fa 100644 --- a/pkg/util/github/github_suite_test.go +++ b/pkg/util/github/github_suite_test.go @@ -1,12 +1,29 @@ package github_test import ( + "net/http" "testing" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + + util_github "github.com/devstream-io/devstream/pkg/util/github" +) + +var ( + mux *http.ServeMux + serverURL string + teardown func() ) +var _ = BeforeSuite(func() { + mux, serverURL, teardown = util_github.Setup() +}) + +var _ = AfterSuite(func() { + teardown() +}) + func TestPlanmanager(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "GitHub Suite") diff --git a/pkg/util/github/github_test.go b/pkg/util/github/github_test.go index d9e211b3b..28fff6f3d 100644 --- a/pkg/util/github/github_test.go +++ b/pkg/util/github/github_test.go @@ -1,7 +1,6 @@ package github import ( - "fmt" "io/ioutil" "net/http" "net/http/httptest" @@ -11,22 +10,21 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - // "github.com/devstream-io/devstream/pkg/util/github" ) const ( - // baseURLPath is a non-empty Client.BaseURL path to use during tests, + // BaseURLPath is a non-empty Client.BaseURL path to use during tests, // to ensure relative URLs are used for all endpoints. - baseURLPath = "/api-v3" + BaseURLPath = "/api-v3" ) var ( - optNotNeedAuth = &Option{ + OptNotNeedAuth = &Option{ Owner: "", Org: "devstream-io", Repo: "dtm-scaffolding-golang", } - optNeedAuth = &Option{ + OptNeedAuth = &Option{ Owner: "", Org: "devstream-io", Repo: "dtm-scaffolding-golang", @@ -34,7 +32,7 @@ var ( } ) -type baseTest struct { +type BaseTest struct { name string //test name client *Client registerUrl string // url resigtered in mock server @@ -44,13 +42,13 @@ type baseTest struct { respBody string // content of response body } -func testMethod(t *testing.T, r *http.Request, want string) { +func DoTestMethod(t *testing.T, r *http.Request, want string) { if got := r.Method; got != want { t.Errorf("Request method: %v, want %v", got, want) } } -func testBody(t *testing.T, r *http.Request, want string) { +func DoTestBody(t *testing.T, r *http.Request, want string) { b, err := ioutil.ReadAll(r.Body) if err != nil { t.Errorf("Error reading request body: %v", err) @@ -60,10 +58,10 @@ func testBody(t *testing.T, r *http.Request, want string) { } } -// setup sets up a test HTTP server along with a github.Client that is +// Setup sets up a test HTTP server along with a github.Client that is // configured to talk to that test server. Tests should register handlers on // mux which provide mock responses for the API method being tested. -func setup(t *testing.T) (mux *http.ServeMux, serverURL string, teardown func()) { +func Setup() (mux *http.ServeMux, serverURL string, teardown func()) { // mux is the HTTP request multiplexer used with the test server. mux = http.NewServeMux() @@ -71,7 +69,7 @@ func setup(t *testing.T) (mux *http.ServeMux, serverURL string, teardown func()) // specified as absolute rather than relative. It only makes a difference // when there's a non-empty base URL path. apiHandler := http.NewServeMux() - apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux)) + apiHandler.Handle(BaseURLPath+"/", http.StripPrefix(BaseURLPath, mux)) // server is a test HTTP server used to provide mock API responses. server := httptest.NewServer(apiHandler) @@ -79,32 +77,45 @@ func setup(t *testing.T) (mux *http.ServeMux, serverURL string, teardown func()) return mux, server.URL, server.Close } -func getClientWithOption(t *testing.T, opt *Option, severUrl string) *Client { +func GetClientWithOption(t *testing.T, opt *Option, severUrl string) *Client { client, err := NewClient(opt) if err != nil { t.Error(err) } - url, _ := url.Parse(severUrl + baseURLPath + "/") + url, _ := url.Parse(severUrl + BaseURLPath + "/") client.Client.BaseURL = url client.Client.UploadURL = url return client } +func NewClientWithOption(opt *Option, severUrl string) (*Client, error) { + client, err := NewClient(opt) + if err != nil { + return nil, err + } + + url, _ := url.Parse(severUrl + BaseURLPath + "/") + + client.Client.BaseURL = url + client.Client.UploadURL = url + return client, nil +} + var _ = Describe("GitHub", func() { Context(("Client with cacahe"), func() { var ghClient *Client var err error BeforeEach(func() { - ghClient, err = NewClient(optNotNeedAuth) + ghClient, err = NewClient(OptNotNeedAuth) Expect(err).NotTo(HaveOccurred()) Expect(ghClient).NotTo(Equal(nil)) }) It("with cacahe client", func() { - ghClient, err = NewClient(optNotNeedAuth) + ghClient, err = NewClient(OptNotNeedAuth) Expect(err).NotTo(HaveOccurred()) Expect(ghClient).NotTo(Equal(nil)) }) @@ -114,18 +125,15 @@ var _ = Describe("GitHub", func() { var ghClient *Client var err error It("", func() { - ghClient, err = NewClient(optNotNeedAuth) + ghClient, err = NewClient(OptNotNeedAuth) Expect(err).NotTo(HaveOccurred()) Expect(ghClient).NotTo(Equal(nil)) }) }) Context("Client with auth enabled but not github token", func() { - var ghClient *Client - var err error It("", func() { - ghClient, err = NewClient(optNeedAuth) - fmt.Printf("--->: %+v\n", ghClient) + _, err := NewClient(OptNeedAuth) Expect(err).To(HaveOccurred()) }) }) @@ -137,7 +145,7 @@ var _ = Describe("GitHub", func() { os.Setenv("GITHUB_TOKEN", "GITHUB_TOKEN") }) It("", func() { - ghClient, err = NewClient(optNeedAuth) + ghClient, err = NewClient(OptNeedAuth) Expect(err).NotTo(HaveOccurred()) Expect(ghClient).NotTo(Equal(nil)) }) diff --git a/pkg/util/github/pullrequest.go b/pkg/util/github/pullrequest.go index 4bd24c0e3..143d119b9 100644 --- a/pkg/util/github/pullrequest.go +++ b/pkg/util/github/pullrequest.go @@ -57,9 +57,8 @@ func (c *Client) MergePullRequest(number int, mergeMethod MergeMethod) error { commitMsg := "Initialized by DevStream" ret, _, err := c.PullRequests.Merge(c.Context, owner, c.Repo, number, commitMsg, &github.PullRequestOptions{ - CommitTitle: commitMsg, - SHA: "", - // "merge", "squash", and "rebase" + CommitTitle: commitMsg, + SHA: "", MergeMethod: string(mergeMethod), DontDefaultIfBlank: false, }) diff --git a/pkg/util/github/pullrequest_test.go b/pkg/util/github/pullrequest_test.go new file mode 100644 index 000000000..48a2d5023 --- /dev/null +++ b/pkg/util/github/pullrequest_test.go @@ -0,0 +1,110 @@ +package github_test + +import ( + "fmt" + "net/http" + + "github.com/devstream-io/devstream/pkg/util/github" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Pullrequest", func() { + Context(("do NewPullRequest 200"), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/devstream-io/dtm-scaffolding-golang/pulls", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{"number":1}`) + }) + }) + + It("do create a new pr", func() { + ghClient, err := github.NewClientWithOption(github.OptNotNeedAuth, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + n, err := ghClient.NewPullRequest("from", "to") + Expect(n).To(Equal(1)) + Expect(err).To(Succeed()) + }) + + }) + + Context(("do NewPullRequest 404"), func() { + + It("do create a new pr with wrong url", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "", + Org: "or", + Repo: "r", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + n, err := ghClient.NewPullRequest("from", "to") + Expect(n).To(Equal(0)) + Expect(err).NotTo(Equal(nil)) + }) + }) + + Context(("do MergePullRequest"), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/devstream-io/dtm-scaffolding-golang/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, ` + { + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "merged": true, + "message": "Pull Request successfully merged" + }`) + }) + }) + + It("200", func() { + ghClient, err := github.NewClientWithOption(github.OptNotNeedAuth, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.MergePullRequest(1, github.MergeMethodRebase) + Expect(err).To(Succeed()) + }) + }) + + Context(("do MergePullRequest"), func() { + + It("404", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "", + Org: "or", + Repo: "r", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.MergePullRequest(1, github.MergeMethodRebase) + Expect(err).To(HaveOccurred()) + }) + }) + + Context(("do MergePullRequest"), func() { + + BeforeEach(func() { + mux.HandleFunc("/repos/or/r/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, ` + { + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "merged": false, + "message": "Pull Request successfully merged" + }`) + }) + }) + + It("return merged false", func() { + ghClient, err := github.NewClientWithOption(&github.Option{ + Owner: "", + Org: "or", + Repo: "r", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(ghClient).NotTo(Equal(nil)) + err = ghClient.MergePullRequest(1, github.MergeMethodRebase) + Expect(err).NotTo(Succeed()) + }) + }) +}) diff --git a/pkg/util/github/release_test.go b/pkg/util/github/release_test.go index 31694f0d3..df844dc18 100644 --- a/pkg/util/github/release_test.go +++ b/pkg/util/github/release_test.go @@ -9,24 +9,24 @@ import ( ) type releaseTest struct { - baseTest + BaseTest wantTag string wantErr bool } func TestClient_GetLatestReleaseTagName(t *testing.T) { - mux, serverUrl, teardown := setup(t) + mux, serverUrl, teardown := Setup() defer teardown() tests := []releaseTest{ { - baseTest{"base err != nil", getClientWithOption( + BaseTest{"base err != nil", GetClientWithOption( t, &Option{Owner: ""}, serverUrl, ), "/repos2/o/r/releases/latest", http.MethodGet, false, "", ""}, "", true}, { - baseTest{"base 200", getClientWithOption( + BaseTest{"base 200", GetClientWithOption( t, &Option{Owner: "", Org: "o", Repo: "r"}, serverUrl, ), "/repos/o/r/releases/latest", http.MethodGet, false, "", `{"id":3,"tag_name":"v1.0.0"}`}, @@ -35,8 +35,8 @@ func TestClient_GetLatestReleaseTagName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mux.HandleFunc(tt.registerUrl, func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, tt.wantMethod) - testBody(t, r, "") + DoTestMethod(t, r, tt.wantMethod) + DoTestBody(t, r, "") fmt.Fprint(w, `{"id":3,"tag_name":"v1.0.0"}`) }) tag, err := tt.client.GetLatestReleaseTagName() diff --git a/pkg/util/github/secrets_test.go b/pkg/util/github/secrets_test.go new file mode 100644 index 000000000..be561cf52 --- /dev/null +++ b/pkg/util/github/secrets_test.go @@ -0,0 +1,132 @@ +package github_test + +import ( + "fmt" + "net/http" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/devstream-io/devstream/pkg/util/github" +) + +var _ = Describe("Secrets", func() { + var owner, repo, org = "o", "r", "or" + var registerUrl string = fmt.Sprintf("/repos/%v/%v/actions/secrets/public-key", org, repo) + sk, sv := "sk", "sv" + + Context(("do AddRepoSecret"), func() { + It("step1: do GetRepoPublicKey with wrong url", func() { + rightClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(rightClient).NotTo(Equal(nil)) + err = rightClient.AddRepoSecret(sk, sv) + Expect(err).NotTo(Succeed()) + }) + + It("step2: do AddRepoSecret with correct url", func() { + registerUrl = fmt.Sprintf("/repos/%v/%v/actions/secrets/public-key", org, repo) + mux.HandleFunc(registerUrl, func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`) + }) + rightClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(rightClient).NotTo(Equal(nil)) + err = rightClient.AddRepoSecret(sk, sv) + Expect(err).NotTo(Succeed()) + }) + + It("step3: do CreateOrUpdateRepoSecret with wrong url", func() { + rightClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(rightClient).NotTo(Equal(nil)) + + err = rightClient.AddRepoSecret(sk, sv) + Expect(err).NotTo(Succeed()) + }) + + It("step3: do CreateOrUpdateRepoSecret with correct url", func() { + registerUrl = fmt.Sprintf("/repos/%v/%v/actions/secrets/%v", org, repo, sk) + mux.HandleFunc(registerUrl, func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`) + }) + rightClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(rightClient).NotTo(Equal(nil)) + err = rightClient.AddRepoSecret(sk, sv) + + Expect(err).To(Succeed()) + }) + + }) + + Context("RepoSecretExists", func() { + It("do RepoSecretExists with wrong url", func() { + wrongClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: "rrrr", + Org: "ororor", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(wrongClient).NotTo(Equal(nil)) + b, err := wrongClient.RepoSecretExists(sk) + Expect(err).To(Succeed()) + Expect(b).To(Equal(false)) + }) + + It("do RepoSecretExists with correct url", func() { + rightClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(rightClient).NotTo(Equal(nil)) + b, err := rightClient.RepoSecretExists(sk) + Expect(err).To(Succeed()) + Expect(b).To(Equal(true)) + }) + }) + + Context("DeleteRepoSecret", func() { + It("do DeleteRepoSecret with wrong url", func() { + wrongClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: "rrrr", + Org: "ororor", + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(wrongClient).NotTo(Equal(nil)) + err = wrongClient.DeleteRepoSecret(sk) + Expect(err).To(Succeed()) + }) + + It("do DeleteRepoSecret with correct url", func() { + rightClient, err := github.NewClientWithOption(&github.Option{ + Owner: owner, + Repo: repo, + Org: org, + }, serverURL) + Expect(err).NotTo(HaveOccurred()) + Expect(rightClient).NotTo(Equal(nil)) + err = rightClient.DeleteRepoSecret(sk) + Expect(err).To(Succeed()) + }) + }) +})