Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add for Buildkite API interaction #3

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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