Skip to content
quarry Artifact [30594524ef]
Login

Artifact [30594524ef]

Artifact 30594524ef536fa57a1b26ac38cc96291a5457cbc0e69b5d356bb2ef1e8b6575:


package api

import (
	"encoding/json"
	"strings"
	"testing"
)

type credView = Credential

// A setup answers a credentials list beside the secret: the agent, the
// repositories, scopes and ceiling of the token it minted, never its value.
func TestASetupAnswerCarriesACredentialSummaryWithNoValue(t *testing.T) {
	g := newSetupRig(t)
	id := g.ask("zeitgeist", pushBody, "")
	if err := g.decide("dmestas", id, true); err != nil {
		t.Fatal(err)
	}
	got := g.get("zeitgeist", id)
	var sec struct{ Value string }
	if err := json.Unmarshal(got.Secret, &sec); err != nil || sec.Value == "" {
		t.Fatalf("FAIL: no secret: %v", err)
	}
	toks, _ := g.db.Tokens("suit-push")
	if len(got.Credentials) != 1 || len(toks) != 1 {
		t.Fatalf("FAIL: credentials: %+v", got.Credentials)
	}
	c := got.Credentials[0]
	if c.Agent != "suit-push" || strings.Join(c.Repos, ",") != "suit" || strings.Join(c.Scopes, ",") != "sync" ||
		c.Ceiling != toks[0].Ceiling || c.TokenID != toks[0].ID {
		t.Errorf("FAIL: credential %+v", c)
	}
	raw, _ := json.Marshal(got.Credentials)
	if strings.Contains(string(raw), sec.Value) {
		t.Errorf("FAIL: the credential holds the token value")
	}
	for _, k := range []string{`"agent"`, `"repos"`, `"scopes"`, `"ceiling"`, `"token_id"`, `"expires"`, `"idle_lapse_days"`} {
		if !strings.Contains(string(raw), k) {
			t.Errorf("FAIL: credential lacks key %s: %s", k, raw)
		}
	}
	// An idle-lapsing token says so: the days, the day, and a sentence.
	if !c.Idle || c.IdleLapseDays != 90 || c.Lapses == "" || c.Note == "" || c.Expires != "" {
		t.Errorf("FAIL: idle fields: %+v", c)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The same list through the pickup route, with secret still last.
func TestAPickupAnswerCarriesTheCredentialSummary(t *testing.T) {
	g := newSetupRig(t)
	key, code := newPickupKey(t)
	id := g.ask("zeitgeist", pushBody, code)
	if err := g.decide("dmestas", id, true); err != nil {
		t.Fatal(err)
	}
	st, body := g.collectNow(key)
	var got struct {
		Credentials []credView `json:"credentials"`
		Secret      struct{ Value string }
	}
	if err := json.Unmarshal([]byte(body), &got); st != 200 || err != nil || len(got.Credentials) != 1 ||
		got.Credentials[0].Agent != "suit-push" || got.Secret.Value == "" || strings.Contains(strings.SplitN(body, `"secret"`, 2)[0], got.Secret.Value) {
		t.Fatalf("FAIL: collecting: %d %v %s", st, err, body)
	}
	if strings.Contains(body[:strings.Index(body, `"secret"`)], `"value"`) {
		t.Errorf("FAIL: a value before the secret key")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// agent.token.new and a plan containing mints: one entry per minted token,
// each naming its agent, with a fixed expiry carrying no idle fields.
func TestATokenNewAndAPlanYieldCredentialEntriesPerMintedToken(t *testing.T) {
	c := newCredRig(t)
	id := c.ask("fbot", "dmestas", "/api/v1/agents/tokens", `{"agent":"fbot","name":"one","scopes":"api","days":30}`)
	out, err := c.r.Invoke(asUser("fbot"), "signoff.get", signOffGetIn{ID: id})
	if err != nil {
		t.Fatal(err)
	}
	g := out.(signOffGetOut)
	toks, _ := c.db.Tokens("fbot")
	if len(g.Credentials) != 1 || len(toks) != 1 {
		t.Fatalf("FAIL: token.new credentials: %+v", g.Credentials)
	}
	one := g.Credentials[0]
	if one.Agent != "fbot" || strings.Join(one.Scopes, ",") != "api" || one.TokenID != toks[0].ID || one.Ceiling != toks[0].Ceiling ||
		one.Expires == "" || one.Idle || one.IdleLapseDays != 0 || one.Repos == nil {
		t.Errorf("FAIL: token.new credential %+v", one)
	}

	step := func(op, in string) planStepIn { return planStepIn{Op: op, Input: json.RawMessage(in)} }
	po, err := c.r.Invoke(asUser("fbot"), planOp, planIn{Title: "two tokens", Steps: []planStepIn{
		step("agent.token.new", `{"agent":"fbot","name":"a","scopes":"api"}`),
		step("agent.token.new", `{"agent":"fbot","name":"b","scopes":"sync","repos":"test"}`),
	}})
	if err != nil {
		t.Fatal(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)
	}
	p := pg.(signOffGetOut)
	if len(p.Credentials) != 2 || p.Credentials[0].Step != 1 || p.Credentials[1].Step != 2 {
		t.Fatalf("FAIL: plan credentials: %+v", p.Credentials)
	}
	if b := p.Credentials[1]; b.Agent != "fbot" || strings.Join(b.Repos, ",") != "test" || strings.Join(b.Scopes, ",") != "sync" || b.TokenID == "" || b.TokenID == p.Credentials[0].TokenID {
		t.Errorf("FAIL: second plan credential %+v", b)
	}
	if !p.Credentials[0].Idle || p.Credentials[0].Note == "" {
		t.Errorf("FAIL: an idle plan token lacks its idle fields: %+v", p.Credentials[0])
	}
	raw, _ := json.Marshal(p.Credentials)
	var secrets map[string]struct{ Value string }
	if err := json.Unmarshal(p.Secret, &secrets); err != nil || len(secrets) != 2 {
		t.Fatalf("FAIL: plan secrets: %v", err)
	}
	for _, s := range secrets {
		if strings.Contains(string(raw), s.Value) {
			t.Errorf("FAIL: a credential holds a token value")
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A pending pickup collect answers the shape of the eventual answer with
// every value blank; an unknown code is the 404 it always was.
func TestAPendingPickupAnswersTheBlankedShapeForAKnownOp(t *testing.T) {
	g := newSetupRig(t)
	key, code := newPickupKey(t)
	g.ask("zeitgeist", pushBody, code)
	st, body := g.collectNow(key)
	var got struct {
		State string `json:"state"`
		Shape struct {
			Credentials []credView        `json:"credentials"`
			Secret      map[string]string `json:"secret"`
		} `json:"shape"`
	}
	if err := json.Unmarshal([]byte(body), &got); st != 202 || err != nil || got.State != "pending" {
		t.Fatalf("FAIL: pending: %d %v %s", st, err, body)
	}
	if v, ok := got.Shape.Secret["value"]; !ok || v != "" {
		t.Errorf("FAIL: the shape's secret: %v", got.Shape.Secret)
	}
	if len(got.Shape.Credentials) != 1 {
		t.Fatalf("FAIL: the shape's credentials: %s", body)
	}
	if c := got.Shape.Credentials[0]; c.Agent != "" || c.TokenID != "" || len(c.Repos) != 0 || len(c.Scopes) != 0 || c.IdleLapseDays != 0 {
		t.Errorf("FAIL: the shape is not blank: %+v", c)
	}
	if !strings.Contains(body, `"repos":[]`) || !strings.Contains(body, `"scopes":[]`) {
		t.Errorf("FAIL: blank lists must be empty arrays: %s", body)
	}
	if strings.Contains(body, "suit") {
		t.Errorf("FAIL: the shape names something of the request")
	}
	wrong, _ := newPickupKey(t)
	if st, body := g.collectNow(wrong); st != 404 || strings.Contains(body, "shape") {
		t.Errorf("FAIL: an unknown key answered %d %s", st, body)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}