oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "reflect"
9 "sort"
10 "strings"
11 "testing"
12 "time"
13
14 "amadan.net/oficina/home/roster"
15)
16
17// probeGet answers GET /api/team as Alta sees it: no cookie, no session.
18func probeGet(t *testing.T, h http.Handler) *httptest.ResponseRecorder {
19 t.Helper()
20 rec := httptest.NewRecorder()
21 h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, bridgeOrigin+teamProbePath, nil))
22 return rec
23}
24
25// probeBody checks the transport half of every answer (200, JSON, no-store,
26// no cookie) and returns the body's members, so a test can assert the exact
27// key set rather than only the keys it knows about.
28func probeBody(t *testing.T, rec *httptest.ResponseRecorder) map[string]any {
29 t.Helper()
30 if rec.Code != http.StatusOK {
31 t.Fatalf("status %d: %s", rec.Code, rec.Body)
32 }
33 if got := rec.Header().Get("Content-Type"); got != "application/json; charset=utf-8" {
34 t.Fatalf("Content-Type %q", got)
35 }
36 if got := rec.Header().Get("Cache-Control"); got != "no-store" {
37 t.Fatalf("Cache-Control %q, want no-store", got)
38 }
39 if c := rec.Result().Cookies(); len(c) != 0 {
40 t.Fatalf("probe set cookies: %v", c)
41 }
42 var body map[string]any
43 if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
44 t.Fatalf("not JSON: %q", rec.Body)
45 }
46 return body
47}
48
49func probeKeys(m map[string]any) []string {
50 var out []string
51 for k := range m {
52 out = append(out, k)
53 }
54 sort.Strings(out)
55 return out
56}
57
58func TestTeamProbeOffWithoutTeamMode(t *testing.T) {
59 standalone := newSampleDocumentsInternalTestApp(t, "0")
60 home := newRouterHomeFixture(t)
61 for name, h := range map[string]http.Handler{"standalone": standalone.Handler(), "home without team": home.app.Handler()} {
62 t.Run(name, func(t *testing.T) {
63 rec := probeGet(t, h)
64 probeBody(t, rec)
65 if got := rec.Body.String(); got != `{"team_mode":false}` {
66 t.Fatalf("body %s", got)
67 }
68 })
69 }
70}
71
72func TestTeamProbeShapeAndRosterStates(t *testing.T) {
73 f := newTeamRouterFixture(t)
74 h := f.app.Handler()
75 want := func(roster string) {
76 t.Helper()
77 body := probeBody(t, probeGet(t, h))
78 if k := probeKeys(body); !reflect.DeepEqual(k, []string{"consumer", "roster", "team_mode"}) {
79 t.Fatalf("keys %v", k)
80 }
81 if body["team_mode"] != true || body["consumer"] != "router-docs" || body["roster"] != roster {
82 t.Fatalf("body %v, want roster %q", body, roster)
83 }
84 }
85 members := f.directory.members
86
87 // Booted, and the directory has never answered: nothing held since boot.
88 f.directory.change(members, true)
89 want("unknown")
90 f.clock.step(roster.RetryAfter)
91 want("unknown")
92
93 // The probe's own poll pulls the first roster, as Alta's does during
94 // Home's verify window.
95 f.directory.change(members, false)
96 f.clock.step(roster.RetryAfter)
97 want("ok")
98 f.clock.step(roster.MaximumAge - time.Second)
99 f.directory.change(members, true)
100 want("ok") // inside MaximumAge a failed refresh does not unseat it
101
102 // Aged out with the directory still down: held once, not now.
103 f.clock.step(2 * time.Second)
104 want("stale")
105 f.clock.step(roster.RetryAfter)
106 want("stale")
107
108 // And back.
109 f.directory.change(members, false)
110 f.clock.step(roster.RetryAfter)
111 want("ok")
112}
113
114func TestTeamProbeCarriesNoAccountData(t *testing.T) {
115 f := newTeamRouterFixture(t)
116 anonymous := probeGet(t, f.app.Handler())
117 probeBody(t, anonymous)
118
119 // A signed-in member asking gets the same bytes: nothing differs with
120 // who is asking.
121 b := f.browser()
122 nonce := b.start(t, "/auth/start?return_to=%2F", "")
123 b.chooseNew(t, b.exchange(t, nonce, teamSubject('b'), time.Now()))
124 member := b.do("GET", teamProbePath, "")
125 probeBody(t, member)
126 if !bytes.Equal(member.Body.Bytes(), anonymous.Body.Bytes()) {
127 t.Fatalf("member saw %s, anonymous %s", member.Body, anonymous.Body)
128 }
129
130 body := anonymous.Body.String()
131 for _, secret := range []string{
132 "pr_", teamSubject('a'), teamSubject('b'), teamSubject('c'), "pr_" + strings.Repeat("2", 26),
133 "alice@example.com", "bob@example.com", "@",
134 bridgeIssuer, "TeamA", "https://home.example", bridgeOrigin,
135 "member", "owner", "epoch", "generation", "count",
136 } {
137 if strings.Contains(body, secret) {
138 t.Fatalf("probe leaked %q: %s", secret, body)
139 }
140 }
141}
142
143func TestTeamProbeAnswersOnlyGet(t *testing.T) {
144 f := newTeamRouterFixture(t)
145 b := f.browser()
146 for _, method := range []string{http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete} {
147 t.Run(method, func(t *testing.T) {
148 // Same-origin, so the CSRF guard passes it and the router decides.
149 r := b.request(method, teamProbePath, "")
150 r.Header.Set("Origin", bridgeOrigin)
151 r.Header.Set("Sec-Fetch-Site", "same-origin")
152 rec := b.serve(r)
153 if rec.Code != http.StatusMethodNotAllowed || rec.Header().Get("Allow") != "GET" {
154 t.Fatalf("status %d Allow %q", rec.Code, rec.Header().Get("Allow"))
155 }
156 if strings.Contains(rec.Body.String(), "team_mode") {
157 t.Fatalf("answered the probe: %s", rec.Body)
158 }
159 })
160 }
161}
162