package web
import (
"net/url"
"strings"
"testing"
)
// A login start is a machine asking to log in as the person: its claim
// page says so with hooks, offers an optional limit and no name or role.
func TestTheClaimPageOfALoginIsAMachineActingAsThePerson(t *testing.T) {
f := newClaimForge(t)
f.kind = "login"
body := f.do("POST", "/setup", url.Values{"code": {"ABCD-EFGH"}}, true).Body.String()
if v, ok := hookAttr(t, body, "setup-claim", "data-kind"); !ok || v != "login" {
t.Fatalf("FAIL: data-kind %q (present %v)", v, ok)
}
if v, _ := hookAttr(t, body, "setup-claim-account", "data-account"); v != "jva" {
t.Errorf("FAIL: the person it acts as is %q", v)
}
if v, _ := hookAttr(t, body, "setup-claim-agent", "data-agent"); v != "e-desktop" {
t.Errorf("FAIL: the machine is %q", v)
}
for _, hook := range []string{"setup-claim-gives", "setup-claim-from", "setup-claim-warn", "setup-claim-discard", "setup-claim-continue", "setup-claim-repos"} {
if !hasHook(body, hook) {
t.Errorf("FAIL: the page has no %s", hook)
}
}
for _, hook := range []string{"setup-claim-role", "setup-claim-rename", "setup-claim-new", "setup-claim-kind"} {
if hasHook(body, hook) {
t.Errorf("FAIL: a login page has %s", hook)
}
}
if v, _ := hookAttr(t, body, "setup-claim-from", "data-match"); v != "true" {
t.Errorf("FAIL: match %q", v)
}
// With no repository to limit to, the page still continues.
f.repos = nil
body = f.do("POST", "/setup", url.Values{"code": {"ABCD-EFGH"}}, true).Body.String()
if hasHook(body, "setup-claim-repos") || !hasHook(body, "setup-claim-continue") || hasHook(body, "setup-claim-empty") {
t.Errorf("FAIL: a login with no repositories to pick")
}
// Continue posts the optional picks and nothing else.
f.repos = repoRows("ctx", "suit")
if rec := f.do("POST", "/setup/claim", url.Values{"code": {"ABCD-EFGH"}, "do": {"continue"}, "repos": {"suit"}}, true); rec.Code != 303 {
t.Errorf("FAIL: continue answered %d", rec.Code)
}
if len(f.claimed) != 1 || f.claimed[0].Repos != "suit" || f.claimed[0].Role != "" {
t.Errorf("FAIL: the claim sent %+v", f.claimed)
}
if !t.Failed() {
t.Log("ok")
}
}
// The approval page's lines are the stored record's: the sentence, the
// machine, the person, the scopes, no ceiling, no named repository and
// the network.
func TestTheApprovalPageOfALoginSaysWhatItGrantsFromTheRecord(t *testing.T) {
code := "3f9a81c207de" + strings.Repeat("0", 52)
sentence := "This machine (pi-c) gets a token that acts as you, jva."
page := renderSetup(t, meSignOff{ID: "abc", Agent: "client of jva", Summary: "Log in pi-c as you", State: "pending",
Requested: "2026-11-13T10:00:00Z", Pickup: code,
Setup: &meSetupView{Kind: "login", Agent: "pi-c", Label: "pi-c", Account: "jva", Gives: sentence, Address: "203.0.113.5",
Scopes: []string{"api", "sync"}, TokenRepos: []string{}, Pickup: code}})
for _, c := range []struct{ hook, attr, want string }{
{"setup", "data-kind", "login"},
{"login-machine", "data-label", "pi-c"},
{"login-account", "data-account", "jva"},
{"setup-scope", "data-scopes", "api,sync"},
{"setup-reach", "data-repos", ""},
{"setup-ceiling", "data-ceiling", ""},
{"setup-expires", "data-days", "0"},
{"setup-collector", "data-pickup", code[:12]},
{"login-from", "data-address", "203.0.113.5"},
{"login-from", "data-match", "false"},
{"login-elsewhere", "data-match", "false"},
} {
if got, ok := hookAttr(t, page, c.hook, c.attr); !ok || got != c.want {
t.Errorf("FAIL: %s %s is %q (present %v), want %q", c.hook, c.attr, got, ok, c.want)
}
}
if !strings.Contains(page, sentence) || !hasHook(page, "login-gives") {
t.Errorf("FAIL: the page does not carry the stored sentence")
}
for _, hook := range []string{"setup-agent", "setup-role", "setup-repos"} {
if hasHook(page, hook) {
t.Errorf("FAIL: a login page draws %s", hook)
}
}
if strings.Index(page, `data-hook="login-gives"`) > strings.Index(page, `data-hook="signoff-approve"`) {
t.Errorf("FAIL: the sentence comes after the buttons")
}
if !t.Failed() {
t.Log("ok")
}
}