Skip to content
quarry Artifact [a7108ab7ec]
Login

Artifact [a7108ab7ec]

Artifact a7108ab7ec7c87377c22608741e2f529a15ab878162ebe325e922e05b1e8f6b9:


package api

import (
	"errors"
	"reflect"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

func browserOnly(t *testing.T, err error) *ops.Refusal {
	t.Helper()
	var ref *ops.Refusal
	if !errors.As(err, &ref) || ref.Code != browserOnlyCode || !errors.Is(err, ops.ErrForbidden) || ref.Fields["next"] == "" {
		return nil
	}
	return ref
}

// What stays with the person at a browser is refused to their login token,
// each with a next pointing at the web page.
func TestALoginTokenIsRefusedTheBrowserOnlyActs(t *testing.T) {
	g := newStartRig(t)
	_, value := g.loggedIn("dmestas", "mac")
	tok, ok := g.db.ResolveToken("dmestas", value, "api")
	if !ok {
		t.Fatal("FAIL: no token")
	}
	tctx := ops.WithToken(ops.WithUser(t.Context(), "dmestas"), ops.TokenAuth{ID: tok.ID, Name: tok.Name, Login: tok.Has("login"),
		Caps: func(repo string) ops.Caps { return g.db.TokenCaps(tok, repo) }, Reaches: tok.Reaches})
	for name, c := range map[string]struct {
		op   string
		in   any
		next string
	}{
		"branch protection":  {"branch.protection.set", branchProtectionSetIn{Repo: "suit", Branch: "trunk", Owners: []string{"dmestas"}}, "/r/suit/settings"},
		"required checks":    {"ci.checks.set", checkRuleSetIn{Repo: "suit", Branch: "trunk"}, "/r/suit/settings"},
		"an accept override": {"landing.accept", landingSayIn{Repo: "suit", ID: "abcd", Override: "ship it"}, "/r/suit/landings"},
		"a delete":           {"repo.delete", repoDeleteIn{Name: "suit", Confirm: "suit"}, ""},
		"a remove":           {"repo.remove", repoRemoveIn{Name: "suit"}, ""},
		"a hand-on":          {"repo.claim", repoClaimIn{Repo: "suit", Account: "jva"}, "/r/suit/settings"},
	} {
		_, err := g.r.Invoke(tctx, c.op, c.in)
		ref := browserOnly(t, err)
		if ref == nil {
			t.Errorf("FAIL: %s on a login token: %v", name, err)
			continue
		}
		if c.next != "" && !strings.HasSuffix(ref.Fields["next"], c.next) {
			t.Errorf("FAIL: %s: next is %q, want it to end %q", name, ref.Fields["next"], c.next)
		}
		// The signed-in person is not stopped by the gate.
		if _, perr := g.r.Invoke(asUser("dmestas"), c.op, c.in); browserOnly(t, perr) != nil {
			t.Errorf("FAIL: %s refused to the person signed in", name)
		}
	}
	// Every site-admin command is refused to it too.
	n := 0
	for _, desc := range g.r.All() {
		if desc.Auth != ops.Admin || desc.Kind != ops.Command {
			continue
		}
		n++
		arg := reflect.ValueOf(desc.NewIn()).Elem().Interface()
		if _, err := g.r.Invoke(tctx, desc.Name, arg); browserOnly(t, err) == nil {
			t.Errorf("FAIL: site-admin %s on a login token: %v", desc.Name, err)
		}
	}
	if n == 0 {
		t.Errorf("FAIL: no site-admin command was tried")
	}
	// An ordinary call is not a hard stop: an accept without an override.
	if _, err := g.r.Invoke(tctx, "landing.accept", landingSayIn{Repo: "suit", ID: "abcd"}); browserOnly(t, err) != nil {
		t.Errorf("FAIL: a plain accept was refused as browser-only")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Only a login token is held to the table: a token that holds its letters
// on a repository keeps using them, and a forge agent's site reach is by
// design.
func TestTheBrowserOnlyGateLeavesOtherTokensAlone(t *testing.T) {
	g := newStartRig(t)
	_, err := g.r.Invoke(tokenCtx("dmestas-bot"), "landing.accept", landingSayIn{Repo: "suit", ID: "abcd", Override: "x"})
	if browserOnly(t, err) != nil {
		t.Errorf("FAIL: an agent's token got the person's browser-only refusal")
	}
	plain := ops.WithToken(ops.WithUser(t.Context(), "dmestas"), ops.TokenAuth{ID: "T", Caps: func(string) ops.Caps { return oltp.CapSet("a") }})
	if _, err := g.r.Invoke(plain, "branch.protection.set", branchProtectionSetIn{Repo: "suit", Branch: "trunk"}); browserOnly(t, err) != nil {
		t.Errorf("FAIL: a token that is not a login got the refusal")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The page's sentence is made from the table the gate refuses by.
func TestTheLoginSentenceNamesEveryStopInTheTable(t *testing.T) {
	sentence := loginSentence("mac", "dmestas")
	for _, h := range hardStops {
		if !strings.Contains(sentence, h.Words) {
			t.Errorf("FAIL: the sentence leaves out %q", h.Words)
		}
	}
	if !strings.Contains(sentence, "mac") || !strings.Contains(sentence, "dmestas") {
		t.Errorf("FAIL: the sentence names no machine or person")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}