fix(webui): bind task requester to authenticated principal
This commit is contained in:
1 parent
4217a50874
commit
cbc4cf309d
7 files changed
+157
-124
No files matched your search
@@ -1,21 +1,30 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
func TestWorkRequestRejectsLongTitle(t *testing.T) {
|
||||
for _, title := range []string{strings.Repeat("a", 81), strings.Repeat("界", 81)} {
|
||||
t.Run(title[:3], func(t *testing.T) {
|
||||
body, err := json.Marshal(workRequest{Identity: "operator", Workflow: "implement", Title: title, Instructions: "Full task description"})
|
||||
body, err := json.Marshal(workRequest{Workflow: "implement", Title: title, Instructions: "Full task description"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/work-requests", strings.NewReader(string(body)))
|
||||
request := httptest.NewRequestWithContext(access.WithPrincipal(t.Context(), access.Principal{IdentityID: "operator"}), http.MethodPost, "/api/work-requests", strings.NewReader(string(body)))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("X-Archie-CSRF", "1")
|
||||
response := httptest.NewRecorder()
|
||||
@@ -26,3 +35,56 @@ func TestWorkRequestRejectsLongTitle(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type workRequestTasks struct {
|
||||
storecontract.TaskStore
|
||||
requester string
|
||||
}
|
||||
|
||||
func (s *workRequestTasks) EnqueueChatTask(_ context.Context, _, _, _, _, _, requester, _, _ string, _ map[string]any) (*task.Task, error) {
|
||||
s.requester = requester
|
||||
return &task.Task{ID: 1}, nil
|
||||
}
|
||||
func (*workRequestTasks) InsertEvent(context.Context, events.Event) (int64, error) { return 1, nil }
|
||||
|
||||
type workRequestControl struct {
|
||||
controlpb.ControlPlaneServiceClient
|
||||
}
|
||||
|
||||
func (workRequestControl) Query(_ context.Context, r *controlpb.QueryRequest, _ ...grpc.CallOption) (*controlpb.QueryResponse, error) {
|
||||
value := []byte(`{}`)
|
||||
if r.Kind == workflowDefinitionsKind {
|
||||
value = []byte(`{"definitions":[{"id":"report","yaml":"id: report\nrepository: none\nidentity: worker\n"}]}`)
|
||||
}
|
||||
return &controlpb.QueryResponse{Resource: &controlpb.Resource{ValueJson: value}}, nil
|
||||
}
|
||||
|
||||
func TestWorkRequestRequester(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, claimed string
|
||||
principal *access.Principal
|
||||
status int
|
||||
requester string
|
||||
}{
|
||||
{"authenticated requester", "", &access.Principal{IdentityID: "signed-in-human"}, http.StatusCreated, "signed-in-human"},
|
||||
{"cannot claim another requester", `,"identity":"another-account"`, &access.Principal{IdentityID: "signed-in-human"}, http.StatusBadRequest, ""},
|
||||
{"missing principal", "", nil, http.StatusUnauthorized, ""},
|
||||
{"empty principal", "", &access.Principal{}, http.StatusUnauthorized, ""},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
if tt.principal != nil {
|
||||
ctx = access.WithPrincipal(ctx, *tt.principal)
|
||||
}
|
||||
req := httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/work-requests", strings.NewReader(`{"workflow":"report","title":"Report","instructions":"Summarise results"`+tt.claimed+`}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Archie-CSRF", "1")
|
||||
tasks := &workRequestTasks{}
|
||||
response := httptest.NewRecorder()
|
||||
(&Server{Store: tasks, ControlPlane: workRequestControl{}}).handleWorkRequest(response, req)
|
||||
if response.Code != tt.status || tasks.requester != tt.requester {
|
||||
t.Fatalf("response = %d %s, requester = %q; want %d, %q", response.Code, response.Body.String(), tasks.requester, tt.status, tt.requester)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user