Skip to content
quarry Artifact [0682920083]
Login

Artifact [0682920083]

Artifact 068292008304849085d4e471915a398a2a763b512faa5787cbfbae65f7224b0f:


package api

import (
	"errors"
	"strings"
	"testing"

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

// A URL names a repository OWNER/NAME; the forge keys it by NAME. A token
// minted with the URL's spelling is stored under the key and reaches it.
func TestATokenMintedByAddressIsStoredUnderTheKeyAndReachesIt(t *testing.T) {
	c := newCredRig(t)
	out, err := c.r.Invoke(asUser("dmestas"), "agent.token.new", agentTokenNewIn{Agent: "fbot", Name: "n", Scopes: "api", Repos: "dmestas/test"})
	if err != nil {
		t.Fatalf("FAIL: minting by address: %v", err)
	}
	toks, _ := c.db.Tokens("fbot")
	if len(toks) != 1 || strings.Join(toks[0].Repos, ",") != "test" {
		t.Fatalf("FAIL: stored repos: %+v", toks)
	}
	_ = out
	if !toks[0].Reaches("test") {
		t.Errorf("FAIL: the token does not reach the repository")
	}
	// The bare key is accepted too, and the list collapses repeats.
	if _, err := c.r.Invoke(asUser("dmestas"), "agent.token.new", agentTokenNewIn{Agent: "fbot", Name: "m", Scopes: "api", Repos: "test, dmestas/test"}); err != nil {
		t.Fatalf("FAIL: minting by key: %v", err)
	}
	toks, _ = c.db.Tokens("fbot")
	for _, tk := range toks {
		if strings.Join(tk.Repos, ",") != "test" {
			t.Errorf("FAIL: stored repos: %+v", tk.Repos)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// agent.token.repos resolves the same way.
func TestTokenReposByAddressStoresTheKey(t *testing.T) {
	c := newCredRig(t)
	if _, err := c.r.Invoke(asUser("dmestas"), "agent.token.new", agentTokenNewIn{Agent: "fbot", Name: "n", Scopes: "api"}); err != nil {
		t.Fatal(err)
	}
	toks, _ := c.db.Tokens("fbot")
	if _, err := c.r.Invoke(asUser("dmestas"), "agent.token.repos", agentTokenReposIn{Agent: "fbot", ID: toks[0].ID, Repos: "dmestas/test"}); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	toks, _ = c.db.Tokens("fbot")
	if strings.Join(toks[0].Repos, ",") != "test" {
		t.Errorf("FAIL: stored repos: %+v", toks[0].Repos)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A name that is no repository is refused at mint, with a 400 that names
// it, and nothing is stored.
func TestAnUnknownRepositoryIsRefusedAtMint(t *testing.T) {
	c := newCredRig(t)
	for _, name := range []string{"dmestas/nope", "nope", "other/test"} {
		_, err := c.r.Invoke(asUser("dmestas"), "agent.token.new", agentTokenNewIn{Agent: "fbot", Name: "n", Scopes: "api", Repos: name})
		if !errors.Is(err, ops.ErrInvalid) || !strings.Contains(err.Error(), name) {
			t.Errorf("FAIL: %q: %v", name, err)
		}
	}
	if toks, _ := c.db.Tokens("fbot"); len(toks) != 0 {
		t.Errorf("FAIL: a token was stored: %+v", toks)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// An asked mint, and a plan step, name the repository by key once
// approved: the credentials summary shows the key.
func TestAnAskedMintShowsTheResolvedName(t *testing.T) {
	c := newCredRig(t)
	id := c.ask("fbot", "dmestas", "/api/v1/agents/tokens", `{"agent":"fbot","name":"one","scopes":"api","repos":"dmestas/test"}`)
	out, err := c.r.Invoke(asUser("fbot"), "signoff.get", signOffGetIn{ID: id})
	if err != nil {
		t.Fatal(err)
	}
	g := out.(signOffGetOut)
	if len(g.Credentials) != 1 || strings.Join(g.Credentials[0].Repos, ",") != "test" {
		t.Errorf("FAIL: credentials: %+v", g.Credentials)
	}
	row, _, err := c.db.SignOffByID(id)
	if err != nil || !strings.Contains(row.Input, `"repos":"test"`) {
		t.Errorf("FAIL: stored input %q: %v", row.Input, err)
	}

	po, err := c.r.Invoke(asUser("fbot"), planOp, planIn{Title: "t", Steps: []planStepIn{
		{Op: "agent.token.new", Input: []byte(`{"agent":"fbot","name":"b","scopes":"api","repos":"dmestas/test"}`)},
	}})
	if err != nil {
		t.Fatalf("FAIL: plan: %v", err)
	}
	pid := po.(planOut).SignOff.ID
	c.approve("dmestas", pid)
	pg, err := c.r.Invoke(asUser("fbot"), "signoff.get", signOffGetIn{ID: pid})
	if err != nil {
		t.Fatal(err)
	}
	if p := pg.(signOffGetOut); len(p.Credentials) != 1 || strings.Join(p.Credentials[0].Repos, ",") != "test" {
		t.Errorf("FAIL: plan credentials: %+v", p.Credentials)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A plan step naming OWNER/NAME is judged, and recorded, as NAME.
func TestAPlanStepByAddressIsJudgedAsTheKey(t *testing.T) {
	c := newCredRig(t)
	if got := c.deps.canonRepo("dmestas/test"); got != "test" {
		t.Errorf("FAIL: canonRepo = %q", got)
	}
	if got := c.deps.canonRepo("test"); got != "test" {
		t.Errorf("FAIL: canonRepo of a key = %q", got)
	}
	if got := c.deps.canonRepo("dmestas/nope"); got != "dmestas/nope" {
		t.Errorf("FAIL: canonRepo of an unknown = %q", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}