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 smart door's memory of an Oficina ID sign-in: written in
4// MintHomeSession, for an admitted session only, with the address of the
5// person who has just signed in, or none.
6
7import (
8 "net/http"
9 "net/http/httptest"
10 "net/url"
11 "strings"
12 "testing"
13 "time"
14
15 "amadan.net/oficina/home/connect"
16 "amadan.net/rastrillo/rastrillo/lastsignin"
17 "amadan.net/rastrillo/rastrillo/sessions"
18)
19
20// signOut signs the browser out, with Home or without.
21func (f *homeRouterFixture) signOut(t *testing.T) {
22 t.Helper()
23 rec := f.do(http.MethodPost, "/signout", "")
24 if rec.Code != http.StatusSeeOther && rec.Code != http.StatusTemporaryRedirect {
25 t.Fatalf("POST /signout = %d", rec.Code)
26 }
27}
28
29// oficinaSignin runs Continue with Oficina ID for subject from path, and
30// returns the exchange's last answer.
31func (f *homeRouterFixture) oficinaSignin(t *testing.T, path, subject string) *httptest.ResponseRecorder {
32 t.Helper()
33 start := f.get(path)
34 if start.Code != http.StatusSeeOther || !strings.HasPrefix(start.Header().Get("Location"), "/auth/start") {
35 t.Fatalf("GET %s: %d %q, want the Home exchange", path, start.Code, start.Header().Get("Location"))
36 }
37 return f.exchange(t, start.Header().Get("Location"), subject)
38}
39
40func TestOficinaIDIsRememberedWithTheResolvedAddress(t *testing.T) {
41 f := newHomeRouterFixture(t)
42 f.link(t)
43 rec, res := f.remembered()
44 if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Provider != lastsignin.ProviderOficina || rec.Address != "alice@example.test" {
45 t.Fatalf("remembered %+v (%v), want Oficina ID for alice", rec, res)
46 }
47 // The next door offers it, never captioned with the address.
48 f.signOut(t)
49 card := door(t, f.get("/signin").Body.String())
50 if !strings.Contains(card, oficinaButton) || strings.Contains(card, "Continue as") {
51 t.Fatalf("the door after an Oficina ID sign-in:\n%s", card)
52 }
53}
54
55// Nothing carries over: the record names who has just signed in, never an
56// address the jar held before.
57func TestOficinaIDCarriesNoAddressOver(t *testing.T) {
58 f := newHomeRouterFixture(t)
59 f.link(t)
60 f.signOut(t)
61 f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "someone@else.test"})
62 homeRedirect(t, f.oficinaSignin(t, "/signin/oficina", "opaque-home-alice"))
63 if rec, _ := f.remembered(); rec.Method != lastsignin.MethodFederated || rec.Address != "alice@example.test" {
64 t.Fatalf("remembered %+v, want alice's own address", rec)
65 }
66}
67
68// An Oficina ID sign-in that mints nothing (nobody here) remembers nothing.
69func TestAnUnknownOficinaSigninRemembersNothing(t *testing.T) {
70 f := newHomeRouterFixture(t)
71 f.oficinaSignin(t, "/signin/oficina", "unknown-home-person")
72 if f.sessionCookie() != nil {
73 t.Fatal("an unknown Oficina identity got a session")
74 }
75 if _, res := f.remembered(); res != lastsignin.Absent {
76 t.Fatalf("an unknown sign-in left a record (%v)", res)
77 }
78}
79
80// rememberOficina judges the session MintHomeSession has just set on the
81// response, strictly, not the one the request arrived with: a refused one
82// is never remembered, an admitted one is.
83func TestRememberOficinaJudgesTheMintedSession(t *testing.T) {
84 f := newHomeRouterFixture(t)
85 f.link(t)
86 f.signOut(t)
87 mint := func() (*httptest.ResponseRecorder, *http.Request) {
88 // The request carries an admitted local session; the response
89 // mints the Home session being remembered.
90 br := f.fresh()
91 br.session(t, "alice@example.test", "magiclink", time.Now())
92 r := br.jarRequest()
93 w := httptest.NewRecorder()
94 if err := f.app.home.sessions.SignIn(w, r, sessions.Session{Subject: "opaque-home-alice", Method: connect.HomeMethod, AuthTime: time.Now()}); err != nil {
95 t.Fatal(err)
96 }
97 return w, r
98 }
99 w, r := mint()
100 f.app.rememberOficina(w, r)
101 if !sets(w, f.app.auth.RememberJar().CookieName()) {
102 t.Fatal("an admitted Home session was not remembered")
103 }
104 f.app.SetOwners([]string{"someone-else@example.test"})
105 w, r = mint()
106 f.app.rememberOficina(w, r)
107 if sets(w, f.app.auth.RememberJar().CookieName()) {
108 t.Fatal("a refused Home session was remembered")
109 }
110 // And the probe ended nothing: the minted session is still there for
111 // its first page to refuse and say why.
112 var minted string
113 for _, c := range w.Result().Cookies() {
114 if c.Name == f.app.sessionCookie() && c.MaxAge >= 0 {
115 minted = c.Value
116 }
117 }
118 if minted == "" || !f.liveSession(minted) {
119 t.Fatal("the strict probe ended the session it was asked about")
120 }
121}
122
123// A linked owner taken off the list who signs in with Oficina ID is not
124// remembered (the loop Fable found in Docs, #139 A2): the exchange still
125// mints, the first page says why, and the door offers nothing to loop on.
126func TestARefusedOficinaSigninIsNotRemembered(t *testing.T) {
127 f := newHomeRouterFixture(t)
128 f.link(t)
129 f.signOut(t)
130 f.cookies = map[string]*http.Cookie{}
131 f.app.SetOwners([]string{"someone-else@example.test"})
132 f.oficinaSignin(t, "/signin/oficina", "opaque-home-alice")
133 if _, res := f.remembered(); res == lastsignin.Valid {
134 t.Fatal("a refused Oficina ID sign-in was remembered")
135 }
136}
137
138// Opening an emailed link (the confirm step) changes nothing remembered.
139func TestTheConfirmStepLeavesTheRecordAlone(t *testing.T) {
140 doorModes(t, func(t *testing.T, f *homeRouterFixture) {
141 f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"})
142 f.mail.body = ""
143 f.do(http.MethodPost, "/auth/begin", url.Values{"address": {"alice@example.test"}, "force": {"1"}}.Encode())
144 link := signinLink.FindString(f.mail.body)
145 if link == "" {
146 t.Fatal("no link")
147 }
148 rec := f.fresh().get(strings.TrimPrefix(link, homeTestOrigin))
149 if rec.Code != http.StatusOK {
150 t.Fatalf("confirm = %d", rec.Code)
151 }
152 for _, c := range rec.Result().Cookies() {
153 if c.Name == f.app.auth.RememberJar().CookieName() {
154 t.Fatal("the confirm step touched the remembered way in")
155 }
156 }
157 })
158}
159