RPGTheory/aeternalis-portal had diverged from this repo (the git/CI/CD copy) with an in-progress control portal feature that was never synced back here, so none of it had gone through CI or been deployed: - New site/game/control routing split: index.html becomes a world directory landing page (site.ts), game.html is the renamed former index.html (main.ts), control.html is a new admin panel (control.ts, portal-shared.ts, portal.css). - Server: new internal/store/portal.go (character/world registry, command queue, admin grants) and internal/transport/control.go (portal API routes), plus a new internal/gitops package that lets the portal provision new worlds via a GitOps repo commit. - game.World gains BornAt/DiedAt/DeathReason and an operator Control() path (close/open admissions, awaken boss, end world, per-character rename/heal/unstuck/retire); death no longer reuses the same character id on rejoin, matching the "new life" permadeath model in Plan.md rather than the old bypass-prevention test's expectations. - deploy/: new control-egress network policy and portal/control values (admin role, GitOps repo/branch/values file/CIDRs). - New e2e coverage: tests/portal.spec.ts, tools/capture-world.mjs. Verified this repo's own independent commits (800ms movement-staleness window, 20s boss-attack poll, SRS/TLS deploy values, ubuntu-22.04 runner pin, DOCKER_HOST fix) were already present in the portal copy before merging, so nothing here regresses them. `tsc --noEmit`, the vite client build, and the protocol contract check all pass; no go.mod changes. Could not run `go build`/`go test` locally (no Go toolchain on PATH, Docker daemon not running) — every new/changed Go symbol was manually cross-referenced and resolves, but this should still be built and tested in CI before merging to master. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
118 lines
4.0 KiB
Go
118 lines
4.0 KiB
Go
package transport
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
"aeternalis/server/internal/store"
|
|
)
|
|
|
|
func TestControlAuthorizationAndExecution(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, err := store.Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "control.db")))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
b, _ := content.Load()
|
|
w := game.New("control-test", 1, time.Now(), b)
|
|
lease, _, err := db.Acquire(ctx, w)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
svc := NewService(w, lease)
|
|
auth, _ := NewAuth(ctx, db, true, "http://127.0.0.1", "", "", "")
|
|
assets := t.TempDir()
|
|
for _, f := range []string{"index.html", "game.html", "control.html"} {
|
|
if err = os.WriteFile(filepath.Join(assets, f), []byte("<!doctype html><title>"+f+"</title>"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
server := httptest.NewServer(NewHTTP(svc, auth, assets, nil).Handler())
|
|
defer server.Close()
|
|
auth.base = server.URL
|
|
request := func(method, path, body, token, origin string) int {
|
|
t.Helper()
|
|
r, _ := http.NewRequest(method, server.URL+path, strings.NewReader(body))
|
|
if token != "" {
|
|
r.AddCookie(&http.Cookie{Name: auth.cookieName(), Value: token})
|
|
}
|
|
r.Header.Set("Origin", origin)
|
|
response, err := server.Client().Do(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer response.Body.Close()
|
|
return response.StatusCode
|
|
}
|
|
for _, path := range []string{"/", "/game", "/control", "/worlds/control-test/api/config"} {
|
|
if status := request("GET", path, "", "", ""); status != 200 {
|
|
t.Fatal(path, status)
|
|
}
|
|
}
|
|
if status := request("GET", "/worlds/another/api/config", "", "", ""); status != 404 {
|
|
t.Fatal("wrong world routed", status)
|
|
}
|
|
if request("GET", "/api/control", "", "", "") != 401 {
|
|
t.Fatal("anonymous admin access")
|
|
}
|
|
token := game.ID() + game.ID()
|
|
_ = db.CreateSession(ctx, hash(token), "operator")
|
|
if request("GET", "/api/control", "", token, "") != 403 {
|
|
t.Fatal("player has admin access")
|
|
}
|
|
_ = db.GrantAdmin(ctx, hash(token), 600)
|
|
if request("GET", "/api/control", "", token, "") != 200 {
|
|
t.Fatal("admin denied")
|
|
}
|
|
body := `{"id":"` + game.ID() + `","worldId":"control-test","op":"close-admissions","reason":"maintenance window"}`
|
|
if request("POST", "/api/control/commands", body, token, "https://evil.example") != 403 {
|
|
t.Fatal("cross-origin admin command")
|
|
}
|
|
if request("POST", "/api/control/commands", body, token, server.URL) != 202 {
|
|
t.Fatal("command not queued")
|
|
}
|
|
if err = svc.controls(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !w.AdmissionsClosed {
|
|
t.Fatal("command not applied by world owner")
|
|
}
|
|
commands, _ := db.Commands(ctx, w.ID, false)
|
|
if len(commands) != 1 || commands[0].Status != "completed" {
|
|
t.Fatal("command not audited")
|
|
}
|
|
bad := `{"id":"` + game.ID() + `","worldId":"control-test","op":"end-world","reason":"maintenance window"}`
|
|
if request("POST", "/api/control/commands", bad, token, server.URL) != 400 {
|
|
t.Fatal("destructive action without confirmation")
|
|
}
|
|
_ = db.DeleteSession(ctx, hash(token))
|
|
if request("GET", "/api/control", "", token, "") != 401 {
|
|
t.Fatal("revoked admin session accepted")
|
|
}
|
|
}
|
|
func TestKeycloakRolesAndReturnURL(t *testing.T) {
|
|
var claims roleClaims
|
|
_ = json.Unmarshal([]byte(`{"realm_access":{"roles":["player"]},"resource_access":{"game":{"roles":["aeternalis-admin"]},"other":{"roles":["foreign-admin"]}}}`), &claims)
|
|
if !claims.admin("aeternalis-admin", "game") || claims.admin("foreign-admin", "game") || claims.admin("", "game") {
|
|
t.Fatal("role scope validation")
|
|
}
|
|
for _, raw := range []string{"https://evil.example", "//evil.example/game", "/\\evil", "/game#fragment", "/control?next=https://evil"} {
|
|
if result := safeReturn(raw); result != "/" && result != "/control" {
|
|
t.Fatal("unsafe return", raw, result)
|
|
}
|
|
}
|
|
if safeReturn("/game?world=world-002&evil=value") != "/game?world=world-002" {
|
|
t.Fatal("game return lost or untrusted query retained")
|
|
}
|
|
}
|