A per-garden public link anyone can open read-only, with no login and no OIDC. Backend: - Migration 0004 adds gardens.public_token (nullable, unique over non-NULL). - Owner-only management: GET/POST/DELETE /gardens/:id/share-link (state / enable-or-rotate / disable). The token is stored raw (it must be shown back to the owner) and never selected into the normal garden payload. - Unauthenticated GET /api/v1/public/gardens/:token returns the read-only /full payload — the token is the capability, so no requireAuth and no redirect. The public view drops MyRole and OwnerID to minimize what an anonymous viewer learns. Unknown/rotated/disabled tokens are masked as 404. - assembleFull is extracted from GardenFull so both reads return one shape. Frontend: - /g/$token route with NO auth guard (SPA fallback already serves it), rendering GardenCanvas read-only via usePublicGarden. - Share dialog gains a Public link section: create, copy, regenerate, turn off. Tests: service lifecycle + ACL (owner-only, non-owner masked, rotate/disable invalidation) and the HTTP surface incl. the cookieless public read. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
125 lines
4.3 KiB
Go
125 lines
4.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
|
|
)
|
|
|
|
// TestPublicShareLink covers the owner-only link lifecycle (enable / idempotent
|
|
// re-enable / rotate / disable) and the unauthenticated read it gates: a valid
|
|
// token returns the read-only /full payload with no role, an unknown/blank/
|
|
// rotated/disabled token is masked as ErrNotFound, and non-owners can't manage
|
|
// the link.
|
|
func TestPublicShareLink(t *testing.T) {
|
|
ctx := context.Background()
|
|
s := newTestService(t, openConfig())
|
|
owner := seedUser(t, s, "[email protected]")
|
|
editor := seedUser(t, s, "[email protected]")
|
|
stranger := seedUser(t, s, "[email protected]")
|
|
|
|
g := seedGarden(t, s, owner)
|
|
bed := seedBed(t, s, owner, g.ID)
|
|
plant := seedOwnPlant(t, s, owner, 10)
|
|
if _, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, XCM: 0, YCM: 0, RadiusCM: 10}); err != nil {
|
|
t.Fatalf("seed plop: %v", err)
|
|
}
|
|
if _, err := s.AddShare(ctx, owner, g.ID, "[email protected]", domain.RoleEditor); err != nil {
|
|
t.Fatalf("share editor: %v", err)
|
|
}
|
|
|
|
// Disabled by default.
|
|
link, err := s.GetPublicShareLink(ctx, owner, g.ID)
|
|
if err != nil {
|
|
t.Fatalf("get link: %v", err)
|
|
}
|
|
if link.Enabled {
|
|
t.Fatalf("a new garden should have no public link")
|
|
}
|
|
|
|
// Non-owners can neither read nor manage the link. A shared editor sees the
|
|
// garden (ErrForbidden); a stranger's view is masked (ErrNotFound).
|
|
if _, err := s.GetPublicShareLink(ctx, editor, g.ID); !errors.Is(err, domain.ErrForbidden) {
|
|
t.Errorf("editor get link = %v, want ErrForbidden", err)
|
|
}
|
|
if _, err := s.EnablePublicShareLink(ctx, editor, g.ID, false); !errors.Is(err, domain.ErrForbidden) {
|
|
t.Errorf("editor enable = %v, want ErrForbidden", err)
|
|
}
|
|
if _, err := s.EnablePublicShareLink(ctx, stranger, g.ID, false); !errors.Is(err, domain.ErrNotFound) {
|
|
t.Errorf("stranger enable = %v, want ErrNotFound", err)
|
|
}
|
|
|
|
// Owner enables → a token; the public read works with no actor.
|
|
link, err = s.EnablePublicShareLink(ctx, owner, g.ID, false)
|
|
if err != nil {
|
|
t.Fatalf("enable: %v", err)
|
|
}
|
|
if !link.Enabled || link.Token == "" {
|
|
t.Fatalf("enable returned %+v, want enabled with a token", link)
|
|
}
|
|
tok := link.Token
|
|
|
|
// Re-enabling without rotate keeps the same token (idempotent).
|
|
again, err := s.EnablePublicShareLink(ctx, owner, g.ID, false)
|
|
if err != nil {
|
|
t.Fatalf("re-enable: %v", err)
|
|
}
|
|
if again.Token != tok {
|
|
t.Errorf("re-enable changed the token: %q -> %q", tok, again.Token)
|
|
}
|
|
|
|
full, err := s.PublicGarden(ctx, tok)
|
|
if err != nil {
|
|
t.Fatalf("public read: %v", err)
|
|
}
|
|
if full.Garden.ID != g.ID {
|
|
t.Errorf("public garden id = %d, want %d", full.Garden.ID, g.ID)
|
|
}
|
|
if full.Garden.MyRole != "" {
|
|
t.Errorf("public garden MyRole = %q, want empty (no role for a public viewer)", full.Garden.MyRole)
|
|
}
|
|
if len(full.Objects) != 1 || len(full.Plantings) != 1 || len(full.Plants) != 1 {
|
|
t.Errorf("public full = %d objs / %d plops / %d plants, want 1/1/1",
|
|
len(full.Objects), len(full.Plantings), len(full.Plants))
|
|
}
|
|
|
|
// Unknown / blank tokens are masked, never a distinct error.
|
|
for _, bad := range []string{"does-not-exist", " ", ""} {
|
|
if _, err := s.PublicGarden(ctx, bad); !errors.Is(err, domain.ErrNotFound) {
|
|
t.Errorf("PublicGarden(%q) = %v, want ErrNotFound", bad, err)
|
|
}
|
|
}
|
|
|
|
// Rotate → fresh token; the old URL stops working.
|
|
rotated, err := s.EnablePublicShareLink(ctx, owner, g.ID, true)
|
|
if err != nil {
|
|
t.Fatalf("rotate: %v", err)
|
|
}
|
|
if rotated.Token == "" || rotated.Token == tok {
|
|
t.Fatalf("rotate token = %q (old %q), want a fresh non-empty token", rotated.Token, tok)
|
|
}
|
|
if _, err := s.PublicGarden(ctx, tok); !errors.Is(err, domain.ErrNotFound) {
|
|
t.Errorf("old token after rotate = %v, want ErrNotFound", err)
|
|
}
|
|
if _, err := s.PublicGarden(ctx, rotated.Token); err != nil {
|
|
t.Errorf("new token = %v, want success", err)
|
|
}
|
|
|
|
// Disable → link off, token no longer resolves.
|
|
if err := s.DisablePublicShareLink(ctx, owner, g.ID); err != nil {
|
|
t.Fatalf("disable: %v", err)
|
|
}
|
|
if _, err := s.PublicGarden(ctx, rotated.Token); !errors.Is(err, domain.ErrNotFound) {
|
|
t.Errorf("token after disable = %v, want ErrNotFound", err)
|
|
}
|
|
link, err = s.GetPublicShareLink(ctx, owner, g.ID)
|
|
if err != nil {
|
|
t.Fatalf("get link after disable: %v", err)
|
|
}
|
|
if link.Enabled {
|
|
t.Errorf("link still enabled after disable")
|
|
}
|
|
}
|