package fichastest // The refusal, and a session the signed-in routes no longer admit // (an address taken off FICHAS_OWNERS): ended at the door, refused on the // plain door only, never judged when it is not a web sign-in, and never // refused because something could not be read. import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/sessions" ) // A link proved for an address the owners list does not hold gets the // door with the refusal, in a 403, with the email field: no session, no // remembered way, and the kept page spent. func TestARefusedLinkGetsTheRefusal(t *testing.T) { wide, narrow, m, _ := sharedApps(t) b := newBrowser() tok := linkFor(t, wide, b, m, TestOther, url.Values{"return_to": {"/cards/new"}}) if !b.has(returnCookie) { t.Fatal("Begin kept no page") } rec := b.post(t, narrow, "/auth/verify", url.Values{"token": {tok}}) if rec.Code != http.StatusForbidden { t.Fatalf("a refused verify: %d, want 403", rec.Code) } body := rec.Body.String() for _, want := range []string{refusalText, `name="address"`, "
", `
`} { if !strings.Contains(body, want) { t.Errorf("the refusal lacks %q:\n%s", want, body) } } if b.has(sessionCookie) { t.Error("a refused address was given a session") } if b.has(rememberName) { t.Error("a refused address was remembered") } if b.has(returnCookie) { t.Error("a refused landing kept the page: every landing spends it") } // Typed into the query, it says nothing: only a proved refusal does. if strings.Contains(get(t, narrow, "/signin?err=refused").Body.String(), refusalText) { t.Error("?err=refused typed in draws the refusal") } } // A session for an address taken off the owners list: the page refuses // it (404), and the door ends it there and then, says why on the plain // door, clears its cookie on the same response, and does not loop. func TestARefusedSessionIsEndedAtTheDoor(t *testing.T) { wide, narrow, m, _ := sharedApps(t) b := newBrowser() if rec := signIn(t, wide, b, m, TestOther, nil); rec.Code != http.StatusSeeOther { t.Fatalf("sign in on the wide app: %d", rec.Code) } if rec := b.get(t, narrow, "/"); rec.Code != http.StatusNotFound { t.Fatalf("the narrow app's page answered %d, want its 404 refusal", rec.Code) } if !b.has(sessionCookie) { t.Fatal("the page ended the session itself: removal belongs to the door") } old := b.cookies[sessionCookie] door := b.get(t, narrow, "/signin") if door.Code != http.StatusOK { t.Fatalf("GET /signin: %d", door.Code) } if !strings.Contains(door.Body.String(), refusalText) || !strings.Contains(door.Body.String(), `name="address"`) { t.Errorf("the door does not say why, with the field:\n%s", door.Body.String()) } if b.has(sessionCookie) { t.Error("the refused session's cookie was not cleared on the door's response") } if door.Header().Get("Cache-Control") != "no-store" { t.Error("the refusal is cacheable") } // Ended, not just forgotten by this browser: the old cookie is dead // on the app that minted it too. if rec := getWith(t, wide, "/", []*http.Cookie{{Name: old.Name, Value: old.Value}}); rec.Code != http.StatusSeeOther { t.Errorf("the ended session still opens the wide app: %d", rec.Code) } // The remembered email way is kept: offered again, Begin refuses it. if !b.has(rememberName) { t.Error("the refusal forgot the email way") } // A reload is plainly signed out: no refusal, no loop. again := b.get(t, narrow, "/signin") if again.Code != http.StatusOK || strings.Contains(again.Body.String(), refusalText) { t.Errorf("reload: %d, refusal drawn again: %v", again.Code, strings.Contains(again.Body.String(), refusalText)) } } // The refusal is on the plain door only: the chooser and a way's own page // are how a refused person reaches another way in. Every state still // ends the refused session. Sent and a problem of rastrillo's own draw no // app problem: rastrillo's state wins there (as Calendar found). func TestTheRefusalIsOnlyOnThePlainDoor(t *testing.T) { for target, refusal := range map[string]bool{ "/signin?ways=1": false, "/signin?way=email": false, "/signin?sent=1": false, "/signin?err=expired": false, "/signin?reauth=1": true, "/signin?force=1": true, } { wide, narrow, m, _ := sharedApps(t) b := newBrowser() signIn(t, wide, b, m, TestOther, nil) rec := b.get(t, narrow, target) if rec.Code != http.StatusOK { t.Errorf("%s: %d", target, rec.Code) } if got := strings.Contains(rec.Body.String(), refusalText); got != refusal { t.Errorf("%s: refusal drawn %v, want %v", target, got, refusal) } if b.has(sessionCookie) { t.Errorf("%s: the refused session was not ended", target) } } } // The door judges web sign-ins only (an emailed link, Keymail). A session // of any other kind is not the door's to speak for: it is drawn the door // as anybody signed out, and is never refused or ended. func TestTheDoorJudgesWebSessionsOnly(t *testing.T) { _, narrow, _, d := sharedApps(t) core, err := sessions.New(sessions.Config{DB: d.Writer(), Origin: testOrigin}) if err != nil { t.Fatal(err) } for _, method := range []string{"home-assertion-native", "passkey", "guest"} { w := httptest.NewRecorder() if err := core.SignIn(w, httptest.NewRequest(http.MethodGet, "/", nil), sessions.Session{Subject: TestOther, Method: method, AuthTime: time.Now()}); err != nil { t.Fatal(err) } b := newBrowser() b.keep(w) rec := b.get(t, narrow, "/signin") if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), refusalText) { t.Errorf("%s: %d, refusal %v; want the plain door", method, rec.Code, strings.Contains(rec.Body.String(), refusalText)) } if !b.has(sessionCookie) { t.Errorf("%s: the door ended a session it does not judge", method) } } } // A session store that cannot be read is no refusal: the door is drawn, // nothing is refused, remembered or forgotten, and the cookie is kept. func TestAnUnreadableSessionIsNoRefusal(t *testing.T) { h, m, d := wiredApp(t) b := newBrowser() signIn(t, h, b, m, TestOwner, nil) if err := d.G.Exec("DROP TABLE sessions").Error; err != nil { t.Fatal(err) } rec := b.get(t, h, "/signin") if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), refusalText) { t.Errorf("GET /signin: %d, refusal %v", rec.Code, strings.Contains(rec.Body.String(), refusalText)) } if !b.has(sessionCookie) || !b.has(rememberName) { t.Error("a read failure cleared the session or the remembered way") } // The page is not answered as "not here" either. if page := b.get(t, h, "/"); page.Code == http.StatusNotFound { t.Error("a read failure was answered as a refusal") } }