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 "net/http"
5 "strconv"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/home/handoff"
11 "amadan.net/oficina/home/roster"
12
13 "amadan.net/oficina/calendar/internal/calendar/teamstate"
14)
15
16func TestTeamHomeOnlyMemberSignsInToAnAddresslessPerson(t *testing.T) {
17 f := newTeamFixture(t)
18 subject := teamSubject('a')
19 p := f.member(subject, "member")
20 if !teamstate.Addressless(p.Address) || !strings.HasPrefix(p.Address, "home-only:") || strings.Contains(p.Address, "@") {
21 t.Fatalf("a Home-only person holds address %q", p.Address)
22 }
23 team, personal := f.calendarsOf(p)
24 if team == 0 || personal == 0 {
25 t.Fatal("no calendars")
26 }
27 f.event(p, subject, team, "Team standup", "2026-11-02T09:00", "2026-11-02T09:15")
28 w := f.get("/day/2026-11-02")
29 body := w.Body.String()
30 if w.Code != http.StatusOK || !strings.Contains(body, "Team standup") {
31 t.Fatalf("day = %d, want the team's event", w.Code)
32 }
33 if strings.Contains(body, "home-only:") || strings.Contains(body, "data-partial") {
34 t.Fatal("the day page showed the placeholder, or a partial notice to a member")
35 }
36 if !strings.Contains(body, `aria-label="Account: Oficina account …`+subject[len(subject)-4:]+`"`) {
37 t.Fatal("the account menu does not name a Home-only member by their label")
38 }
39 // A page outside the session group names them the same way, and
40 // never by the placeholder.
41 if open := f.get("/help/open-source").Body.String(); !strings.Contains(open, "Oficina account …"+subject[len(subject)-4:]) || strings.Contains(open, "home-only:") {
42 t.Fatal("a public page does not name a Home-only member by their label")
43 }
44 // Signing in again finds the same person and allocates nothing.
45 var before int64
46 f.app.db.G.Model(&Person{}).Count(&before)
47 if w := f.signInHome(subject); w.Code != http.StatusSeeOther {
48 t.Fatalf("second sign-in = %d", w.Code)
49 }
50 var after int64
51 f.app.db.G.Model(&Person{}).Count(&after)
52 if f.personFor(subject).ID != p.ID || before != after {
53 t.Fatal("a second sign-in allocated a person")
54 }
55}
56
57func TestTeamOnlyACurrentMemberIsAllocated(t *testing.T) {
58 f := newTeamFixture(t)
59 people := func() int64 {
60 var n int64
61 f.app.db.G.Model(&Person{}).Count(&n)
62 return n
63 }
64 // A stranger: connect's unknown, nothing made.
65 if w := f.signInHome(teamSubject('s')); w.Code != http.StatusForbidden {
66 t.Fatalf("stranger = %d, want 403", w.Code)
67 }
68 if people() != 0 || f.cookies[f.app.home.sessions.CookieName()] != nil {
69 t.Fatal("a stranger got a person or a session")
70 }
71 // Unknown: 503, nothing made.
72 f.dir.Set(teamSubject('u'), "member")
73 f.expired()
74 if w := f.signInHome(teamSubject('u')); w.Code != http.StatusServiceUnavailable {
75 t.Fatalf("unknown = %d, want 503", w.Code)
76 }
77 if people() != 0 {
78 t.Fatal("Unknown allocated a person")
79 }
80 // Over somebody else's live session: refused, nothing made, the
81 // session kept.
82 f.refreshed()
83 f.member(teamSubject('m'), "member")
84 kept := f.cookies[f.app.home.sessions.CookieName()]
85 f.dir.Set(teamSubject('n'), "member")
86 f.refreshed()
87 // Over a Home session handoff's own store refuses a second subject
88 // (400, CurrentSubject) before connect is asked; over a local one it is
89 // connect's "unknown" (403, below). Either way nothing is made.
90 if w := f.exchange(t, handoff.StartPath, teamSubject('n')); w.Code != http.StatusBadRequest {
91 t.Fatalf("unknown arrival over a live Home session = %d, want 400", w.Code)
92 }
93 if people() != 1 {
94 t.Fatal("an arrival over a live session allocated a person")
95 }
96 if c := f.cookies[f.app.home.sessions.CookieName()]; c == nil || c.Value != kept.Value {
97 t.Fatal("the live session did not survive")
98 }
99 f.localSession("alice@example.test")
100 kept = f.cookies[f.app.home.sessions.CookieName()]
101 if w := f.exchange(t, handoff.StartPath, teamSubject('n')); w.Code != http.StatusForbidden {
102 t.Fatalf("unknown arrival over a local session = %d, want 403", w.Code)
103 }
104 if c := f.cookies[f.app.home.sessions.CookieName()]; people() != 1 || c == nil || c.Value != kept.Value {
105 t.Fatal("an arrival over a local session allocated a person or ended the session")
106 }
107 // A known Home-only member arriving over another's session: 403, and
108 // that session survives too.
109 f.member(teamSubject('k'), "member")
110 f.localSession("alice@example.test")
111 kept = f.cookies[f.app.home.sessions.CookieName()]
112 if w := f.exchange(t, handoff.StartPath, teamSubject('k')); w.Code != http.StatusForbidden {
113 t.Fatalf("known arrival over another session = %d, want 403", w.Code)
114 }
115 if c := f.cookies[f.app.home.sessions.CookieName()]; c == nil || c.Value != kept.Value {
116 t.Fatal("the other session did not survive")
117 }
118}
119
120func TestTeamRemovalSignsOutAnExistingSession(t *testing.T) {
121 f := newTeamFixture(t)
122 subject := teamSubject('r')
123 p := f.member(subject, "member")
124 team, _ := f.calendarsOf(p)
125 f.event(p, subject, team, "Board meeting", "2026-11-02T10:00", "2026-11-02T11:00")
126 if w := f.get("/day/2026-11-02"); !strings.Contains(w.Body.String(), "Board meeting") {
127 t.Fatal("member cannot see the team's event")
128 }
129 f.dir.Set(subject, "")
130 f.refreshed()
131 w := f.get("/day/2026-11-02")
132 if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" {
133 t.Fatalf("removed member's page = %d %q, want 303 to the removed door", w.Code, w.Header().Get("Location"))
134 }
135 if strings.Contains(w.Body.String(), "Board meeting") {
136 t.Fatal("the removed member saw the event")
137 }
138 if f.cookies[f.app.home.sessions.CookieName()] != nil {
139 t.Fatal("the removed member kept their session")
140 }
141 door := f.get("/signin?err=removed")
142 if door.Code != http.StatusOK || !strings.Contains(door.Body.String(), "no longer on this team") {
143 t.Fatalf("removed door = %d, want the removed sentence", door.Code)
144 }
145 // Signing in again finds them, allocates nothing, and offboards them
146 // on their first page.
147 if w := f.signInHome(subject); w.Code != http.StatusSeeOther {
148 t.Fatalf("re-sign-in = %d", w.Code)
149 }
150 if w := f.get("/"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" {
151 t.Fatalf("removed member after re-sign-in = %d %q", w.Code, w.Header().Get("Location"))
152 }
153 // A POST is offboarded too, and writes nothing.
154 f.signInHome(subject)
155 var before int64
156 f.app.db.G.Model(&Event{}).Count(&before)
157 w = f.post("/events", "calendar_id=1&title=Late&start_local=2026-11-03T09:00&end_local=2026-11-03T10:00&tz=UTC&kind=meeting")
158 var after int64
159 f.app.db.G.Model(&Event{}).Count(&after)
160 if w.Code != http.StatusSeeOther || before != after {
161 t.Fatalf("removed member's POST = %d, events %d→%d", w.Code, before, after)
162 }
163 // /signin with that session ends it and says why, on the door itself,
164 // rather than signing them back in.
165 f.signInHome(subject)
166 if w := f.get("/signin"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "no longer on this team") ||
167 f.cookies[f.app.home.sessions.CookieName()] != nil {
168 t.Fatalf("/signin for a removed member = %d %q, want the door with the removal and no session", w.Code, w.Header().Get("Location"))
169 }
170 // Re-added: they are back.
171 f.dir.Set(subject, "member")
172 f.refreshed()
173 f.signInHome(subject)
174 if w := f.get("/day/2026-11-02"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Board meeting") {
175 t.Fatalf("re-added member = %d", w.Code)
176 }
177}
178
179func TestTeamStandingEndsAtTheLeaseUnderAFailingRefresh(t *testing.T) {
180 f := newTeamFixture(t)
181 subject := teamSubject('l')
182 p := f.member(subject, "member")
183 team, _ := f.calendarsOf(p)
184 e := f.event(p, subject, team, "Quarterly review", "2026-11-02T10:00", "2026-11-02T11:00")
185 f.refreshed()
186 f.dir.SetDown(true)
187 f.clock.Step(roster.MaximumAge - time.Second)
188 if w := f.get("/day/2026-11-02"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Quarterly review") || strings.Contains(w.Body.String(), "data-partial") {
189 t.Fatalf("59 s = %d, want the whole day", w.Code)
190 }
191 if w := f.get("/events/" + itoa(e.ID)); w.Code != http.StatusOK {
192 t.Fatalf("59 s event = %d", w.Code)
193 }
194 f.clock.Step(2 * time.Second)
195 w := f.get("/day/2026-11-02")
196 if w.Code != http.StatusOK || strings.Contains(w.Body.String(), "Quarterly review") || !strings.Contains(w.Body.String(), "data-partial") {
197 t.Fatalf("61 s day = %d, want partial and without the event", w.Code)
198 }
199 retryable(t, "61 s event", f.get("/events/"+itoa(e.ID)))
200 retryable(t, "61 s missing event", f.get("/events/999999"))
201 // Still signed in: Unknown ends nothing.
202 if f.cookies[f.app.home.sessions.CookieName()] == nil {
203 t.Fatal("Unknown signed the member out")
204 }
205 // /signin under Unknown is the ordinary shortcut, never an offboard.
206 if w := f.get("/signin"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/" || f.cookies[f.app.home.sessions.CookieName()] == nil {
207 t.Fatalf("/signin under Unknown = %d %q", w.Code, w.Header().Get("Location"))
208 }
209}
210
211func TestTeamEmptyPartialListSaysPartialNotEmpty(t *testing.T) {
212 f := newTeamFixture(t)
213 f.member(teamSubject('e'), "member")
214 f.expired()
215 for path, empty := range map[string]string{
216 "/day/2026-11-02": "Nothing on this day",
217 "/agenda": "Nothing in the next fortnight",
218 "/week/2026-11-02": "",
219 "/month/2026-11": "",
220 "/calendars": "",
221 } {
222 w := f.get(path)
223 body := w.Body.String()
224 if w.Code != http.StatusOK || !strings.Contains(body, "data-partial") || !strings.Contains(body, partialTitle) {
225 t.Fatalf("%s under Unknown = %d, want the partial notice", path, w.Code)
226 }
227 if empty != "" && strings.Contains(body, empty) {
228 t.Fatalf("%s under Unknown drew its empty state, which reads as a free day", path)
229 }
230 if strings.Contains(body, "cal-sentence>Nothing") || strings.Contains(body, "Shared with the team") {
231 t.Fatalf("%s under Unknown said something about the team's time", path)
232 }
233 }
234 if w := f.get("/"); w.Code != http.StatusSeeOther {
235 t.Fatalf("/ under Unknown = %d, want today's redirect", w.Code)
236 }
237}
238
239func TestTeamUnknownItemsAreRetryable(t *testing.T) {
240 f := newTeamFixture(t)
241 subject := teamSubject('i')
242 p := f.member(subject, "member")
243 team, _ := f.calendarsOf(p)
244 e := f.event(p, subject, team, "Offsite", "2026-11-02T10:00", "2026-11-02T11:00")
245 f.expired()
246 for _, path := range []string{
247 "/events/" + itoa(e.ID), "/events/" + itoa(e.ID) + "/edit", "/events/" + itoa(e.ID) + "/delete",
248 "/events/new", "/calendars/" + itoa(team) + ".ics", "/calendars/" + itoa(team) + "/share",
249 "/import", "/settings", "/settings/booking", "/settings/booking/new",
250 "/api/native/session", "/api/native/events?from=2026-11-01&to=2026-11-08",
251 } {
252 retryable(t, path, f.get(path))
253 }
254 for _, path := range []string{"/events", "/events/quick", "/events/" + itoa(e.ID), "/settings", "/calendars/" + itoa(team) + "/feeds"} {
255 retryable(t, "POST "+path, f.post(path, "title=x"))
256 }
257}
258
259func itoa(n int64) string { return strconv.FormatInt(n, 10) }
260