package api
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
const loginLabelForTest = "mac-mini, pi-c"
func loginCtx(label string) (c ops.TokenAuth) {
return ops.TokenAuth{ID: "login-token-1", Name: label, Login: true, Caps: func(string) ops.Caps { return oltp.CapSet("oi") }}
}
// A landing accepted on a login token is the person's accept, said "via"
// the token's label: the merge's author is the person, its message and
// the forge journal name the machine.
func TestALandingAcceptedOnALoginTokenIsThePersonsViaItsLabel(t *testing.T) {
dir, ids, ix, pipeSync := premergeStore(t)
root := ztip(t, dir, "trunk")
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
ci.green(ztip(t, dir, "one"), root)
acts := &recordedActs{}
d := Deps{Version: "v", Checks: mergeChecks{}, CI: ci, Acts: acts, Repos: NewRepos(map[string]*Repo{
"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
ctx := ops.WithToken(ops.WithUser(t.Context(), "dan"), loginCtx(loginLabelForTest))
if got := ops.Via(ctx); got != "login-token-1" || ops.ViaLabel(ctx) != loginLabelForTest {
t.Errorf("FAIL: via is %q / %q", got, ops.ViaLabel(ctx))
}
if _, err := d.landingSay(ctx, landingSayIn{Repo: "test", ID: ids[0]}, "accept"); err != nil {
t.Fatal(err)
}
merges, err := ix.MergesForLandings([]string{ids[0]})
if err != nil {
t.Fatal(err)
}
m := merges[ids[0]]
if m.User != "dan" || !strings.Contains(m.Comment, "via "+loginLabelForTest) {
t.Errorf("FAIL: the merge is by %q, comment %q", m.User, m.Comment)
}
if len(acts.acts) != 1 || acts.acts[0].Actor != "dan via "+loginLabelForTest || acts.acts[0].Op != "landing.accept" || acts.acts[0].Repo != "test" {
t.Errorf("FAIL: the journal: %+v", acts.acts)
}
if l, ok, _ := ix.LandingByPrefix(ids[0]); !ok || l.State != "accepted" {
t.Errorf("FAIL: the landing is %+v", l)
}
if !t.Failed() {
t.Log("ok")
}
}
// An accept pressed by the person alone says no "via".
func TestAnAcceptWithoutATokenSaysNoVia(t *testing.T) {
dir, ids, ix, pipeSync := premergeStore(t)
root := ztip(t, dir, "trunk")
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
ci.green(ztip(t, dir, "one"), root)
acts := &recordedActs{}
d := Deps{Version: "v", Checks: mergeChecks{}, CI: ci, Acts: acts, Repos: NewRepos(map[string]*Repo{
"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
if _, err := d.landingSay(as("dan", "i"), landingSayIn{Repo: "test", ID: ids[0]}, "accept"); err != nil {
t.Fatal(err)
}
merges, _ := ix.MergesForLandings([]string{ids[0]})
if strings.Contains(merges[ids[0]].Comment, "via") || len(acts.acts) != 0 {
t.Errorf("FAIL: a person's own accept says via: %q %+v", merges[ids[0]].Comment, acts.acts)
}
if !t.Failed() {
t.Log("ok")
}
}
// The deferred accept of a landing armed on a login token reads the same.
func TestAnAutoLandArmedOnALoginTokenIsViaItsLabel(t *testing.T) {
dir, ids, ix, pipeSync := premergeStore(t)
root := ztip(t, dir, "trunk")
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
ci.green(ztip(t, dir, "two"), root)
acts := &recordedActs{}
d := Deps{Version: "v", ZitVersion: zitcli.MinVersion, Checks: mergeChecks{}, CI: ci, Notify: &fakeNotify{}, Acts: acts,
Repos: NewRepos(map[string]*Repo{"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
store := &sweepStub{rows: []oltp.AutoAccept{{Repo: "test", Landing: ids[1], Account: "dan", Tip: ztip(t, dir, "two"), Via: loginLabelForTest}}}
if landed := d.AutoAcceptSweep(store); len(landed) != 1 {
t.Fatalf("FAIL: the sweep landed %v", landed)
}
merges, _ := ix.MergesForLandings([]string{ids[1]})
m := merges[ids[1]]
if m.User != "dan" || !strings.Contains(m.Comment, "via "+loginLabelForTest) {
t.Errorf("FAIL: the merge is by %q, comment %q", m.User, m.Comment)
}
if len(acts.acts) != 1 || acts.acts[0].Actor != "dan via "+loginLabelForTest {
t.Errorf("FAIL: the journal: %+v", acts.acts)
}
if !t.Failed() {
t.Log("ok")
}
}
// Only a login token's label is written "via", and only in the label's own
// alphabet: another token's name, however it is spelt, never reaches a merge
// message or the journal.
func TestOnlyALoginTokensCleanLabelIsWrittenVia(t *testing.T) {
for name, tok := range map[string]ops.TokenAuth{
"an agent token named with a newline": {ID: "T", Name: "evil\nSigned-off-by: root", Caps: func(string) ops.Caps { return oltp.CapSet("oi") }},
"a plain token with a clean name": {ID: "T", Name: "plain", Caps: func(string) ops.Caps { return oltp.CapSet("oi") }},
"a login token with a bad label": {ID: "T", Name: "evil\nroot", Login: true, Caps: func(string) ops.Caps { return oltp.CapSet("oi") }},
} {
dir, ids, ix, pipeSync := premergeStore(t)
root := ztip(t, dir, "trunk")
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
ci.green(ztip(t, dir, "one"), root)
acts := &recordedActs{}
d := Deps{Version: "v", Checks: mergeChecks{}, CI: ci, Acts: acts, Repos: NewRepos(map[string]*Repo{
"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
ctx := ops.WithToken(ops.WithUser(t.Context(), "dan"), tok)
if _, err := d.landingSay(ctx, landingSayIn{Repo: "test", ID: ids[0]}, "accept"); err != nil {
t.Fatalf("%s: %v", name, err)
}
merges, _ := ix.MergesForLandings([]string{ids[0]})
if c := merges[ids[0]].Comment; strings.Contains(c, "via") || strings.Contains(c, "Signed-off-by") || strings.Contains(c, "root") || len(acts.acts) != 0 {
t.Errorf("FAIL: %s: the merge says %q, journal %+v", name, c, acts.acts)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// A login token cannot be minted under a label that is not the label rule's.
func TestALoginTokenIsNotMintedUnderABadLabel(t *testing.T) {
g := newStartRig(t)
for _, name := range []string{"a\nb", "<b>x</b>", " lead", "trail ", "", strings.Repeat("a", 65)} {
if _, _, err := g.db.MintToken("jva", oltp.TokenSpec{Name: name, Scopes: []string{oltp.ScopeAPI, oltp.ScopeSync, oltp.ScopeLogin}}); err == nil {
t.Errorf("FAIL: a login token was minted as %q", name)
}
}
if _, _, err := g.db.MintToken("jva", oltp.TokenSpec{Name: "mac-mini, pi-c", Scopes: []string{oltp.ScopeAPI, oltp.ScopeSync, oltp.ScopeLogin}}); err != nil {
t.Errorf("FAIL: a clean label was refused: %v", err)
}
if !t.Failed() {
t.Log("ok")
}
}