f1bcb8cd3a
repospawner turns JSON new-repo requests into terraform-git pull requests via kubernetes Jobs, follows those PRs to merge and optionally activates the repository in Woodpecker.
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package woodpecker
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestEnable(t *testing.T) {
|
|
var gotQuery, gotAuth, gotMethod string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotQuery, gotAuth, gotMethod = r.URL.RawQuery, r.Header.Get("Authorization"), r.Method
|
|
_, _ = w.Write([]byte(`{"id":5,"full_name":"unkin/widget","active":true}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
repo, err := New(srv.URL, "wp-token").Enable(context.Background(), 91)
|
|
if err != nil {
|
|
t.Fatalf("Enable: %v", err)
|
|
}
|
|
if repo.ID != 5 || !repo.Active {
|
|
t.Errorf("repo = %+v", repo)
|
|
}
|
|
if gotMethod != http.MethodPost || gotQuery != "forge_remote_id=91" {
|
|
t.Errorf("%s ?%s", gotMethod, gotQuery)
|
|
}
|
|
if gotAuth != "Bearer wp-token" {
|
|
t.Errorf("Authorization = %q", gotAuth)
|
|
}
|
|
}
|
|
|
|
func TestEnableTreatsConflictAsSuccess(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusConflict)
|
|
_, _ = w.Write([]byte(`{"message":"repository is already active"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
if _, err := New(srv.URL, "wp-token").Enable(context.Background(), 91); err != nil {
|
|
t.Fatalf("an already-active repository must not be an error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEnableSurfacesUnauthorized(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
_, err := New(srv.URL, "bad").Enable(context.Background(), 91)
|
|
if err == nil || !strings.Contains(err.Error(), "401") {
|
|
t.Fatalf("error = %v, want one naming status 401", err)
|
|
}
|
|
}
|
|
|
|
func TestLookup(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/repos/lookup/unkin/widget" {
|
|
t.Errorf("path = %q", r.URL.Path)
|
|
}
|
|
_, _ = w.Write([]byte(`{"id":5,"full_name":"unkin/widget","active":true}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
repo, err := New(srv.URL, "wp-token").Lookup(context.Background(), "unkin/widget")
|
|
if err != nil {
|
|
t.Fatalf("Lookup: %v", err)
|
|
}
|
|
if !repo.Active || repo.FullName != "unkin/widget" {
|
|
t.Errorf("repo = %+v", repo)
|
|
}
|
|
}
|