Files
archie/internal/app/controlplane/client_org_test.go
T
sam 96408c8e6a feat(controlplane): carry the caller org from the request principal
The storage and wire contract already key resources by (org, kind), but no
caller sent an org, so every request acted in the default org. The org now
travels on the request context: webui's authorize middleware attaches the
verified principal's org, and one request-constructor per wire shape
(controlplanerpc's Query/History/Watch/CommandRequest) stamps it back, so a
call site cannot send the wrong org and a new one cannot forget it.

No behaviour change today: the shared token's principal is the default org's
owner, no production path creates another org, and a context with no principal
yields DefaultOrgID (what the server already derived from an empty org_id).
2026-09-30 04:01:52 +10:00

86 lines
3.0 KiB
Go

package controlplane
import (
"context"
"io"
"testing"
"google.golang.org/grpc"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// orgRecordingClient is the rpc end that records the org of every request it
// is handed, so a test can read back what the client put on the wire.
type orgRecordingClient struct {
queryOrg string
commandOrg string
watchOrg string
}
func (*orgRecordingClient) Catalog(context.Context, *pb.CatalogRequest, ...grpc.CallOption) (*pb.CatalogResponse, error) {
panic("Catalog is not part of the org-stamping path")
}
func (c *orgRecordingClient) Query(_ context.Context, in *pb.QueryRequest, _ ...grpc.CallOption) (*pb.QueryResponse, error) {
c.queryOrg = in.GetOrgId()
return &pb.QueryResponse{Resource: &pb.Resource{Kind: in.GetKind()}}, nil
}
func (*orgRecordingClient) History(context.Context, *pb.HistoryRequest, ...grpc.CallOption) (*pb.HistoryResponse, error) {
panic("History is not part of the org-stamping path")
}
func (*orgRecordingClient) Audit(context.Context, *pb.AuditRequest, ...grpc.CallOption) (*pb.AuditResponse, error) {
return &pb.AuditResponse{}, nil
}
func (c *orgRecordingClient) Command(_ context.Context, in *pb.CommandRequest, _ ...grpc.CallOption) (*pb.CommandResponse, error) {
c.commandOrg = in.GetOrgId()
return &pb.CommandResponse{Resource: &pb.Resource{Version: 1}}, nil
}
func (c *orgRecordingClient) Watch(_ context.Context, in *pb.WatchRequest, _ ...grpc.CallOption) (grpc.ServerStreamingClient[pb.WatchResponse], error) {
c.watchOrg = in.GetOrgId()
return eofWatchStream{}, nil
}
// eofWatchStream is the client end of a stream that ends before it carries
// anything: the watch under test only builds the request.
type eofWatchStream struct{ grpc.ClientStream }
func (eofWatchStream) Recv() (*pb.WatchResponse, error) { return nil, io.EOF }
func TestClientStampsCallerOrgOnEveryRequestShape(t *testing.T) {
tests := []struct {
name string
ctx context.Context
want string
}{
{name: "no org on the context is the default org", ctx: context.Background(), want: string(org.DefaultOrgID)},
{name: "org on the context travels on the wire", ctx: org.WithOrg(context.Background(), "acme"), want: "acme"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rpc := &orgRecordingClient{}
client := NewRPCClient(rpc)
if _, _, err := client.Query(tt.ctx, ChannelSettingsKind, func([]byte) error { return nil }); err != nil {
t.Fatalf("Query: %v", err)
}
if _, err := client.ReplaceSchedules(tt.ctx, nil, 0, "actor", "source", "request"); err != nil {
t.Fatalf("ReplaceSchedules: %v", err)
}
if _, err := client.WatchResource(tt.ctx, WorkflowExecutionSettingsKind, 0); err != nil {
t.Fatalf("WatchResource: %v", err)
}
if rpc.queryOrg != tt.want || rpc.commandOrg != tt.want || rpc.watchOrg != tt.want {
t.Fatalf("wire orgs = query %q command %q watch %q, want %q each",
rpc.queryOrg, rpc.commandOrg, rpc.watchOrg, tt.want)
}
})
}
}