Skip to content
quarry Artifact [e03544f377]
Login

Artifact [e03544f377]

Artifact e03544f3775dcbc995707c534e521efdae04853f43e646da9aaca1a88a66fee9:


package cibridge

import (
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"sync/atomic"
	"testing"

	"craftdesign.group/quarry/internal/ci"
)

// The check route answers a worker token from the forge's own record,
// without the engine: a token recorded for this repository is 200, one
// recorded for another is named, one not recorded is the bridge's 401.
func TestTheCheckRouteAnswersFromTheLedgerAlone(t *testing.T) {
	var hits atomic.Int32
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		hits.Add(1)
		http.Error(w, "engine", http.StatusTeapot)
	}))
	defer engine.Close()
	ledger := map[string]string{ci.TokenHash("ALPHA"): "alpha", ci.TokenHash("BETA"): "beta"}
	owner := func(h string) (string, bool) { r, ok := ledger[h]; return r, ok }
	h, err := Handler(engine.URL, "ADMIN", func(repo string) string { return "g-" + repo },
		func(repo string) bool { return repo == "alpha" }, nil, nil, WithTokenOwner(owner))
	if err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(h)
	defer srv.Close()
	get := func(tok string) (*http.Response, string) {
		req, _ := http.NewRequest("GET", srv.URL+"/r/alpha/ci/bridge/check", nil)
		if tok != "" {
			req.Header.Set("Authorization", "Bearer "+tok)
		}
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		defer resp.Body.Close()
		b, _ := io.ReadAll(resp.Body)
		return resp, strings.TrimSpace(string(b))
	}
	if r, b := get("ALPHA"); r.StatusCode != 200 || b != `{"repository":"alpha"}` {
		t.Errorf("FAIL: own token got %d %q", r.StatusCode, b)
	}
	if r, b := get("BETA"); r.StatusCode != 403 || r.Header.Get(ReasonHeader) != "other-repo" || b != `{"reason":"other-repo","repository":"beta"}` {
		t.Errorf("FAIL: another repository's token got %d %q %q", r.StatusCode, r.Header.Get(ReasonHeader), b)
	}
	none, nb := get("")
	for _, tok := range []string{"UNRECORDED", "ADMIN"} {
		r, b := get(tok)
		if r.StatusCode != 401 || b != nb || none.StatusCode != 401 || r.Header.Get("WWW-Authenticate") != none.Header.Get("WWW-Authenticate") || r.Header.Get(ReasonHeader) != "" {
			t.Errorf("FAIL: %s got %d %q, no token %d %q", tok, r.StatusCode, b, none.StatusCode, nb)
		}
	}
	if n := hits.Load(); n != 0 {
		t.Errorf("FAIL: the engine was contacted %d times", n)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Without a token ledger the route is not there, which is how a
// client tells a forge that cannot answer it.
func TestTheCheckRouteIsAbsentWithoutALedger(t *testing.T) {
	h, err := Handler("http://127.0.0.1:1", "A", func(string) string { return "g" }, func(string) bool { return true }, nil, nil)
	if err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(h)
	defer srv.Close()
	req, _ := http.NewRequest("GET", srv.URL+"/r/alpha/ci/bridge/check", nil)
	req.Header.Set("Authorization", "Bearer X")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		t.Fatal(err)
	}
	resp.Body.Close()
	if resp.StatusCode != 404 {
		t.Errorf("FAIL: got %d", resp.StatusCode)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}