Skip to content
quarry Artifact [42ec2cd258]
Login

Artifact [42ec2cd258]

Artifact 42ec2cd258aad81f0a76eb7b5bbc7f0e2199c81f6fc9a06b6528644ca916d8ab:


package cibridge

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

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

// A token the forge recorded at mint as another repository's is named,
// to its holder, when the engine refuses it at this bridge. A token with
// no record gets the engine's answer untouched, as does one recorded for
// this very repository; the engine's 401 is never rewritten.
func TestAWorkerTokenMintedForAnotherRepositoryIsNamed(t *testing.T) {
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Header.Get("Authorization") == "Bearer NOPE" {
			http.Error(w, "unauthorized", http.StatusUnauthorized)
			return
		}
		w.Header().Set("X-Engine", "1")
		http.Error(w, "engine says scoped elsewhere", http.StatusForbidden)
	}))
	defer engine.Close()
	ledger := map[string]string{ci.TokenHash("BETA"): "beta", ci.TokenHash("ALPHA"): "alpha"}
	owner := func(h string) (string, bool) { r, ok := ledger[h]; return r, ok }
	build := func(opts ...Option) *httptest.Server {
		h, err := Handler(engine.URL, "ADMIN", func(repo string) string { return "g-" + repo },
			func(repo string) bool { return repo == "alpha" }, nil, nil, opts...)
		if err != nil {
			t.Fatal(err)
		}
		return httptest.NewServer(h)
	}
	front, plain := build(WithTokenOwner(owner)), build()
	defer front.Close()
	defer plain.Close()
	post := func(srv *httptest.Server, path, tok string) (*http.Response, string) {
		req, _ := http.NewRequest("POST", srv.URL+"/r/alpha/ci/bridge/v1/"+path, strings.NewReader(`{"task_types":["t"]}`))
		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, string(b)
	}
	for _, path := range []string{"tasks/poll", "workers/connect"} {
		resp, body := post(front, path, "BETA")
		if resp.StatusCode != 403 || resp.Header.Get(ReasonHeader) != "other-repo" || body != `{"reason":"other-repo","repository":"beta"}` {
			t.Errorf("FAIL: %s: a beta token got %d %q %q", path, resp.StatusCode, resp.Header.Get(ReasonHeader), body)
		}
		for _, tok := range []string{"UNRECORDED", "ALPHA"} {
			a, ab := post(front, path, tok)
			b, bb := post(plain, path, tok)
			if a.StatusCode != 403 || ab != bb || a.Header.Get(ReasonHeader) != "" || a.Header.Get("X-Engine") != b.Header.Get("X-Engine") || a.Header.Get("X-Engine") == "" {
				t.Errorf("FAIL: %s: %s got %d %q, plain bridge %d %q", path, tok, a.StatusCode, ab, b.StatusCode, bb)
			}
		}
		a, ab := post(front, path, "NOPE")
		b, bb := post(plain, path, "NOPE")
		if a.StatusCode != 401 || ab != bb || a.Header.Get(ReasonHeader) != "" || a.Header.Get("Content-Type") != b.Header.Get("Content-Type") {
			t.Errorf("FAIL: %s: an unknown token got %d %q, plain bridge %d %q", path, a.StatusCode, ab, b.StatusCode, bb)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}