Skip to content

Commit

Permalink
Allow github token to not be set when getting releases (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: Sophie Wigmore <swigmore@vmware.com>

Co-authored-by: Josh Zarrabi <jzarrabi@vmware.com>
  • Loading branch information
Sophie Wigmore and joshzarrabi authored Jan 12, 2021
1 parent f8a1d85 commit 2837bff
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 10 deletions.
16 changes: 13 additions & 3 deletions github/release_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (rs ReleaseService) Get(org, repo string) (Release, error) {
if err != nil {
return Release{}, err
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", rs.config.Token))

if rs.config.Token != "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", rs.config.Token))
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand All @@ -65,7 +68,11 @@ func (rs ReleaseService) GetReleaseAsset(asset ReleaseAsset) (io.ReadCloser, err
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", rs.config.Token))

if rs.config.Token != "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", rs.config.Token))
}

req.Header.Add("Accept", "application/octet-stream")

resp, err := http.DefaultClient.Do(req)
Expand All @@ -85,7 +92,10 @@ func (rs ReleaseService) GetReleaseTarball(url string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", rs.config.Token))

if rs.config.Token != "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", rs.config.Token))
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down
120 changes: 120 additions & 0 deletions github/release_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,52 @@ func testReleaseService(t *testing.T, context spec.G, it spec.S) {
}))
})

context("when no github token is specified", func() {
var authToken string
it.Before(func() {

api = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
dump, _ := httputil.DumpRequest(req, true)

authToken = req.Header.Get("Authorization")

switch req.URL.Path {
case "/repos/some-org/some-repo/releases/latest":
w.Write([]byte(`{
"tag_name": "some-tag",
"assets": [
{
"url": "some-url"
}
],
"tarball_url": "some-tarball-url"
}`))
default:
Fail(fmt.Sprintf("unexpected request:\n%s", dump))
}

}))
service = github.NewReleaseService(github.Config{
Endpoint: api.URL,
})

})
it("makes the call without any authorization header", func() {
release, err := service.Get("some-org", "some-repo")
Expect(err).ToNot(HaveOccurred())
Expect(release).To(Equal(github.Release{
TagName: "some-tag",
Assets: []github.ReleaseAsset{
{
URL: "some-url",
},
},
TarballURL: "some-tarball-url",
}))
Expect(authToken).To(Equal(""))
})
})

context("failure cases", func() {
context("when the request url is malformed", func() {
it.Before(func() {
Expand Down Expand Up @@ -139,6 +185,44 @@ func testReleaseService(t *testing.T, context spec.G, it spec.S) {
Expect(response.Close()).To(Succeed())
})

context("when no github token is specified", func() {
var authToken string
it.Before(func() {

api = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
dump, _ := httputil.DumpRequest(req, true)

authToken = req.Header.Get("Authorization")

switch req.URL.Path {
case "/some-url":
w.Write([]byte(`some-asset`))
default:
Fail(fmt.Sprintf("unexpected request:\n%s", dump))
}

}))

service = github.NewReleaseService(github.Config{
Endpoint: api.URL,
})

})
it("fetches the latest release without any authorization header", func() {
response, err := service.GetReleaseAsset(github.ReleaseAsset{
URL: fmt.Sprintf("%s/some-url", api.URL),
})
Expect(err).ToNot(HaveOccurred())

content, err := ioutil.ReadAll(response)
Expect(err).ToNot(HaveOccurred())
Expect(string(content)).To(Equal("some-asset"))

Expect(response.Close()).To(Succeed())
Expect(authToken).To(Equal(""))
})
})

context("failure cases", func() {
context("when the url is malformed", func() {
it("returns an error", func() {
Expand Down Expand Up @@ -210,6 +294,42 @@ func testReleaseService(t *testing.T, context spec.G, it spec.S) {
Expect(response.Close()).To(Succeed())
})

context("when no github token is specified", func() {
var authToken string
it.Before(func() {

api = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
dump, _ := httputil.DumpRequest(req, true)

authToken = req.Header.Get("Authorization")

switch req.URL.Path {
case "/some-tarball-url":
w.Write([]byte(`some-tarball`))
default:
Fail(fmt.Sprintf("unexpected request:\n%s", dump))
}

}))

service = github.NewReleaseService(github.Config{
Endpoint: api.URL,
})

})
it("fetches the latest release tarball without any authorization header", func() {
response, err := service.GetReleaseTarball(fmt.Sprintf("%s/some-tarball-url", api.URL))
Expect(err).ToNot(HaveOccurred())

content, err := ioutil.ReadAll(response)
Expect(err).ToNot(HaveOccurred())
Expect(string(content)).To(Equal("some-tarball"))

Expect(response.Close()).To(Succeed())
Expect(authToken).To(Equal(""))
})
})

context("failure cases", func() {
context("when the url is malformed", func() {
it("returns an error", func() {
Expand Down
7 changes: 0 additions & 7 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package freezer_test

import (
"os"
"testing"

"github.com/sclevine/spec"
Expand All @@ -11,12 +10,6 @@ import (
var gitToken string

func TestFreezer(t *testing.T) {
var ok bool
gitToken, ok = os.LookupEnv("GIT_TOKEN")
if !ok {
t.Fatal("$GIT_TOKEN environment variable must be set")
}

suite := spec.New("freezer", spec.Report(report.Terminal{}))
suite("CacheManager", testCacheManager)
suite("FileSystem", testFileSystem)
Expand Down

0 comments on commit 2837bff

Please sign in to comment.