Build image / build-and-push (push) Successful in 7s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
100 lines
3.7 KiB
Go
100 lines
3.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func plantingPath(id int64) string { return "/api/v1/plantings/" + strconv.FormatInt(id, 10) }
|
|
func objectPlantingsPath(id int64) string { return objectPath(id) + "/plantings" }
|
|
|
|
func TestPlantingCreatePatchFullDelete(t *testing.T) {
|
|
r := authEngine(t, localCfg())
|
|
cookie := registerAndCookie(t, r, "[email protected]")
|
|
gid := createGardenAPI(t, r, cookie, "Yard")
|
|
|
|
// A plantable bed centered in the garden.
|
|
w := doJSON(t, r, http.MethodPost, objectsPath(gid),
|
|
map[string]any{"kind": "bed", "xCm": 500, "yCm": 500, "widthCm": 200, "heightCm": 200}, cookie)
|
|
oid := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
|
|
|
|
// A seeded built-in plant to place.
|
|
w = doJSON(t, r, http.MethodGet, "/api/v1/plants", nil, cookie)
|
|
plantID, _ := firstBuiltinID(t, w.Body.Bytes())
|
|
|
|
// Place a plop → 201 with a derivedCount.
|
|
w = doJSON(t, r, http.MethodPost, objectPlantingsPath(oid),
|
|
map[string]any{"plantId": plantID, "xCm": 0, "yCm": 0, "radiusCm": 20}, cookie)
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("create planting: status %d, body %s", w.Code, w.Body.String())
|
|
}
|
|
pl := decodeMap(t, w.Body.Bytes())
|
|
pid := int64(pl["id"].(float64))
|
|
dc, ok := pl["derivedCount"].(float64)
|
|
if !ok || dc < 1 {
|
|
t.Errorf("response missing a positive derivedCount: %+v", pl)
|
|
}
|
|
|
|
// /full includes the plop and its referenced plant.
|
|
w = doJSON(t, r, http.MethodGet, fullPath(gid), nil, cookie)
|
|
var full struct {
|
|
Plantings []map[string]any `json:"plantings"`
|
|
Plants []map[string]any `json:"plants"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &full); err != nil {
|
|
t.Fatalf("decode full: %v", err)
|
|
}
|
|
if len(full.Plantings) != 1 || len(full.Plants) != 1 {
|
|
t.Fatalf("full = %d plantings / %d plants, want 1 / 1", len(full.Plantings), len(full.Plants))
|
|
}
|
|
|
|
// Soft-remove (clear-bed style) → it leaves /full.
|
|
w = doJSON(t, r, http.MethodPatch, plantingPath(pid), map[string]any{"removedAt": "2030-06-01", "version": 1}, cookie)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("soft-remove: status %d, body %s", w.Code, w.Body.String())
|
|
}
|
|
w = doJSON(t, r, http.MethodGet, fullPath(gid), nil, cookie)
|
|
full.Plantings = nil
|
|
json.Unmarshal(w.Body.Bytes(), &full)
|
|
if len(full.Plantings) != 0 {
|
|
t.Errorf("removed plop still in /full: %d", len(full.Plantings))
|
|
}
|
|
|
|
// Hard delete → 204.
|
|
if w := doJSON(t, r, http.MethodDelete, plantingPath(pid), nil, cookie); w.Code != http.StatusNoContent {
|
|
t.Errorf("delete planting = %d, want 204", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestPlantingIntoNonPlantableIsRejected(t *testing.T) {
|
|
r := authEngine(t, localCfg())
|
|
cookie := registerAndCookie(t, r, "[email protected]")
|
|
gid := createGardenAPI(t, r, cookie, "Yard")
|
|
|
|
// A tree is not plantable.
|
|
w := doJSON(t, r, http.MethodPost, objectsPath(gid),
|
|
map[string]any{"kind": "tree", "xCm": 500, "yCm": 500, "widthCm": 100, "heightCm": 100}, cookie)
|
|
oid := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
|
|
|
|
w = doJSON(t, r, http.MethodGet, "/api/v1/plants", nil, cookie)
|
|
plantID, _ := firstBuiltinID(t, w.Body.Bytes())
|
|
|
|
w = doJSON(t, r, http.MethodPost, objectPlantingsPath(oid),
|
|
map[string]any{"plantId": plantID, "xCm": 0, "yCm": 0, "radiusCm": 10}, cookie)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("plant into tree = %d, want 400", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestPlantingsRequireAuth(t *testing.T) {
|
|
r := authEngine(t, localCfg())
|
|
if w := doJSON(t, r, http.MethodPost, "/api/v1/objects/1/plantings", map[string]any{"plantId": 1}, nil); w.Code != http.StatusUnauthorized {
|
|
t.Errorf("unauthenticated create = %d, want 401", w.Code)
|
|
}
|
|
if w := doJSON(t, r, http.MethodDelete, "/api/v1/plantings/1", nil, nil); w.Code != http.StatusUnauthorized {
|
|
t.Errorf("unauthenticated delete = %d, want 401", w.Code)
|
|
}
|
|
}
|