| 1 | package memoria |
| 2 | |
| 3 | // Signed out, and forgetting: "Forget me on this device" on the signed-out |
| 4 | // page, chained on to Oficina ID's own Forget with Home. |
| 5 | |
| 6 | import ( |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 14 | ) |
| 15 | |
| 16 | // /signed-out answers in both modes (it once 404'd with Home not |
| 17 | // configured), drawn signed out, with Forget, chained to Oficina ID with |
| 18 | // Home only. |
| 19 | func TestTheSignedOutPageOffersForget(t *testing.T) { |
| 20 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 21 | rec := f.get("/signed-out") |
| 22 | if rec.Code != http.StatusOK || rec.Header().Get("Cache-Control") != "no-store" { |
| 23 | t.Fatalf("GET /signed-out = %d %q", rec.Code, rec.Header().Get("Cache-Control")) |
| 24 | } |
| 25 | body := rec.Body.String() |
| 26 | for _, want := range []string{"You’re signed out", `href="/signin"`, `<form estilo-signin-forget method="post" action="/signin/forget">`, "Forget me on this device", "<title>Signed out · Memoria</title>"} { |
| 27 | if !strings.Contains(body, want) { |
| 28 | t.Errorf("the signed-out page lacks %q", want) |
| 29 | } |
| 30 | } |
| 31 | chained := strings.Contains(body, `name="chain" value="oficina"`) |
| 32 | if chained != (f.app.home != nil) { |
| 33 | t.Errorf("chain=oficina on the signed-out page: %v, want it with Home only", chained) |
| 34 | } |
| 35 | if strings.Contains(body, "estilo-account") || strings.Contains(body, "<footer") { |
| 36 | t.Error("the signed-out page is drawn signed in") |
| 37 | } |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | // Sign out lands there, in both modes. |
| 42 | func TestSignOutLandsOnTheSignedOutPageInBothModes(t *testing.T) { |
| 43 | f := newStandaloneDoor(t) |
| 44 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 45 | redirectsTo(t, f.do(http.MethodPost, "/signout", ""), "/signed-out") |
| 46 | h := newHomeRouterFixture(t) |
| 47 | h.local(t) |
| 48 | w := h.do(http.MethodPost, "/signout", "") |
| 49 | if w.Code != http.StatusTemporaryRedirect || !strings.HasPrefix(w.Header().Get("Location"), homeTestIssuer+"/signout") { |
| 50 | t.Fatalf("Home sign-out = %d %q, want the chain to id.*", w.Code, w.Header().Get("Location")) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // With Home, Forget with chain=oficina forgets here and goes on to Oficina |
| 55 | // ID's Forget, which returns to this origin's signed-out page; without the |
| 56 | // chain (the door's "Use a different email") it stays on the door; a |
| 57 | // cross-site post is refused and chains nowhere. |
| 58 | func TestForgetChainsOnToOficinaID(t *testing.T) { |
| 59 | f := newHomeRouterFixture(t) |
| 60 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 61 | w := f.do(http.MethodPost, signinForgetPath, "chain=oficina") |
| 62 | redirectsTo(t, w, homeTestIssuer+"/forget?return=https%3A%2F%2Fmemoria.example.test%2Fsigned-out") |
| 63 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 64 | t.Fatal("Forget kept the record") |
| 65 | } |
| 66 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 67 | w = f.do(http.MethodPost, signinForgetPath, "way=email") |
| 68 | if loc := w.Header().Get("Location"); w.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/signin") { |
| 69 | t.Fatalf("Use a different email = %d %q, want the door", w.Code, loc) |
| 70 | } |
| 71 | cross := httptest.NewRequest(http.MethodPost, homeTestOrigin+signinForgetPath, strings.NewReader("chain=oficina")) |
| 72 | cross.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 73 | cross.Header.Set("Origin", "https://evil.example") |
| 74 | cross.Header.Set("Sec-Fetch-Site", "cross-site") |
| 75 | if rec := f.send(cross); rec.Code == http.StatusSeeOther && strings.Contains(rec.Header().Get("Location"), homeTestIssuer) { |
| 76 | t.Fatal("a cross-site Forget chained on to Oficina ID") |
| 77 | } |
| 78 | // Standalone there is no Oficina ID to chain to. |
| 79 | s := newStandaloneDoor(t) |
| 80 | if loc := s.do(http.MethodPost, signinForgetPath, "chain=oficina").Header().Get("Location"); strings.Contains(loc, "forget?return") { |
| 81 | t.Fatalf("standalone Forget chained to %q", loc) |
| 82 | } |
| 83 | } |
| 84 | |