oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "amadan.net/oficina/calendar/internal/calendar/teamstate"
5 "context"
6 "errors"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13)
14
15func TestTeamUnlinkedLocalAccountIsSentToLink(t *testing.T) {
16 f := newTeamFixture(t)
17 f.localSession("alice@example.test")
18 if w := f.get("/day/2026-11-02"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/account/home" {
19 t.Fatalf("unlinked local view = %d %q, want /account/home", w.Code, w.Header().Get("Location"))
20 }
21 if w := f.get("/events/new"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/account/home" {
22 t.Fatalf("unlinked local form = %d", w.Code)
23 }
24 var before int64
25 f.app.db.G.Model(&Event{}).Count(&before)
26 if w := f.post("/events", "title=x"); w.Code != http.StatusNotFound {
27 t.Fatalf("unlinked local POST = %d, want 404", w.Code)
28 }
29 if w := f.get("/account/home"); w.Code != http.StatusOK {
30 t.Fatalf("unlinked local link page = %d", w.Code)
31 }
32 // Off the allowlist, a local session speaks for nobody: in team mode
33 // the list is the local door, and only that.
34 f.localSession("mallory@example.test")
35 if w := f.get("/day/2026-11-02"); w.Code != http.StatusNotFound {
36 t.Fatalf("local session off the list = %d, want 404", w.Code)
37 }
38}
39
40func TestTeamLinkedAccountStandsOnTheRosterNotTheAllowlist(t *testing.T) {
41 f := newTeamFixture(t)
42 subject := teamSubject('b')
43 alice := f.linkedAlice(subject, "member")
44 team, _ := f.calendarsOf(alice)
45 f.event(alice, subject, team, "Planning", "2026-11-02T10:00", "2026-11-02T11:00")
46 // Locally signed in and linked: a member.
47 if w := f.get("/day/2026-11-02"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Planning") {
48 t.Fatalf("linked local member = %d", w.Code)
49 }
50 // Through Home, off the allowlist entirely: still the roster's member.
51 delete(f.app.home.admitted.addrs, "alice@example.test")
52 f.signInHome(subject)
53 if w := f.get("/day/2026-11-02"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Planning") {
54 t.Fatalf("linked member off the allowlist = %d", w.Code)
55 }
56 if !strings.Contains(f.get("/day/2026-11-02").Body.String(), `aria-label="Account: alice@example.test"`) {
57 t.Fatal("a linked member is not named by their address")
58 }
59 // Off the roster: offboarded, allowlist or not.
60 f.app.home.admitted.addrs["alice@example.test"] = struct{}{}
61 f.dir.Set(subject, "")
62 f.refreshed()
63 if w := f.get("/day/2026-11-02"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" {
64 t.Fatalf("linked member off the roster = %d %q", w.Code, w.Header().Get("Location"))
65 }
66 f.localSession("alice@example.test")
67 if w := f.get("/day/2026-11-02"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" {
68 t.Fatalf("linked local session off the roster = %d %q", w.Code, w.Header().Get("Location"))
69 }
70}
71
72func TestTeamStorageFailureIsNeverASignOut(t *testing.T) {
73 f := newTeamFixture(t)
74 f.member(teamSubject('f'), "member")
75 if _, err := f.app.db.G.DB(); err != nil {
76 t.Fatal(err)
77 }
78 // The Home-only table unreadable: identity cannot be read now.
79 if err := f.app.db.G.Exec("ALTER TABLE calendar_team_home_people RENAME TO calendar_team_home_people_away").Error; err != nil {
80 t.Fatal(err)
81 }
82 retryable(t, "identity storage down", f.get("/day/2026-11-02"))
83 if f.cookies[f.app.home.sessions.CookieName()] == nil {
84 t.Fatal("a storage failure signed the member out")
85 }
86 if err := f.app.db.G.Exec("ALTER TABLE calendar_team_home_people_away RENAME TO calendar_team_home_people").Error; err != nil {
87 t.Fatal(err)
88 }
89 if w := f.get("/day/2026-11-02"); w.Code != http.StatusOK {
90 t.Fatalf("after storage returned = %d", w.Code)
91 }
92}
93
94func TestTeamHomeOnlyRowsNeverGainAnAddress(t *testing.T) {
95 f := newTeamFixture(t)
96 p := f.member(teamSubject('g'), "member")
97 if err := f.app.db.G.Exec("UPDATE people SET address = 'g@example.test' WHERE id = ?", p.ID).Error; err == nil {
98 t.Fatal("a Home-only person was given an address")
99 }
100 // Held by the trigger, not only by the foreign key: with keys off
101 // (an import tool, a hand repair), the row still stays.
102 if err := f.app.db.G.Exec("PRAGMA foreign_keys = OFF").Error; err != nil {
103 t.Fatal(err)
104 }
105 if err := f.app.db.G.Exec("DELETE FROM people WHERE id = ?", p.ID).Error; err == nil {
106 t.Fatal("a Home-only person was deleted")
107 }
108 if err := f.app.db.G.Exec("PRAGMA foreign_keys = ON").Error; err != nil {
109 t.Fatal(err)
110 }
111 if err := f.app.db.G.Exec("DELETE FROM calendar_team_home_people").Error; err == nil {
112 t.Fatal("a Home-only row was deleted")
113 }
114 if err := f.app.db.G.Exec("UPDATE calendar_team_home_people SET subject = 'x'").Error; err == nil {
115 t.Fatal("a Home-only row was moved")
116 }
117 // A row for a person who has an address is refused.
118 local, err := f.app.store.EnsurePerson(context.Background(), "carol@example.test")
119 if err != nil {
120 t.Fatal(err)
121 }
122 if err := f.app.db.G.Exec("INSERT INTO calendar_team_home_people(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('h'), local.ID).Error; err == nil {
123 t.Fatal("a person with an address was made Home-only")
124 }
125 // A link to a Home-only person or subject is refused.
126 if err := f.app.db.G.Exec("INSERT INTO home_identity_links(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('g'), local.ID).Error; err == nil {
127 t.Fatal("a Home-only subject was linked")
128 }
129 // A Home-only row for a subject already linked to an account is
130 // refused: that subject is that account.
131 linked := f.linkedAlice(teamSubject('j'), "member")
132 _ = linked
133 if err := f.app.db.G.Exec(`INSERT INTO people(address,zone,created_at,updated_at,week_start,default_reminder_min,default_all_day_min) VALUES('home-only:spare','UTC',0,0,1,0,0)`).Error; err != nil {
134 t.Fatal(err)
135 }
136 var spare int64
137 f.app.db.G.Raw("SELECT id FROM people WHERE address = 'home-only:spare'").Scan(&spare)
138 if err := f.app.db.G.Exec("INSERT INTO calendar_team_home_people(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('j'), spare).Error; err == nil {
139 t.Fatal("a linked subject was made Home-only")
140 }
141 // Settings still save: only the address is frozen.
142 if err := f.app.db.G.Exec("UPDATE people SET zone = 'Europe/Dublin' WHERE id = ?", p.ID).Error; err != nil {
143 t.Fatalf("a Home-only person's zone: %v", err)
144 }
145}
146
147// /signin with a session that speaks for nobody here ends it and draws the
148// door with the refusal; it never shortcuts it to a page that 404s.
149func TestTeamSigninEndsASessionForNobody(t *testing.T) {
150 f := newTeamFixture(t)
151 f.localSession("mallory@example.test")
152 w := f.get("/signin")
153 if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), signinRefusal) {
154 t.Fatalf("/signin for nobody = %d %q, want the door with the refusal", w.Code, w.Header().Get("Location"))
155 }
156 if f.cookies[f.app.home.sessions.CookieName()] != nil {
157 t.Fatal("/signin kept a session that speaks for nobody")
158 }
159}
160
161// Off team mode nothing sends err=removed, and if it arrives it reads as
162// any other unknown err does, rastrillo's generic problem: the team
163// sentence is team mode's alone.
164func TestTeamRemovedSentenceIsTeamModesAlone(t *testing.T) {
165 a := NewForTest(t, t.TempDir())
166 w := httptest.NewRecorder()
167 a.Handler().ServeHTTP(w, httptest.NewRequest("GET", "/signin?err=removed", nil))
168 if strings.Contains(w.Body.String(), "no longer on this team") || !strings.Contains(w.Body.String(), "Something went wrong. Try again.") {
169 t.Fatal("team mode off drew the team's removed sentence")
170 }
171}
172
173// teamstate's own refusals, asked directly: a subject linked to a local
174// account is that account and never gets a Home-only person (connect asks
175// Unlinked only for an unlinked subject, but a link can land in between),
176// and nothing shaped like an address is ever a subject.
177func TestTeamHomeOnlyAllocationRefusesLinksAndAddresses(t *testing.T) {
178 f := newTeamFixture(t)
179 subject := teamSubject('l')
180 f.linkedAlice(subject, "member")
181 people := func() int64 {
182 var n int64
183 f.app.db.G.Model(&Person{}).Count(&n)
184 return n
185 }
186 before := people()
187 defaults := teamstate.Defaults{Zone: "UTC", WeekStart: 1}
188 if _, _, err := teamstate.EnsureHomePerson(context.Background(), f.app.team.db, homeTestIssuer, subject, defaults); !errors.Is(err, teamstate.ErrRefused) {
189 t.Fatalf("a linked subject = %v, want refused", err)
190 }
191 for _, s := range []string{"a@b.example", "pr_x@y", ""} {
192 if _, _, err := teamstate.EnsureHomePerson(context.Background(), f.app.team.db, homeTestIssuer, s, defaults); !errors.Is(err, teamstate.ErrRefused) {
193 t.Fatalf("subject %q = %v, want refused", s, err)
194 }
195 if _, err := teamstate.HomePerson(context.Background(), f.app.team.db, homeTestIssuer, s); !errors.Is(err, teamstate.ErrRefused) {
196 t.Fatalf("lookup of %q = %v, want refused", s, err)
197 }
198 }
199 if people() != before {
200 t.Fatal("a refused allocation made a person")
201 }
202}
203
204// Every scoped read joins through grantJoin, which withholds the team
205// rung without Member standing (team_reach.go). A raw grant read written
206// anywhere else would reach the team calendar on no standing at all —
207// or rest on a guard it might one day be moved in front of (Fable #35 N3).
208func TestTeamEveryGrantJoinIsTheRungs(t *testing.T) {
209 files, err := filepath.Glob("*.go")
210 if err != nil {
211 t.Fatal(err)
212 }
213 for _, name := range files {
214 if strings.HasSuffix(name, "_test.go") || name == "team_reach.go" {
215 continue
216 }
217 src, err := os.ReadFile(name)
218 if err != nil {
219 t.Fatal(err)
220 }
221 // A grant read by any spelling: a join, a model, a table or a
222 // FROM. ensureGrant's FirstOrCreate (store_calendar.go) writes a
223 // person's own grant and reads nothing, and is the one other use.
224 upper := strings.ToUpper(string(src))
225 for _, spelling := range []string{"JOIN GRANTS", "MODEL(&GRANT{})", `TABLE("GRANTS")`, "FROM GRANTS"} {
226 if strings.Contains(upper, spelling) {
227 t.Fatalf("%s reads grants itself (%s); use grantJoin", name, spelling)
228 }
229 }
230 }
231}
232