package statestore import ( "context" "io" "testing" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/logging" ) // tasksByOrg answers TaskByID the way the store does: a scoped read sees only // its own org's tasks. type tasksByOrg map[int64]org.OrgID func (t tasksByOrg) TaskByID(ctx context.Context, id int64) (*task.Task, error) { owner, ok := t[id] if scope, scoped := org.Scope(ctx); !ok || (scoped && scope != owner) { return nil, nil } return &task.Task{ID: id}, nil } // everyLog has a log for every task. type everyLog struct{} func (everyLog) TaskLog(context.Context, int64, int, logging.Query) (logging.TaskLogPage, error) { return logging.TaskLogPage{Found: true}, nil } func (everyLog) TaskLogContent(context.Context, int64, int, io.Writer) (bool, error) { return true, nil } // TestOrgScopedReads pins that a caller scoped to one org reads nothing of // another org's, while an unscoped internal service reads everything. func TestOrgScopedReads(t *testing.T) { logs := orgTaskLogs{TaskLogStore: everyLog{}, tasks: tasksByOrg{1: "acme"}} reads := map[string]func(ctx context.Context) bool{ "ReadTaskLog": func(ctx context.Context) bool { page, err := logs.TaskLog(ctx, 1, 0, logging.Query{}) return err == nil && page.Found }, "StreamTaskLogContent": func(ctx context.Context) bool { found, err := logs.TaskLogContent(ctx, 1, 0, io.Discard) return err == nil && found }, } for _, tt := range []struct { name string scope org.OrgID want bool }{ {"unscoped service", "", true}, {"owning org", "acme", true}, {"another org", "globex", false}, } { for rpc, read := range reads { t.Run(rpc+"/"+tt.name, func(t *testing.T) { ctx := context.Background() if tt.scope != "" { ctx = org.WithScope(ctx, tt.scope) } if got := read(ctx); got != tt.want { t.Fatalf("read = %v, want %v", got, tt.want) } }) } } }