| 1 | package fichastest |
| 2 | |
| 3 | // The refusal, and a session the signed-in routes no longer admit |
| 4 | // (an address taken off FICHAS_OWNERS): ended at the door, refused on the |
| 5 | // plain door only, never judged when it is not a web sign-in, and never |
| 6 | // refused because something could not be read. |
| 7 | |
| 8 | import ( |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/url" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/rastrillo/rastrillo/sessions" |
| 17 | ) |
| 18 | |
| 19 | // A link proved for an address the owners list does not hold gets the |
| 20 | // door with the refusal, in a 403, with the email field: no session, no |
| 21 | // remembered way, and the kept page spent. |
| 22 | func TestARefusedLinkGetsTheRefusal(t *testing.T) { |
| 23 | wide, narrow, m, _ := sharedApps(t) |
| 24 | b := newBrowser() |
| 25 | tok := linkFor(t, wide, b, m, TestOther, url.Values{"return_to": {"/cards/new"}}) |
| 26 | if !b.has(returnCookie) { |
| 27 | t.Fatal("Begin kept no page") |
| 28 | } |
| 29 | rec := b.post(t, narrow, "/auth/verify", url.Values{"token": {tok}}) |
| 30 | if rec.Code != http.StatusForbidden { |
| 31 | t.Fatalf("a refused verify: %d, want 403", rec.Code) |
| 32 | } |
| 33 | body := rec.Body.String() |
| 34 | for _, want := range []string{refusalText, `name="address"`, "<div estilo-signin-screen>", `<div rst-signin-door="one-way">`} { |
| 35 | if !strings.Contains(body, want) { |
| 36 | t.Errorf("the refusal lacks %q:\n%s", want, body) |
| 37 | } |
| 38 | } |
| 39 | if b.has(sessionCookie) { |
| 40 | t.Error("a refused address was given a session") |
| 41 | } |
| 42 | if b.has(rememberName) { |
| 43 | t.Error("a refused address was remembered") |
| 44 | } |
| 45 | if b.has(returnCookie) { |
| 46 | t.Error("a refused landing kept the page: every landing spends it") |
| 47 | } |
| 48 | // Typed into the query, it says nothing: only a proved refusal does. |
| 49 | if strings.Contains(get(t, narrow, "/signin?err=refused").Body.String(), refusalText) { |
| 50 | t.Error("?err=refused typed in draws the refusal") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // A session for an address taken off the owners list: the page refuses |
| 55 | // it (404), and the door ends it there and then, says why on the plain |
| 56 | // door, clears its cookie on the same response, and does not loop. |
| 57 | func TestARefusedSessionIsEndedAtTheDoor(t *testing.T) { |
| 58 | wide, narrow, m, _ := sharedApps(t) |
| 59 | b := newBrowser() |
| 60 | if rec := signIn(t, wide, b, m, TestOther, nil); rec.Code != http.StatusSeeOther { |
| 61 | t.Fatalf("sign in on the wide app: %d", rec.Code) |
| 62 | } |
| 63 | if rec := b.get(t, narrow, "/"); rec.Code != http.StatusNotFound { |
| 64 | t.Fatalf("the narrow app's page answered %d, want its 404 refusal", rec.Code) |
| 65 | } |
| 66 | if !b.has(sessionCookie) { |
| 67 | t.Fatal("the page ended the session itself: removal belongs to the door") |
| 68 | } |
| 69 | old := b.cookies[sessionCookie] |
| 70 | door := b.get(t, narrow, "/signin") |
| 71 | if door.Code != http.StatusOK { |
| 72 | t.Fatalf("GET /signin: %d", door.Code) |
| 73 | } |
| 74 | if !strings.Contains(door.Body.String(), refusalText) || !strings.Contains(door.Body.String(), `name="address"`) { |
| 75 | t.Errorf("the door does not say why, with the field:\n%s", door.Body.String()) |
| 76 | } |
| 77 | if b.has(sessionCookie) { |
| 78 | t.Error("the refused session's cookie was not cleared on the door's response") |
| 79 | } |
| 80 | if door.Header().Get("Cache-Control") != "no-store" { |
| 81 | t.Error("the refusal is cacheable") |
| 82 | } |
| 83 | // Ended, not just forgotten by this browser: the old cookie is dead |
| 84 | // on the app that minted it too. |
| 85 | if rec := getWith(t, wide, "/", []*http.Cookie{{Name: old.Name, Value: old.Value}}); rec.Code != http.StatusSeeOther { |
| 86 | t.Errorf("the ended session still opens the wide app: %d", rec.Code) |
| 87 | } |
| 88 | // The remembered email way is kept: offered again, Begin refuses it. |
| 89 | if !b.has(rememberName) { |
| 90 | t.Error("the refusal forgot the email way") |
| 91 | } |
| 92 | // A reload is plainly signed out: no refusal, no loop. |
| 93 | again := b.get(t, narrow, "/signin") |
| 94 | if again.Code != http.StatusOK || strings.Contains(again.Body.String(), refusalText) { |
| 95 | t.Errorf("reload: %d, refusal drawn again: %v", again.Code, strings.Contains(again.Body.String(), refusalText)) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // The refusal is on the plain door only: the chooser and a way's own page |
| 100 | // are how a refused person reaches another way in. Every state still |
| 101 | // ends the refused session. Sent and a problem of rastrillo's own draw no |
| 102 | // app problem: rastrillo's state wins there (as Calendar found). |
| 103 | func TestTheRefusalIsOnlyOnThePlainDoor(t *testing.T) { |
| 104 | for target, refusal := range map[string]bool{ |
| 105 | "/signin?ways=1": false, |
| 106 | "/signin?way=email": false, |
| 107 | "/signin?sent=1": false, |
| 108 | "/signin?err=expired": false, |
| 109 | "/signin?reauth=1": true, |
| 110 | "/signin?force=1": true, |
| 111 | } { |
| 112 | wide, narrow, m, _ := sharedApps(t) |
| 113 | b := newBrowser() |
| 114 | signIn(t, wide, b, m, TestOther, nil) |
| 115 | rec := b.get(t, narrow, target) |
| 116 | if rec.Code != http.StatusOK { |
| 117 | t.Errorf("%s: %d", target, rec.Code) |
| 118 | } |
| 119 | if got := strings.Contains(rec.Body.String(), refusalText); got != refusal { |
| 120 | t.Errorf("%s: refusal drawn %v, want %v", target, got, refusal) |
| 121 | } |
| 122 | if b.has(sessionCookie) { |
| 123 | t.Errorf("%s: the refused session was not ended", target) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // The door judges web sign-ins only (an emailed link, Keymail). A session |
| 129 | // of any other kind is not the door's to speak for: it is drawn the door |
| 130 | // as anybody signed out, and is never refused or ended. |
| 131 | func TestTheDoorJudgesWebSessionsOnly(t *testing.T) { |
| 132 | _, narrow, _, d := sharedApps(t) |
| 133 | core, err := sessions.New(sessions.Config{DB: d.Writer(), Origin: testOrigin}) |
| 134 | if err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | for _, method := range []string{"home-assertion-native", "passkey", "guest"} { |
| 138 | w := httptest.NewRecorder() |
| 139 | if err := core.SignIn(w, httptest.NewRequest(http.MethodGet, "/", nil), sessions.Session{Subject: TestOther, Method: method, AuthTime: time.Now()}); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | b := newBrowser() |
| 143 | b.keep(w) |
| 144 | rec := b.get(t, narrow, "/signin") |
| 145 | if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), refusalText) { |
| 146 | t.Errorf("%s: %d, refusal %v; want the plain door", method, rec.Code, strings.Contains(rec.Body.String(), refusalText)) |
| 147 | } |
| 148 | if !b.has(sessionCookie) { |
| 149 | t.Errorf("%s: the door ended a session it does not judge", method) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // A session store that cannot be read is no refusal: the door is drawn, |
| 155 | // nothing is refused, remembered or forgotten, and the cookie is kept. |
| 156 | func TestAnUnreadableSessionIsNoRefusal(t *testing.T) { |
| 157 | h, m, d := wiredApp(t) |
| 158 | b := newBrowser() |
| 159 | signIn(t, h, b, m, TestOwner, nil) |
| 160 | if err := d.G.Exec("DROP TABLE sessions").Error; err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | rec := b.get(t, h, "/signin") |
| 164 | if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), refusalText) { |
| 165 | t.Errorf("GET /signin: %d, refusal %v", rec.Code, strings.Contains(rec.Body.String(), refusalText)) |
| 166 | } |
| 167 | if !b.has(sessionCookie) || !b.has(rememberName) { |
| 168 | t.Error("a read failure cleared the session or the remembered way") |
| 169 | } |
| 170 | // The page is not answered as "not here" either. |
| 171 | if page := b.get(t, h, "/"); page.Code == http.StatusNotFound { |
| 172 | t.Error("a read failure was answered as a refusal") |
| 173 | } |
| 174 | } |
| 175 | |