package api
import (
"encoding/json"
"errors"
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// RFC 0030, Several repositories: one request, one approval, one agent
// granted its role on each listed repository, one token limited to
// exactly those, one pickup.
func TestAPushSetupOnSeveralRepositoriesIsOnePersonAgentWithOneToken(t *testing.T) {
g := newSetupRig(t)
key, code := newPickupKey(t)
id := g.ask("dmestas-bot", `{"name":"multi-push","kind":"push","repositories":["suit","ctx"]}`, code)
v := g.get("dmestas", id).Setup
if v == nil || strings.Join(v.Repos, ",") != "suit,ctx" || strings.Join(v.TokenRepos, ",") != "suit,ctx" ||
v.Belongs != "person" || v.Owner != "dmestas" || v.Role != "developer" {
t.Fatalf("FAIL: the view before approval: %+v", v)
}
if err := g.decide("dmestas", id, true); err != nil {
t.Fatal(err)
}
a, ok := g.db.AccountByName("multi-push")
if !ok || a.Operator != "dmestas" {
t.Fatalf("FAIL: the agent is not the person's own: %+v", a)
}
for _, repo := range []string{"suit", "ctx"} {
if stored, _ := g.db.CapsOf("multi-push", repo); stored != oltp.Write.Caps() {
t.Errorf("FAIL: the grant on %s is %q", repo, stored)
}
}
if grants, _ := g.db.GrantsFor("multi-push"); len(grants) != 2 {
t.Errorf("FAIL: %d grants, want one per repository", len(grants))
}
toks, _ := g.db.Tokens("multi-push")
if len(toks) != 1 || strings.Join(toks[0].Scopes, ",") != "sync" || strings.Join(toks[0].Repos, ",") != "suit,ctx" ||
toks[0].Ceiling != oltp.Write.Caps() {
t.Fatalf("FAIL: the token: %+v", toks)
}
st, body := g.collectNow(key)
var got struct {
Result agentSetupOut
Secret struct{ Value string }
}
if err := json.Unmarshal([]byte(body), &got); st != 200 || err != nil || !strings.HasPrefix(got.Secret.Value, "qat_") || len(got.Result.Grants) != 2 {
t.Fatalf("FAIL: collecting: %d %v", st, err)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestAnInputNamingRepositoriesTwoWaysOrTooManyIsRefused(t *testing.T) {
g := newSetupRig(t)
eleven := `["a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11"]`
for what, body := range map[string]string{
"both fields": `{"name":"x-both","kind":"read","repos":"suit","repositories":["ctx"]}`,
"over the cap": `{"name":"x-many","kind":"read","repositories":` + eleven + `}`,
"none": `{"name":"x-none","kind":"read","repositories":[]}`,
"push over cap": `{"name":"x-pmany","kind":"push","repositories":` + eleven + `}`,
} {
if status, _ := g.post(tokenCtx("dmestas-bot"), body, true, ""); status != 400 {
t.Errorf("FAIL: %s answered %d", what, status)
}
}
if !t.Failed() {
t.Log("ok")
}
}
func TestDuplicatesAndCaseVariantsCollapseToOneGrantEach(t *testing.T) {
g := newSetupRig(t)
id := g.ask("dmestas-bot", `{"name":"e-dups","kind":"read","repositories":["suit","SUIT","ctx","suit"]}`, "")
if v := g.get("dmestas", id).Setup; v == nil || strings.Join(v.Repos, ",") != "suit,ctx" {
t.Errorf("FAIL: the view: %+v", v)
}
if err := g.decide("dmestas", id, true); err != nil {
t.Fatal(err)
}
if grants, _ := g.db.GrantsFor("e-dups"); len(grants) != 2 {
t.Errorf("FAIL: %d grants", len(grants))
}
if !t.Failed() {
t.Log("ok")
}
}
// An agent set up for several repositories is a person's, never a
// repository's: asking for the other is refused, and a person who
// administers every one still gets the person's agent by default.
func TestSeveralRepositoriesAreNeverARepositorysAgent(t *testing.T) {
g := newSetupRig(t)
if status, _ := g.post(tokenCtx("dmestas-bot"), `{"name":"x-repo","kind":"push","repositories":["suit","ctx"],"belongs_to":"repository"}`, true, ""); status != 400 {
t.Errorf("FAIL: a repository's agent on two repositories answered %d", status)
}
if status, _ := g.post(tokenCtx("dmestas-bot"), `{"name":"x-new","kind":"push","repositories":["suit","ctx"],"new_repo":true}`, true, ""); status == 202 {
t.Errorf("FAIL: a new repository was asked for with a list")
}
g.nothingMade("x-repo")
// One repository keeps the rule as before.
id := g.ask("zeitgeist", `{"name":"suit-one","kind":"push","repositories":["suit"]}`, "")
if v := g.get("dmestas", id).Setup; v == nil || v.Belongs != "repository" || v.Owner != "suit" {
t.Errorf("FAIL: a single repository's view: %+v", v)
}
if !t.Failed() {
t.Log("ok")
}
}
// Every repository is judged for asking and for approving: the asker may
// ask only for what its person could grant, an unseeable repository
// answers as a missing one, and nothing is made when any one fails.
func TestOneRepositoryTheCallerCannotGrantOnRefusesTheWholeRequest(t *testing.T) {
g := newSetupRig(t)
if err := g.db.Revoke("dmestas", "secret"); err != nil {
t.Fatal(err)
}
// dmestas is a site admin, so a repository he lost is judged by what
// he still holds; jva holds nothing anywhere.
for _, body := range []string{
`{"name":"j-multi","kind":"push","repositories":["suit","ctx"]}`,
`{"name":"j-multi","kind":"read","repositories":["suit","secret"]}`,
} {
if status, _ := g.post(tokenCtx("jva-bot"), body, true, ""); status == 202 {
t.Errorf("FAIL: jva's agent asked for %s", body)
}
}
_, missing := g.post(tokenCtx("jva-bot"), `{"name":"j-multi","kind":"read","repositories":["suit","secret"]}`, true, "")
_, absent := g.post(tokenCtx("jva-bot"), `{"name":"j-multi","kind":"read","repositories":["suit","no-such"]}`, true, "")
if missing != absent {
t.Errorf("FAIL: an unseeable repository answers differently from a missing one")
}
g.nothingMade("j-multi")
// Approved with the second repository lost since asking: failed whole.
id := g.ask("dmestas-bot", `{"name":"p-multi","kind":"push","repositories":["suit","ctx"]}`, "")
if err := g.db.Revoke("dmestas", "ctx"); err != nil {
t.Fatal(err)
}
if err := g.decide("dmestas", id, true); err == nil {
t.Errorf("FAIL: approved with a repository the approver can no longer give the role on")
}
g.nothingMade("p-multi")
if !t.Failed() {
t.Log("ok")
}
}
func TestAClaimNamingSeveralRepositoriesFilesOneRequest(t *testing.T) {
g := newStartRig(t)
_, pickup := newPickupKey(t)
push := g.begin("multi-claim", "push", pickup)
if _, err := g.claim("dmestas", push, "suit,ctx", "maintainer"); err != nil {
t.Fatal(err)
}
rows := g.requestsOf("dmestas")
if len(rows) != 1 {
t.Fatalf("FAIL: %d requests", len(rows))
}
v := g.get("dmestas", rows[0].ID).Setup
if v == nil || strings.Join(v.Repos, ",") != "suit,ctx" || v.Belongs != "person" || v.Role != "maintainer" || v.Pickup != pickup {
t.Errorf("FAIL: the view: %+v", v)
}
// The list field claims the same way, and not alongside the other.
_, pickupB := newPickupKey(t)
again := g.begin("multi-claim-b", "push", pickupB)
if _, err := g.r.Invoke(asUser("dmestas"), "setup.start.claim", setupClaimIn{Code: again, Repos: "suit", Repositories: []string{"ctx"}}); !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: both claim fields: %v", err)
}
if _, err := g.r.Invoke(asUser("dmestas"), "setup.start.claim", setupClaimIn{Code: again, Repositories: []string{"suit", "ctx"}}); err != nil {
t.Errorf("FAIL: the list field: %v", err)
}
if !t.Failed() {
t.Log("ok")
}
}