Files
archie/modules/events/api_mapping_scope_test.go
T
sam f0f09e9189
Quality / check (pull_request) Successful in 10m21s
fix(events): enforce source ownership for mappings
2026-11-26 20:35:40 +11:00

228 lines
7.7 KiB
Go

package events
import (
"context"
"database/sql"
"errors"
"sort"
"testing"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/events/internal/mapping"
"github.com/samcharles93/archie-core/modules/events/internal/store"
"github.com/samcharles93/archie-core/modules/governance"
)
// TestMappingsAreVisibleOnlyThroughVisibleEventTypes guards person-scoped
// sources: mappings of Alice's private event type must not be listed or read
// by Bob, while organisation mappings remain visible to both.
func TestMappingsAreVisibleOnlyThroughVisibleEventTypes(t *testing.T) {
for _, tt := range []struct {
name string
caller governance.IdentityID
wantPrivateRead bool
}{
{name: "owner", caller: "alice", wantPrivateRead: true},
{name: "other member", caller: "bob"},
} {
t.Run(tt.name, func(t *testing.T) {
m, _, ids := newMappingScopeModule()
wantList := []uuid.UUID{ids.orgMapping}
if tt.wantPrivateRead {
wantList = []uuid.UUID{ids.aliceMapping, ids.orgMapping}
}
ctx := governance.WithPrincipal(t.Context(), governance.Principal{IdentityID: tt.caller})
listed, err := m.mappings(ctx, &struct{}{})
if err != nil {
t.Fatalf("mappings: %v", err)
}
got := make([]uuid.UUID, 0, len(listed.Body.Mappings))
for _, row := range listed.Body.Mappings {
got = append(got, row.ID)
}
if len(got) != len(wantList) {
t.Errorf("mapping ids = %v, want %v", got, wantList)
}
for i, id := range wantList {
if i < len(got) && got[i] != id {
t.Errorf("mapping ids = %v, want %v", got, wantList)
}
}
read, err := m.mapping(ctx, &idInput{ID: ids.aliceMapping.String()})
if tt.wantPrivateRead {
if err != nil {
t.Fatalf("owner mapping read: %v", err)
}
if read.Body.ID != ids.aliceMapping {
t.Fatalf("mapping id = %s, want %s", read.Body.ID, ids.aliceMapping)
}
return
}
var denied *huma.ErrorModel
if !errors.As(err, &denied) || denied.Status != 404 {
t.Fatalf("other member mapping read error = %v, want 404", err)
}
})
}
}
func TestMappingCannotUseAnotherPersonsEventType(t *testing.T) {
m, _, ids := newMappingScopeModule()
ctx := governance.WithPrincipal(t.Context(), governance.Principal{IdentityID: "bob"})
_, err := m.checkedMapping(ctx, mappingBody{
Name: "private fields", EventTypeID: ids.aliceType.String(), Fields: mappingScopeFields(),
})
var denied *huma.ErrorModel
if !errors.As(err, &denied) || denied.Status != 400 {
t.Fatalf("mapping for another person's event type error = %v, want 400", err)
}
}
func TestMappingCannotModifyAnotherPersonsMapping(t *testing.T) {
for _, operation := range []string{"update", "delete"} {
t.Run(operation, func(t *testing.T) {
m, repo, ids := newMappingScopeModule()
ctx := governance.WithPrincipal(t.Context(), governance.Principal{IdentityID: "bob"})
var err error
switch operation {
case "update":
_, err = m.updateMapping(ctx, &updateMappingInput{
ID: ids.aliceMapping.String(),
Body: mappingBody{
Name: "moved fields", EventTypeID: ids.orgType.String(), Fields: mappingScopeFields(),
},
})
case "delete":
_, err = m.deleteMapping(ctx, &idInput{ID: ids.aliceMapping.String()})
}
var denied *huma.ErrorModel
if !errors.As(err, &denied) || denied.Status != 404 {
t.Fatalf("%s private mapping error = %v, want 404", operation, err)
}
if len(repo.writes) != 0 {
t.Fatalf("unauthorised mapping writes = %v, want none", repo.writes)
}
})
}
}
type mappingScopeIDs struct {
aliceType, orgType uuid.UUID
aliceMapping, orgMapping uuid.UUID
}
func newMappingScopeModule() (*Module, *mappingScopeStore, mappingScopeIDs) {
aliceSource, orgSource := uuid.New(), uuid.New()
ids := mappingScopeIDs{aliceType: uuid.New(), orgType: uuid.New(), aliceMapping: uuid.New(), orgMapping: uuid.New()}
now := time.Now()
repo := &mappingScopeStore{
sources: map[uuid.UUID]store.Source{
aliceSource: {ID: aliceSource, OrgID: "org", Scope: store.ScopePerson, PersonID: sql.NullString{String: "alice", Valid: true}},
orgSource: {ID: orgSource, OrgID: "org", Scope: store.ScopeOrg},
},
eventTypes: map[uuid.UUID]store.EventType{
ids.aliceType: {ID: ids.aliceType, OrgID: "org", SourceID: aliceSource, Name: "private", Rule: []byte(`{}`), Schema: []byte(`{"id":"string"}`), CreatedAt: now, UpdatedAt: now},
ids.orgType: {ID: ids.orgType, OrgID: "org", SourceID: orgSource, Name: "shared", Rule: []byte(`{}`), Schema: []byte(`{"id":"string"}`), CreatedAt: now, UpdatedAt: now},
},
mappings: map[uuid.UUID]store.Mapping{
// SourceID is only a hint; the event type's source defines visibility.
ids.aliceMapping: {ID: ids.aliceMapping, OrgID: "org", Name: "alice fields", SourceID: uuid.NullUUID{UUID: orgSource, Valid: true}, EventTypeID: ids.aliceType, Fields: []byte(`[]`), CreatedAt: now, UpdatedAt: now},
ids.orgMapping: {ID: ids.orgMapping, OrgID: "org", Name: "shared fields", SourceID: uuid.NullUUID{UUID: aliceSource, Valid: true}, EventTypeID: ids.orgType, Fields: []byte(`[]`), CreatedAt: now, UpdatedAt: now},
},
}
return &Module{orgID: "org", mappingStore: repo, reader: repo}, repo, ids
}
func mappingScopeFields() []mapping.Field {
return []mapping.Field{{Name: "id", Path: "id", Type: mapping.TypeString}}
}
type mappingScopeStore struct {
sources map[uuid.UUID]store.Source
eventTypes map[uuid.UUID]store.EventType
mappings map[uuid.UUID]store.Mapping
writes []string
}
func (s *mappingScopeStore) Mappings(_ context.Context, orgID string) ([]store.Mapping, error) {
out := make([]store.Mapping, 0, len(s.mappings))
for _, row := range s.mappings {
if row.OrgID == orgID {
out = append(out, row)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out, nil
}
func (s *mappingScopeStore) Mapping(_ context.Context, orgID string, id uuid.UUID) (store.Mapping, error) {
row, ok := s.mappings[id]
if !ok || row.OrgID != orgID {
return store.Mapping{}, store.ErrNotFound
}
return row, nil
}
func (s *mappingScopeStore) EventTypes(_ context.Context, orgID, person string) ([]store.EventType, error) {
out := make([]store.EventType, 0, len(s.eventTypes))
for _, row := range s.eventTypes {
if row.OrgID != orgID {
continue
}
source := s.sources[row.SourceID]
if !source.PersonID.Valid || source.PersonID.String == person {
out = append(out, row)
}
}
return out, nil
}
func (s *mappingScopeStore) EventType(_ context.Context, orgID string, id uuid.UUID) (store.EventType, error) {
row, ok := s.eventTypes[id]
if !ok || row.OrgID != orgID {
return store.EventType{}, store.ErrNotFound
}
return row, nil
}
func (s *mappingScopeStore) InsertMapping(_ context.Context, row store.Mapping) error {
s.writes = append(s.writes, "insert")
s.mappings[row.ID] = row
return nil
}
func (s *mappingScopeStore) UpdateMapping(_ context.Context, row store.Mapping) error {
if _, ok := s.mappings[row.ID]; !ok {
return store.ErrNotFound
}
s.writes = append(s.writes, "update")
s.mappings[row.ID] = row
return nil
}
func (s *mappingScopeStore) DeleteMapping(_ context.Context, orgID string, id uuid.UUID) error {
row, ok := s.mappings[id]
if !ok || row.OrgID != orgID {
return store.ErrNotFound
}
s.writes = append(s.writes, "delete")
delete(s.mappings, id)
return nil
}
func (s *mappingScopeStore) Source(_ context.Context, orgID string, id uuid.UUID) (store.Source, error) {
source, ok := s.sources[id]
if !ok || source.OrgID != orgID {
return store.Source{}, store.ErrNotFound
}
return source, nil
}
func (s *mappingScopeStore) SourceByPath(context.Context, string, string) (store.Source, error) {
return store.Source{}, store.ErrNotFound
}