fix(captures): scope the capture list to the acting org

This commit is contained in:
sam committed 2026-10-08 21:26:07 +11:00
1 parent d6b693a9b2
commit bea6567933
4 files changed
+69 -4

No files matched your search

@@ -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)
}
})
}
}
+5 -2
View File
@@ -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)
}
@@ -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
}
@@ -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;