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(""+f+""), 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") } }