oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package connect
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "testing"
7 "time"
8)
9
10func TestIsHomeDerivedIsExactlyTheThreeHomeMethods(t *testing.T) {
11 for method, want := range map[string]bool{
12 HomeMethod: true, NativeMethod: true, NativeWebMethod: true,
13 "magiclink": false, "keymail": false, "guest": false, "": false,
14 "home-assertion-native ": false, "Home-Assertion": false,
15 } {
16 if IsHomeDerived(method) != want {
17 t.Errorf("IsHomeDerived(%q) = %v", method, !want)
18 }
19 }
20}
21
22func TestExpiredIsSevenDaysAbsoluteForNativeSessions(t *testing.T) {
23 now := time.Unix(1_800_000_000, 0)
24 for _, c := range []struct {
25 method string
26 at time.Time
27 want bool
28 }{
29 {NativeMethod, now.Add(-NativeLifetime), false},
30 {NativeMethod, now.Add(-NativeLifetime - time.Second), true},
31 {NativeWebMethod, now.Add(-NativeLifetime - time.Second), true},
32 {NativeMethod, time.Time{}, true},
33 {NativeMethod, now.Add(time.Hour), true},
34 // A browser or local session keeps the session store's own expiry.
35 {HomeMethod, now.Add(-30 * 24 * time.Hour), false},
36 {"magiclink", time.Time{}, false},
37 } {
38 if got := Expired(Session{Subject: "pr_x", Method: c.method, At: c.at}, now); got != c.want {
39 t.Errorf("Expired(%s, at %v) = %v", c.method, now.Sub(c.at), got)
40 }
41 }
42}
43
44// A native session resolves, signs in and holds the same-person rules like a
45// Home one, until seven days after it was minted, however recently used.
46func TestNativeSessionResolvesUntilSevenDays(t *testing.T) {
47 f, h := newHomeOnlyFixture(t)
48 if _, err := h.db.Exec("INSERT INTO home_only(issuer,subject) VALUES(?,?)", bridgeIssuer, "pr_member"); err != nil {
49 t.Fatal(err)
50 }
51 token, err := f.sessions.Mint(Session{Subject: "pr_member", Method: NativeMethod, AuthTime: time.Now().Add(-time.Minute)})
52 if err != nil {
53 t.Fatal(err)
54 }
55 cookie := &http.Cookie{Name: f.b.sessionCookie, Value: token}
56 if p, err := f.b.ResolveSession(bridgeRequest(http.MethodGet, "/", []*http.Cookie{cookie})); err != nil || p.ID <= 1000 {
57 t.Fatalf("native session did not resolve: %+v %v", p, err)
58 }
59 routes := f.routes(t)
60 w := httptest.NewRecorder()
61 routes.SigninOficina(w, bridgeRequest(http.MethodGet, "/signin/oficina?return_to=%2Fd%2Fx", []*http.Cookie{cookie}))
62 if w.Code != 303 || w.Header().Get("Location") != "/d/x" {
63 t.Fatalf("a live native session is not signed in: %d %q", w.Code, w.Header().Get("Location"))
64 }
65 w = httptest.NewRecorder()
66 routes.SigninDoor(w, bridgeRequest(http.MethodGet, "/signin?return_to=%2Fd%2Fx", []*http.Cookie{cookie}))
67 if w.Code != 303 || w.Header().Get("Location") != "/d/x" {
68 t.Fatalf("the door kept a live native session: %d %q", w.Code, w.Header().Get("Location"))
69 }
70
71 s := f.sessions.values[token]
72 s.At = time.Now().Add(-NativeLifetime - time.Minute)
73 f.sessions.values[token] = s
74 if _, err := f.b.ResolveSession(bridgeRequest(http.MethodGet, "/", []*http.Cookie{cookie})); err == nil {
75 t.Fatal("a native session resolved after seven days")
76 }
77 w = httptest.NewRecorder()
78 routes.SigninOficina(w, bridgeRequest(http.MethodGet, "/signin/oficina?return_to=%2Fd%2Fx", []*http.Cookie{cookie}))
79 if w.Header().Get("Location") == "/d/x" {
80 t.Fatal("an expired native session counted as signed in")
81 }
82}
83