package calls // The smart door's memory of an Oficina ID sign-in: written in // MintHomeSession, for an admitted session only, with the address of the // person who has just signed in, or none. import ( "net/http" "net/http/httptest" "strings" "testing" "time" "amadan.net/oficina/home/connect" "amadan.net/rastrillo/rastrillo/lastsignin" "amadan.net/rastrillo/rastrillo/sessions" ) func TestOficinaIDIsRememberedWithTheResolvedAddress(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) rec, res := f.remembered() if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Provider != lastsignin.ProviderOficina || rec.Address != "alice@example.test" { t.Fatalf("remembered %+v (%v), want Oficina ID for alice", rec, res) } // The next door offers it, never captioned with the address. homeSignout(t, f.do(http.MethodPost, "/signout", "")) card := door(t, f.get("/signin").Body.String()) if !strings.Contains(card, oficinaButton) || strings.Contains(card, "Continue as") { t.Fatalf("the door after an Oficina ID sign-in:\n%s", card) } } // Nothing carries over: the record names who has just signed in, never an // address the jar held before. func TestOficinaIDCarriesNoAddressOver(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) homeSignout(t, f.do(http.MethodPost, "/signout", "")) f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "someone@else.test"}) homeRedirect(t, f.oficinaSignin(t, "/signin/oficina", "opaque-home-alice")) if rec, _ := f.remembered(); rec.Method != lastsignin.MethodFederated || rec.Address != "alice@example.test" { t.Fatalf("remembered %+v, want alice's own address", rec) } } // An Oficina ID sign-in that mints nothing (nobody here) remembers nothing. func TestAnUnknownOficinaSigninRemembersNothing(t *testing.T) { f := newHomeRouterFixture(t) if w := f.oficinaSignin(t, "/signin/oficina", "unknown-home-person"); w.Code != http.StatusForbidden { t.Fatalf("unknown = %d", w.Code) } if _, res := f.remembered(); res != lastsignin.Absent { t.Fatalf("an unknown sign-in left a record (%v)", res) } } // rememberOficina judges the session MintHomeSession has just set on the // response, strictly, not the one the request arrived with: a refused one // is never remembered, an admitted one is. func TestRememberOficinaJudgesTheMintedSession(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) homeSignout(t, f.do(http.MethodPost, "/signout", "")) mint := func() (*httptest.ResponseRecorder, *http.Request) { // The request carries an admitted local session; the response // mints the Home session being remembered. br := f.fresh() br.session(t, "alice@example.test", "magiclink", time.Now()) r := br.jarRequest() w := httptest.NewRecorder() if err := f.app.home.sessions.SignIn(w, r, sessions.Session{Subject: "opaque-home-alice", Method: connect.HomeMethod, AuthTime: time.Now()}); err != nil { t.Fatal(err) } return w, r } w, r := mint() f.app.rememberOficina(w, r) if !sets(w, f.app.auth.RememberJar().CookieName()) { t.Fatal("an admitted Home session was not remembered") } f.app.cfg.allow = allowlist{} w, r = mint() f.app.rememberOficina(w, r) if sets(w, f.app.auth.RememberJar().CookieName()) { t.Fatal("a refused Home session was remembered") } // And the probe ended nothing: the minted session is still there for // its first page to refuse and say why. var minted string for _, c := range w.Result().Cookies() { if c.Name == f.app.sessionCookie() && c.MaxAge >= 0 { minted = c.Value } } if minted == "" || !f.liveSession(minted) { t.Fatal("the strict probe ended the session it was asked about") } } // Opening an emailed link (the confirm step) changes nothing remembered. func TestTheConfirmStepLeavesTheRecordAlone(t *testing.T) { doorModes(t, func(t *testing.T, f *homeRouterFixture) { f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}) f.mail.body = "" f.do(http.MethodPost, "/signin", "address=alice%40example.test") link := signinLink.FindString(f.mailBody()) if link == "" { t.Fatal("no link") } rec := f.fresh().get(strings.TrimPrefix(link, homeTestOrigin)) if rec.Code != http.StatusOK { t.Fatalf("confirm = %d", rec.Code) } for _, c := range rec.Result().Cookies() { if c.Name == f.app.auth.RememberJar().CookieName() { t.Fatal("the confirm step touched the remembered way in") } } }) }