From bb338f0055d23bcd7268457cef318e59fa5d03e4 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 8 Oct 2026 21:26:07 +1100 Subject: [PATCH] fix(captures): scope the capture list to the acting org --- .../postgres/capture_list_org_test.go | 55 +++++++++++++++++++ internal/infrastructure/postgres/edastore.go | 7 ++- .../postgres/postgresdb/edastore.sql.go | 10 +++- .../postgres/queries/edastore.sql | 1 + 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 internal/infrastructure/postgres/capture_list_org_test.go diff --git a/internal/infrastructure/postgres/capture_list_org_test.go b/internal/infrastructure/postgres/capture_list_org_test.go new file mode 100644 index 000000000..38ae54269 --- /dev/null +++ b/internal/infrastructure/postgres/capture_list_org_test.go @@ -0,0 +1,55 @@ +package postgres_test + +import ( + "context" + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" +) + +// ListCaptures returns only the acting org's captures; a caller with no org +// in context keeps the default org's. +func TestListCapturesIsOrgScoped(t *testing.T) { + pool := pgstore.Pool(t) + eda := postgres.NewEDA(pool, nil) + ctx := t.Context() + + insert := func(source string) string { + id, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{Source: source, Body: "{}"}, time.Hour, 100) + if err != nil { + t.Fatal(err) + } + return id + } + sysID, acmeID := insert("sys-hook"), insert("acme-hook") + // Captures take their org from the source, which current code always + // writes to the default org, so reassign the fixture directly. + if _, err := pool.Exec(ctx, "UPDATE captures SET org_id=$1 WHERE id=$2", "acme", acmeID); err != nil { + t.Fatal(err) + } + + cases := []struct { + name string + ctx context.Context + want string + }{ + {"no org keeps the default org", ctx, sysID}, + {"default org lists only its own", org.WithOrg(ctx, org.DefaultOrgID), sysID}, + {"other org lists only its own", org.WithOrg(ctx, "acme"), acmeID}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, err := eda.ListCaptures(tc.ctx, 10) + if err != nil { + t.Fatal(err) + } + if len(got) != 1 || got[0].ID != tc.want { + t.Fatalf("captures = %#v, want only %s", got, tc.want) + } + }) + } +} diff --git a/internal/infrastructure/postgres/edastore.go b/internal/infrastructure/postgres/edastore.go index c5339fb23..d006c7957 100644 --- a/internal/infrastructure/postgres/edastore.go +++ b/internal/infrastructure/postgres/edastore.go @@ -143,9 +143,12 @@ func captureValue(r postgresdb.Capture) storecontract.CapturedEvent { } } -// ListCaptures returns the newest captures first. +// ListCaptures returns the newest captures first in the acting org. func (s *EDA) ListCaptures(ctx context.Context, limit int) ([]storecontract.CapturedEvent, error) { - rows, err := s.q.ListCaptures(ctx, int32(limit)) + rows, err := s.q.ListCaptures(ctx, postgresdb.ListCapturesParams{ + OrgID: string(org.OrgFromContext(ctx)), + Limit: int32(limit), + }) if err != nil { return nil, fmt.Errorf("edastore: list captures: %w", err) } diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index eba516c1f..33ae83501 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -756,12 +756,18 @@ func (q *Queries) ListBindings(ctx context.Context) ([]ListBindingsRow, error) { const listCaptures = `-- name: ListCaptures :many SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id, delivery FROM captures +WHERE org_id = $2 ORDER BY received_at DESC LIMIT $1 ` -func (q *Queries) ListCaptures(ctx context.Context, limit int32) ([]Capture, error) { - rows, err := q.db.Query(ctx, listCaptures, limit) +type ListCapturesParams struct { + Limit int32 + OrgID string +} + +func (q *Queries) ListCaptures(ctx context.Context, arg ListCapturesParams) ([]Capture, error) { + rows, err := q.db.Query(ctx, listCaptures, arg.Limit, arg.OrgID) if err != nil { return nil, err } diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index 62c625c6f..50a62b67e 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -9,6 +9,7 @@ FROM (SELECT 1) intake LEFT JOIN sources s ON s.path = $2; -- name: ListCaptures :many SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id, delivery FROM captures +WHERE org_id = @org_id ORDER BY received_at DESC LIMIT $1;