| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "log/slog" |
| 7 | |
| 8 | "amadan.net/oficina/home/connect" |
| 9 | "database/sql" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "net/url" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/home/handoff" |
| 18 | "amadan.net/oficina/home/roster" |
| 19 | "amadan.net/rastrillo/rastrillo/assertion" |
| 20 | "amadan.net/rastrillo/rastrillo/sessions" |
| 21 | ) |
| 22 | |
| 23 | // Ownership is the team rung's: a removed owner whom somebody has granted |
| 24 | // their own deck reads it, and owns nothing. |
| 25 | func TestTeamRemovedOwnerOwnsNothing(t *testing.T) { |
| 26 | f := newTeamFixture(t) |
| 27 | colleague := f.member(teamSubject('b'), "member") |
| 28 | u := f.member(teamSubject('a'), "member") |
| 29 | own := f.deck(u, "My own", rungTeam) |
| 30 | f.grant(own, u, colleague) |
| 31 | f.dir.Set(teamSubject('a'), "") |
| 32 | f.refreshed() |
| 33 | page := f.get(viewerPath(own)) |
| 34 | if page.Code != 200 || strings.Contains(page.Body.String(), deckPath(own)+"/share") { |
| 35 | t.Fatalf("removed owner's granted deck = %d (owner mode?)", page.Code) |
| 36 | } |
| 37 | for _, p := range []string{"/settings", "/share", "/delete"} { |
| 38 | if got := f.get(deckPath(own) + p); got.Code != 404 { |
| 39 | t.Fatalf("removed owner's %s = %d, want 404", p, got.Code) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Under Unknown, an owner's screen on a deck reached by grant cannot be |
| 45 | // decided either way (the viewer might be a roster owner of an orphan): |
| 46 | // the retryable 503, not a 404. |
| 47 | func TestTeamOwnerScreensUnderUnknownAreRetryable(t *testing.T) { |
| 48 | f := newTeamFixture(t) |
| 49 | colleague := f.member(teamSubject('b'), "member") |
| 50 | shared := f.deck(colleague, "Shared", rungPrivate) |
| 51 | u := f.member(teamSubject('a'), "member") |
| 52 | f.grant(shared, u, colleague) |
| 53 | f.expired() |
| 54 | if got := f.get(deckPath(shared) + "/settings"); got.Code != http.StatusServiceUnavailable { |
| 55 | t.Fatalf("settings on a granted deck under Unknown = %d, want 503", got.Code) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // An owner whose standing cannot be read is not gone: the roster owner |
| 60 | // does not take their deck. |
| 61 | func TestTeamOwnerOfUnknownStandingIsNotAnOrphan(t *testing.T) { |
| 62 | f := newTeamFixture(t) |
| 63 | owner := f.member(teamSubject('l'), "member") |
| 64 | kept := f.deck(owner, "Live owner's deck", rungTeam) |
| 65 | f.member(teamSubject('o'), "owner") |
| 66 | broken, err := sql.Open("sqlite", "file:closed3?mode=memory") |
| 67 | if err == nil { |
| 68 | broken.Close() |
| 69 | } |
| 70 | real := f.a.team.db |
| 71 | f.a.team.db = broken |
| 72 | got := f.get(deckPath(kept) + "/settings") |
| 73 | f.a.team.db = real |
| 74 | if got.Code != 404 { |
| 75 | t.Fatalf("roster owner on a deck whose owner's standing cannot be read = %d, want 404", got.Code) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // A reach through the team rung never stands without a lease: whenever |
| 80 | // the lease lapses — before the reach query, between it and the standing |
| 81 | // check, or after — a stream or a talk that rested on it either ends or |
| 82 | // re-decides on a timer, never "reaches, nothing to renew". The roster |
| 83 | // clock ticks on every reading, and the lapse is swept across each |
| 84 | // reading in turn, so one of them lands between the query and the check. |
| 85 | func TestTeamTeamRungReachNeverStandsWithoutALease(t *testing.T) { |
| 86 | for _, what := range []string{"deck stream", "talk"} { |
| 87 | for k := 1; k <= 12; k++ { |
| 88 | f := newTeamFixture(t) |
| 89 | colleague := f.member(teamSubject('b'), "member") |
| 90 | plan := f.deck(colleague, "Team plan", rungTeam) |
| 91 | u := f.member(teamSubject('a'), "member") |
| 92 | r := httptest.NewRequest("GET", homeTestOrigin+"/", nil) |
| 93 | for _, c := range f.jar { |
| 94 | r.AddCookie(c) |
| 95 | } |
| 96 | s, _ := f.a.homeSessions.From(r) |
| 97 | r = sessions.WithSession(r, s) |
| 98 | |
| 99 | f.dir.SetDown(true) |
| 100 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond) |
| 101 | f.clock.SetTick(time.Millisecond) |
| 102 | var stands bool |
| 103 | if what == "deck stream" { |
| 104 | decision, renew := f.a.teamStreamAccess(r, plan.ID) |
| 105 | stands = decision == streamDelivers && renew == 0 |
| 106 | } else { |
| 107 | reach, renew := f.a.presenterStanding(context.Background(), Presentation{Actor: actorFor(u), DeckID: plan.ID}) |
| 108 | stands = reach == presenterReaches && renew == 0 |
| 109 | } |
| 110 | if stands { |
| 111 | t.Fatalf("k=%d: a %s on the team rung stands with nothing to renew", k, what) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // A share-link follower follows only a talk whose presenter still reaches |
| 118 | // the deck: a removed presenter's talk is not followed, and is ended. |
| 119 | func TestTeamFollowersDoNotFollowARemovedPresenter(t *testing.T) { |
| 120 | f := newTeamFixture(t) |
| 121 | colleague := f.member(teamSubject('b'), "member") |
| 122 | plan := f.deck(colleague, "Team plan", rungTeam) |
| 123 | f.member(teamSubject('a'), "member") |
| 124 | session := f.startTalk(t, plan) |
| 125 | r := httptest.NewRequest("GET", homeTestOrigin+"/", nil) |
| 126 | if _, ok := f.a.liveSessionFor(r, plan.ID); !ok { |
| 127 | t.Fatal("the talk is not followable") |
| 128 | } |
| 129 | f.dir.Set(teamSubject('a'), "") |
| 130 | f.refreshed() |
| 131 | if _, ok := f.a.liveSessionFor(r, plan.ID); ok { |
| 132 | t.Fatal("a removed presenter's talk is followable") |
| 133 | } |
| 134 | var p Presentation |
| 135 | f.d.G.Where("sqid = ?", session).Take(&p) |
| 136 | if p.EndedAt == nil { |
| 137 | t.Fatal("the removed presenter's talk was not ended") |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Off the list is not off the team: in team mode a local or linked |
| 142 | // account the allowlist no longer names keeps its signed-in chrome, its |
| 143 | // presenter identity and its link flow; the roster decides what it |
| 144 | // reaches. And a team instance in development never admits everybody. |
| 145 | func TestTeamTheAllowlistDecidesNothingButLocalSignIn(t *testing.T) { |
| 146 | f := newTeamFixture(t, "other@example.com") |
| 147 | if f.a.allowed.allows("anyone@example.com") { |
| 148 | t.Fatal("a team allowlist admits an address it does not name") |
| 149 | } |
| 150 | g := newTeamFixture(t) |
| 151 | if g.a.allowed.allows("anyone@example.com") { |
| 152 | t.Fatal("an empty team allowlist in development admits everybody") |
| 153 | } |
| 154 | |
| 155 | // A local session the list no longer names: signed-in chrome. |
| 156 | f.localUser("x@example.com") |
| 157 | f.localSession("x@example.com") |
| 158 | if got := f.get("/nope"); !strings.Contains(got.Body.String(), `action="/signout"`) { |
| 159 | t.Fatal("an unlisted local session's error page is signed out") |
| 160 | } |
| 161 | // It links its Home identity. |
| 162 | f.linkHome(t, teamSubject('x')) |
| 163 | if p, err := f.a.home.Resolve(context.Background(), teamSubject('x')); err != nil || p.ID != f.userFor(teamSubject('x')).ID { |
| 164 | t.Fatalf("link from an unlisted local session: %v", err) |
| 165 | } |
| 166 | // Signed in with Home, the linked account is the presenter of its talk |
| 167 | // and has signed-in chrome on pages outside the session gate. |
| 168 | f.dir.Set(teamSubject('x'), "member") |
| 169 | f.refreshed() |
| 170 | f.freshHomeSignIn(teamSubject('x')) |
| 171 | colleague := f.member(teamSubject('b'), "member") |
| 172 | plan := f.deck(colleague, "Team plan", rungTeam) |
| 173 | f.freshHomeSignIn(teamSubject('x')) |
| 174 | session := f.startTalk(t, plan) |
| 175 | r := httptest.NewRequest("GET", homeTestOrigin+"/", nil) |
| 176 | for _, c := range f.jar { |
| 177 | r.AddCookie(c) |
| 178 | } |
| 179 | if got, _ := f.a.admittedPresenter(httptest.NewRecorder(), r); got == "" { |
| 180 | t.Fatal("an unlisted linked account is nobody to presentation.go") |
| 181 | } |
| 182 | if got := f.get("/p/" + session + "/notes"); got.Code != 200 { |
| 183 | t.Fatalf("the presenter's notes by session = %d", got.Code) |
| 184 | } |
| 185 | if got := f.get("/nope"); !strings.Contains(got.Body.String(), `action="/signout"`) { |
| 186 | t.Fatal("an unlisted linked account's error page is signed out") |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // linkHome runs connect's explicit link flow for the jar's local session |
| 191 | // and subject, as TestHomeRealSignedExchangeExplicitLinkAndReplay does. |
| 192 | func (f *teamFixture) linkHome(t *testing.T, subject string) { |
| 193 | t.Helper() |
| 194 | start := f.post("/account/home", nil) |
| 195 | if start.Code != http.StatusSeeOther { |
| 196 | t.Fatalf("link start = %d %s", start.Code, start.Body.String()) |
| 197 | } |
| 198 | auth := f.get(start.Header().Get("Location")) |
| 199 | target, err := url.Parse(auth.Header().Get("Location")) |
| 200 | if err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | id, _ := assertion.RandomID() |
| 204 | now := time.Now().Unix() |
| 205 | token, err := assertion.Sign(f.key, assertion.Claims{Version: 1, Issuer: teamTestIssuer, Audience: homeTestOrigin, Subject: subject, KeyID: "test", ID: id, Nonce: target.Query().Get("nonce"), IssuedAt: now, ExpiresAt: now + 90, AuthTime: now}) |
| 206 | if err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | if cb := f.do("POST", handoff.CallbackPath, url.Values{"assertion": {token}}.Encode(), "application/x-www-form-urlencoded", teamTestIssuer); cb.Code != http.StatusSeeOther { |
| 210 | t.Fatalf("link callback = %d", cb.Code) |
| 211 | } |
| 212 | if fin := f.get(handoff.FinishPath); fin.Code != http.StatusSeeOther { |
| 213 | t.Fatalf("link finish = %d %s", fin.Code, fin.Body.String()) |
| 214 | } |
| 215 | if c := f.post("/account/home/confirm", nil); c.Code != http.StatusSeeOther { |
| 216 | t.Fatalf("link confirm = %d %s", c.Code, c.Body.String()) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // A first local sign-in on a team instance seeds no sample deck — the |
| 221 | // account is a guest until it links — and so logs no failed seed |
| 222 | // (Fable #33, note 3). |
| 223 | func TestTeamFirstLocalSignInSeedsNothingAndLogsNothing(t *testing.T) { |
| 224 | var logged bytes.Buffer |
| 225 | key := teamKey(t) |
| 226 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key, "new@example.com") |
| 227 | cfg.Logger = slog.New(slog.NewTextHandler(&logged, nil)) |
| 228 | d := testDB(t) |
| 229 | mux, a, err := New(d, cfg) |
| 230 | if err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | t.Cleanup(func() { a.Close() }) |
| 234 | f := &teamFixture{t: t, mux: mux, a: a, d: d, key: key, jar: map[string]*http.Cookie{}} |
| 235 | f.localSession("new@example.com") |
| 236 | if got := f.get(connect.LocalCompletePath); got.Code >= 500 { |
| 237 | t.Fatalf("local complete = %d", got.Code) |
| 238 | } |
| 239 | var users, decks int64 |
| 240 | d.G.Model(&User{}).Count(&users) |
| 241 | d.G.Model(&Deck{}).Count(&decks) |
| 242 | if users != 1 || decks != 0 { |
| 243 | t.Fatalf("first local sign-in made users=%d decks=%d, want 1 and 0", users, decks) |
| 244 | } |
| 245 | if strings.Contains(logged.String(), "level=ERROR") { |
| 246 | t.Fatalf("a first local sign-in logged an error:\n%s", logged.String()) |
| 247 | } |
| 248 | } |
| 249 | |