Add reusable content sets publication previews and focused input handling

This commit is contained in:
dingo
2026-09-08 13:02:20 -04:00
parent 34ab668531
commit 0c8b695483
20 changed files with 1030 additions and 149 deletions
+40 -32
View File
@@ -29,38 +29,46 @@ func (h *HTTP) catalogRoutes(mux *http.ServeMux) {
}
respond(w, 200, map[string]any{"catalog": catalog, "changes": changes})
})
mux.HandleFunc("POST /api/control/catalog", func(w http.ResponseWriter, r *http.Request) {
actor, ok := h.admin(w, r)
if !ok {
return
}
var change store.CatalogChange
r.Body = http.MaxBytesReader(w, r.Body, 32768)
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&change); err != nil {
fail(w, 400, "invalid catalog change")
return
}
var extra any
if err := decoder.Decode(&extra); err != io.EOF {
fail(w, 400, "invalid catalog change")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
result, err := h.auth.db.PublishCatalog(ctx, actor, change)
switch {
case errors.Is(err, store.ErrCatalogConflict), errors.Is(err, store.ErrCatalogRequest):
fail(w, 409, err.Error())
case errors.Is(err, store.ErrCatalogInvalid):
fail(w, 400, err.Error())
case err != nil:
fail(w, 503, "catalog publication unavailable")
default:
respond(w, 200, result)
}
})
for _, path := range []string{"/api/control/catalog", "/api/control/catalog/preview"} {
mux.HandleFunc("POST "+path, func(w http.ResponseWriter, r *http.Request) {
actor, ok := h.admin(w, r)
if !ok {
return
}
var change store.CatalogChange
r.Body = http.MaxBytesReader(w, r.Body, 32768)
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&change); err != nil {
fail(w, 400, "invalid catalog change")
return
}
var extra any
if err := decoder.Decode(&extra); err != io.EOF {
fail(w, 400, "invalid catalog change")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
var result any
var err error
if r.URL.Path == "/api/control/catalog/preview" {
result, err = h.auth.db.PreviewCatalog(ctx, change)
} else {
result, err = h.auth.db.PublishCatalog(ctx, actor, change)
}
switch {
case errors.Is(err, store.ErrCatalogConflict), errors.Is(err, store.ErrCatalogRequest):
fail(w, 409, err.Error())
case errors.Is(err, store.ErrCatalogInvalid):
fail(w, 400, err.Error())
case err != nil:
fail(w, 503, "catalog publication unavailable")
default:
respond(w, 200, result)
}
})
}
}
// Caller holds s.mu. Checkpoint a catalog before it can reach a snapshot.
+23
View File
@@ -65,15 +65,38 @@ func TestControlAuthorizationAndExecution(t *testing.T) {
if request("GET", "/api/control", "", "", "") != 401 {
t.Fatal("anonymous admin access")
}
if request("POST", "/api/control/catalog/preview", "{}", "", server.URL) != 401 {
t.Fatal("anonymous preview 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")
}
if request("POST", "/api/control/catalog/preview", "{}", token, server.URL) != 403 {
t.Fatal("player preview access")
}
_ = db.GrantAdmin(ctx, hash(token), 600)
if request("GET", "/api/control", "", token, "") != 200 {
t.Fatal("admin denied")
}
previewBody := `{"id":"` + game.ID() + `","expectedVersion":1,"op":"create-effect-set","reason":"Preview effects","effectSet":{"id":"test_effect","name":"Test effect","description":"","modifiers":[{"stat":"strength","flat":2,"percent":0}]}}`
if request("POST", "/api/control/catalog/preview", previewBody, token, "https://evil.example") != 403 {
t.Fatal("cross-origin preview")
}
if request("POST", "/api/control/catalog/preview", previewBody, token, server.URL) != 200 {
t.Fatal("valid preview rejected")
}
if request("POST", "/api/control/catalog/preview", strings.Replace(previewBody, `"flat":2`, `"flat":2,"script":"bad"`, 1), token, server.URL) != 400 {
t.Fatal("unknown nested field accepted")
}
if request("POST", "/api/control/catalog/preview", previewBody+` {}`, token, server.URL) != 400 {
t.Fatal("trailing JSON accepted")
}
previewCatalog, err := db.Catalog(ctx)
if err != nil || previewCatalog.Version != 1 || len(previewCatalog.EffectSets) != 0 {
t.Fatal("preview changed catalog", err)
}
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")