package controlplanerpc import ( "context" "errors" "strings" "testing" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/access" ) // fakeControlPlane records the commands it is sent and refuses a replace the // way the control plane does when a field it audits is missing. type fakeControlPlane struct { pb.ControlPlaneServiceClient commands []*pb.CommandRequest catalog *pb.CatalogResponse catalogErr error } func (*fakeControlPlane) Query(context.Context, *pb.QueryRequest, ...grpc.CallOption) (*pb.QueryResponse, error) { return &pb.QueryResponse{Resource: &pb.Resource{ValueJson: []byte(`{"drafts":[]}`), Version: 3}}, nil } func (f *fakeControlPlane) Catalog(context.Context, *pb.CatalogRequest, ...grpc.CallOption) (*pb.CatalogResponse, error) { if f.catalogErr != nil { return nil, f.catalogErr } return f.catalog, nil } func (f *fakeControlPlane) Command(_ context.Context, request *pb.CommandRequest, _ ...grpc.CallOption) (*pb.CommandResponse, error) { f.commands = append(f.commands, request) if request.GetActor() == "" || request.GetSource() == "" || request.GetRequestId() == "" { return nil, status.Error(codes.InvalidArgument, "actor, source, and request ID are required") } return &pb.CommandResponse{}, nil } func TestPutDraftAttributesTheCaller(t *testing.T) { for _, tt := range []struct { name string principal *access.Principal saves int wantErr bool }{ {"authenticated caller is the actor", &access.Principal{IdentityID: "alice"}, 1, false}, {"no principal", nil, 1, true}, {"each save is its own request", &access.Principal{IdentityID: "alice"}, 2, false}, } { t.Run(tt.name, func(t *testing.T) { client := &fakeControlPlane{} ctx := t.Context() if tt.principal != nil { ctx = access.WithPrincipal(ctx, *tt.principal) } workflows := NewWorkflows(client) for range tt.saves { err := workflows.PutDraft(ctx, "report", "id: report\n") if (err != nil) != tt.wantErr { t.Fatalf("PutDraft error = %v, want error %t", err, tt.wantErr) } } if tt.wantErr { if len(client.commands) != 0 { t.Fatalf("issued %d commands, want none", len(client.commands)) } return } if len(client.commands) != tt.saves { t.Fatalf("issued %d commands, want %d", len(client.commands), tt.saves) } requestIDs := make(map[string]bool, len(client.commands)) for _, command := range client.commands { if got, want := command.GetActor(), string(tt.principal.IdentityID); got != want { t.Errorf("actor = %q, want %q", got, want) } if command.GetSource() == "" || command.GetRequestId() == "" { t.Errorf("command %+v lacks a source or request ID", command) } if requestIDs[command.GetRequestId()] { t.Errorf("request ID %q repeats an earlier save", command.GetRequestId()) } requestIDs[command.GetRequestId()] = true if command.GetExpectedVersion() != 3 { t.Errorf("expected version = %d, want 3", command.GetExpectedVersion()) } if !strings.Contains(string(command.GetValueJson()), `"id":"report"`) { t.Errorf("value = %s, want the draft for report", command.GetValueJson()) } } }) } } // TestStepTypesReadsTheServedVocabulary pins the catalogue's production // reader: StepTypes returns the x-step-types extension of the // workflow-definitions descriptor the control plane's catalogue serves, the // payload the dashboard workflow editor and the workflow_step_types tool // consume, and it reports an absent catalogue or an unavailable control // plane rather than inventing an empty one. func TestStepTypesReadsTheServedVocabulary(t *testing.T) { vocabulary := `[{"name":"agent.run","needs_repository":true,"settings":{"type":"object"}}]` for _, tt := range []struct { name string resources []*pb.ResourceDescriptor catalogErr error want string wantErr bool wantUnavailable bool }{ { name: "the workflow-definitions descriptor serves the vocabulary", resources: []*pb.ResourceDescriptor{ {Kind: "workflow-drafts", SchemaJson: `{"x-step-types":[{"name":"not.a.step"}]}`}, {Kind: "workflow-definitions", SchemaJson: `{"x-step-types":` + vocabulary + `}`}, }, want: vocabulary, }, { name: "the catalog serves no workflow definitions", resources: []*pb.ResourceDescriptor{{Kind: "workflow-drafts", SchemaJson: `{}`}}, wantErr: true, }, { name: "the control plane is unavailable", catalogErr: errors.New("connection refused"), wantErr: true, wantUnavailable: true, }, } { t.Run(tt.name, func(t *testing.T) { client := &fakeControlPlane{catalog: &pb.CatalogResponse{Resources: tt.resources}, catalogErr: tt.catalogErr} got, err := NewWorkflows(client).StepTypes(t.Context()) if (err != nil) != tt.wantErr { t.Fatalf("StepTypes() error = %v, want error %t", err, tt.wantErr) } if tt.wantErr { if unavailable := errors.Is(err, ErrUnavailable); unavailable != tt.wantUnavailable { t.Fatalf("error is ErrUnavailable = %t, want %t (err %v)", unavailable, tt.wantUnavailable, err) } return } if string(got) != tt.want { t.Fatalf("StepTypes() = %s, want %s", got, tt.want) } }) } }