A chat profile bound to the operator identity passed a strict scope to the action gate, so it refused root-owned tasks whose identity is the daemon's empty sentinel (task 22: belongs to "", not "d898c8c8-..."). The dashboard acts with a nil scope and the chat read paths already treat the operator as acting across identities, so the write gate was the only surface that disagreed. Exempt the operator identity from the scope comparison and share that rule with the chat adapters that had their own copy.
120 lines
4.2 KiB
Go
120 lines
4.2 KiB
Go
package taskactions
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
// cancelStore is a task store whose cancel also cancels one waited-on callee.
|
|
type cancelStore struct {
|
|
Store
|
|
task Task
|
|
callees []int64
|
|
// failure is returned with the callees, as the store does when only their
|
|
// cancels failed.
|
|
failure error
|
|
}
|
|
|
|
func (f *cancelStore) TaskByID(context.Context, int64) (*Task, error) {
|
|
task := f.task
|
|
return &task, nil
|
|
}
|
|
|
|
func (f *cancelStore) CancelExecution(context.Context, int64, string, string) ([]int64, error) {
|
|
return f.callees, f.failure
|
|
}
|
|
|
|
func (f *cancelStore) InsertEvent(context.Context, events.Event) (int64, error) { return 1, nil }
|
|
|
|
// Every operator action that cancels a run must also stop the runs of the
|
|
// callees the store cancelled with it, or they run on to completion.
|
|
func TestCancellingActionsStopCallees(t *testing.T) {
|
|
failure := errors.New("cancel callee 7: boom")
|
|
tests := []struct {
|
|
name string
|
|
status string
|
|
action taskstate.Action
|
|
failure error
|
|
// parked is a task with no live run of its own to stop.
|
|
parked bool
|
|
}{
|
|
{name: "cancel", status: taskstate.Queued, action: taskstate.ActionCancel},
|
|
{name: "stop", status: taskstate.Running, action: taskstate.ActionStop},
|
|
{name: "reject", status: taskstate.Running, action: taskstate.ActionReject},
|
|
{name: "abandon", status: taskstate.Parked, action: taskstate.ActionAbandon, parked: true},
|
|
{name: "stop with a failed callee cancel", status: taskstate.Running, action: taskstate.ActionStop, failure: failure},
|
|
{name: "cancel with a failed callee cancel", status: taskstate.Queued, action: taskstate.ActionCancel, failure: failure},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var stopped []int64
|
|
s := Service{
|
|
Store: &cancelStore{task: Task{ID: 1, Status: tt.status}, callees: []int64{7, 9}, failure: tt.failure},
|
|
CancelTask: func(id int64) bool {
|
|
stopped = append(stopped, id)
|
|
return true
|
|
},
|
|
}
|
|
if err := s.Apply(t.Context(), nil, Actor{}, 1, tt.action, ActionPayload{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []int64{7, 9}
|
|
if !tt.parked {
|
|
want = append(want, 1)
|
|
}
|
|
for _, callee := range want {
|
|
if !slices.Contains(stopped, callee) {
|
|
t.Fatalf("stopped %v, want %d among them", stopped, callee)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A chat profile acting for the operator acts across identities, as the
|
|
// dashboard does. Empty identity is the daemon's root sentinel and belongs to
|
|
// the operator, so it must not refuse the operator the task it can already see.
|
|
func TestActionScope(t *testing.T) {
|
|
operator := string(identity.OperatorID())
|
|
other := "e0a0a0a0-0000-4000-8000-000000000001"
|
|
third := "e0a0a0a0-0000-4000-8000-000000000002"
|
|
tests := []struct {
|
|
name string
|
|
scope *string
|
|
taskIdentity string
|
|
allowed bool
|
|
}{
|
|
{name: "dashboard acts across identities", taskIdentity: other, allowed: true},
|
|
{name: "operator acts on a task with no identity", scope: &operator, taskIdentity: "", allowed: true},
|
|
{name: "operator acts on another identity's task", scope: &operator, taskIdentity: other, allowed: true},
|
|
{name: "another profile acts on its own task", scope: &other, taskIdentity: other, allowed: true},
|
|
{name: "another profile cannot act on a task with no identity", scope: &other, taskIdentity: "", allowed: false},
|
|
{name: "another profile cannot act on a third identity's task", scope: &other, taskIdentity: third, allowed: false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := Service{
|
|
Store: &cancelStore{task: Task{ID: 1, Status: taskstate.Running, Identity: tt.taskIdentity}},
|
|
CancelTask: func(int64) bool { return true },
|
|
}
|
|
err := s.Apply(t.Context(), tt.scope, Actor{}, 1, taskstate.ActionStop, ActionPayload{})
|
|
if tt.allowed {
|
|
if err != nil {
|
|
t.Fatalf("allowed action refused: %v", err)
|
|
}
|
|
return
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), "belongs to") {
|
|
t.Fatalf("err = %v, want a belongs-to refusal", err)
|
|
}
|
|
})
|
|
}
|
|
}
|