| 1 | package calls |
| 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 | |
| 7 | import ( |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/home/connect" |
| 15 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 16 | "amadan.net/rastrillo/rastrillo/sessions" |
| 17 | ) |
| 18 | |
| 19 | func TestOficinaIDIsRememberedWithTheResolvedAddress(t *testing.T) { |
| 20 | f := newHomeRouterFixture(t) |
| 21 | f.link(t) |
| 22 | rec, res := f.remembered() |
| 23 | if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Provider != lastsignin.ProviderOficina || rec.Address != "alice@example.test" { |
| 24 | t.Fatalf("remembered %+v (%v), want Oficina ID for alice", rec, res) |
| 25 | } |
| 26 | // The next door offers it, never captioned with the address. |
| 27 | homeSignout(t, f.do(http.MethodPost, "/signout", "")) |
| 28 | card := door(t, f.get("/signin").Body.String()) |
| 29 | if !strings.Contains(card, oficinaButton) || strings.Contains(card, "Continue as") { |
| 30 | t.Fatalf("the door after an Oficina ID sign-in:\n%s", card) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Nothing carries over: the record names who has just signed in, never an |
| 35 | // address the jar held before. |
| 36 | func TestOficinaIDCarriesNoAddressOver(t *testing.T) { |
| 37 | f := newHomeRouterFixture(t) |
| 38 | f.link(t) |
| 39 | homeSignout(t, f.do(http.MethodPost, "/signout", "")) |
| 40 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "someone@else.test"}) |
| 41 | homeRedirect(t, f.oficinaSignin(t, "/signin/oficina", "opaque-home-alice")) |
| 42 | if rec, _ := f.remembered(); rec.Method != lastsignin.MethodFederated || rec.Address != "alice@example.test" { |
| 43 | t.Fatalf("remembered %+v, want alice's own address", rec) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // An Oficina ID sign-in that mints nothing (nobody here) remembers nothing. |
| 48 | func TestAnUnknownOficinaSigninRemembersNothing(t *testing.T) { |
| 49 | f := newHomeRouterFixture(t) |
| 50 | if w := f.oficinaSignin(t, "/signin/oficina", "unknown-home-person"); w.Code != http.StatusForbidden { |
| 51 | t.Fatalf("unknown = %d", w.Code) |
| 52 | } |
| 53 | if _, res := f.remembered(); res != lastsignin.Absent { |
| 54 | t.Fatalf("an unknown sign-in left a record (%v)", res) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // rememberOficina judges the session MintHomeSession has just set on the |
| 59 | // response, strictly, not the one the request arrived with: a refused one |
| 60 | // is never remembered, an admitted one is. |
| 61 | func TestRememberOficinaJudgesTheMintedSession(t *testing.T) { |
| 62 | f := newHomeRouterFixture(t) |
| 63 | f.link(t) |
| 64 | homeSignout(t, f.do(http.MethodPost, "/signout", "")) |
| 65 | mint := func() (*httptest.ResponseRecorder, *http.Request) { |
| 66 | // The request carries an admitted local session; the response |
| 67 | // mints the Home session being remembered. |
| 68 | br := f.fresh() |
| 69 | br.session(t, "alice@example.test", "magiclink", time.Now()) |
| 70 | r := br.jarRequest() |
| 71 | w := httptest.NewRecorder() |
| 72 | if err := f.app.home.sessions.SignIn(w, r, sessions.Session{Subject: "opaque-home-alice", Method: connect.HomeMethod, AuthTime: time.Now()}); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | return w, r |
| 76 | } |
| 77 | w, r := mint() |
| 78 | f.app.rememberOficina(w, r) |
| 79 | if !sets(w, f.app.auth.RememberJar().CookieName()) { |
| 80 | t.Fatal("an admitted Home session was not remembered") |
| 81 | } |
| 82 | f.app.cfg.allow = allowlist{} |
| 83 | w, r = mint() |
| 84 | f.app.rememberOficina(w, r) |
| 85 | if sets(w, f.app.auth.RememberJar().CookieName()) { |
| 86 | t.Fatal("a refused Home session was remembered") |
| 87 | } |
| 88 | // And the probe ended nothing: the minted session is still there for |
| 89 | // its first page to refuse and say why. |
| 90 | var minted string |
| 91 | for _, c := range w.Result().Cookies() { |
| 92 | if c.Name == f.app.sessionCookie() && c.MaxAge >= 0 { |
| 93 | minted = c.Value |
| 94 | } |
| 95 | } |
| 96 | if minted == "" || !f.liveSession(minted) { |
| 97 | t.Fatal("the strict probe ended the session it was asked about") |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Opening an emailed link (the confirm step) changes nothing remembered. |
| 102 | func TestTheConfirmStepLeavesTheRecordAlone(t *testing.T) { |
| 103 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 104 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}) |
| 105 | f.mail.body = "" |
| 106 | f.do(http.MethodPost, "/signin", "address=alice%40example.test") |
| 107 | link := signinLink.FindString(f.mailBody()) |
| 108 | if link == "" { |
| 109 | t.Fatal("no link") |
| 110 | } |
| 111 | rec := f.fresh().get(strings.TrimPrefix(link, homeTestOrigin)) |
| 112 | if rec.Code != http.StatusOK { |
| 113 | t.Fatalf("confirm = %d", rec.Code) |
| 114 | } |
| 115 | for _, c := range rec.Result().Cookies() { |
| 116 | if c.Name == f.app.auth.RememberJar().CookieName() { |
| 117 | t.Fatal("the confirm step touched the remembered way in") |
| 118 | } |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | |