| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/home/roster" |
| 5 | "context" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestTeamBookingPageStandsOnItsHost(t *testing.T) { |
| 14 | w := newTeamWriteWorld(t) |
| 15 | f := w.f |
| 16 | box := f.mailbox() |
| 17 | jar := f.cookies |
| 18 | f.cookies = nil |
| 19 | stranger := func(method, path, body string) *httptest.ResponseRecorder { |
| 20 | f.cookies = map[string]*http.Cookie{} |
| 21 | defer func() { f.cookies = nil }() |
| 22 | return f.do(method, path, body) |
| 23 | } |
| 24 | if r := stranger("GET", w.path, ""); r.Code != http.StatusOK { |
| 25 | t.Fatalf("host's page = %d", r.Code) |
| 26 | } |
| 27 | f.expired() |
| 28 | if r := stranger("GET", w.path, ""); r.Code != http.StatusServiceUnavailable || r.Header().Get("Retry-After") == "" || !strings.Contains(r.Header().Get("Cache-Control"), "no-store") { |
| 29 | t.Fatalf("host's page under Unknown = %d", r.Code) |
| 30 | } |
| 31 | retryable(t, "confirm under Unknown", stranger("POST", "/book/m/"+w.heldToken+"/confirm", "")) |
| 32 | f.dir.Set(teamSubject('w'), "") |
| 33 | f.refreshed() |
| 34 | if r := stranger("GET", w.path, ""); r.Code != http.StatusNotFound { |
| 35 | t.Fatalf("a removed host's page = %d, want 404", r.Code) |
| 36 | } |
| 37 | if r := stranger("POST", w.path, "start="+w.slots[2]+"&zone=UTC&name=Kim&address=kim%40example.org"); r.Code != http.StatusNotFound { |
| 38 | t.Fatalf("a hold on a removed host's page = %d, want 404", r.Code) |
| 39 | } |
| 40 | if r := stranger("POST", "/book/m/"+w.heldToken+"/confirm", ""); r.Code != http.StatusNotFound { |
| 41 | t.Fatalf("confirming with a removed host = %d, want 404", r.Code) |
| 42 | } |
| 43 | if r := stranger("GET", "/book/m/"+w.booked+"/reschedule", ""); r.Code != http.StatusNotFound { |
| 44 | t.Fatalf("rescheduling with a removed host = %d, want 404", r.Code) |
| 45 | } |
| 46 | // The booker's own booking stays theirs: they can see it and cancel. |
| 47 | if r := stranger("GET", "/book/m/"+w.booked, ""); r.Code != http.StatusOK { |
| 48 | t.Fatalf("the booker's manage page = %d", r.Code) |
| 49 | } |
| 50 | if r := stranger("POST", "/book/m/"+w.booked+"/cancel", ""); r.Code != http.StatusOK { |
| 51 | t.Fatalf("the booker's cancel = %d", r.Code) |
| 52 | } |
| 53 | var b Booking |
| 54 | f.app.db.G.Where("state = ?", BookingCancelled).First(&b) |
| 55 | if b.ID == 0 { |
| 56 | t.Fatal("the booker could not cancel") |
| 57 | } |
| 58 | // The booker is told; the removed host is not. |
| 59 | f.app.h.drain(context.Background(), time.Now()) |
| 60 | if len(box.to("jo@example.org")) == 0 { |
| 61 | t.Fatal("the booker was not told of their cancellation") |
| 62 | } |
| 63 | if got := box.to("alice@example.test"); len(got) != 0 { |
| 64 | t.Fatalf("a removed host was mailed: %v", got) |
| 65 | } |
| 66 | var owed int64 |
| 67 | f.app.db.G.Model(&BookingMail{}).Where("sent_at IS NULL AND recipient = ?", ToHost).Count(&owed) |
| 68 | if owed != 0 { |
| 69 | t.Fatal("a removed host's mail was left owed rather than retired") |
| 70 | } |
| 71 | f.cookies = jar |
| 72 | } |
| 73 | |
| 74 | // The picker is the host's whole busy list or a retry: a lapse while busy |
| 75 | // time was being read must never offer a slot the host is busy in (B23's |
| 76 | // fail-closed, on the host's standing). |
| 77 | func TestTeamBookingPickerNeverOffersBusyTimeDuringALapse(t *testing.T) { |
| 78 | seen := map[string]bool{} |
| 79 | for k := 1; k <= 40; k++ { |
| 80 | w := newTeamWriteWorld(t) |
| 81 | f := w.f |
| 82 | day, _ := time.Parse("2006-01-02", w.day) |
| 83 | busy := day.Add(14 * time.Hour) |
| 84 | _, personal := f.calendarsOf(w.alice) |
| 85 | f.event(w.alice, teamSubject('w'), personal, "Busy", busy.Format("2006-01-02T15:04"), busy.Add(30*time.Minute).Format("2006-01-02T15:04")) |
| 86 | f.cookies = map[string]*http.Cookie{} |
| 87 | f.dir.SetDown(true) |
| 88 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond) |
| 89 | f.clock.SetTick(time.Millisecond) |
| 90 | got := f.get(w.path + "?day=" + w.day + "&zone=UTC") |
| 91 | switch { |
| 92 | case got.Code == http.StatusServiceUnavailable: |
| 93 | seen["retry"] = true |
| 94 | case got.Code == http.StatusOK && !strings.Contains(got.Body.String(), `value="`+itoa(busy.Unix())+`"`): |
| 95 | seen["whole"] = true |
| 96 | default: |
| 97 | t.Fatalf("k=%d: picker = %d, offering the busy slot %v", k, got.Code, strings.Contains(got.Body.String(), itoa(busy.Unix()))) |
| 98 | } |
| 99 | } |
| 100 | if !seen["whole"] || !seen["retry"] { |
| 101 | t.Fatalf("the sweep saw %v", seen) |
| 102 | } |
| 103 | } |
| 104 | |