Copy a garden (deep duplicate) from the gardens list (#46)
Build image / build-and-push (push) Successful in 16s
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:
@@ -6,6 +6,7 @@ import (
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
|
||||
)
|
||||
@@ -249,3 +250,281 @@ func TestDeleteGarden(t *testing.T) {
|
||||
t.Errorf("re-delete err = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyGardenDuplicatesContents(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
|
||||
src, err := s.CreateGarden(ctx, owner, GardenInput{
|
||||
Name: "Home", WidthCM: 732, HeightCM: 732, UnitPref: domain.UnitImperial,
|
||||
Notes: "the real one", GridSizeCM: 30, SnapToGrid: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateGarden: %v", err)
|
||||
}
|
||||
bed := seedBed(t, s, owner, src.ID)
|
||||
plant := seedOwnPlant(t, s, owner, 20)
|
||||
plop, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, XCM: 10, YCM: -20, RadiusCM: 15})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePlanting: %v", err)
|
||||
}
|
||||
|
||||
dup, err := s.CopyGarden(ctx, owner, src.ID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CopyGarden: %v", err)
|
||||
}
|
||||
|
||||
if dup.ID == src.ID {
|
||||
t.Fatal("copy reused the source id")
|
||||
}
|
||||
if dup.Name != "Home (copy)" {
|
||||
t.Errorf("name = %q, want 'Home (copy)'", dup.Name)
|
||||
}
|
||||
if dup.OwnerID != owner {
|
||||
t.Errorf("owner = %d, want %d", dup.OwnerID, owner)
|
||||
}
|
||||
if dup.Version != 1 {
|
||||
t.Errorf("version = %d, want a fresh 1", dup.Version)
|
||||
}
|
||||
if dup.WidthCM != src.WidthCM || dup.HeightCM != src.HeightCM ||
|
||||
dup.UnitPref != src.UnitPref || dup.Notes != src.Notes ||
|
||||
dup.GridSizeCM != src.GridSizeCM || dup.SnapToGrid != src.SnapToGrid {
|
||||
t.Errorf("settings not carried over: %+v vs source %+v", dup, src)
|
||||
}
|
||||
|
||||
full, err := s.GardenFull(ctx, owner, dup.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GardenFull(copy): %v", err)
|
||||
}
|
||||
if len(full.Objects) != 1 {
|
||||
t.Fatalf("copy has %d objects, want 1", len(full.Objects))
|
||||
}
|
||||
got := full.Objects[0]
|
||||
if got.ID == bed.ID {
|
||||
t.Error("copied object reused the source object id")
|
||||
}
|
||||
if got.GardenID != dup.ID {
|
||||
t.Errorf("copied object garden = %d, want the copy %d", got.GardenID, dup.ID)
|
||||
}
|
||||
if got.Kind != bed.Kind || got.XCM != bed.XCM || got.YCM != bed.YCM ||
|
||||
got.WidthCM != bed.WidthCM || got.HeightCM != bed.HeightCM || got.Plantable != bed.Plantable {
|
||||
t.Errorf("copied object differs: %+v vs source %+v", got, bed)
|
||||
}
|
||||
|
||||
if len(full.Plantings) != 1 {
|
||||
t.Fatalf("copy has %d plantings, want 1", len(full.Plantings))
|
||||
}
|
||||
gotPlop := full.Plantings[0]
|
||||
if gotPlop.ID == plop.ID {
|
||||
t.Error("copied planting reused the source planting id")
|
||||
}
|
||||
if gotPlop.ObjectID != got.ID {
|
||||
t.Errorf("copied planting parent = %d, want the copied object %d", gotPlop.ObjectID, got.ID)
|
||||
}
|
||||
if gotPlop.PlantID != plop.PlantID || gotPlop.XCM != plop.XCM || gotPlop.YCM != plop.YCM ||
|
||||
gotPlop.RadiusCM != plop.RadiusCM {
|
||||
t.Errorf("copied planting differs: %+v vs source %+v", gotPlop, plop)
|
||||
}
|
||||
|
||||
// The source is untouched by the copy.
|
||||
srcFull, err := s.GardenFull(ctx, owner, src.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GardenFull(source): %v", err)
|
||||
}
|
||||
if len(srcFull.Objects) != 1 || len(srcFull.Plantings) != 1 {
|
||||
t.Errorf("source changed: %d objects, %d plantings", len(srcFull.Objects), len(srcFull.Plantings))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyGardenSkipsRemovedPlantings(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
src := seedGarden(t, s, owner)
|
||||
bed := seedBed(t, s, owner, src.ID)
|
||||
plant := seedOwnPlant(t, s, owner, 20)
|
||||
|
||||
if _, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, RadiusCM: 10}); err != nil {
|
||||
t.Fatalf("CreatePlanting(first): %v", err)
|
||||
}
|
||||
if _, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, XCM: 50, RadiusCM: 10}); err != nil {
|
||||
t.Fatalf("CreatePlanting(cleared): %v", err)
|
||||
}
|
||||
// Clear the bed, then re-plant one plop: the copy should carry the active plop
|
||||
// only, not the soft-removed history.
|
||||
if _, err := s.ClearObject(ctx, owner, bed.ID); err != nil {
|
||||
t.Fatalf("ClearObject: %v", err)
|
||||
}
|
||||
replanted, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, XCM: 25, RadiusCM: 10})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePlanting(replanted): %v", err)
|
||||
}
|
||||
|
||||
dup, err := s.CopyGarden(ctx, owner, src.ID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CopyGarden: %v", err)
|
||||
}
|
||||
full, err := s.GardenFull(ctx, owner, dup.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GardenFull: %v", err)
|
||||
}
|
||||
if len(full.Plantings) != 1 {
|
||||
t.Fatalf("copy has %d plantings, want only the 1 active one", len(full.Plantings))
|
||||
}
|
||||
if full.Plantings[0].XCM != replanted.XCM {
|
||||
t.Errorf("copied plop x = %v, want the active %v", full.Plantings[0].XCM, replanted.XCM)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyGardenNaming(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
g, err := s.CreateGarden(ctx, owner, GardenInput{Name: "Yard"})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateGarden: %v", err)
|
||||
}
|
||||
|
||||
// An explicit name wins, and is trimmed like any other garden name.
|
||||
dup, err := s.CopyGarden(ctx, owner, g.ID, " Experiment ")
|
||||
if err != nil {
|
||||
t.Fatalf("CopyGarden: %v", err)
|
||||
}
|
||||
if dup.Name != "Experiment" {
|
||||
t.Errorf("name = %q, want trimmed 'Experiment'", dup.Name)
|
||||
}
|
||||
|
||||
// An over-long explicit name is rejected rather than silently truncated.
|
||||
if _, err := s.CopyGarden(ctx, owner, g.ID, strings.Repeat("a", maxGardenNameLen+1)); !errors.Is(err, domain.ErrInvalidInput) {
|
||||
t.Errorf("long name err = %v, want ErrInvalidInput", err)
|
||||
}
|
||||
|
||||
// A source already at the cap still yields a valid derived name.
|
||||
long, err := s.CreateGarden(ctx, owner, GardenInput{Name: strings.Repeat("b", maxGardenNameLen)})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateGarden(long): %v", err)
|
||||
}
|
||||
dupLong, err := s.CopyGarden(ctx, owner, long.ID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CopyGarden(long): %v", err)
|
||||
}
|
||||
if len(dupLong.Name) > maxGardenNameLen {
|
||||
t.Errorf("derived name is %d bytes, over the %d cap", len(dupLong.Name), maxGardenNameLen)
|
||||
}
|
||||
if !strings.HasSuffix(dupLong.Name, " (copy)") {
|
||||
t.Errorf("derived name = %q, want a ' (copy)' suffix", dupLong.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyGardenNameRuneBoundary(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
|
||||
// A name at the cap made of multi-byte runes: truncating for the suffix must
|
||||
// not split one and leave invalid UTF-8.
|
||||
name := strings.Repeat("é", maxGardenNameLen/2) // 2 bytes each == the cap
|
||||
g, err := s.CreateGarden(ctx, owner, GardenInput{Name: name})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateGarden: %v", err)
|
||||
}
|
||||
dup, err := s.CopyGarden(ctx, owner, g.ID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CopyGarden: %v", err)
|
||||
}
|
||||
if !utf8.ValidString(dup.Name) {
|
||||
t.Errorf("derived name %q is not valid UTF-8", dup.Name)
|
||||
}
|
||||
if len(dup.Name) > maxGardenNameLen {
|
||||
t.Errorf("derived name is %d bytes, over the %d cap", len(dup.Name), maxGardenNameLen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyGardenPermissions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestService(t, openConfig())
|
||||
alice := seedUser(t, s, "[email protected]")
|
||||
bob := seedUser(t, s, "[email protected]")
|
||||
g := seedGarden(t, s, alice)
|
||||
|
||||
// A stranger can't see the garden at all, let alone copy it.
|
||||
if _, err := s.CopyGarden(ctx, bob, g.ID, ""); !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Errorf("stranger copy err = %v, want ErrNotFound", err)
|
||||
}
|
||||
|
||||
// An editor may edit contents but not duplicate the garden (see CopyGarden's
|
||||
// note on cross-owner plant references).
|
||||
if _, err := s.AddShare(ctx, alice, g.ID, "[email protected]", domain.RoleEditor); err != nil {
|
||||
t.Fatalf("AddShare: %v", err)
|
||||
}
|
||||
if _, err := s.CopyGarden(ctx, bob, g.ID, ""); !errors.Is(err, domain.ErrForbidden) {
|
||||
t.Errorf("editor copy err = %v, want ErrForbidden", err)
|
||||
}
|
||||
|
||||
// A missing garden is ErrNotFound.
|
||||
if _, err := s.CopyGarden(ctx, alice, g.ID+9999, ""); !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Errorf("missing garden err = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyGardenDoesNotInheritPublicLink(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
g := seedGarden(t, s, owner)
|
||||
|
||||
link, err := s.EnablePublicShareLink(ctx, owner, g.ID, false)
|
||||
if err != nil {
|
||||
t.Fatalf("EnablePublicShareLink: %v", err)
|
||||
}
|
||||
if link.Token == "" {
|
||||
t.Fatal("no token issued")
|
||||
}
|
||||
|
||||
dup, err := s.CopyGarden(ctx, owner, g.ID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CopyGarden: %v", err)
|
||||
}
|
||||
|
||||
// The copy must start private: no token of its own...
|
||||
dupLink, err := s.GetPublicShareLink(ctx, owner, dup.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPublicShareLink(copy): %v", err)
|
||||
}
|
||||
if dupLink.Token != "" {
|
||||
t.Errorf("copy inherited a public token %q", dupLink.Token)
|
||||
}
|
||||
// ...and the source's token still resolves to the source, not the copy.
|
||||
pub, err := s.PublicGarden(ctx, link.Token)
|
||||
if err != nil {
|
||||
t.Fatalf("PublicGarden: %v", err)
|
||||
}
|
||||
if pub.Garden.ID != g.ID {
|
||||
t.Errorf("source token resolves to garden %d, want the source %d", pub.Garden.ID, g.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyGardenDoesNotInheritShares(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := newTestService(t, openConfig())
|
||||
alice := seedUser(t, s, "[email protected]")
|
||||
bob := seedUser(t, s, "[email protected]")
|
||||
g := seedGarden(t, s, alice)
|
||||
if _, err := s.AddShare(ctx, alice, g.ID, "[email protected]", domain.RoleEditor); err != nil {
|
||||
t.Fatalf("AddShare: %v", err)
|
||||
}
|
||||
|
||||
dup, err := s.CopyGarden(ctx, alice, g.ID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CopyGarden: %v", err)
|
||||
}
|
||||
|
||||
// Bob keeps his access to the original but gets none to the copy.
|
||||
if _, err := s.GetGarden(ctx, bob, g.ID); err != nil {
|
||||
t.Errorf("bob lost access to the source: %v", err)
|
||||
}
|
||||
if _, err := s.GetGarden(ctx, bob, dup.ID); !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Errorf("bob can see the copy: err = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user