| 1 | package calendar |
| 2 | |
| 3 | // The smart door in team mode: connect's member choice (Config.Admit), the |
| 4 | // removal, what is remembered for a Home-only member, and a roster or |
| 5 | // identity store that cannot be read. |
| 6 | |
| 7 | import ( |
| 8 | "html" |
| 9 | "net/http" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 15 | ) |
| 16 | |
| 17 | // A roster Member with no account here is asked to choose, in connect's |
| 18 | // English, word for word: "I'm new here" makes the Home-only person and |
| 19 | // signs them in, remembered as Oficina ID with no address; "Connect my |
| 20 | // existing account" goes to the email-only door. Without a live proof the |
| 21 | // choice is connect's "failed", in a 403. |
| 22 | func TestTeamMemberChooses(t *testing.T) { |
| 23 | f := newTeamFixture(t) |
| 24 | subject := teamSubject('c') |
| 25 | f.dir.Set(subject, "member") |
| 26 | f.refreshed() |
| 27 | redirectsTo(t, f.signInHomeChoosing(subject), connect.ChoosePath) |
| 28 | page := f.get(connect.ChoosePath) |
| 29 | for _, key := range []string{"choose_title", "choose_body", "choose_connect_action", "choose_new_action"} { |
| 30 | if want := connect.English[key]; !strings.Contains(html.UnescapeString(page.Body.String()), want) { |
| 31 | t.Errorf("the choose page lacks connect's %s %q", key, want) |
| 32 | } |
| 33 | } |
| 34 | if page.Code != http.StatusOK || f.sessionCookie() != nil { |
| 35 | t.Fatalf("choose page = %d, session %v", page.Code, f.sessionCookie() != nil) |
| 36 | } |
| 37 | if w := f.choose("new"); w.Code != http.StatusSeeOther || f.sessionCookie() == nil { |
| 38 | t.Fatalf("I'm new here = %d, session %v", w.Code, f.sessionCookie() != nil) |
| 39 | } |
| 40 | rec, res := f.remembered() |
| 41 | if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Address != "" { |
| 42 | t.Errorf("a Home-only member is remembered as %+v (%v), want Oficina ID with no address", rec, res) |
| 43 | } |
| 44 | if w := f.get("/calendars"); w.Code != http.StatusOK { |
| 45 | t.Fatalf("the new member's first page = %d", w.Code) |
| 46 | } |
| 47 | |
| 48 | // Connect my existing account: the email-only door. |
| 49 | g := newTeamFixture(t) |
| 50 | g.dir.Set(subject, "member") |
| 51 | g.refreshed() |
| 52 | redirectsTo(t, g.signInHomeChoosing(subject), connect.ChoosePath) |
| 53 | w := g.choose("connect") |
| 54 | loc := w.Header().Get("Location") |
| 55 | if w.Code != http.StatusSeeOther || !strings.HasPrefix(loc, connect.SigninLocalPath) { |
| 56 | t.Fatalf("connect my existing account = %d %q, want the email-only door", w.Code, loc) |
| 57 | } |
| 58 | if card := door(t, g.get(loc).Body.String()); strings.Contains(card, "Oficina ID") || !strings.Contains(card, emailField) { |
| 59 | t.Errorf("the door after connect:\n%s", card) |
| 60 | } |
| 61 | |
| 62 | // No live proof: connect's failed page, in a 403, with the way on to |
| 63 | // signing in to an existing account. |
| 64 | h := newTeamFixture(t) |
| 65 | if w := h.get(connect.ChoosePath); w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), "Could not connect accounts") || !strings.Contains(w.Body.String(), `href="/signin/local"`) { |
| 66 | t.Errorf("choose with no proof = %d\n%s", w.Code, w.Body.String()) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // A stranger to the roster gets no choice: connect's unknown, no session, |
| 71 | // nothing remembered, and the door still draws. |
| 72 | func TestTeamStrangerGetsNoChoice(t *testing.T) { |
| 73 | f := newTeamFixture(t) |
| 74 | w := f.signInHomeChoosing(teamSubject('s')) |
| 75 | if w.Code == http.StatusSeeOther && w.Header().Get("Location") == connect.ChoosePath { |
| 76 | t.Fatal("a stranger was asked to choose") |
| 77 | } |
| 78 | if f.sessionCookie() != nil { |
| 79 | t.Fatal("a stranger got a session") |
| 80 | } |
| 81 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 82 | t.Error("a stranger was remembered") |
| 83 | } |
| 84 | if w := f.get("/signin"); w.Code != http.StatusOK { |
| 85 | t.Errorf("/signin after a stranger = %d", w.Code) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // A member the roster has removed: their live session at the door is ended |
| 90 | // on that response, and the door says why (the removal, not the |
| 91 | // allowlist's refusal), forgetting a remembered Oficina ID; the chooser |
| 92 | // says nothing of it. Signing in again through Oficina ID remembers |
| 93 | // nothing, and their first page offboards them, told why. |
| 94 | func TestTeamRemovedMemberAtTheDoor(t *testing.T) { |
| 95 | f := newTeamFixture(t) |
| 96 | subject := teamSubject('q') |
| 97 | f.member(subject, "member") |
| 98 | if _, res := f.remembered(); res != lastsignin.Valid { |
| 99 | t.Fatal("a member's Oficina ID sign-in was not remembered") |
| 100 | } |
| 101 | f.dir.Set(subject, "") |
| 102 | f.refreshed() |
| 103 | w := f.get("/signin") |
| 104 | card := door(t, w.Body.String()) |
| 105 | if w.Code != http.StatusOK || !strings.Contains(card, signinRemoved) || strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) { |
| 106 | t.Fatalf("/signin for a removed member = %d\n%s", w.Code, card) |
| 107 | } |
| 108 | if !clears(w, f.app.sessionCookie()) { |
| 109 | t.Error("the removed member's cookie was not cleared on the door's response") |
| 110 | } |
| 111 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 112 | t.Error("the removed member's Oficina ID was kept") |
| 113 | } |
| 114 | if card := door(t, f.get("/signin?err=removed&ways=1").Body.String()); strings.Contains(card, signinRemoved) { |
| 115 | t.Errorf("the chooser drew the removal:\n%s", card) |
| 116 | } |
| 117 | |
| 118 | f.cookies = map[string]*http.Cookie{} |
| 119 | if w := f.signInHome(subject); w.Code != http.StatusSeeOther || f.sessionCookie() == nil { |
| 120 | t.Fatalf("re-sign-in = %d, session %v", w.Code, f.sessionCookie() != nil) |
| 121 | } |
| 122 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 123 | t.Error("a removed member's Oficina ID sign-in was remembered") |
| 124 | } |
| 125 | redirectsTo(t, f.get("/day/2026-11-02"), offboardPath) |
| 126 | if card := door(t, f.get(offboardPath).Body.String()); !strings.Contains(card, signinRemoved) { |
| 127 | t.Errorf("offboard's landing:\n%s", card) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Identity storage that cannot be read in team mode is a 503 at the gate, |
| 132 | // and the door reads that as undecided: no refusal, the session and the |
| 133 | // remembered Oficina ID kept. |
| 134 | func TestTeamUnreadableIdentityIsNoRefusal(t *testing.T) { |
| 135 | f := newTeamFixture(t) |
| 136 | f.member(teamSubject('u'), "member") |
| 137 | if err := f.app.db.G.Exec("ALTER TABLE people RENAME TO people_away").Error; err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | retryable(t, "a page with the people table unreadable", f.get("/calendars")) |
| 141 | w := f.get("/signin") |
| 142 | if strings.Contains(w.Body.String(), signinRefusal) || strings.Contains(w.Body.String(), signinRemoved) { |
| 143 | t.Error("an unreadable identity store was read as a refusal") |
| 144 | } |
| 145 | if f.sessionCookie() == nil { |
| 146 | t.Error("an undecided /signin ended the session") |
| 147 | } |
| 148 | if _, res := f.remembered(); res != lastsignin.Valid { |
| 149 | t.Error("an undecided /signin forgot Oficina ID") |
| 150 | } |
| 151 | } |
| 152 | |