| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/home/dashboard" |
| 5 | "github.com/go-json-experiment/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestDashboardUsesExistingAccountAndDocumentGrants(t *testing.T) { |
| 14 | t.Setenv("OFICINA_DASHBOARD_TEAM", "blue") |
| 15 | f := newRouterHomeFixtureDirectory(t, "https://oficina.example.test") |
| 16 | s := f.app.store |
| 17 | ctx := t.Context() |
| 18 | alice, e := s.EnsurePerson(ctx, "alice@example.com") |
| 19 | if e != nil { |
| 20 | t.Fatal(e) |
| 21 | } |
| 22 | bob, e := s.EnsurePerson(ctx, "bob@example.com") |
| 23 | if e != nil { |
| 24 | t.Fatal(e) |
| 25 | } |
| 26 | own, e := s.createDocument(ctx, alice, "Own", "", VisibilityPrivate) |
| 27 | if e != nil { |
| 28 | t.Fatal(e) |
| 29 | } |
| 30 | shared, e := s.createDocument(ctx, bob, "Shared", "", VisibilityPrivate) |
| 31 | if e != nil { |
| 32 | t.Fatal(e) |
| 33 | } |
| 34 | if _, e = s.createDocument(ctx, bob, "Private", "", VisibilityPrivate); e != nil { |
| 35 | t.Fatal(e) |
| 36 | } |
| 37 | if e = s.Grant(ctx, bob, shared.Sqid, "alice@example.com", RoleWriter); e != nil { |
| 38 | t.Fatal(e) |
| 39 | } |
| 40 | subject := "pr_abcdefghijklmnopqrstuvwxyz" |
| 41 | b := f.browser() |
| 42 | b.localSignin(t, "alice@example.com") |
| 43 | start := routerRedirect(t, b.do("POST", "/account/home", ""), "") |
| 44 | nonce := b.start(t, start, "300") |
| 45 | routerRedirect(t, b.exchange(t, nonce, subject, time.Now().Add(-time.Minute)), "/account/home/confirm") |
| 46 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), "") |
| 47 | call := func(sub string) *httptest.ResponseRecorder { |
| 48 | jti, _ := dashboard.ID() |
| 49 | nonce, _ := dashboard.ID() |
| 50 | now := time.Now().Unix() |
| 51 | token, e := dashboard.Sign(f.key, dashboard.Claims{Version: 1, Issuer: bridgeIssuer, Audience: bridgeOrigin, Directory: "https://oficina.example.test", Subject: sub, Team: "blue", Purpose: "dashboard-read", KeyID: f.keyID, ID: jti, Nonce: nonce, Issued: now, Expires: now + 120}) |
| 52 | if e != nil { |
| 53 | t.Fatal(e) |
| 54 | } |
| 55 | r := httptest.NewRequest("GET", bridgeOrigin+dashboard.Path, nil) |
| 56 | r.Header.Set("Authorization", "Bearer "+token) |
| 57 | w := httptest.NewRecorder() |
| 58 | f.app.Handler().ServeHTTP(w, r) |
| 59 | return w |
| 60 | } |
| 61 | w := call(subject) |
| 62 | var data dashboard.Response |
| 63 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &data) != nil || len(data.Items) != 2 { |
| 64 | t.Fatal("feed", w.Code, w.Body.String()) |
| 65 | } |
| 66 | for _, item := range data.Items { |
| 67 | if item.ID != own.Sqid && item.ID != shared.Sqid { |
| 68 | t.Fatal("private document exposed") |
| 69 | } |
| 70 | } |
| 71 | if w := call("pr_bbbbbbbbbbbbbbbbbbbbbbbbbb"); w.Code != 403 { |
| 72 | t.Fatal("unknown link", w.Code) |
| 73 | } |
| 74 | if routerRows(t, f, "people") != 2 { |
| 75 | t.Fatal("dashboard created account") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Expire the exact membership lease between response preparation and emission. |
| 80 | type dashboardDeadlineRecorder struct { |
| 81 | *teamDeadlineRecorder |
| 82 | clock *teamTestClock |
| 83 | } |
| 84 | |
| 85 | func (w *dashboardDeadlineRecorder) SetWriteDeadline(time.Time) error { |
| 86 | if w.clock != nil { |
| 87 | w.clock.step(60 * time.Second) |
| 88 | w.clock = nil |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | func TestDashboardTeamResponseUsesEmissionGuard(t *testing.T) { |
| 93 | f := newTeamRouterFixtureWithDashboard(t, true) |
| 94 | b := f.browser() |
| 95 | b.localSignin(t, "alice@example.com") |
| 96 | b.linkProof(t, teamSubject('a'), "alice@example.com") |
| 97 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 98 | request := func() *http.Request { |
| 99 | jti, _ := dashboard.ID() |
| 100 | nonce, _ := dashboard.ID() |
| 101 | now := time.Now().Unix() |
| 102 | token, e := dashboard.Sign(f.key, dashboard.Claims{Version: 1, Issuer: bridgeIssuer, Audience: bridgeOrigin, Directory: f.cfg.Directory, Subject: teamSubject('a'), Team: "TeamA", Purpose: "dashboard-read", KeyID: f.keyID, ID: jti, Nonce: nonce, Issued: now, Expires: now + 120}) |
| 103 | if e != nil { |
| 104 | t.Fatal(e) |
| 105 | } |
| 106 | r := httptest.NewRequest("GET", bridgeOrigin+dashboard.Path, nil) |
| 107 | r.Header.Set("Authorization", "Bearer "+token) |
| 108 | return r |
| 109 | } |
| 110 | good := &teamDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()} |
| 111 | f.app.Handler().ServeHTTP(good, request()) |
| 112 | if good.Code != 200 || !strings.Contains(good.Body.String(), "Alice original") || strings.Contains(good.Body.String(), "Bob private") { |
| 113 | t.Fatal("team scope", good.Code, good.Body.String()) |
| 114 | } |
| 115 | late := &dashboardDeadlineRecorder{teamDeadlineRecorder: &teamDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()}, clock: f.clock} |
| 116 | aborted := false |
| 117 | func() { |
| 118 | defer func() { |
| 119 | if v := recover(); v != nil { |
| 120 | if v != http.ErrAbortHandler { |
| 121 | panic(v) |
| 122 | } |
| 123 | aborted = true |
| 124 | } |
| 125 | }() |
| 126 | f.app.Handler().ServeHTTP(late, request()) |
| 127 | }() |
| 128 | if !aborted || late.Body.Len() != 0 { |
| 129 | t.Fatal("expired lease emitted data", late.Code, late.Body.String()) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Under Unknown the feed is unavailable, not short: Home renders it where |
| 134 | // no partial notice can be shown, and a reader would take the shares alone |
| 135 | // for the whole list (Paul's ruling on list pages, 2026-10-28, condition 3). |
| 136 | func TestDashboardTeamFeedIsUnavailableUnderUnknown(t *testing.T) { |
| 137 | f := newTeamRouterFixtureWithDashboard(t, true) |
| 138 | b := f.browser() |
| 139 | b.localSignin(t, "alice@example.com") |
| 140 | b.linkProof(t, teamSubject('a'), "alice@example.com") |
| 141 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 142 | call := func() *httptest.ResponseRecorder { |
| 143 | jti, _ := dashboard.ID() |
| 144 | nonce, _ := dashboard.ID() |
| 145 | now := time.Now().Unix() |
| 146 | token, e := dashboard.Sign(f.key, dashboard.Claims{Version: 1, Issuer: bridgeIssuer, Audience: bridgeOrigin, Directory: f.cfg.Directory, Subject: teamSubject('a'), Team: "TeamA", Purpose: "dashboard-read", KeyID: f.keyID, ID: jti, Nonce: nonce, Issued: now, Expires: now + 120}) |
| 147 | if e != nil { |
| 148 | t.Fatal(e) |
| 149 | } |
| 150 | r := httptest.NewRequest("GET", bridgeOrigin+dashboard.Path, nil) |
| 151 | r.Header.Set("Authorization", "Bearer "+token) |
| 152 | w := &teamDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()} |
| 153 | f.app.Handler().ServeHTTP(w, r) |
| 154 | return w.ResponseRecorder |
| 155 | } |
| 156 | if w := call(); w.Code != 200 || !strings.Contains(w.Body.String(), "Alice original") || !strings.Contains(w.Body.String(), "Bob shared") { |
| 157 | t.Fatal("member feed", w.Code, w.Body.String()) |
| 158 | } |
| 159 | f.directory.change(nil, true) |
| 160 | f.clock.step(61 * time.Second) |
| 161 | if w := call(); w.Code != 503 || strings.Contains(w.Body.String(), "Bob shared") { |
| 162 | t.Fatal("feed under Unknown was not unavailable", w.Code, w.Body.String()) |
| 163 | } |
| 164 | } |
| 165 | |