Skip to content
quarry Artifact [27b01624f5]
Login

Artifact [27b01624f5]

Artifact 27b01624f50bf8c390450fe562ee624db32f9ed42be669c11a9f0214fcb92661:


package api

import (
	"context"
	"errors"
	"strings"
	"testing"

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

// limitedTo is a token of who limited to the one repository repo.
func limitedTo(who, repo string) context.Context {
	return ops.WithToken(ops.WithUser(context.Background(), who), ops.TokenAuth{ID: "L",
		Caps:    func(string) ops.Caps { return oltp.CapSet("") },
		Limited: true,
		Reaches: func(r string) bool { return r == repo }})
}

// A token limited to repositories reads its own requests about them,
// which the limited-token gate used to refuse, but sees no request about
// another repository or about none, and never collects a credential.
func TestALimitedTokenReadsItsOwnRequestsButCollectsNothing(t *testing.T) {
	c := newCredRig(t)
	id := c.ask("jva-bot", "jva", "/api/v1/r/test/ci/tokens", `{"label":"ci"}`)

	out, err := c.r.Invoke(limitedTo("jva-bot", "test"), "signoff.get", signOffGetIn{ID: id})
	if err != nil {
		t.Fatalf("FAIL: a limited token reading its own request: %v", err)
	}
	got := out.(signOffGetOut)
	if got.Request.ID != id || len(got.Secret) != 0 || got.Note == "" {
		t.Errorf("FAIL: limited read: id %q secret %d note %q", got.Request.ID, len(got.Secret), got.Note)
	}

	if _, err := c.r.Invoke(limitedTo("jva-bot", "elsewhere"), "signoff.get", signOffGetIn{ID: id}); !errors.Is(err, ops.ErrNotFound) {
		t.Errorf("FAIL: a token limited to another repository: %v", err)
	}

	// The secret is still there for the account's unlimited token.
	if first := c.get("jva-bot", id); len(first.Secret) == 0 {
		t.Errorf("FAIL: the limited read used up the secret")
	}

	c.agentOf("jva-lost", "jva")
	c.mint("jva-lost", "work", "api", []string{"test"}, 30)
	_, about := c.file("jva-bot", reissuePath, `{"name":"jva-lost"}`)
	if _, err := c.r.Invoke(limitedTo("jva-bot", "test"), "signoff.get", signOffGetIn{ID: about}); !errors.Is(err, ops.ErrNotFound) {
		t.Errorf("FAIL: a limited token read a request about no repository: %v", err)
	}

	if _, err := c.r.Invoke(limitedTo("jva-bot", "test"), "me.account", meAccountIn{}); err != nil {
		t.Errorf("FAIL: me.account refused to a limited token: %v", err)
	}
	if _, err := c.r.Invoke(limitedTo("jva-bot", "test"), "signoff.list", signOffListIn{}); err == nil {
		t.Errorf("FAIL: signoff.list opened to a limited token")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A request bound to a pickup code never releases a secret, so a limited
// token reading it is told nothing about collecting.
func TestALimitedTokenReadsAPickupBoundRequestWithoutANote(t *testing.T) {
	c := newCredRig(t)
	_, pcode := newPickupKey(t)
	code, id, body := c.fileWith("jva-bot", fakeAuth, pcode, "/api/v1/r/test/ci/tokens", `{"label":"ci"}`)
	if code != 202 || id == "" {
		t.Fatalf("FAIL: asking with a pickup code: %d %s", code, body)
	}
	c.approve("jva", id)
	out, err := c.r.Invoke(limitedTo("jva-bot", "test"), "signoff.get", signOffGetIn{ID: id})
	if err != nil {
		t.Fatalf("FAIL: a limited token reading a pickup request: %v", err)
	}
	got := out.(signOffGetOut)
	if len(got.Secret) != 0 || got.Note != "" {
		t.Errorf("FAIL: pickup request: secret %d note %q", len(got.Secret), got.Note)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A lapsed request shows its credentials revoked to a limited token, and
// the lapse note wins over the limited one.
func TestALimitedTokenReadsALapsedRequestAsLapsed(t *testing.T) {
	c := newCredRig(t)
	id := c.ask("jva-bot", "jva", "/api/v1/r/test/ci/tokens", `{"label":"ci"}`)
	if n := c.sweep(lateEnough); n != 1 {
		t.Fatalf("FAIL: the sweep cleared %d, want 1", n)
	}
	out, err := c.r.Invoke(limitedTo("jva-bot", "test"), "signoff.get", signOffGetIn{ID: id})
	if err != nil {
		t.Fatalf("FAIL: a limited token reading a lapsed request: %v", err)
	}
	got := out.(signOffGetOut)
	if len(got.Secret) != 0 || !strings.Contains(got.Note, "lapsed") {
		t.Errorf("FAIL: lapsed request: secret %d note %q", len(got.Secret), got.Note)
	}
	for _, cr := range got.Credentials {
		if cr.Idle || cr.Lapses != "" || !strings.Contains(cr.Note, "revoked") {
			t.Errorf("FAIL: credential %+v not shown revoked", cr)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Only the ops named here run for a token limited to repositories
// without a repository to check; adding one must be deliberate.
func TestOnlyTheNamedOpsAreOwn(t *testing.T) {
	r := ops.NewRegistry()
	if err := RegisterAll(r, fxDeps(t)); err != nil {
		t.Fatal(err)
	}
	got := map[string]bool{}
	for _, d := range r.All() {
		if d.Own {
			got[d.Name] = true
		}
	}
	want := map[string]bool{"me.account": true, "signoff.get": true, "me.token.end": true}
	if len(got) != len(want) {
		t.Errorf("FAIL: Own ops %v, want %v", got, want)
	}
	for n := range want {
		if !got[n] {
			t.Errorf("FAIL: %s is not Own", n)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A token limited to one repository sees only that repository in
// me.account, and no team; an unlimited token of the account sees all.
func TestALimitedTokenSeesOnlyItsRepositoriesInMeAccount(t *testing.T) {
	accts := newFakeAccounts()
	accts.accounts["jan"] = Account{Name: "jan", Created: "2026-01-01T00:00:00Z"}
	accts.grants["jan"] = map[string]string{"test": "reader", "elsewhere": "reader"}
	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", Accounts: accts}); err != nil {
		t.Fatal(err)
	}
	out, err := r.Invoke(limitedTo("jan", "test"), "me.account", meAccountIn{})
	if err != nil {
		t.Fatal(err)
	}
	got := out.(meAccountOut)
	if len(got.Repos) != 1 || got.Repos[0].Repo != "test" || len(got.Teams) != 0 {
		t.Errorf("FAIL: limited me.account: repos %+v teams %v", got.Repos, got.Teams)
	}
	out, err = r.Invoke(ops.WithUser(context.Background(), "jan"), "me.account", meAccountIn{})
	if err != nil {
		t.Fatal(err)
	}
	if n := len(out.(meAccountOut).Repos); n != 2 {
		t.Errorf("FAIL: unlimited me.account saw %d repositories, want 2", n)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}