Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"testing"
|
|
"time"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
func TestAuthSessionLifetimeIsDomainScopedAndFixedAtIssuance(t *testing.T) {
|
|
database, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
database.SetMaxOpenConns(1)
|
|
t.Cleanup(func() { database.Close() })
|
|
if err = db.Migrate(database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := NewWithDocumentRoot(database, t.TempDir()+"/documents")
|
|
if err = s.EnsureAuthSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.EnsureConfigTable(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := s.BootstrapOwner("domain", "admin", "a long test password", "Admin", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.AuthSessionLifetimeMinutes("domain") != 1440 {
|
|
t.Fatal("default changed")
|
|
}
|
|
for _, minutes := range []int{-1, 0, 4, 43201} {
|
|
if s.SetAuthSessionLifetimeMinutes("domain", minutes) == nil {
|
|
t.Fatalf("accepted %d", minutes)
|
|
}
|
|
}
|
|
if err = s.SetAuthSessionLifetimeMinutes("domain", 60); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.AuthSessionLifetimeMinutes("other") != 1440 {
|
|
t.Fatal("policy leaked across Domains")
|
|
}
|
|
for _, browser := range []bool{false, true} {
|
|
var token string
|
|
if browser {
|
|
token, err = s.CreateBrowserAuthSession("domain", p.UUID, 0)
|
|
} else {
|
|
token, err = s.CreateAuthSession("domain", p.UUID)
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var duration int64
|
|
if err = database.QueryRow(`SELECT expires_at_ms-auth_time_ms FROM auth_sessions ORDER BY rowid DESC LIMIT 1`).Scan(&duration); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if duration != time.Hour.Milliseconds() {
|
|
t.Fatalf("lifetime %d", duration)
|
|
}
|
|
if _, err = s.PrincipalForSession("domain", token); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err = s.SetAuthSessionLifetimeMinutes("domain", 5); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var changed int
|
|
if err = database.QueryRow(`SELECT count(*) FROM auth_sessions WHERE expires_at_ms-auth_time_ms<>?`, time.Hour.Milliseconds()).Scan(&changed); err != nil || changed != 0 {
|
|
t.Fatalf("existing sessions changed: %d %v", changed, err)
|
|
}
|
|
token, err := s.CreateAuthSession("domain", p.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = database.Exec(`UPDATE auth_sessions SET expires_at_ms=?`, time.Now().Add(-time.Second).UnixMilli()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.PrincipalForSession("domain", token); err == nil {
|
|
t.Fatal("expired token authenticated")
|
|
}
|
|
}
|