Files
pages/internal/server/auth_browser_test.go
Anya Tokugawa b90aef79d0
Build and publish container images / images (push) Successful in 11m37s
Release container images / release (push) Successful in 22s
feat: add browser upload queue and trusted sessions with bounded projections
2026-11-08 20:27:04 +09:00

59 lines
2.2 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestBrowserCookieLoginLogoutChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, apiToken, domain := configFixture(t)
s.mux.HandleFunc("/_/auth/", s.handleAuth)
script, err := os.ReadFile("internal/server/testdata/auth-browser.js")
if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Real browser cookies and Fetch Metadata: do not inject credentials or
// rewrite Origin/Host. localhost maps to this fixture's default Domain.
if r.Method == "GET" && (strings.HasPrefix(r.URL.Path, "/_/auth/") || r.URL.Path == "/-/sys/config") {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for key, values := range rec.Header() {
w.Header()[key] = values
}
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
target := strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + "/_/auth/login?return=%2F-%2Fsys%2Fconfig%3Fpanel%3Dauth"
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", target, t.TempDir())
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("cookie browser flow: %v %s", err, out)
}
if _, err := s.store.PrincipalForSession(domain, apiToken); err != nil {
t.Fatalf("browser logout revoked another API session: %v", err)
}
var revoked int
if err := s.store.DB.QueryRow(`SELECT count(*) FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&revoked); err != nil || revoked != 1 {
t.Fatalf("browser revocations=%d %v", revoked, err)
}
var lifetime int64
if err := s.store.DB.QueryRow(`SELECT expires_at_ms-auth_time_ms FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&lifetime); err != nil || lifetime != (30*24*time.Hour).Milliseconds() {
t.Fatalf("trusted browser lifetime=%d %v", lifetime, err)
}
}