Copy a garden (deep duplicate) from the gardens list
Adds POST /gardens/:id/copy: duplicates a garden the actor owns, along
with its objects and their currently-planted plops, into a new garden
owned by the actor. A blank name derives "<source> (copy)".
Deliberately not carried over:
* public_token — the share link is a capability granted to the
original; a copy must not silently inherit a live public URL
* garden_shares — a copy is private to whoever made it
* removed plantings — the copy is a fresh layout, not a history
Owner-only for now: a copy's plops keep pointing at the SOURCE's
plant_ids, so letting a viewer copy someone else's garden would hand
them plants outside their catalog and pin the original owner's plants
against deletion (plantings.plant_id is ON DELETE RESTRICT). Copying a
shared garden needs a plant-cloning policy first.
The whole copy runs in one transaction, so a failure part-way leaves no
half-populated garden. The object/planting list scans are factored into
queryObjects/queryPlantings so the same code serves a plain read and a
read inside that transaction.
UI: a Copy action on the owner's garden card opens a modal prefilled
with the derived name, then lands in the new garden.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
@@ -181,3 +181,69 @@ func TestGardenUpdateRejectsBadVersion(t *testing.T) {
|
||||
func gardenPath(id int64) string {
|
||||
return "/api/v1/gardens/" + strconv.FormatInt(id, 10)
|
||||
}
|
||||
|
||||
func TestCopyGardenEndpoint(t *testing.T) {
|
||||
r := authEngine(t, localCfg())
|
||||
cookie := registerAndCookie(t, r, "[email protected]")
|
||||
|
||||
w := doJSON(t, r, http.MethodPost, "/api/v1/gardens",
|
||||
map[string]any{"name": "Home", "widthCm": 400, "heightCm": 400}, cookie)
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("create status = %d, body %s", w.Code, w.Body.String())
|
||||
}
|
||||
src := decodeMap(t, w.Body.Bytes())
|
||||
id := int64(src["id"].(float64))
|
||||
path := "/api/v1/gardens/" + strconv.FormatInt(id, 10) + "/copy"
|
||||
|
||||
// No body at all → 201 with the derived name.
|
||||
w = doJSON(t, r, http.MethodPost, path, nil, cookie)
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("copy status = %d, body %s", w.Code, w.Body.String())
|
||||
}
|
||||
dup := decodeMap(t, w.Body.Bytes())
|
||||
if dup["name"] != "Home (copy)" {
|
||||
t.Errorf("name = %v, want 'Home (copy)'", dup["name"])
|
||||
}
|
||||
if int64(dup["id"].(float64)) == id {
|
||||
t.Error("copy reused the source id")
|
||||
}
|
||||
if dup["myRole"] != "owner" {
|
||||
t.Errorf("myRole = %v, want owner", dup["myRole"])
|
||||
}
|
||||
|
||||
// An explicit name is honored.
|
||||
w = doJSON(t, r, http.MethodPost, path, map[string]any{"name": "Plan B"}, cookie)
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("named copy status = %d, body %s", w.Code, w.Body.String())
|
||||
}
|
||||
if got := decodeMap(t, w.Body.Bytes())["name"]; got != "Plan B" {
|
||||
t.Errorf("name = %v, want 'Plan B'", got)
|
||||
}
|
||||
|
||||
// Both copies plus the source show up in the list.
|
||||
w = doJSON(t, r, http.MethodGet, "/api/v1/gardens", nil, cookie)
|
||||
var list []map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &list); err != nil {
|
||||
t.Fatalf("decode list: %v", err)
|
||||
}
|
||||
if len(list) != 3 {
|
||||
t.Errorf("list has %d gardens, want 3", len(list))
|
||||
}
|
||||
|
||||
// Unauthenticated → 401; someone else's garden → 404.
|
||||
if w := doJSON(t, r, http.MethodPost, path, nil, nil); w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("anon copy status = %d, want 401", w.Code)
|
||||
}
|
||||
other := registerAndCookie(t, r, "[email protected]")
|
||||
if w := doJSON(t, r, http.MethodPost, path, nil, other); w.Code != http.StatusNotFound {
|
||||
t.Errorf("stranger copy status = %d, want 404", w.Code)
|
||||
}
|
||||
|
||||
// A non-numeric id is a 400, and a malformed body is rejected.
|
||||
if w := doJSON(t, r, http.MethodPost, "/api/v1/gardens/not-a-number/copy", nil, cookie); w.Code != http.StatusBadRequest {
|
||||
t.Errorf("bad id status = %d, want 400", w.Code)
|
||||
}
|
||||
if w := doJSON(t, r, http.MethodPost, path, map[string]any{"name": 42}, cookie); w.Code != http.StatusBadRequest {
|
||||
t.Errorf("bad body status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user