oficina / memoria Public

Clone
git clone https://amadan.net/oficina/memoria

Plain git — no account needed to clone.

Download

Download this file

1package memoria
2
3// The door in team mode: connect's member choice (connect.New requires
4// Admit with HomeOnly/Unlinked), a removed member at the door, and the
5// roster's answers the door must not read as refusals.
6
7import (
8 "net/http"
9 "strings"
10 "testing"
11
12 "amadan.net/oficina/home/connect"
13 "amadan.net/rastrillo/rastrillo/lastsignin"
14)
15
16// A roster member with no account here is asked to choose, in connect's
17// own English; "I'm new here" signs them in as a Home-only person, who is
18// remembered with no address.
19func TestTeamAMemberChoosesNewAndIsRememberedWithNoAddress(t *testing.T) {
20 f := newTeamFixture(t)
21 subject := teamSubject('n')
22 f.dir.Set(subject, "member")
23 f.refreshed()
24 f.cookies = map[string]*http.Cookie{}
25 redirectsTo(t, f.signInChoosing(subject), connect.ChoosePath)
26 page := f.page(connect.ChoosePath).Body.String()
27 for _, want := range []string{"Welcome to the team", "Do you already have an account here that signs in with email?", "Connect my existing account", "I’m new here — continue with Oficina ID"} {
28 if !strings.Contains(page, want) {
29 t.Errorf("the choose page lacks %q", want)
30 }
31 }
32 if w := f.choose("new"); w.Code != http.StatusSeeOther {
33 t.Fatalf("I'm new here = %d", w.Code)
34 }
35 if w := f.page("/"); w.Code != http.StatusOK {
36 t.Fatalf("the new member's front page = %d", w.Code)
37 }
38 rec, res := f.remembered()
39 if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Address != "" {
40 t.Fatalf("remembered %+v (%v), want Oficina ID with no address", rec, res)
41 }
42}
43
44// "Connect my existing account" goes to the email-only door.
45func TestTeamAMemberChoosesConnect(t *testing.T) {
46 f := newTeamFixture(t, "carol@example.test")
47 subject := teamSubject('o')
48 f.dir.Set(subject, "member")
49 f.refreshed()
50 f.cookies = map[string]*http.Cookie{}
51 redirectsTo(t, f.signInChoosing(subject), connect.ChoosePath)
52 w := f.choose("connect")
53 if w.Code != http.StatusSeeOther || !strings.HasPrefix(w.Header().Get("Location"), connect.SigninLocalPath) {
54 t.Fatalf("connect = %d %q, want the email-only door", w.Code, w.Header().Get("Location"))
55 }
56 if card := door(t, f.get(w.Header().Get("Location")).Body.String()); strings.Contains(card, oficinaButton) {
57 t.Fatalf("the connect door offers Oficina ID:\n%s", card)
58 }
59}
60
61// No proof, no choice: the choose page without a live choice is "failed",
62// with the way on to connecting an account, in a 403.
63func TestTeamTheChoosePageWithNoChoiceFails(t *testing.T) {
64 f := newTeamFixture(t)
65 rec := f.page(connect.ChoosePath)
66 if rec.Code != http.StatusForbidden || strings.Contains(rec.Body.String(), "Welcome to the team") {
67 t.Fatalf("the choose page with no choice = %d", rec.Code)
68 }
69}
70
71// A stranger to the roster is offered no choice, and remembered nothing.
72func TestTeamAStrangerGetsNoChoice(t *testing.T) {
73 f := newTeamFixture(t)
74 f.cookies = map[string]*http.Cookie{}
75 if w := f.signInChoosing(teamSubject('s')); w.Code != http.StatusForbidden {
76 t.Fatalf("a stranger's sign-in = %d %q, want 403", w.Code, w.Header().Get("Location"))
77 }
78 if _, res := f.remembered(); res != lastsignin.Absent {
79 t.Fatal("a stranger was remembered")
80 }
81 if rec := f.get("/signin"); rec.Code != http.StatusOK {
82 t.Fatalf("/signin after a stranger's sign-in = %d", rec.Code)
83 }
84}
85
86// A removed member's session at the door: the removal, the session ended
87// on the door's own response, Oficina ID forgotten. Signing in again
88// through Oficina ID remembers nothing (the strict probe refuses the
89// minted session, and ends nothing: the first page does, and says why).
90func TestTeamARemovedMemberAtTheDoor(t *testing.T) {
91 f := newTeamFixture(t)
92 subject := teamSubject('q')
93 f.member(subject, "member")
94 if _, res := f.remembered(); res != lastsignin.Valid {
95 t.Fatal("the member's Oficina ID sign-in was not remembered")
96 }
97 f.dir.Set(subject, "")
98 f.refreshed()
99 rec := f.get("/signin")
100 if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), signinRemoved) {
101 t.Fatalf("/signin for a removed member = %d", rec.Code)
102 }
103 if !clears(rec, f.app.sessionCookie()) || f.cookies[f.app.sessionCookie()] != nil {
104 t.Fatal("the door kept the removed member's session")
105 }
106 if _, res := f.remembered(); res == lastsignin.Valid {
107 t.Fatal("the door kept the removed member's Oficina ID")
108 }
109 if w := f.signIn(subject); w.Code != http.StatusSeeOther {
110 t.Fatalf("a removed member's sign-in = %d", w.Code)
111 }
112 if _, res := f.remembered(); res == lastsignin.Valid {
113 t.Fatal("a removed member's sign-in was remembered")
114 }
115 if w := f.page("/"); w.Code != http.StatusSeeOther || !strings.Contains(w.Header().Get("Location"), "err=removed") {
116 t.Fatalf("the removed member's first page = %d %q", w.Code, w.Header().Get("Location"))
117 }
118}
119
120// An unlinked local account the owners list admits is no refusal: /signin
121// sends it on, and the page sends it to connect its Oficina account.
122func TestTeamAnUnlinkedLocalAccountIsNoRefusal(t *testing.T) {
123 f := newTeamFixture(t, "carol@example.test")
124 f.localSession("carol@example.test")
125 redirectsTo(t, f.get("/signin"), "/")
126 if f.cookies[f.app.sessionCookie()] == nil {
127 t.Fatal("the door ended an unlinked local account's session")
128 }
129 if w := f.page("/"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/account/home" {
130 t.Fatalf("the unlinked account's front page = %d %q, want /account/home", w.Code, w.Header().Get("Location"))
131 }
132}
133
134// The Home-only identity storage unreadable is a 503, never a refusal at
135// the door, and the session and its remembered way in are kept.
136func TestTeamAnUndecidedDoorRefusesNothing(t *testing.T) {
137 f := newTeamFixture(t)
138 f.member(teamSubject('u'), "member")
139 if _, err := f.app.db.Writer().Exec("ALTER TABLE memoria_team_home_members RENAME TO unavailable_home_members"); err != nil {
140 t.Fatal(err)
141 }
142 if w := f.page("/documents/1"); w.Code != http.StatusServiceUnavailable {
143 t.Fatalf("an item page with identity storage down = %d, want 503", w.Code)
144 }
145 rec := f.get("/signin")
146 if strings.Contains(rec.Body.String(), signinRemoved) || strings.Contains(rec.Body.String(), signinRefusal) || len(rec.Result().Cookies()) != 0 {
147 t.Fatalf("the door for an undecided member = %d, cookies %v", rec.Code, rec.Result().Cookies())
148 }
149 if _, res := f.remembered(); res != lastsignin.Valid {
150 t.Fatal("the door forgot an undecided member's Oficina ID")
151 }
152 if f.cookies[f.app.sessionCookie()] == nil {
153 t.Fatal("the door ended an undecided member's session")
154 }
155}
156