Skip to content

Commit

Permalink
test: add for Buildkite API interaction (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestelfer authored Apr 24, 2024
1 parent 46c06ea commit 253c141
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
9 changes: 8 additions & 1 deletion internal/buildkite/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"net/url"

"github.com/buildkite/go-buildkite/v3/buildkite"
"github.com/jamestelfer/ghauth/internal/config"
Expand All @@ -21,6 +22,12 @@ func New(cfg config.BuildkiteConfig) PipelineLookup {

client := buildkite.NewClient(transport.Client())

if cfg.ApiURL != "" {
url, _ := url.Parse(cfg.ApiURL)
transport.APIHost = url.Host
client.BaseURL, _ = url.Parse(cfg.ApiURL)
}

return PipelineLookup{
client,
}
Expand All @@ -35,7 +42,7 @@ func (p PipelineLookup) RepositoryLookup(ctx context.Context, organizationSlug,

repo := pipeline.Repository
if repo == nil {
return "", fmt.Errorf("no configured repository for pipeline %s/%s", organizationSlug, pipelineSlug)
return "", fmt.Errorf("no configured repository for pipeline %s/%s", organizationSlug, pipelineSlug)
}

return *repo, nil
Expand Down
141 changes: 141 additions & 0 deletions internal/buildkite/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package buildkite_test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

api "github.com/buildkite/go-buildkite/v3/buildkite"
"github.com/jamestelfer/ghauth/internal/buildkite"
"github.com/jamestelfer/ghauth/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRepositoryLookup_Succeeds(t *testing.T) {
router := http.NewServeMux()

router.HandleFunc("/v2/organizations/{organization}/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) {
org := r.PathValue("organization")
pipeline := r.PathValue("pipeline")

w.Header().Set("Content-Type", "application/json")
pl := &api.Pipeline{
Name: &pipeline,
Slug: &pipeline,
Repository: api.String("urn:expected-repository-url"),
Description: &org,
Tags: []string{
"token:" + r.Header.Get("Authorization"),
},
}
res, _ := json.Marshal(&pl)
_, _ = w.Write(res)
})

svr := httptest.NewServer(router)
defer svr.Close()

bk := buildkite.New(config.BuildkiteConfig{
Token: "expected-token",
ApiURL: svr.URL,
})

repo, err := bk.RepositoryLookup(context.Background(), "expected-organization", "expected-pipeline")

require.NoError(t, err)
assert.Equal(t, "urn:expected-repository-url", repo)
}

func TestRepositoryLookup_SendsAuthToken(t *testing.T) {
router := http.NewServeMux()

var actualToken string

router.HandleFunc("/v2/organizations/{organization}/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) {
org := r.PathValue("organization")
pipeline := r.PathValue("pipeline")

// capture the token to assert against
actualToken = r.Header.Get("Authorization")

w.Header().Set("Content-Type", "application/json")
pl := &api.Pipeline{
Name: &pipeline,
Slug: &pipeline,
Repository: api.String("urn:expected-repository-url"),
Description: &org,
Tags: []string{
"token:" + r.Header.Get("Authorization"),
},
}
res, _ := json.Marshal(&pl)
_, _ = w.Write(res)
})

svr := httptest.NewServer(router)
defer svr.Close()

bk := buildkite.New(config.BuildkiteConfig{
Token: "expected-token",
ApiURL: svr.URL,
})

_, err := bk.RepositoryLookup(context.Background(), "expected-organization", "expected-pipeline")

require.NoError(t, err)
assert.Equal(t, "Bearer expected-token", actualToken)
}

func TestRepositoryLookup_FailsWhenRepoNotConfigured(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/v2/organizations/{organization}/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) {
org := r.PathValue("organization")
pipeline := r.PathValue("pipeline")
w.Header().Set("Content-Type", "application/json")
pl := &api.Pipeline{
Name: &pipeline,
Slug: &pipeline,
//Repository: // repository purposefully blank
Description: &org,
}
res, _ := json.Marshal(&pl)
_, _ = w.Write(res)
})

svr := httptest.NewServer(router)
defer svr.Close()

bk := buildkite.New(config.BuildkiteConfig{
Token: "expected-token",
ApiURL: svr.URL,
})

_, err := bk.RepositoryLookup(context.Background(), "expected-organization", "expected-pipeline")

require.Error(t, err)
assert.ErrorContains(t, err, "no configured repository for pipeline expected-organization/expected-pipeline")
}

func TestRepositoryLookup_Fails(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/v2/organizations/{organization}/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) {
// teapot is useful for test
w.WriteHeader(http.StatusTeapot)
})

svr := httptest.NewServer(router)
defer svr.Close()

bk := buildkite.New(config.BuildkiteConfig{
Token: "expected-token",
ApiURL: svr.URL,
})

_, err := bk.RepositoryLookup(context.Background(), "expected-organization", "expected-pipeline")

require.Error(t, err)
assert.ErrorContains(t, err, ": 418")
}
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type AuthorizationConfig struct {
}

type BuildkiteConfig struct {
Token string `env:"BUILDKITE_API_TOKEN, required"`
Token string `env:"BUILDKITE_API_TOKEN, required"`
ApiURL string // internal only
}

type GithubConfig struct {
Expand Down

0 comments on commit 253c141

Please sign in to comment.