oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package idtest
2
3// The admission probe through Equipo's real guard: removal at the door,
4// the refusal on the plain door only, an undecided guard, and sessions the
5// door never judges.
6
7import (
8 "net/http"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/equipo/oficinaid"
14 "amadan.net/rastrillo/rastrillo/pow"
15)
16
17// A removed member's session reaching the door is ended there, on the
18// door's own response, and the door says why; the next load is plainly
19// signed out, with no refusal and no loop. A remembered email way is kept.
20func TestDoorEndsARemovedMembersSession(t *testing.T) {
21 t.Parallel()
22 modes(t, func(t *testing.T, f *fixture, home bool) {
23 const who = "removed@example.test"
24 f.member(t, who)
25 s, record := f.emailRecord(t, who)
26 f.remove(t, who)
27 if w := f.request("GET", "/work", "", nil, s); w.Code != http.StatusForbidden {
28 t.Fatal("the guard admits a removed member", w.Code)
29 }
30 door := f.request("GET", "/signin", "", nil, s, record)
31 body := door.Body.String()
32 if door.Code != http.StatusOK || !strings.Contains(body, refusal) || title(body) != "Problem signing in to Equipo" || !strings.Contains(body, `name="address"`) {
33 t.Fatalf("refused door %d %q", door.Code, title(body))
34 }
35 if _, clears := set(door, sessionCookie); !clears || f.sessionAlive(t, s) {
36 t.Fatal("the refused session outlived the door")
37 }
38 if _, clears := set(door, jarCookie); clears {
39 t.Fatal("the door forgot a remembered email way")
40 }
41 if strings.Contains(body, "rst-shell") || strings.Contains(body, "/signout") {
42 t.Fatal("the refused door is drawn signed in")
43 }
44 again := f.request("GET", "/signin", "", nil, record)
45 if again.Code != http.StatusOK || strings.Contains(again.Body.String(), refusal) {
46 t.Fatal("the refusal outlived the session", again.Code)
47 }
48 })
49}
50
51// The refusal is on the plain door only: the chooser and a way's own page
52// are how a refused person reaches another way in, Oficina ID included.
53// The session is ended there too.
54func TestDoorRefusalLeavesOtherWaysOpen(t *testing.T) {
55 t.Parallel()
56 modes(t, func(t *testing.T, f *fixture, home bool) {
57 f.remove(t, address)
58 for _, target := range []string{"/signin?ways=1", "/signin?way=email"} {
59 s := f.local(t, address)
60 w := f.request("GET", target, "", nil, s)
61 if w.Code != http.StatusOK || strings.Contains(w.Body.String(), refusal) {
62 t.Errorf("%s: %d, or drew the refusal", target, w.Code)
63 }
64 if f.sessionAlive(t, s) {
65 t.Errorf("%s: the refused session lived on", target)
66 }
67 }
68 if home {
69 s := f.local(t, address)
70 w := f.request("GET", "/signin/oficina?return_to=%2Fwork", "", nil, s)
71 if _, clears := set(w, sessionCookie); w.Header().Get("Location") != "/auth/home?return_to=%2Fwork" || !clears {
72 t.Fatal("a refused session at /signin/oficina", w.Header().Get("Location"))
73 }
74 local := f.request("GET", "/signin/local", "", nil, f.local(t, address))
75 if !strings.Contains(local.Body.String(), refusal) {
76 t.Fatal("/signin/local drew no refusal")
77 }
78 }
79 // Anybody can type ?err=refused; it says nothing.
80 if w := f.request("GET", "/signin?err=refused", "", nil); strings.Contains(w.Body.String(), refusal) {
81 t.Fatal("a typed query drew the refusal")
82 }
83 })
84}
85
86// An Oficina ID session whose member was removed: refused, ended, and the
87// remembered Oficina ID forgotten, so the door cannot offer it again.
88func TestDoorForgetsOficinaIDForARefusedSession(t *testing.T) {
89 t.Parallel()
90 f := newFixture(t, true)
91 f.link(t)
92 done := f.oficinaSignin(t)
93 s, record := cookie(t, done, sessionCookie), cookie(t, done, jarCookie)
94 f.remove(t, address)
95 door := f.request("GET", "/signin", "", nil, s, record)
96 if !strings.Contains(door.Body.String(), refusal) {
97 t.Fatal("no refusal", door.Code)
98 }
99 if _, clears := set(door, jarCookie); !clears {
100 t.Fatal("the door kept offering Oficina ID to a refused person")
101 }
102 if _, clears := set(door, sessionCookie); !clears || f.sessionAlive(t, s) {
103 t.Fatal("the refused Oficina ID session lived on")
104 }
105}
106
107// A guard that cannot decide (storage failed) is no refusal: the door is
108// drawn, nothing is ended or forgotten, and the page answers 503.
109func TestDoorUndecidedIsNoRefusal(t *testing.T) {
110 t.Parallel()
111 modes(t, func(t *testing.T, f *fixture, home bool) {
112 sessions := []*http.Cookie{f.local(t, address)}
113 var record *http.Cookie
114 if home {
115 f.link(t)
116 done := f.oficinaSignin(t)
117 sessions = append(sessions, cookie(t, done, sessionCookie))
118 record = cookie(t, done, jarCookie)
119 }
120 f.breakRoster(t)
121 for _, s := range sessions {
122 if w := f.request("GET", "/work", "", nil, s); w.Code != http.StatusServiceUnavailable {
123 t.Errorf("the guard on a storage failure: %d, want 503", w.Code)
124 }
125 door := f.request("GET", "/signin?sent=1", "", nil, s, record)
126 if door.Code != http.StatusOK || strings.Contains(door.Body.String(), refusal) {
127 t.Errorf("undecided door %d, or a refusal", door.Code)
128 }
129 if c, _ := set(door, sessionCookie); c != nil || !f.sessionAlive(t, s) {
130 t.Error("an undecided session was ended")
131 }
132 if c, _ := set(door, jarCookie); c != nil {
133 t.Error("an undecided door touched the remembered way")
134 }
135 }
136 })
137}
138
139// The native apps' sessions are never judged at the door.
140func TestDoorNeverJudgesANativeSession(t *testing.T) {
141 t.Parallel()
142 f := newFixture(t, true)
143 f.remove(t, address)
144 native := f.sessionAs(t, person, oficinaid.NativeMethod)
145 w := f.request("GET", "/signin", "", nil, native)
146 if w.Code != http.StatusOK || strings.Contains(w.Body.String(), refusal) || !f.sessionAlive(t, native) {
147 t.Fatal("the door judged a native session", w.Code)
148 }
149}
150
151// An address that verified but may not sign in (auth's Refused) gets the
152// door with the refusal, in a 403, rather than rastrillo's bare text.
153func TestDoorRefusesAVerifiedStranger(t *testing.T) {
154 t.Parallel()
155 modes(t, func(t *testing.T, f *fixture, home bool) {
156 page := f.request("GET", "/signin?way=email", "", nil)
157 form := fields(page.Body.String(), "/auth/begin")
158 form.Set("address", "stranger@example.test")
159 time.Sleep(pow.DefaultMinAge + 20*time.Millisecond)
160 begin := f.request("POST", "/auth/begin", f.origin, form)
161 jar := mergeCookies(page.Result().Cookies(), begin.Result().Cookies())
162 confirm := f.request("GET", f.mailedLink(t), "", nil, jar...)
163 done := f.request("POST", "/auth/verify", f.origin, fields(confirm.Body.String(), "/auth/verify"), mergeCookies(jar, confirm.Result().Cookies())...)
164 if done.Code != http.StatusForbidden || !strings.Contains(done.Body.String(), refusal) || !strings.Contains(done.Body.String(), "estilo-signin-screen") {
165 t.Fatal("verified stranger", done.Code, done.Body.String())
166 }
167 if c, _ := set(done, sessionCookie); c != nil {
168 t.Fatal("a refused sign-in minted a session")
169 }
170 })
171}
172