package calendartest import ( "bytes" "context" "io" "log/slog" "net/http" "net/url" "path/filepath" "regexp" "strings" "testing" "time" calendar "amadan.net/oficina/calendar/internal/calendar" ) // The sweep through the router (booking spec ยง8), and the two rules // about what must never reach a log. func TestTheTickExpiresHoldsAndWakesForThem(t *testing.T) { t.Setenv("CARLOS_ADMIN_TOKEN", "test-token") app, _, path, _ := publishedPage(t) h := app.Handler() _, starts := futureDaySlots(t, h, path) strangerPost(t, h, path, holdForm(starts[0], "A", "a@example.com")) if _, err := app.DBForTest().Exec(`UPDATE bookings SET hold_expires_at = ?`, time.Now().Add(-time.Minute).Unix()); err != nil { t.Fatal(err) } if !app.NextDue().Before(time.Now()) { t.Error("NextDue does not see the expired hold") } if rec := tick(t, h); rec.Code != http.StatusNoContent { t.Fatalf("tick: %d", rec.Code) } if again := strangerPost(t, h, path, holdForm(starts[0], "B", "b@example.com")); again.Code != 200 { t.Errorf("the slot did not come back after the sweep: %d", again.Code) } } func TestTheTickRetriesOwedMailAndDrainOnRequestDoesOneThing(t *testing.T) { t.Setenv("CARLOS_ADMIN_TOKEN", "test-token") app, mailer, path, _ := publishedPage(t) h := app.Handler() _, starts := futureDaySlots(t, h, path) strangerPost(t, h, path, holdForm(starts[0], "Doc", "doc@tito.io")) link := reManage.FindString(mailer.body(0)) // The host's mailbox bounces for now. mailer.refuse = "host@example.com" strangerPost(t, h, link+"/confirm", nil) if mailer.count() != 1 { t.Fatalf("mails after a refused confirm = %d, want the hold mail only", mailer.count()) } if !app.NextDue().Before(time.Now().Add(6 * time.Minute)) { t.Error("an owed mail is not on NextDue") } // Age the row past its lease and let the tick retry it, now that // the mailbox is back. mailer.refuse = "" app.DBForTest().Exec(`UPDATE booking_mails SET due_at = ? WHERE sent_at IS NULL`, time.Now().Add(-time.Second).Unix()) if rec := tick(t, h); rec.Code != http.StatusNoContent { t.Fatalf("tick: %d", rec.Code) } if mailer.count() != 2 || mailer.to(1) != "host@example.com" { t.Errorf("the tick did not deliver the owed host mail: %d to %q", mailer.count(), mailer.to(1)) } // DrainOnRequest: a signed-in page view does one thing. Queue two // mails (a reschedule), refuse nothing, age them, load one page. session := app.SignInForTest(t, "host@example.com") picker := strangerGet(t, h, link+"/reschedule?day="+time.Now().UTC().AddDate(0, 0, 1).Format("2006-01-02")+"&zone=UTC").Body.String() rev := regexp.MustCompile(`name="rev" value="(\d+)"`).FindStringSubmatch(picker) next := reStart.FindAllStringSubmatch(picker, -1) mailer.refuse = "nobody-yet" // stop the immediate try from sending mailer.refuse = "@" // refuse everything for the immediate try strangerPost(t, h, link+"/reschedule", url.Values{"start": {next[1][1]}, "zone": {"UTC"}, "rev": {rev[1]}}) mailer.refuse = "" before := mailer.count() app.DBForTest().Exec(`UPDATE booking_mails SET due_at = ? WHERE sent_at IS NULL`, time.Now().Add(-time.Second).Unix()) getAs(t, h, session, "/today") if mailer.count() != before+1 { t.Errorf("a page view sent %d mails, want exactly one", mailer.count()-before) } } // No address, no token, no title, in any log line, after a failed send. func TestNothingSealedReachesTheLog(t *testing.T) { var buf bytes.Buffer mailer := &pickyMailer{refuse: "dermot"} app, err := calendar.New(calendar.Config{ DBPath: filepath.Join(t.TempDir(), "calendar.db"), Env: "development", Origin: testOrigin, Mailer: mailer, Logger: slog.New(slog.NewTextHandler(&buf, nil)), }) if err != nil { t.Fatal(err) } t.Cleanup(func() { app.Close() }) session := app.SignInForTest(t, "host@example.com") h := app.Handler() postAs(t, h, session, "/settings", url.Values{"zone": {"UTC"}, "week_start": {"1"}, "default_reminder_min": {"10"}, "default_all_day_min": {"0"}, "handle": {"paul"}}) postAs(t, h, session, "/settings/booking", url.Values{"title": {"Interrogation"}, "availability": {"every day 9-5"}, "duration_min": {"30"}, "buffer_min": {"0"}, "notice_min": {"0"}, "horizon_days": {"30"}}) postAs(t, h, session, "/settings/booking/1/publish", url.Values{"publish": {"1"}}) path := regexp.MustCompile(`/book/paul/[a-z0-9]{8}`).FindString(getAs(t, h, session, "/settings/booking").Body.String()) _, starts := futureDaySlots(t, h, path) strangerPost(t, h, path, holdForm(starts[0], "Dermot Lacey", "dermot@elsewhere.example")) log := buf.String() if !strings.Contains(log, "booking.mail") { t.Fatalf("the failed send was not logged at all:\n%s", log) } for _, never := range []string{"dermot", "Dermot", "Interrogation"} { if strings.Contains(log, never) { t.Errorf("the log carries %q:\n%s", never, log) } } if regexp.MustCompile(`[0-9a-f]{64}`).MatchString(log) { t.Error("the log carries something shaped like a token") } _ = io.Discard _ = context.Background }