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 "amadan.net/oficina/home/roster"
5 "bufio"
6 "context"
7 "io"
8 "net/http"
9 "net/http/httptest"
10 "net/url"
11 "strings"
12 "testing"
13 "time"
14)
15
16func teamOpenActualStream(t *testing.T, f *teamRouterFixture, b *routerBrowser, srv *httptest.Server) *http.Response {
17 t.Helper()
18 r, e := http.NewRequestWithContext(t.Context(), "GET", srv.URL+"/d/"+f.own.Sqid+"/stream", nil)
19 if e != nil {
20 t.Fatal(e)
21 }
22 r.Host = strings.TrimPrefix(bridgeOrigin, "https://")
23 for _, c := range b.cookies {
24 r.AddCookie(c)
25 }
26 res, e := srv.Client().Do(r)
27 if e != nil {
28 t.Fatal(e)
29 }
30 t.Cleanup(func() { res.Body.Close() })
31 if res.StatusCode != 200 {
32 raw, _ := io.ReadAll(res.Body)
33 t.Fatal("stream refused", res.StatusCode, res.Request.URL.String(), string(raw))
34 }
35 reader := bufio.NewReader(res.Body)
36 name, _ := nextEvent(t, reader, time.Second)
37 if name != "update" {
38 t.Fatal("initial sequence event missing")
39 }
40 // Keep the same buffered reader: it may already contain the next frame.
41 res.Body = struct {
42 io.Reader
43 io.Closer
44 }{reader, res.Body}
45 return res
46}
47func TestTeamStreamActualSessionWriterWaitCannotExtendOriginalLease(t *testing.T) {
48 f := newTeamRouterFixture(t)
49 b := f.browser()
50 b.localSignin(t, "alice@example.com")
51 b.linkProof(t, teamSubject('a'), "alice@example.com")
52 routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/"))
53 if r := b.do("GET", "/d/"+f.own.Sqid, ""); r.Code != 200 {
54 t.Fatal("initial membership unavailable", r.Code)
55 }
56 streamDurations(t, f.app, 40*time.Millisecond, 5*time.Second)
57 f.directory.change(nil, true)
58 f.clock.step(59 * time.Second)
59 if e := f.app.store.team.cache.Refresh(t.Context()); e == nil {
60 t.Fatal("failed directory refresh accepted")
61 }
62 srv := httptest.NewServer(f.app.Handler())
63 defer srv.Close()
64 res := teamOpenActualStream(t, f, b, srv)
65 defer res.Body.Close()
66 writer := f.app.db.Writer()
67 tx, e := writer.BeginTx(t.Context(), nil)
68 if e != nil {
69 t.Fatal(e)
70 }
71 defer tx.Rollback()
72 baseline := writer.Stats().WaitCount
73 done := make(chan struct{})
74 go func() { io.Copy(io.Discard, res.Body); close(done) }()
75 deadline := time.Now().Add(300 * time.Millisecond)
76 for writer.Stats().WaitCount == baseline && time.Now().Before(deadline) {
77 time.Sleep(time.Millisecond)
78 }
79 if writer.Stats().WaitCount == baseline {
80 t.Fatal("actual session reader never queued behind writer")
81 }
82 select {
83 case <-done:
84 case <-time.After(1300 * time.Millisecond):
85 t.Fatal("writer wait held stream beyond its original lease")
86 }
87 // The stream ended while the writer remains locked; cancellation did not need
88 // the transaction to finish and did not mint a new session.
89 if e = tx.Rollback(); e != nil {
90 t.Fatal(e)
91 }
92 if _, ok := f.sessions.From(b.request("GET", "/", "")); !ok {
93 t.Fatal("stream cancellation revoked legacy session")
94 }
95}
96
97// A publication that still lists the member with the same role keeps the
98// stream's lease (Home #97: before it, every refresh ended every lease, and
99// this test asserted that). One that changes their role ends it at once, and
100// the reconnect is decided afresh. The lease's deadline never moves, so the
101// 60 s contract is untouched (TestTeamStreamActualSessionWriterWait...).
102func TestTeamStreamPublicationKeepsAListedLeaseAndClosesAChangedOne(t *testing.T) {
103 f := newTeamRouterFixture(t)
104 b := f.browser()
105 b.localSignin(t, "alice@example.com")
106 b.linkProof(t, teamSubject('a'), "alice@example.com")
107 routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/"))
108 streamDurations(t, f.app, 30*time.Millisecond, 5*time.Second)
109 srv := httptest.NewServer(f.app.Handler())
110 defer srv.Close()
111 res := teamOpenActualStream(t, f, b, srv)
112 defer res.Body.Close()
113 done := make(chan struct{})
114 go func() { io.Copy(io.Discard, res.Body); close(done) }()
115 f.clock.step(roster.RetryAfter)
116 if e := f.app.store.team.cache.Refresh(context.Background()); e != nil {
117 t.Fatal(e)
118 }
119 select {
120 case <-done:
121 t.Fatal("a publication that still lists the member closed the stream")
122 case <-time.After(300 * time.Millisecond):
123 }
124 f.directory.change([]roster.MemberEntry{{Subject: teamSubject('a'), Role: "owner"}, {Subject: teamSubject('b'), Role: "member"}, {Subject: teamSubject('c'), Role: "member"}}, false)
125 f.clock.step(roster.RetryAfter)
126 if e := f.app.store.team.cache.Refresh(context.Background()); e != nil {
127 t.Fatal(e)
128 }
129 select {
130 case <-done:
131 case <-time.After(time.Second):
132 t.Fatal("old stream renewed instead of closing after a role change")
133 }
134 fresh := teamOpenActualStream(t, f, b, srv)
135 fresh.Body.Close()
136}
137
138func TestTeamStreamGuestGrantAndActualSessionRevocation(t *testing.T) {
139 for _, reason := range []string{"grant", "session"} {
140 t.Run(reason, func(t *testing.T) {
141 f := newTeamRouterFixture(t)
142 guest := f.browser()
143 guest.localSignin(t, "alice@example.com")
144 owner := f.browser()
145 owner.localSignin(t, "bob@example.com")
146 owner.linkProof(t, teamSubject('b'), "bob@example.com")
147 routerRedirect(t, owner.do("POST", "/account/home/confirm", ""), connectedURL("/"))
148 streamDurations(t, f.app, 30*time.Millisecond, 5*time.Second)
149 srv := httptest.NewServer(f.app.Handler())
150 defer srv.Close()
151 shared := *f
152 shared.own = f.shared
153 res := teamOpenActualStream(t, &shared, guest, srv)
154 defer res.Body.Close()
155 if reason == "grant" {
156 routerRedirect(t, owner.do("POST", "/d/"+f.shared.Sqid+"/share/revoke", url.Values{"address": {"alice@example.com"}}.Encode()), "/d/"+f.shared.Sqid+"/share")
157 } else {
158 signoutChained(t, guest.do("POST", "/signout", ""))
159 }
160 done := make(chan struct{})
161 go func() { io.Copy(io.Discard, res.Body); close(done) }()
162 select {
163 case <-done:
164 case <-time.After(time.Second):
165 t.Fatal("live stream survived actual " + reason + " revocation")
166 }
167 })
168 }
169}
170