Merge origin/main (bf4e24d, Calls a ready Home consumer) into feat/r2-shared-chrome
Conflicts taken from this branch: connect stays at Home main 7a0f19c (past 01166fc); "All apps" is the estilo switcher's, not a link in the old corner; the layout is estilo's. requireSignedInPage keeps main's homeReturnTo rule in Home mode (every signed-out GET carries return_to) and asks for an HTML Accept standalone. One homeSignout helper (main's).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+137
−11
internal/calls/home_consumer_test.go+118 −0internal/calls/home_router_test.go+13 −10internal/calls/signin_return.go+6 −1
diff --git a/internal/calls/home_consumer_test.go b/internal/calls/home_consumer_test.go| new file mode 100644 |
| index 0000000..02575a9 |
| --- /dev/null |
| +++ b/internal/calls/home_consumer_test.go |
| @@ -0,0 +1,118 @@ |
| +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) |
| + } |
| +} |
diff --git a/internal/calls/home_router_test.go b/internal/calls/home_router_test.go| index 65e45c8..e1feb68 100644 |
| --- a/internal/calls/home_router_test.go |
| +++ b/internal/calls/home_router_test.go |
| @@ -50,11 +50,23 @@ func newHomeRouterFixture(t *testing.T) *homeRouterFixture { |
| if err != nil { |
| t.Fatal(err) |
| } |
| + return newHomeRouterFixtureWith(t, d, logger, key, &connect.Credentials{Issuer: homeTestIssuer, ConsumerID: "calls-test", KeysBase64: homeKeysBase64(key), DEKBase64: base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32))}) |
| +} |
| + |
| +// homeKeysBase64 is the public-key manifest Home publishes for key. |
| +func homeKeysBase64(key *assertioncrypto.Keypair) string { |
| sum := sha256.Sum256(key.SignPub()) |
| id := hex.EncodeToString(sum[:]) |
| raw, _ := json.Marshal(map[string]string{id: base64.StdEncoding.EncodeToString(key.SignPub())}) |
| + return base64.StdEncoding.EncodeToString(raw) |
| +} |
| + |
| +// newHomeRouterFixtureWith builds the app with Home configured by creds, |
| +// whose assertions key signs. |
| +func newHomeRouterFixtureWith(t *testing.T, d *db.DB, logger *slog.Logger, key *assertioncrypto.Keypair, creds *connect.Credentials) *homeRouterFixture { |
| + t.Helper() |
| mail := &homeTestMail{} |
| - mux, a, err := build(t.Context(), d, Config{Origin: homeTestOrigin, InstanceKey: "test-only-instance", DEK: base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{3}, 32)), AllowedAddresses: []string{"alice@example.test"}, Mailer: mail, Logger: logger, Home: &connect.Credentials{Issuer: homeTestIssuer, ConsumerID: "calls-test", KeysBase64: base64.StdEncoding.EncodeToString(raw), DEKBase64: base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32))}}) |
| + mux, a, err := build(t.Context(), d, Config{Origin: homeTestOrigin, InstanceKey: "test-only-instance", DEK: base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{3}, 32)), AllowedAddresses: []string{"alice@example.test"}, Mailer: mail, Logger: logger, Home: creds}) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -96,15 +108,6 @@ func homeRedirect(t *testing.T, w *httptest.ResponseRecorder) string { |
| return w.Header().Get("Location") |
| } |
| -// homeSignout is POST /signout with Home configured: the app session |
| -// ends and the same POST is handed on, as a 307, to id.*'s /signout |
| -// (spec shared-chrome §9.5), which is what ends the id.* session too. |
| -func homeSignout(t *testing.T, w *httptest.ResponseRecorder) { |
| - t.Helper() |
| - if w.Code != 307 || w.Header().Get("Location") != homeTestIssuer+"/signout" { |
| - t.Fatalf("sign-out did not chain to id.*: %d %q", w.Code, w.Header().Get("Location")) |
| - } |
| -} |
| func (f *homeRouterFixture) local(t *testing.T) { |
| t.Helper() |
| homeRedirect(t, f.do("POST", "/signin", "address=alice%40example.test&force=1")) |
diff --git a/internal/calls/signin_return.go b/internal/calls/signin_return.go| index e22b03c..a0faa0c 100644 |
| --- a/internal/calls/signin_return.go |
| +++ b/internal/calls/signin_return.go |
| @@ -168,10 +168,15 @@ func (a *app) rememberedReturn(r *http.Request) (string, bool) { |
| // room's stream, signal, ICE and info fetches, which never ask for HTML — |
| // goes to the guard and gets exactly today's answer, which is what |
| // room.js's readRefusal reads a sign-out from (spec §9.4). |
| +// |
| +// With Home configured every signed-out GET carries return_to, as |
| +// homeReturnTo on main (bf4e24d, #22) already did: a fetch redirected to |
| +// /signin?return_to=… has still gone somewhere else, which is what |
| +// readRefusal reads, so the room's answer is unchanged there too. |
| func (a *app) requireSignedInPage(next http.Handler) http.Handler { |
| guarded := a.auth.RequireSession(next) |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| - if (r.Method == http.MethodGet || r.Method == http.MethodHead) && wantsHTML(r) { |
| + if (r.Method == http.MethodGet || r.Method == http.MethodHead) && (wantsHTML(r) || a.home != nil) { |
| if _, ok := a.auth.SessionFrom(r); !ok { |
| w.Header().Set("Cache-Control", "no-store") |
| http.Redirect(w, r, signinPath+"?return_to="+url.QueryEscape(r.URL.RequestURI()), http.StatusSeeOther) |