| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "mime/multipart" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "regexp" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // teamWriteWorld is a member with something on every write route to act |
| 15 | // on: alice, linked and on the roster, with a handle, a published booking |
| 16 | // page, a single event with a reminder and attendees, a daily series, a |
| 17 | // feed, and a stranger's held booking and confirmed booking on her page. |
| 18 | type teamWriteWorld struct { |
| 19 | f *teamFixture |
| 20 | alice Person |
| 21 | team int64 |
| 22 | single, series Event |
| 23 | reminder int64 |
| 24 | notices []int64 // alice's two unread in-app notices |
| 25 | feed int64 |
| 26 | path string // the public booking page |
| 27 | day string // a future day with free slots |
| 28 | slots []string |
| 29 | heldToken, booked string // manage tokens |
| 30 | } |
| 31 | |
| 32 | var ( |
| 33 | reWorldManage = regexp.MustCompile(`/book/m/([0-9a-f]{64})`) |
| 34 | reWorldStart = regexp.MustCompile(`name="start" value="(\d+)"`) |
| 35 | reWorldDay = regexp.MustCompile(`\?day=(\d{4}-\d{2}-\d{2})`) |
| 36 | reWorldRev = regexp.MustCompile(`name="rev" value="(\d+)"`) |
| 37 | reWorldPage = regexp.MustCompile(`/book/alice/[a-z0-9]{8}`) |
| 38 | ) |
| 39 | |
| 40 | func newTeamWriteWorld(t *testing.T) *teamWriteWorld { |
| 41 | t.Helper() |
| 42 | f := newTeamFixture(t) |
| 43 | w := &teamWriteWorld{f: f} |
| 44 | subject := teamSubject('w') |
| 45 | w.alice = f.linkedAlice(subject, "member") |
| 46 | w.team, _ = f.calendarsOf(w.alice) |
| 47 | mustRedirect(t, "settings", f.post("/settings", "zone=UTC&week_start=1&default_reminder_min=10&default_all_day_min=0&handle=alice")) |
| 48 | mustRedirect(t, "page", f.post("/settings/booking", "title=Chat&description=Half+an+hour&availability=every+day+9-5&duration_min=30&buffer_min=0¬ice_min=0&horizon_days=30")) |
| 49 | mustRedirect(t, "publish", f.post("/settings/booking/1/publish", "publish=1")) |
| 50 | w.path = reWorldPage.FindString(f.get("/settings/booking").Body.String()) |
| 51 | if w.path == "" { |
| 52 | t.Fatal("no public booking URL") |
| 53 | } |
| 54 | ctx := f.withAccess(subject) |
| 55 | var err error |
| 56 | w.single, err = f.app.store.CreateEvent(ctx, w.alice.ID, EventInput{CalendarID: w.team, Title: "Single", StartLocal: "2027-01-05T10:00", EndLocal: "2027-01-05T11:00", TZ: "UTC", Kind: KindMeeting, Status: StatusConfirmed}) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | w.series, err = f.app.store.CreateEvent(ctx, w.alice.ID, EventInput{CalendarID: w.team, Title: "Daily", StartLocal: "2027-01-04T09:00", EndLocal: "2027-01-04T09:15", TZ: "UTC", Kind: KindMeeting, Status: StatusConfirmed, RRule: "FREQ=DAILY"}) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if err := f.app.store.AddReminder(ctx, w.alice.ID, w.single.ID, 15); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | f.app.db.G.Model(&Reminder{}).Where("event_id = ?", w.single.ID).Pluck("id", &w.reminder) |
| 68 | // Two unread in-app notices, one per rule, for the dismissals. |
| 69 | if err := f.app.store.AddReminderOn(ctx, w.alice.ID, w.series.ID, 5, ChannelInApp); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | var second int64 |
| 73 | f.app.db.G.Model(&Reminder{}).Where("event_id = ?", w.series.ID).Pluck("id", &second) |
| 74 | for _, n := range []ReminderNotice{ |
| 75 | {ReminderID: w.reminder, PersonID: w.alice.ID, EventID: w.single.ID, OccurrenceKey: itoa(w.single.ID) + "/2027-01-05T10:00", StartUTC: w.single.StartUTC, FiredAt: time.Now().Unix()}, |
| 76 | {ReminderID: second, PersonID: w.alice.ID, EventID: w.series.ID, OccurrenceKey: itoa(w.series.ID) + "/2027-01-04T09:00", StartUTC: w.series.StartUTC, FiredAt: time.Now().Unix()}, |
| 77 | } { |
| 78 | if err := f.app.db.G.Create(&n).Error; err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | w.notices = append(w.notices, n.ID) |
| 82 | } |
| 83 | if _, err := f.app.store.MintFeed(ctx, w.alice.ID, w.team); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | f.app.db.G.Model(&Feed{}).Pluck("id", &w.feed) |
| 87 | // A stranger's two bookings: one held, one confirmed. |
| 88 | jar := f.cookies |
| 89 | f.cookies = map[string]*http.Cookie{} |
| 90 | page := f.get(w.path) |
| 91 | today := time.Now().UTC().Format("2006-01-02") |
| 92 | day := "" |
| 93 | for _, m := range reWorldDay.FindAllStringSubmatch(page.Body.String(), -1) { |
| 94 | if m[1] > today { |
| 95 | day = m[1] |
| 96 | break |
| 97 | } |
| 98 | } |
| 99 | w.day = day |
| 100 | for _, m := range reWorldStart.FindAllStringSubmatch(f.get(w.path+"?day="+day+"&zone=UTC").Body.String(), -1) { |
| 101 | w.slots = append(w.slots, m[1]) |
| 102 | } |
| 103 | if len(w.slots) < 6 { |
| 104 | t.Fatalf("only %d slots", len(w.slots)) |
| 105 | } |
| 106 | hold := func(start string) string { |
| 107 | if r := f.post(w.path, "start="+start+"&zone=UTC&name=Jo&address=jo%40example.org¬e=hi"); r.Code != http.StatusOK { |
| 108 | t.Fatalf("hold = %d", r.Code) |
| 109 | } |
| 110 | f.app.signinMail.wait() |
| 111 | m := reWorldManage.FindStringSubmatch(f.app.mailRecorder.last()) |
| 112 | if m == nil { |
| 113 | t.Fatal("no manage link mailed") |
| 114 | } |
| 115 | return m[1] |
| 116 | } |
| 117 | w.booked = hold(w.slots[0]) |
| 118 | if r := f.post("/book/m/"+w.booked+"/confirm", ""); r.Code != http.StatusOK { |
| 119 | t.Fatalf("confirm = %d", r.Code) |
| 120 | } |
| 121 | w.heldToken = hold(w.slots[1]) |
| 122 | f.cookies = jar |
| 123 | return w |
| 124 | } |
| 125 | |
| 126 | func mustRedirect(t *testing.T, what string, w *httptest.ResponseRecorder) { |
| 127 | t.Helper() |
| 128 | if w.Code != http.StatusSeeOther { |
| 129 | t.Fatalf("%s = %d %s", what, w.Code, w.Body.String()) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // importICS posts a one-event .ics file to the team calendar. |
| 134 | func (w *teamWriteWorld) importICS() *httptest.ResponseRecorder { |
| 135 | return w.importFile("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:imported-1\r\nSUMMARY:Imported\r\nDTSTART;TZID=UTC:20270111T100000\r\nDTEND;TZID=UTC:20270111T110000\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") |
| 136 | } |
| 137 | |
| 138 | // importFile posts ics as a file to the team calendar. |
| 139 | func (w *teamWriteWorld) importFile(ics string) *httptest.ResponseRecorder { |
| 140 | var body bytes.Buffer |
| 141 | mw := multipart.NewWriter(&body) |
| 142 | _ = mw.WriteField("calendar_id", itoa(w.team)) |
| 143 | fw, _ := mw.CreateFormFile("file", "in.ics") |
| 144 | fmt.Fprint(fw, ics) |
| 145 | mw.Close() |
| 146 | r := httptest.NewRequest("POST", homeTestOrigin+"/import", &body) |
| 147 | r.Header.Set("Content-Type", mw.FormDataContentType()) |
| 148 | r.Header.Set("Origin", homeTestOrigin) |
| 149 | for _, c := range w.f.cookies { |
| 150 | r.AddCookie(c) |
| 151 | } |
| 152 | rec := httptest.NewRecorder() |
| 153 | w.f.app.mux.ServeHTTP(rec, r) |
| 154 | return rec |
| 155 | } |
| 156 | |