package equipotest import ( "encoding/json" "net/url" "strings" "testing" "amadan.net/oficina/equipo/internal/runnerproto" "amadan.net/oficina/equipo/internal/wire" ) // One tap (Mac runners spec ยง1.1): the link a runner prints carries its // code, so opening it shows "Add to your team" with the one // confirmed button; nothing is approved until that POST. A wrong code // shows nothing to add. func TestEnrolmentLinkIsOneTap(t *testing.T) { f := newEnrolHTTP(t) c := signinClient(t, f.server.URL) signinEmail(t, c, f.server.URL, testOrigin, f.mail, ownerAddress) pub, key := machine(t) status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: "Tito Studio"}, "") if status != 201 { t.Fatalf("begin HTTP: %d %s", status, body) } var code runnerproto.DeviceCode if err := json.Unmarshal(body, &code); err != nil { t.Fatal(err) } view := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(code.UserCode), 200) if !strings.Contains(view, "Add Tito Studio to your team") || !strings.Contains(view, `name="action" value="approve"`) { t.Fatalf("the link did not show the machine to add:\n%s", view) } // Opening it twice still shows it: the GET inspects, never approves. if again := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(code.UserCode), 200); !strings.Contains(again, "Add Tito Studio to your team") { t.Fatal("opening the link changed the enrolment") } pagePost(t, c, f.server.URL, "/runners/enrol", testOrigin, formFields(t, view, "/runners/enrol"), 303) if status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments/token", runnerproto.ExchangeEnrolmentRequest{Version: 1, DeviceSecret: code.DeviceSecret, Proof: deviceProof(code, key)}, ""); status != 200 { t.Fatalf("the one button did not add it: %d %s", status, body) } res, err := c.Get(f.server.URL + "/runners/enrol?code=ZZZZZZZZZZ") if err != nil { t.Fatal(err) } if wrong := readBody(t, res); res.StatusCode == 200 && strings.Contains(wrong, "to your team") { t.Fatal("a wrong code showed a machine") } } // A Mac can only be a worker: for a person with no primary runner its // link says so in words and it cannot be added (it would become their // primary). Once they have a primary, the same kind of link adds it, as // a worker. func TestMacEnrolmentNeedsAPrimary(t *testing.T) { f := newEnrolHTTP(t) c := signinClient(t, f.server.URL) signinEmail(t, c, f.server.URL, testOrigin, f.mail, ownerAddress) begin := func(label string, worker bool) runnerproto.DeviceCode { t.Helper() pub, _ := machine(t) status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: label, WorkerOnly: worker}, "") if status != 201 { t.Fatalf("begin HTTP: %d %s", status, body) } var code runnerproto.DeviceCode if err := json.Unmarshal(body, &code); err != nil { t.Fatal(err) } return code } mac := begin("Tito Studio", true) res, err := c.Get(f.server.URL + "/runners/enrol?code=" + url.QueryEscape(mac.UserCode)) if err != nil { t.Fatal(err) } page := readBody(t, res) if strings.Contains(page, `value="approve"`) || !strings.Contains(page, "Add your main runner first") { t.Fatalf("a Mac offered as a first runner (%d):\n%s", res.StatusCode, page) } res = clientPost(t, c, f.server.URL+"/runners/enrol", testOrigin, url.Values{"user_code": {mac.UserCode}, "action": {"inspect"}}) if body := readBody(t, res); strings.Contains(body, `value="approve"`) { t.Fatal("inspect offered to add a Mac with no primary") } main := begin("docs-t1", false) view := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(main.UserCode), 200) pagePost(t, c, f.server.URL, "/runners/enrol", testOrigin, formFields(t, view, "/runners/enrol"), 303) if view := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(mac.UserCode), 200); !strings.Contains(view, "Add Tito Studio to your team") { t.Fatal("a Mac not offered once there is a primary") } } // Paul's Studio, 2026-11-10: a Mac joins a person who already has a // Linux primary. Approved through the page, its exchange gives it a // worker's credential; nothing on the way answers with a server error. func TestMacEnrolmentApprovedAfterAPrimary(t *testing.T) { f := newEnrolHTTP(t) c := signinClient(t, f.server.URL) signinEmail(t, c, f.server.URL, testOrigin, f.mail, ownerAddress) enrol := func(label string, worker bool) runnerproto.RunnerCredential { t.Helper() pub, key := machine(t) status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: label, WorkerOnly: worker}, "") if status != 201 { t.Fatalf("begin HTTP: %d %s", status, body) } var code runnerproto.DeviceCode if err := json.Unmarshal(body, &code); err != nil { t.Fatal(err) } view := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(code.UserCode), 200) pagePost(t, c, f.server.URL, "/runners/enrol", testOrigin, formFields(t, view, "/runners/enrol"), 303) status, body = enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments/token", runnerproto.ExchangeEnrolmentRequest{Version: 1, DeviceSecret: code.DeviceSecret, Proof: deviceProof(code, key)}, "") if status != 200 { t.Fatalf("%s exchange: %d %s", label, status, body) } var cred runnerproto.RunnerCredential if err := json.Unmarshal(body, &cred); err != nil { t.Fatal(err) } return cred } if cred := enrol("docs-t1", false); cred.Role == wire.RoleWorker { t.Fatal("the first runner is not the primary") } if cred := enrol("Tito Studio", true); cred.Role != wire.RoleWorker { t.Fatalf("the Mac enrolled as %q", cred.Role) } } // A machine enrolling cannot say what went wrong on the server, so the // server logs it: the route and the error, never the device secret. func TestEnrolmentServerErrorIsLogged(t *testing.T) { f := newEnrolHTTP(t) pub, key := machine(t) status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: "Tito Studio", WorkerOnly: true}, "") if status != 201 { t.Fatalf("begin HTTP: %d %s", status, body) } var code runnerproto.DeviceCode if err := json.Unmarshal(body, &code); err != nil { t.Fatal(err) } if _, err := f.db.Writer().Exec("ALTER TABLE runner_enrolments RENAME TO unavailable_enrolments"); err != nil { t.Fatal(err) } if status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments/token", runnerproto.ExchangeEnrolmentRequest{Version: 1, DeviceSecret: code.DeviceSecret, Proof: deviceProof(code, key)}, ""); status != 503 || !strings.Contains(string(body), "service.unavailable") { t.Fatalf("exchange: %d %s", status, body) } logs := f.logs.String() if !strings.Contains(logs, "A runner request failed on the server.") || !strings.Contains(logs, "route=/api/enrolments/token") || !strings.Contains(logs, "status=503") || !strings.Contains(logs, "runner_enrolments") { t.Fatalf("not logged: %s", logs) } if strings.Contains(logs, code.DeviceSecret) { t.Fatal("the device secret was logged") } }