| 1 | package teammode_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/home/roster" |
| 11 | "amadan.net/oficina/home/teammode" |
| 12 | ) |
| 13 | |
| 14 | func probe(t *testing.T, rt *teammode.Runtime, cookies ...*http.Cookie) string { |
| 15 | t.Helper() |
| 16 | r := httptest.NewRequest(http.MethodGet, testOrigin+teammode.ProbePath, nil) |
| 17 | for _, c := range cookies { |
| 18 | r.AddCookie(c) |
| 19 | } |
| 20 | w := httptest.NewRecorder() |
| 21 | teammode.Probe(rt).ServeHTTP(w, r) |
| 22 | if w.Code != 200 || w.Header().Get("Cache-Control") != "no-store" || w.Header().Get("Content-Type") != "application/json; charset=utf-8" || w.Header().Get("X-Content-Type-Options") != "nosniff" { |
| 23 | t.Fatalf("probe %d %v", w.Code, w.Header()) |
| 24 | } |
| 25 | return w.Body.String() |
| 26 | } |
| 27 | |
| 28 | func TestProbeTeamModeOff(t *testing.T) { |
| 29 | if body := probe(t, nil); body != `{"team_mode":false}` { |
| 30 | t.Fatalf("team off body %s", body) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // unknown before any roster, ok while one decides, stale once it has aged |
| 35 | // out — and nothing about anybody, whoever asks. |
| 36 | func TestProbeRosterStates(t *testing.T) { |
| 37 | f := provisioned(t) |
| 38 | f.dir.SetDown(true) |
| 39 | f.boot() |
| 40 | f.dir.Set(alice, "owner") |
| 41 | unknown := `{"team_mode":true,"consumer":"` + testConsumer + `","roster":"unknown"}` |
| 42 | if body := probe(t, f.rt); body != unknown { |
| 43 | t.Fatalf("before any roster %s", body) |
| 44 | } |
| 45 | f.dir.SetDown(false) |
| 46 | f.clock.Step(roster.RetryAfter) |
| 47 | ok := strings.Replace(unknown, "unknown", "ok", 1) |
| 48 | signedIn := &http.Cookie{Name: "__Host-session", Value: "x"} |
| 49 | if body := probe(t, f.rt); body != ok { |
| 50 | t.Fatalf("with a roster %s", body) |
| 51 | } |
| 52 | if body := probe(t, f.rt, signedIn); body != ok { |
| 53 | t.Fatal("the answer differs with who asks") |
| 54 | } |
| 55 | for _, secret := range []string{alice, "owner", f.bindings.Team, f.bindings.Issuer, f.bindings.Directory} { |
| 56 | if strings.Contains(ok, secret) { |
| 57 | t.Fatalf("probe leaks %q", secret) |
| 58 | } |
| 59 | } |
| 60 | f.expired() |
| 61 | if body := probe(t, f.rt); body != strings.Replace(unknown, "unknown", "stale", 1) { |
| 62 | t.Fatalf("aged out %s", body) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // rosterHeld is set by the one Check every path shares: a request's |
| 67 | // decision alone makes a later outage "stale", though the probe never saw |
| 68 | // "ok". |
| 69 | func TestProbeStaleAfterARequestHeldTheRoster(t *testing.T) { |
| 70 | f := newFixture(t) |
| 71 | f.dir.Set(alice, "member") |
| 72 | if f.rt.Access(context.Background(), alice).State() != roster.Member { |
| 73 | t.Fatal("no roster") |
| 74 | } |
| 75 | f.expired() |
| 76 | if body := probe(t, f.rt); !strings.Contains(body, `"roster":"stale"`) { |
| 77 | t.Fatalf("held roster not recorded: %s", body) |
| 78 | } |
| 79 | } |
| 80 | |