package connect import ( "net/http" "net/http/httptest" "testing" "time" ) func TestIsHomeDerivedIsExactlyTheThreeHomeMethods(t *testing.T) { for method, want := range map[string]bool{ HomeMethod: true, NativeMethod: true, NativeWebMethod: true, "magiclink": false, "keymail": false, "guest": false, "": false, "home-assertion-native ": false, "Home-Assertion": false, } { if IsHomeDerived(method) != want { t.Errorf("IsHomeDerived(%q) = %v", method, !want) } } } func TestExpiredIsSevenDaysAbsoluteForNativeSessions(t *testing.T) { now := time.Unix(1_800_000_000, 0) for _, c := range []struct { method string at time.Time want bool }{ {NativeMethod, now.Add(-NativeLifetime), false}, {NativeMethod, now.Add(-NativeLifetime - time.Second), true}, {NativeWebMethod, now.Add(-NativeLifetime - time.Second), true}, {NativeMethod, time.Time{}, true}, {NativeMethod, now.Add(time.Hour), true}, // A browser or local session keeps the session store's own expiry. {HomeMethod, now.Add(-30 * 24 * time.Hour), false}, {"magiclink", time.Time{}, false}, } { if got := Expired(Session{Subject: "pr_x", Method: c.method, At: c.at}, now); got != c.want { t.Errorf("Expired(%s, at %v) = %v", c.method, now.Sub(c.at), got) } } } // A native session resolves, signs in and holds the same-person rules like a // Home one, until seven days after it was minted, however recently used. func TestNativeSessionResolvesUntilSevenDays(t *testing.T) { f, h := newHomeOnlyFixture(t) if _, err := h.db.Exec("INSERT INTO home_only(issuer,subject) VALUES(?,?)", bridgeIssuer, "pr_member"); err != nil { t.Fatal(err) } token, err := f.sessions.Mint(Session{Subject: "pr_member", Method: NativeMethod, AuthTime: time.Now().Add(-time.Minute)}) if err != nil { t.Fatal(err) } cookie := &http.Cookie{Name: f.b.sessionCookie, Value: token} if p, err := f.b.ResolveSession(bridgeRequest(http.MethodGet, "/", []*http.Cookie{cookie})); err != nil || p.ID <= 1000 { t.Fatalf("native session did not resolve: %+v %v", p, err) } routes := f.routes(t) w := httptest.NewRecorder() routes.SigninOficina(w, bridgeRequest(http.MethodGet, "/signin/oficina?return_to=%2Fd%2Fx", []*http.Cookie{cookie})) if w.Code != 303 || w.Header().Get("Location") != "/d/x" { t.Fatalf("a live native session is not signed in: %d %q", w.Code, w.Header().Get("Location")) } w = httptest.NewRecorder() routes.SigninDoor(w, bridgeRequest(http.MethodGet, "/signin?return_to=%2Fd%2Fx", []*http.Cookie{cookie})) if w.Code != 303 || w.Header().Get("Location") != "/d/x" { t.Fatalf("the door kept a live native session: %d %q", w.Code, w.Header().Get("Location")) } s := f.sessions.values[token] s.At = time.Now().Add(-NativeLifetime - time.Minute) f.sessions.values[token] = s if _, err := f.b.ResolveSession(bridgeRequest(http.MethodGet, "/", []*http.Cookie{cookie})); err == nil { t.Fatal("a native session resolved after seven days") } w = httptest.NewRecorder() routes.SigninOficina(w, bridgeRequest(http.MethodGet, "/signin/oficina?return_to=%2Fd%2Fx", []*http.Cookie{cookie})) if w.Header().Get("Location") == "/d/x" { t.Fatal("an expired native session counted as signed in") } }