Files
pages/internal/server/auth_browser_test.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

55 lines
1.9 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)
}
}