oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "encoding/json"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/home/roster"
12)
13
14func probe(t *testing.T, app *App) (*httptest.ResponseRecorder, map[string]any) {
15 t.Helper()
16 rec := do(t, app, httptest.NewRequest(http.MethodGet, testOrigin+teamProbePath, nil))
17 var body map[string]any
18 if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &body) != nil {
19 t.Fatalf("probe = %d %q", rec.Code, rec.Body.String())
20 }
21 if rec.Header().Get("Cache-Control") != "no-store" || !strings.HasPrefix(rec.Header().Get("Content-Type"), "application/json") {
22 t.Errorf("probe headers: %v", rec.Header())
23 }
24 return rec, body
25}
26
27// With team mode off the probe says so and nothing else.
28func TestTeamProbeOffSaysOnlyThat(t *testing.T) {
29 app, _ := appWithOrigin(t, testOrigin)
30 if rec, _ := probe(t, app); rec.Body.String() != `{"team_mode":false}` {
31 t.Fatalf("team-off probe = %q", rec.Body.String())
32 }
33}
34
35// unknown before any roster, ok once one is held, stale when it ages out
36// with the directory down — and never a word of account data, whoever
37// asks, signed in or not.
38func TestTeamProbeStatesAndNoAccountData(t *testing.T) {
39 f := newTeamFixture(t)
40 a := teamSubject('a')
41 f.dir.Set(a, "owner")
42 f.dir.SetDown(true)
43 if _, body := probe(t, f.app); body["roster"] != "unknown" || body["team_mode"] != true || body["consumer"] != "sheets-preview" {
44 t.Fatalf("probe before any roster = %v", body)
45 }
46 f.dir.SetDown(false)
47 f.clock.Step(roster.RetryAfter + time.Second)
48 _, body := probe(t, f.app)
49 if body["roster"] != "ok" {
50 t.Fatalf("probe with a roster = %v", body)
51 }
52 f.signInHome(t, a)
53 signedIn := httptest.NewRequest(http.MethodGet, testOrigin+teamProbePath, nil)
54 for _, c := range f.jar {
55 signedIn.AddCookie(c)
56 }
57 rec := do(t, f.app, signedIn)
58 if len(body) != 3 {
59 t.Errorf("probe fields = %v, want exactly team_mode, consumer, roster", body)
60 }
61 for _, leak := range []string{a, teamTestTeam, homeTestIssuer, teamTestDirectory, "owner", "member", "@"} {
62 if strings.Contains(rec.Body.String(), leak) {
63 t.Errorf("probe for a signed-in owner carries %q: %s", leak, rec.Body.String())
64 }
65 }
66 anonymous, _ := probe(t, f.app)
67 if rec.Body.String() != anonymous.Body.String() {
68 t.Errorf("probe differs by who asks: %q vs %q", rec.Body.String(), anonymous.Body.String())
69 }
70 f.expired(t)
71 if _, body := probe(t, f.app); body["roster"] != "stale" {
72 t.Fatalf("probe past the lease = %v", body)
73 }
74}
75