Skip to content
quarry Artifact [99c74efbbe]
Login

Artifact [99c74efbbe]

Artifact 99c74efbbec74dce4c7774a5cc1f092c261f13d908672a8592393923844bcbfd:


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-approve", "setup-claim-repos",
		"setup-claim-reach", "setup-claim-expires", "setup-claim-collector", "setup-claim-lapse"} {
		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", "setup-claim-continue", "setup-claim-code"} {
		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)
	}
	// The repository limit is folded away and nothing is ticked.
	if !hasHook(body, "setup-claim-limit") || strings.Contains(body, `data-hook="setup-claim-repo" checked`) || strings.Contains(body, `data-hook="setup-claim-limit" open`) {
		t.Errorf("FAIL: the limit is not a collapsed, unticked choice")
	}
	// 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-approve") || hasHook(body, "setup-claim-empty") {
		t.Errorf("FAIL: a login with no repositories to pick")
	}
	// Approve is one press: it claims with the optional picks and answers
	// the request it filed, as the person.
	f.repos = repoRows("ctx", "suit")
	rec := f.do("POST", "/setup/claim", url.Values{"code": {"ABCD-EFGH"}, "do": {"approve"}, "repos": {"suit"}}, true)
	if rec.Code != 303 || rec.Header().Get("Location") != "/me/requests/req123" {
		t.Errorf("FAIL: approve answered %d to %q", rec.Code, rec.Header().Get("Location"))
	}
	if len(f.claimed) != 1 || f.claimed[0].Repos != "suit" || f.claimed[0].Role != "" {
		t.Errorf("FAIL: the claim sent %+v", f.claimed)
	}
	if len(f.decided) != 1 || f.decided[0].ID != "req123" || !f.decided[0].Approve {
		t.Errorf("FAIL: the decision: %+v", f.decided)
	}
	// Deny throws the start away and decides nothing.
	if rec := f.do("POST", "/setup/claim", url.Values{"code": {"ABCD-EFGH"}, "do": {"discard"}}, true); rec.Code != 303 || f.discarded != 1 || len(f.decided) != 1 {
		t.Errorf("FAIL: deny answered %d, discarded %d, decided %d", rec.Code, f.discarded, len(f.decided))
	}
	// A kind that is not a login never decides for the person.
	f.kind = "read"
	f.do("POST", "/setup/claim", url.Values{"code": {"ABCD-EFGH"}, "do": {"approve"}, "repos": {"suit"}}, true)
	if len(f.decided) != 1 {
		t.Errorf("FAIL: a read client's claim was decided for the person")
	}
	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", "login"}, 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,login"},
		{"setup-reach", "data-repos", ""},
		{"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", "setup-ceiling"} {
		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")
	}
}