Files
archie/internal/app/archiegateway/chat_tasks_test.go
T

72 lines
2.6 KiB
Go

package archiegateway
import (
"context"
"testing"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/taskactions"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/logging"
)
type emptyTaskLogs struct{ storecontract.TaskLogStore }
func (emptyTaskLogs) TaskLog(context.Context, int64, int, logging.Query) (logging.TaskLogPage, error) {
return logging.TaskLogPage{}, nil
}
// TestChatTaskScope pins the chat grant boundary: the operator profile sees
// and approves every task, and any other identity only its own.
func TestChatTaskScope(t *testing.T) {
operator := string(identity.OperatorID())
tasks := []workflow.Task{{ID: 1, Identity: "alice"}, {ID: 2, Identity: "bob"}}
byID := func(_ context.Context, id int64) (*workflow.Task, error) {
for i := range tasks {
if tasks[i].ID == id {
return &tasks[i], nil
}
}
return nil, nil
}
tests := []struct {
name string
chat string
wantList int
wantScope *string
otherLogs bool
}{
{name: "operator sees every task", chat: operator, wantList: 2, otherLogs: true},
{name: "an identity sees only its own", chat: "alice", wantList: 1, wantScope: new("alice")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lister := chatTaskListerAdapter{tasks: func(context.Context, int) ([]workflow.Task, error) { return tasks, nil }}
got, err := lister.ListChatTasks(t.Context(), tt.chat, 10)
if err != nil || len(got) != tt.wantList {
t.Fatalf("list = %d tasks, %v; want %d", len(got), err, tt.wantList)
}
var scope *string
controller := chatTaskControllerAdapter{approve: func(_ context.Context, s *string, _ taskactions.Actor, _ int64, _ taskactions.ActionPayload) error {
scope = s
return nil
}}
if err := controller.ApproveChatTask(t.Context(), 2, taskactions.Actor{Identity: identity.IdentityID(tt.chat)}); err != nil {
t.Fatalf("approve: %v", err)
}
if (scope == nil) != (tt.wantScope == nil) || (scope != nil && *scope != *tt.wantScope) {
t.Fatalf("approve scope = %v, want %v", scope, tt.wantScope)
}
reader := chatTaskLogReaderAdapter{tasks: byID, taskLogs: emptyTaskLogs{}}
_, err = reader.ReadChatTaskLogs(t.Context(), tt.chat, 2, 0, gateway.ChatTaskLogQuery{})
if refused := err != nil && err.Error() == "task 2 belongs to another identity"; refused == tt.otherLogs {
t.Fatalf("reading another identity's logs: err = %v, want allowed=%v", err, tt.otherLogs)
}
})
}
}