Copy a garden (deep duplicate) from the gardens list (#46)
Build image / build-and-push (push) Successful in 16s

Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #46.
This commit is contained in:
2026-07-21 03:58:39 +00:00
committed by steve
parent e74fb308c1
commit e22bdd6cab
15 changed files with 752 additions and 52 deletions
+1
View File
@@ -81,6 +81,7 @@ func New(cfg *config.Config, svc *service.Service) *gin.Engine {
gardens.GET("/:id", h.getGarden)
gardens.PATCH("/:id", h.updateGarden)
gardens.DELETE("/:id", h.deleteGarden)
gardens.POST("/:id/copy", h.copyGarden) // duplicate a garden the actor owns
gardens.GET("/:id/full", h.getGardenFull) // one-shot editor load
gardens.POST("/:id/objects", h.createObject)
+29
View File
@@ -2,6 +2,7 @@ package api
import (
"errors"
"io"
"net/http"
"github.com/gin-gonic/gin"
@@ -103,6 +104,34 @@ func (h *handlers) updateGarden(c *gin.Context) {
c.JSON(http.StatusOK, g)
}
// gardenCopyRequest is the (optional) body of POST /gardens/:id/copy. A blank or
// absent name lets the service derive "<source> (copy)".
type gardenCopyRequest struct {
Name string `json:"name"`
}
func (h *handlers) copyGarden(c *gin.Context) {
id, ok := parseIDParam(c, "id")
if !ok {
return
}
// The body is optional — POST with no body means "copy under the default
// name". An absent body surfaces as io.EOF from the decoder, which is the
// reliable signal: Content-Length is -1 (not 0) for a chunked request, so
// testing the length would misread an empty chunked body as malformed.
var req gardenCopyRequest
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "name must be a string")
return
}
g, err := h.svc.CopyGarden(c.Request.Context(), mustActor(c).ID, id, req.Name)
if err != nil {
writeServiceError(c, err)
return
}
c.JSON(http.StatusCreated, g)
}
func (h *handlers) deleteGarden(c *gin.Context) {
id, ok := parseIDParam(c, "id")
if !ok {
+98
View File
@@ -2,8 +2,11 @@ package api
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/gin-gonic/gin"
@@ -181,3 +184,98 @@ 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)
}
}
// A body of unknown length (chunked transfer, Content-Length -1) that turns out
// to be empty must take the default-name path, not fail as malformed.
func TestCopyGardenEmptyChunkedBody(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"}, cookie)
id := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
// A plain io.Reader (not a *strings.Reader/*bytes.Buffer) leaves
// ContentLength at -1, which is what a chunked request looks like server-side.
req := httptest.NewRequest(http.MethodPost,
"/api/v1/gardens/"+strconv.FormatInt(id, 10)+"/copy", io.NopCloser(strings.NewReader("")))
req.Header.Set("Content-Type", "application/json")
req.AddCookie(cookie)
if req.ContentLength != -1 {
t.Fatalf("test setup: ContentLength = %d, want -1 (unknown)", req.ContentLength)
}
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code != http.StatusCreated {
t.Fatalf("status = %d, want 201; body %s", rec.Code, rec.Body.String())
}
if got := decodeMap(t, rec.Body.Bytes())["name"]; got != "Home (copy)" {
t.Errorf("name = %v, want the derived 'Home (copy)'", got)
}
}