commit
This commit is contained in:
@@ -12,7 +12,9 @@ import (
|
||||
"shared-album/internal/auth"
|
||||
"shared-album/internal/members"
|
||||
"shared-album/internal/models"
|
||||
"shared-album/internal/photos"
|
||||
"shared-album/internal/sharelinks"
|
||||
"shared-album/internal/sse"
|
||||
)
|
||||
|
||||
func testPool(t *testing.T) *pgxpool.Pool {
|
||||
@@ -187,6 +189,96 @@ func TestMemberRepository(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlbumCover(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
defer pool.Close()
|
||||
ctx := context.Background()
|
||||
|
||||
owner := newUser(t, ctx, pool, "Owner")
|
||||
t.Cleanup(func() { _, _ = pool.Exec(ctx, `DELETE FROM users WHERE id = $1`, owner.ID) })
|
||||
|
||||
albumStore := albums.NewStore(pool)
|
||||
photosStore := photos.NewStore(pool)
|
||||
|
||||
a := &models.Album{ID: auth.NewID(), OwnerID: owner.ID, Title: "A"}
|
||||
if err := albumStore.Create(ctx, a); err != nil {
|
||||
t.Fatalf("create album: %v", err)
|
||||
}
|
||||
|
||||
p1 := &models.Photo{ID: auth.NewID(), AlbumID: a.ID, UploaderID: owner.ID, StorageKey: "k1", Width: 1, Height: 1}
|
||||
p2 := &models.Photo{ID: auth.NewID(), AlbumID: a.ID, UploaderID: owner.ID, StorageKey: "k2", Width: 1, Height: 1}
|
||||
for _, p := range []*models.Photo{p1, p2} {
|
||||
if err := photosStore.Create(ctx, p); err != nil {
|
||||
t.Fatalf("create photo: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
photosStore.SetCover(ctx, a.ID, p1.ID)
|
||||
got, _ := albumStore.GetByID(ctx, a.ID)
|
||||
if got.CoverPhotoID == nil || *got.CoverPhotoID != p1.ID {
|
||||
t.Fatalf("auto-cover not set: %v", got.CoverPhotoID)
|
||||
}
|
||||
|
||||
photosStore.SetCover(ctx, a.ID, p2.ID)
|
||||
got, _ = albumStore.GetByID(ctx, a.ID)
|
||||
if got.CoverPhotoID == nil || *got.CoverPhotoID != p1.ID {
|
||||
t.Fatalf("auto-cover overwrote existing cover: %v", got.CoverPhotoID)
|
||||
}
|
||||
|
||||
photosStore.ClearCover(ctx, a.ID, p2.ID)
|
||||
got, _ = albumStore.GetByID(ctx, a.ID)
|
||||
if got.CoverPhotoID == nil {
|
||||
t.Fatalf("clear with non-cover photo should be a no-op")
|
||||
}
|
||||
photosStore.ClearCover(ctx, a.ID, p1.ID)
|
||||
got, _ = albumStore.GetByID(ctx, a.ID)
|
||||
if got.CoverPhotoID != nil {
|
||||
t.Fatalf("cover not cleared: %v", got.CoverPhotoID)
|
||||
}
|
||||
|
||||
if ok, err := photosStore.PhotoInAlbum(ctx, p1.ID, a.ID); err != nil || !ok {
|
||||
t.Fatalf("photo should belong to album: ok=%v err=%v", ok, err)
|
||||
}
|
||||
|
||||
other := &models.Album{ID: auth.NewID(), OwnerID: owner.ID, Title: "B"}
|
||||
if err := albumStore.Create(ctx, other); err != nil {
|
||||
t.Fatalf("create other album: %v", err)
|
||||
}
|
||||
pForeign := &models.Photo{ID: auth.NewID(), AlbumID: other.ID, UploaderID: owner.ID, StorageKey: "k3", Width: 1, Height: 1}
|
||||
if err := photosStore.Create(ctx, pForeign); err != nil {
|
||||
t.Fatalf("create foreign photo: %v", err)
|
||||
}
|
||||
if ok, _ := photosStore.PhotoInAlbum(ctx, pForeign.ID, a.ID); ok {
|
||||
t.Fatalf("foreign photo should not belong to album")
|
||||
}
|
||||
|
||||
svc := albums.NewService(albumStore, members.NewStore(pool), sse.NewHub(16), nil, photosStore)
|
||||
|
||||
if _, err := svc.Update(ctx, a.ID, owner.ID, albums.UpdateInput{CoverPhotoID: &pForeign.ID}); err == nil {
|
||||
t.Fatalf("expected error when setting foreign photo as cover")
|
||||
}
|
||||
got, _ = albumStore.GetByID(ctx, a.ID)
|
||||
if got.CoverPhotoID != nil {
|
||||
t.Fatalf("foreign cover should not be applied: %v", got.CoverPhotoID)
|
||||
}
|
||||
|
||||
if _, err := svc.Update(ctx, a.ID, owner.ID, albums.UpdateInput{CoverPhotoID: &p2.ID}); err != nil {
|
||||
t.Fatalf("valid cover update: %v", err)
|
||||
}
|
||||
got, _ = albumStore.GetByID(ctx, a.ID)
|
||||
if got.CoverPhotoID == nil || *got.CoverPhotoID != p2.ID {
|
||||
t.Fatalf("valid cover not applied: %v", got.CoverPhotoID)
|
||||
}
|
||||
|
||||
if _, err := svc.Update(ctx, a.ID, owner.ID, albums.UpdateInput{ClearCover: true}); err != nil {
|
||||
t.Fatalf("clear cover via service: %v", err)
|
||||
}
|
||||
got, _ = albumStore.GetByID(ctx, a.ID)
|
||||
if got.CoverPhotoID != nil {
|
||||
t.Fatalf("cover not cleared via service: %v", got.CoverPhotoID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShareLinkRepository(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
defer pool.Close()
|
||||
|
||||
Reference in New Issue
Block a user