oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "encoding/json"
5 "net/url"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/runnerproto"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// One tap (Mac runners spec §1.1): the link a runner prints carries its
14// code, so opening it shows "Add <machine> to your team" with the one
15// confirmed button; nothing is approved until that POST. A wrong code
16// shows nothing to add.
17func TestEnrolmentLinkIsOneTap(t *testing.T) {
18 f := newEnrolHTTP(t)
19 c := signinClient(t, f.server.URL)
20 signinEmail(t, c, f.server.URL, testOrigin, f.mail, ownerAddress)
21 pub, key := machine(t)
22 status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: "Tito Studio"}, "")
23 if status != 201 {
24 t.Fatalf("begin HTTP: %d %s", status, body)
25 }
26 var code runnerproto.DeviceCode
27 if err := json.Unmarshal(body, &code); err != nil {
28 t.Fatal(err)
29 }
30 view := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(code.UserCode), 200)
31 if !strings.Contains(view, "Add Tito Studio to your team") || !strings.Contains(view, `name="action" value="approve"`) {
32 t.Fatalf("the link did not show the machine to add:\n%s", view)
33 }
34 // Opening it twice still shows it: the GET inspects, never approves.
35 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") {
36 t.Fatal("opening the link changed the enrolment")
37 }
38 pagePost(t, c, f.server.URL, "/runners/enrol", testOrigin, formFields(t, view, "/runners/enrol"), 303)
39 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 {
40 t.Fatalf("the one button did not add it: %d %s", status, body)
41 }
42 res, err := c.Get(f.server.URL + "/runners/enrol?code=ZZZZZZZZZZ")
43 if err != nil {
44 t.Fatal(err)
45 }
46 if wrong := readBody(t, res); res.StatusCode == 200 && strings.Contains(wrong, "to your team") {
47 t.Fatal("a wrong code showed a machine")
48 }
49}
50
51// A Mac can only be a worker: for a person with no primary runner its
52// link says so in words and it cannot be added (it would become their
53// primary). Once they have a primary, the same kind of link adds it, as
54// a worker.
55func TestMacEnrolmentNeedsAPrimary(t *testing.T) {
56 f := newEnrolHTTP(t)
57 c := signinClient(t, f.server.URL)
58 signinEmail(t, c, f.server.URL, testOrigin, f.mail, ownerAddress)
59 begin := func(label string, worker bool) runnerproto.DeviceCode {
60 t.Helper()
61 pub, _ := machine(t)
62 status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: label, WorkerOnly: worker}, "")
63 if status != 201 {
64 t.Fatalf("begin HTTP: %d %s", status, body)
65 }
66 var code runnerproto.DeviceCode
67 if err := json.Unmarshal(body, &code); err != nil {
68 t.Fatal(err)
69 }
70 return code
71 }
72 mac := begin("Tito Studio", true)
73 res, err := c.Get(f.server.URL + "/runners/enrol?code=" + url.QueryEscape(mac.UserCode))
74 if err != nil {
75 t.Fatal(err)
76 }
77 page := readBody(t, res)
78 if strings.Contains(page, `value="approve"`) || !strings.Contains(page, "Add your main runner first") {
79 t.Fatalf("a Mac offered as a first runner (%d):\n%s", res.StatusCode, page)
80 }
81 res = clientPost(t, c, f.server.URL+"/runners/enrol", testOrigin, url.Values{"user_code": {mac.UserCode}, "action": {"inspect"}})
82 if body := readBody(t, res); strings.Contains(body, `value="approve"`) {
83 t.Fatal("inspect offered to add a Mac with no primary")
84 }
85 main := begin("docs-t1", false)
86 view := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(main.UserCode), 200)
87 pagePost(t, c, f.server.URL, "/runners/enrol", testOrigin, formFields(t, view, "/runners/enrol"), 303)
88 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") {
89 t.Fatal("a Mac not offered once there is a primary")
90 }
91}
92
93// Paul's Studio, 2026-11-10: a Mac joins a person who already has a
94// Linux primary. Approved through the page, its exchange gives it a
95// worker's credential; nothing on the way answers with a server error.
96func TestMacEnrolmentApprovedAfterAPrimary(t *testing.T) {
97 f := newEnrolHTTP(t)
98 c := signinClient(t, f.server.URL)
99 signinEmail(t, c, f.server.URL, testOrigin, f.mail, ownerAddress)
100 enrol := func(label string, worker bool) runnerproto.RunnerCredential {
101 t.Helper()
102 pub, key := machine(t)
103 status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: label, WorkerOnly: worker}, "")
104 if status != 201 {
105 t.Fatalf("begin HTTP: %d %s", status, body)
106 }
107 var code runnerproto.DeviceCode
108 if err := json.Unmarshal(body, &code); err != nil {
109 t.Fatal(err)
110 }
111 view := taskGet(t, c, f.server.URL, "/runners/enrol?code="+url.QueryEscape(code.UserCode), 200)
112 pagePost(t, c, f.server.URL, "/runners/enrol", testOrigin, formFields(t, view, "/runners/enrol"), 303)
113 status, body = enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments/token", runnerproto.ExchangeEnrolmentRequest{Version: 1, DeviceSecret: code.DeviceSecret, Proof: deviceProof(code, key)}, "")
114 if status != 200 {
115 t.Fatalf("%s exchange: %d %s", label, status, body)
116 }
117 var cred runnerproto.RunnerCredential
118 if err := json.Unmarshal(body, &cred); err != nil {
119 t.Fatal(err)
120 }
121 return cred
122 }
123 if cred := enrol("docs-t1", false); cred.Role == wire.RoleWorker {
124 t.Fatal("the first runner is not the primary")
125 }
126 if cred := enrol("Tito Studio", true); cred.Role != wire.RoleWorker {
127 t.Fatalf("the Mac enrolled as %q", cred.Role)
128 }
129}
130
131// A machine enrolling cannot say what went wrong on the server, so the
132// server logs it: the route and the error, never the device secret.
133func TestEnrolmentServerErrorIsLogged(t *testing.T) {
134 f := newEnrolHTTP(t)
135 pub, key := machine(t)
136 status, body := enrolJSON(t, f.server.Client(), f.server.URL+"/api/enrolments", runnerproto.BeginEnrolmentRequest{Version: 1, MachinePublicKey: pub, Label: "Tito Studio", WorkerOnly: true}, "")
137 if status != 201 {
138 t.Fatalf("begin HTTP: %d %s", status, body)
139 }
140 var code runnerproto.DeviceCode
141 if err := json.Unmarshal(body, &code); err != nil {
142 t.Fatal(err)
143 }
144 if _, err := f.db.Writer().Exec("ALTER TABLE runner_enrolments RENAME TO unavailable_enrolments"); err != nil {
145 t.Fatal(err)
146 }
147 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") {
148 t.Fatalf("exchange: %d %s", status, body)
149 }
150 logs := f.logs.String()
151 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") {
152 t.Fatalf("not logged: %s", logs)
153 }
154 if strings.Contains(logs, code.DeviceSecret) {
155 t.Fatal("the device secret was logged")
156 }
157}
158