package messagerpc import ( "context" "errors" "testing" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) type fakeRuns map[string]*workflow.Task func (f fakeRuns) TaskForCredential(_ context.Context, token string) (*workflow.Task, error) { if task, ok := f[token]; ok { return task, nil } return nil, errors.New("unknown credential") } func TestAuthorize(t *testing.T) { runs := fakeRuns{ "sys": {Identity: "archie"}, "org": {Identity: "archie", Org: "org-acme"}, "test": {Identity: "archie", Source: task.SourceTest}, "package": {Identity: "archie", WorkflowDefinitionYAML: "package: vendor/thing\nname: x\n"}, } tests := []struct { name, identity, credential string ok bool }{ {"system org run", "archie", "sys", true}, {"no credential", "archie", "", false}, {"unknown credential", "archie", "nope", false}, {"other identity", "someone-else", "sys", false}, {"non-system org", "archie", "org", false}, {"test run", "archie", "test", false}, {"package workflow", "archie", "package", false}, } s := &Server{Runs: runs} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := s.authorize(context.Background(), tt.identity, tt.credential) if (err == nil) != tt.ok { t.Fatalf("authorize() err = %v, want ok %v", err, tt.ok) } }) } }