package calls import ( "bytes" "encoding/base64" "io" "log/slog" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" "amadan.net/oficina/home/connect" assertioncrypto "amadan.net/rastrillo/rastrillo/crypto" "amadan.net/rastrillo/rastrillo/db" ) const homeTestDirectory = "https://directory.example.test" // newHomeConsumerFixture configures Home the way production does: from the // environment, through connect.CredentialsFromEnv (cmd/calls/main.go), so // HOME_DIRECTORY_ORIGIN (unprefixed, the name Home itself reads) is read // exactly as it is live. func newHomeConsumerFixture(t *testing.T, directory string) *homeRouterFixture { t.Helper() logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(t.TempDir(), "calls.db"), logger) if err != nil { t.Fatal(err) } t.Cleanup(func() { d.Close() }) key, err := assertioncrypto.Generate() if err != nil { t.Fatal(err) } t.Setenv("CALLS_HOME_ISSUER", homeTestIssuer) t.Setenv("CALLS_HOME_CONSUMER_ID", "calls-test") t.Setenv("CALLS_HOME_KEYS_BASE64", homeKeysBase64(key)) t.Setenv("CALLS_HOME_DEK_BASE64", base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32))) t.Setenv("HOME_DIRECTORY_ORIGIN", directory) return newHomeRouterFixtureWith(t, d, logger, key, connect.CredentialsFromEnv("CALLS")) } // Sign out in Calls ends the id.* session too: the app session ends and // the same POST is handed on (307) to {issuer}/signout, which returns the // browser to /signed-out. Without it the next "Sign in" passed silently // through the live id.* session (spec shared-chrome ยง9.5). func TestHomeSignoutChainsToIssuer(t *testing.T) { f := newHomeConsumerFixture(t, "") f.local(t) w := f.do("POST", "/signout", "") if w.Code != http.StatusTemporaryRedirect || w.Header().Get("Location") != homeTestIssuer+"/signout" { t.Fatalf("POST /signout: status %d Location %q, want 307 to %s/signout", w.Code, w.Header().Get("Location"), homeTestIssuer) } if got := w.Header().Get("Referrer-Policy"); got != "strict-origin-when-cross-origin" { t.Fatalf("Referrer-Policy %q: id.* admits the null-Origin hop by Referer", got) } if f.cookies[f.app.home.sessions.CookieName()] != nil { t.Fatal("sign-out kept the Calls session cookie") } } // A signed-in person who follows a "Sign in" link goes straight to where // they were headed, with no second exchange. func TestHomeSigninWhenSignedInReturnsToPage(t *testing.T) { f := newHomeConsumerFixture(t, "") f.local(t) if got := homeRedirect(t, f.do("GET", "/signin?return_to=%2Fsettings", "")); got != "/settings" { t.Fatalf("signed-in /signin went to %q, want /settings", got) } } // "All apps" goes to the hub directory, from HOME_DIRECTORY_ORIGIN, in the // account corner, and is absent when no directory is configured. func TestAllAppsLinksToDirectory(t *testing.T) { link := `href="` + homeTestDirectory + `/"` f := newHomeConsumerFixture(t, homeTestDirectory) f.local(t) for _, path := range []string{"/", "/settings"} { w := f.do("GET", path, "") if body := w.Body.String(); w.Code != http.StatusOK || !strings.Contains(body, link) || !strings.Contains(body, ">All apps<") { t.Fatalf("GET %s (%d): no All apps link to %s/", path, w.Code, homeTestDirectory) } } bare := newHomeConsumerFixture(t, "") bare.local(t) if body := bare.do("GET", "/", "").Body.String(); strings.Contains(body, "All apps") { t.Fatal("All apps shown with no directory configured") } } // homeSignout asserts the Home-mode sign-out response: a 307 on to the // issuer's /signout, which returns the browser to /signed-out. func homeSignout(t *testing.T, w *httptest.ResponseRecorder) { t.Helper() if w.Code != http.StatusTemporaryRedirect || w.Header().Get("Location") != homeTestIssuer+"/signout" { t.Fatalf("POST /signout: status %d Location %q, want 307 to %s/signout", w.Code, w.Header().Get("Location"), homeTestIssuer) } } // A signed-out visit to a page in Home mode goes to sign-in carrying the // page, and Home sign-in comes back to that exact page, not the root. func TestHomeSigninReturnsToTheExactPage(t *testing.T) { f := newHomeConsumerFixture(t, "") f.link(t) homeSignout(t, f.do("POST", "/signout", "")) signin := homeRedirect(t, f.do("GET", "/settings", "")) if signin != "/signin?return_to=%2Fsettings" { t.Fatalf("signed-out /settings went to %q, want /signin?return_to=%%2Fsettings", signin) } start := homeRedirect(t, f.do("GET", signin, "")) if got := homeRedirect(t, f.exchange(t, start, "opaque-home-alice")); got != "/settings" { t.Fatalf("Home sign-in landed on %q, want /settings", got) } }