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 "encoding/json"
5 "slices"
6 "strings"
7 "testing"
8 "time"
9)
10
11func agentGetAttendees(t *testing.T, a *App, id int64) []string {
12 t.Helper()
13 w := calendarAgentCall(a, "calendar_get_event", map[string]any{"team": "acme", "id": id})
14 if w.Code != 200 {
15 t.Fatal(w.Code, w.Body.String())
16 }
17 var out struct {
18 Attendees []string `json:"attendees"`
19 }
20 if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil || out.Attendees == nil {
21 t.Fatal("attendees missing", err, w.Body.String())
22 }
23 return out.Attendees
24}
25
26func TestAgentAttendeesAreRecordedSealedAndNeverMailed(t *testing.T) {
27 f, _, person, cal := linkedCalendarAgent(t)
28 mailedBefore := f.app.mailRecorder.last()
29 input := eventAgentInput(cal)
30 input["attendees"] = []string{" Doc.Holliday+cal@Tito.io ", "doc.holliday+cal@tito.io", "ana_8c1f@example.com"}
31 first := calendarAgentCall(f.app, "calendar_create_event", input)
32 if first.Code != 200 || !strings.Contains(first.Body.String(), `"invitations_sent":false`) {
33 t.Fatal(first.Code, first.Body.String())
34 }
35 var receipt struct{ ID int64 }
36 json.Unmarshal(first.Body.Bytes(), &receipt)
37 want := []string{"doc.holliday+cal@tito.io", "ana_8c1f@example.com"}
38 if got := agentGetAttendees(t, f.app, receipt.ID); !slices.Equal(got, want) {
39 t.Fatal("create round trip", got)
40 }
41 // Stored as the UI stores them: the same rows quick add writes.
42 if got, err := f.app.store.AttendeesFor(t.Context(), receipt.ID); err != nil || !slices.Equal(got, want) {
43 t.Fatal("store", got, err)
44 }
45 if w := calendarAgentCall(f.app, "calendar_create_event", input); w.Body.String() != first.Body.String() {
46 t.Fatal("identical retry", w.Code, w.Body.String())
47 }
48 input["attendees"] = []string{"someone-else@example.com"}
49 if w := calendarAgentCall(f.app, "calendar_create_event", input); w.Code != 409 {
50 t.Fatal("changed attendees reused client_id", w.Code)
51 }
52 if _, err := f.app.db.Writer().Exec("PRAGMA wal_checkpoint(TRUNCATE)"); err != nil {
53 t.Fatal(err)
54 }
55 for _, needle := range []string{"holliday", "ana_8c1f"} {
56 if found, _ := f.app.GrepDatabaseForTest(t, needle); found {
57 t.Fatal("plaintext attendee address", needle)
58 }
59 }
60 if f.app.mailRecorder.last() != mailedBefore {
61 t.Fatal("an attendee was mailed")
62 }
63 var queued int64
64 if err := f.app.db.G.Model(&BookingMail{}).Count(&queued).Error; err != nil || queued != 0 {
65 t.Fatal("mail queued", queued, err)
66 }
67
68 e, err := f.app.store.Event(t.Context(), person.ID, receipt.ID)
69 if err != nil {
70 t.Fatal(err)
71 }
72 update := agentUpdateInput(e.ID, e.Sequence, map[string]any{"attendees": []string{"New.Person@example.com"}})
73 w := calendarAgentCall(f.app, "calendar_update_event", update)
74 if w.Code != 200 || !strings.Contains(w.Body.String(), `"invitations_sent":false`) {
75 t.Fatal(w.Code, w.Body.String())
76 }
77 if got := agentGetAttendees(t, f.app, e.ID); !slices.Equal(got, []string{"new.person@example.com"}) {
78 t.Fatal("update replaced", got)
79 }
80 after, _ := f.app.store.Event(t.Context(), person.ID, e.ID)
81 if after.Sequence != e.Sequence+1 || after.Title != e.Title {
82 t.Fatal("attendee change is a revision", after.Sequence, e.Sequence)
83 }
84 if w := calendarAgentCall(f.app, "calendar_update_event", update); w.Code != 200 {
85 t.Fatal("identical update retry", w.Code)
86 }
87 // Omitting attendees keeps them.
88 if w := calendarAgentCall(f.app, "calendar_update_event", agentUpdateInput(e.ID, after.Sequence, map[string]any{"title": "Renamed"})); w.Code != 200 {
89 t.Fatal(w.Code, w.Body.String())
90 }
91 if got := agentGetAttendees(t, f.app, e.ID); !slices.Equal(got, []string{"new.person@example.com"}) {
92 t.Fatal("omitted attendees changed", got)
93 }
94 after, _ = f.app.store.Event(t.Context(), person.ID, e.ID)
95 if w := calendarAgentCall(f.app, "calendar_update_event", agentUpdateInput(e.ID, after.Sequence, map[string]any{"attendees": []string{}})); w.Code != 200 {
96 t.Fatal(w.Code, w.Body.String())
97 }
98 if got := agentGetAttendees(t, f.app, e.ID); len(got) != 0 {
99 t.Fatal("cleared", got)
100 }
101 if f.app.mailRecorder.last() != mailedBefore {
102 t.Fatal("an attendee was mailed on update")
103 }
104}
105
106func TestAgentAttendeesRefuseWhatIsNotAnAddress(t *testing.T) {
107 f, _, person, cal := linkedCalendarAgent(t)
108 e := createdAgentEvent(t, f.app, person, cal)
109 many := []string{}
110 for range 51 {
111 many = append(many, "a@example.com")
112 }
113 for i, attendees := range []any{[]string{"Doc Holliday"}, []string{"doc@tito.io, ana@example.com"}, []string{"<doc@tito.io>"}, []string{""}, many, "doc@tito.io", []any{nil}} {
114 input := eventAgentInput(cal)
115 input["client_id"] = []string{"0b6a5b1c-6f0e-4f43-8f6c-4f4a3a1c0d01", "0b6a5b1c-6f0e-4f43-8f6c-4f4a3a1c0d02", "0b6a5b1c-6f0e-4f43-8f6c-4f4a3a1c0d03", "0b6a5b1c-6f0e-4f43-8f6c-4f4a3a1c0d04", "0b6a5b1c-6f0e-4f43-8f6c-4f4a3a1c0d05", "0b6a5b1c-6f0e-4f43-8f6c-4f4a3a1c0d06", "0b6a5b1c-6f0e-4f43-8f6c-4f4a3a1c0d07"}[i]
116 input["attendees"] = attendees
117 if w := calendarAgentCall(f.app, "calendar_create_event", input); w.Code != 422 && w.Code != 400 {
118 t.Fatal("create accepted", attendees, w.Code, w.Body.String())
119 }
120 if w := calendarAgentCall(f.app, "calendar_update_event", agentUpdateInput(e.ID, e.Sequence, map[string]any{"attendees": attendees})); w.Code != 422 && w.Code != 400 {
121 t.Fatal("update accepted", attendees, w.Code, w.Body.String())
122 }
123 }
124 if w := calendarAgentCall(f.app, "calendar_update_event", agentUpdateInput(e.ID, e.Sequence, map[string]any{"attendees": nil})); w.Code != 422 && w.Code != 400 {
125 t.Fatal("null attendees", w.Code)
126 }
127 var count int64
128 if err := f.app.db.G.Model(&Event{}).Count(&count).Error; err != nil || count != 1 {
129 t.Fatal("refusal wrote an event", count, err)
130 }
131 if err := f.app.db.G.Model(&Attendee{}).Count(&count).Error; err != nil || count != 0 {
132 t.Fatal("refusal wrote attendees", count, err)
133 }
134 if after, _ := f.app.store.Event(t.Context(), person.ID, e.ID); after.Sequence != e.Sequence {
135 t.Fatal("refusal bumped revision")
136 }
137}
138
139func TestAgentAttendeesOfABookedEventBelongToTheBooking(t *testing.T) {
140 f, _, person, cal := linkedCalendarAgent(t)
141 e := createdAgentEvent(t, f.app, person, cal)
142 if err := f.app.store.SetAttendees(t.Context(), e.ID, []string{"booker@example.com"}); err != nil {
143 t.Fatal(err)
144 }
145 now := time.Now().UTC()
146 if err := f.app.db.G.Create(&Booking{PageID: 1, EventID: e.ID, TokenHash: "fixture", State: "confirmed", BookerAddressFP: "fixture", BookerZone: "UTC", HoldExpiresAt: now.Unix(), CreatedAt: now, UpdatedAt: now}).Error; err != nil {
147 t.Fatal(err)
148 }
149 if w := calendarAgentCall(f.app, "calendar_update_event", agentUpdateInput(e.ID, e.Sequence, map[string]any{"attendees": []string{"other@example.com"}})); w.Code != 422 {
150 t.Fatal("booked attendees replaced", w.Code, w.Body.String())
151 }
152 if got, _ := f.app.store.AttendeesFor(t.Context(), e.ID); !slices.Equal(got, []string{"booker@example.com"}) {
153 t.Fatal("booker lost", got)
154 }
155}
156
157func TestAgentCreateReceiptIsUnchangedWithoutAttendees(t *testing.T) {
158 // Receipts written before attendees existed must still match an
159 // identical retry, so an absent list must not change the fingerprint.
160 payload, _ := json.Marshal(agentEventInput{Team: "acme", ClientID: "44fca936-6f81-4132-9184-f22c61b7a478", Title: "t"})
161 if strings.Contains(string(payload), "attendees") {
162 t.Fatal("absent attendees changed the receipt content", string(payload))
163 }
164}
165