package calendartest import ( "context" "net/http" "net/url" "regexp" "strconv" "strings" "testing" "time" ) // The host's side of booking, through the router (booking spec §2.1, // §3.4, §7.4, B18, B25). func settingsForm(handle string) url.Values { return url.Values{"zone": {"Europe/Dublin"}, "week_start": {"1"}, "default_reminder_min": {"10"}, "default_all_day_min": {"360"}, "handle": {handle}} } func pageForm() url.Values { return url.Values{"title": {"Quick chat"}, "description": {"Fifteen minutes."}, "availability": {"mondays 12-3, tuesdays 12-2, 5-7"}, "duration_min": {"15"}, "buffer_min": {"10"}, "notice_min": {"120"}, "horizon_days": {"30"}} } var rePublicURL = regexp.MustCompile(`/book/paul/([a-z0-9]{8})`) func TestAHostMakesAPageAndSeesItsLinkOnlyOncePublished(t *testing.T) { app, session := signedIn(t, "host@example.com") h := app.Handler() if rec := postAs(t, h, session, "/settings", settingsForm("paul")); rec.Code != http.StatusSeeOther { t.Fatalf("settings: %d\n%s", rec.Code, rec.Body.String()) } if rec := postAs(t, h, session, "/settings/booking", pageForm()); rec.Code != http.StatusSeeOther { t.Fatalf("create: %d\n%s", rec.Code, rec.Body.String()) } body := getAs(t, h, session, "/settings/booking").Body.String() for _, want := range []string{"Quick chat", "Not published", "Tuesday 12:00–14:00, 17:00–19:00", "Anyone with this link can see when you are free"} { if !strings.Contains(body, want) { t.Errorf("list lacks %q", want) } } if rePublicURL.MatchString(body) { t.Error("an unpublished page shows a public URL") } if rec := postAs(t, h, session, "/settings/booking/1/publish", url.Values{"publish": {"1"}}); rec.Code != http.StatusSeeOther { t.Fatalf("publish: %d", rec.Code) } body = getAs(t, h, session, "/settings/booking").Body.String() if !rePublicURL.MatchString(body) || !strings.Contains(body, "Published") { t.Errorf("a published page shows no public URL:\n%s", body) } } func TestAPageWithAnUnreadableSentenceIsNotSaved(t *testing.T) { app, session := signedIn(t, "host@example.com") h := app.Handler() postAs(t, h, session, "/settings", settingsForm("paul")) form := pageForm() form.Set("availability", "mondays whenever") rec := postAs(t, h, session, "/settings/booking", form) if rec.Code != http.StatusUnprocessableEntity { t.Fatalf("status %d, want 422", rec.Code) } if !strings.Contains(rec.Body.String(), "whenever") { t.Error("the refusal does not quote the clause") } if strings.Contains(getAs(t, h, session, "/settings/booking").Body.String(), "Quick chat") { t.Error("the page was saved anyway") } } func TestThePaneFragmentDescribesTheWindows(t *testing.T) { app, session := signedIn(t, "host@example.com") rec := postAs(t, app.Handler(), session, "/settings/booking/parse", url.Values{"text": {"fri 9-5"}}) body := rec.Body.String() if !strings.Contains(body, "Friday 09:00–17:00") || strings.Contains(strings.ToLower(body), "