Files
archie/internal/webui/api_control_plane_test.go
T
sam 5891d581cb fix(webui): the workflow handlers act in the caller's org
The definitions query was built by hand, so it carried no org and the server
resolved the default one, while the enablement document read beside it and the
enablement write went to the default org outright. A caller in a non-default
org therefore read one org's definitions beside another org's enablement, and
toggled that other org's workflows.

workflowCollection now builds its query with controlplanerpc.QueryRequest, and
the three enablement reads and the write use org.OrgFromContext, so every one of
them names the org the principal put on the request context.
2026-09-30 07:33:16 +10:00

226 lines
9.2 KiB
Go

package webui
import (
"bytes"
"context"
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
type controlPlaneClientStub struct {
command *controlpb.CommandRequest
// query is the resource a kind-less Query falls back to; queries is every
// request Query was handed, so a test can read back the org each one
// carried.
query *controlpb.Resource
queries []*controlpb.QueryRequest
history *controlpb.HistoryRequest
revisions []*controlpb.Revision
audit *controlpb.AuditRequest
entries []*controlpb.AuditEntry
watch grpc.ServerStreamingClient[controlpb.WatchResponse]
err error
// resources, when set, answers Query per kind and keeps what Command
// writes, falling back to query for a kind it does not hold.
resources map[string]*controlpb.Resource
}
func (f *controlPlaneClientStub) Catalog(context.Context, *controlpb.CatalogRequest, ...grpc.CallOption) (*controlpb.CatalogResponse, error) {
return &controlpb.CatalogResponse{}, f.err
}
func (f *controlPlaneClientStub) Query(_ context.Context, request *controlpb.QueryRequest, _ ...grpc.CallOption) (*controlpb.QueryResponse, error) {
f.queries = append(f.queries, request)
if resource, ok := f.resources[request.Kind]; ok {
return &controlpb.QueryResponse{Resource: resource}, f.err
}
return &controlpb.QueryResponse{Resource: f.query}, f.err
}
func (f *controlPlaneClientStub) History(_ context.Context, request *controlpb.HistoryRequest, _ ...grpc.CallOption) (*controlpb.HistoryResponse, error) {
f.history = request
return &controlpb.HistoryResponse{Revisions: f.revisions}, f.err
}
func (f *controlPlaneClientStub) Audit(_ context.Context, request *controlpb.AuditRequest, _ ...grpc.CallOption) (*controlpb.AuditResponse, error) {
f.audit = request
return &controlpb.AuditResponse{Entries: f.entries}, f.err
}
func (f *controlPlaneClientStub) Command(_ context.Context, request *controlpb.CommandRequest, _ ...grpc.CallOption) (*controlpb.CommandResponse, error) {
f.command = request
if f.err != nil {
return nil, f.err
}
resource := &controlpb.Resource{Kind: request.Kind, Version: request.ExpectedVersion + 1, ValueJson: request.ValueJson}
if f.resources != nil {
f.resources[request.Kind] = resource
}
return &controlpb.CommandResponse{Resource: resource}, nil
}
func (f *controlPlaneClientStub) Watch(context.Context, *controlpb.WatchRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[controlpb.WatchResponse], error) {
return f.watch, f.err
}
// The multiplexed stream answers before any topic changes. An unchanged
// resource must not leave the connection pill at Connecting indefinitely.
func TestLiveStreamOpensBeforeTheFirstChange(t *testing.T) {
server := httptest.NewServer((&Server{Store: &fakeEventStore{}, Log: slog.New(slog.DiscardHandler)}).Handler())
t.Cleanup(server.Close)
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
request, _ := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/api/stream", nil)
response, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatalf("stream did not answer before its first change: %v", err)
}
defer response.Body.Close()
if got := response.Header.Get("Content-Type"); got != "text/event-stream" {
t.Fatalf("Content-Type = %q", got)
}
}
func TestControlPlaneCommandSuppliesTrustedAttribution(t *testing.T) {
client := &controlPlaneClientStub{}
server := &Server{ControlPlane: client}
body := bytes.NewBufferString(`{"value":{"max_model_tool_steps":12},"expected_version":1,"actor":"browser"}`)
request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/control-plane/resources/workflow-execution-settings/commands/replace", body)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-Archie-CSRF", "1")
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
if client.command.Actor != string(identity.SystemID) || client.command.Source != "archie-ui" {
t.Fatalf("attribution = %q/%q, want the System identity from archie-ui", client.command.Actor, client.command.Source)
}
if !strings.HasPrefix(client.command.RequestId, "ui-") || client.command.ExpectedVersion != 1 {
t.Fatalf("request = %+v", client.command)
}
var got struct {
Resource controlPlaneResource `json:"resource"`
}
if err := json.Unmarshal(response.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if got.Resource.Version != 2 || string(got.Resource.Value) != `{"max_model_tool_steps":12}` {
t.Fatalf("resource = %+v", got.Resource)
}
}
func TestControlPlaneConflictMapsToHTTPConflict(t *testing.T) {
server := &Server{ControlPlane: &controlPlaneClientStub{err: status.Error(codes.Aborted, "version conflict")}}
request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/control-plane/resources/settings/commands/replace", strings.NewReader(`{"value":{},"expected_version":1}`))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-Archie-CSRF", "1")
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, request)
if response.Code != http.StatusConflict {
t.Fatalf("status = %d, want 409", response.Code)
}
}
func TestControlPlaneHistoryRendersRevisionsForRestore(t *testing.T) {
client := &controlPlaneClientStub{revisions: []*controlpb.Revision{
{Version: 2, ValueJson: []byte(`{"max_model_tool_steps":40}`), Actor: string(identity.SystemID), Source: "archie-ui", RequestId: "ui-2"},
{Version: 1, ValueJson: []byte(`{"max_model_tool_steps":25}`), Actor: "system:migration", Source: "legacy-config", RequestId: "import"},
}}
server := &Server{ControlPlane: client}
request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/control-plane/resources/workflow-execution-settings/history", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
if client.history.GetKind() != "workflow-execution-settings" {
t.Fatalf("history requested for %q", client.history.GetKind())
}
var got struct {
Revisions []struct {
Version int64 `json:"version"`
Value json.RawMessage `json:"value"`
Actor string `json:"actor"`
Source string `json:"source"`
} `json:"revisions"`
}
if err := json.Unmarshal(response.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if len(got.Revisions) != 2 || got.Revisions[0].Version != 2 || got.Revisions[0].Actor != string(identity.SystemID) {
t.Fatalf("revisions = %+v", got.Revisions)
}
// The value is the restore payload: it has to survive as JSON, not base64.
if string(got.Revisions[1].Value) != `{"max_model_tool_steps":25}` {
t.Fatalf("older value = %s, want the JSON a replace can send back", got.Revisions[1].Value)
}
}
func TestControlPlaneHistoryWithoutAControlPlaneIsUnavailable(t *testing.T) {
server := &Server{}
request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/control-plane/resources/limits/history", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, request)
if response.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503", response.Code)
}
}
// TestControlPlaneAuditFiltersByTableAndRecord: the page names its table and
// records; the endpoint forwards exactly those and returns each changed field
// with its JSON values intact.
func TestControlPlaneAuditFiltersByTableAndRecord(t *testing.T) {
client := &controlPlaneClientStub{entries: []*controlpb.AuditEntry{{
Id: 9, Table: "resources", RecordKey: "workflow-execution-settings", Field: "max_model_tool_steps",
OldValueJson: []byte("90"), NewValueJson: []byte("120"), Version: 2, Actor: "system", Source: "archie-ui",
}}}
server := &Server{ControlPlane: client}
request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/control-plane/audit?table=resources&key=workflow-execution-settings&key=model-settings", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
if client.audit.GetTable() != "resources" || strings.Join(client.audit.GetRecordKeys(), ",") != "workflow-execution-settings,model-settings" {
t.Fatalf("forwarded %+v", client.audit)
}
var got struct {
Entries []struct {
Field string `json:"field"`
OldValue json.RawMessage `json:"old_value"`
NewValue json.RawMessage `json:"new_value"`
Version int64 `json:"version"`
} `json:"entries"`
}
if err := json.Unmarshal(response.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if len(got.Entries) != 1 || got.Entries[0].Field != "max_model_tool_steps" || string(got.Entries[0].OldValue) != "90" || string(got.Entries[0].NewValue) != "120" || got.Entries[0].Version != 2 {
t.Fatalf("entries = %+v", got.Entries)
}
}