mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
A reporter chat bound to one task ran commands in that task's sandbox, while the chat page kept querying the empty session container. Save the task or session target on the conversation and point the badge, uploads, and later turns at it.
130 lines
4.4 KiB
Go
130 lines
4.4 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/internal/sandbox"
|
|
)
|
|
|
|
func TestSandboxTargetForTrigger(t *testing.T) {
|
|
task := sandboxTargetForTrigger("reporter", 5, false, 18)
|
|
if task.Scope != sandbox.ScopeTask || task.OwnerID != "5" {
|
|
t.Fatalf("reporter task = %+v", task)
|
|
}
|
|
merged := sandboxTargetForTrigger("reporter", 5, true, 19)
|
|
if merged.Scope != sandbox.ScopeSession || merged.OwnerID != "conv-19" {
|
|
t.Fatalf("multi-task reporter = %+v", merged)
|
|
}
|
|
other := sandboxTargetForTrigger("auto", 5, false, 20)
|
|
if other.Scope != sandbox.ScopeSession || other.OwnerID != "conv-20" {
|
|
t.Fatalf("non-reporter = %+v", other)
|
|
}
|
|
noTask := sandboxTargetForTrigger("reporter", 0, false, 21)
|
|
if noTask.Scope != sandbox.ScopeSession || noTask.OwnerID != "conv-21" {
|
|
t.Fatalf("reporter without task = %+v", noTask)
|
|
}
|
|
}
|
|
|
|
func TestResolveConversationSandbox(t *testing.T) {
|
|
savedTask := &db.Conversation{ID: 18, SandboxScope: "task", SandboxOwnerID: "5"}
|
|
scope, owner := resolveConversationSandbox(savedTask, 9, true)
|
|
if scope != "task" || owner != "5" {
|
|
t.Fatalf("stored task binding must win, got %s %s", scope, owner)
|
|
}
|
|
|
|
savedSession := &db.Conversation{ID: 19, SandboxScope: "session", SandboxOwnerID: "conv-19"}
|
|
scope, owner = resolveConversationSandbox(savedSession, 5, true)
|
|
if scope != "session" || owner != "conv-19" {
|
|
t.Fatalf("stored session binding must win over findings, got %s %s", scope, owner)
|
|
}
|
|
|
|
historical := &db.Conversation{ID: 20}
|
|
scope, owner = resolveConversationSandbox(historical, 7, true)
|
|
if scope != "task" || owner != "7" {
|
|
t.Fatalf("historical finding fallback = %s %s", scope, owner)
|
|
}
|
|
|
|
plain := &db.Conversation{ID: 21}
|
|
scope, owner = resolveConversationSandbox(plain, 0, false)
|
|
if scope != "session" || owner != "conv-21" {
|
|
t.Fatalf("plain conversation = %s %s", scope, owner)
|
|
}
|
|
}
|
|
|
|
func TestConversationListReturnsSandboxTarget(t *testing.T) {
|
|
s, _ := newRetestServer(t)
|
|
pg := s.m.pg
|
|
task, err := pg.CreateTask("conversation sandbox api", "goal", nil, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reporter, err := pg.CreateConversation("reporter", "historical report", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
merged, err := pg.CreateConversation("reporter", "merged report", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plain, err := pg.CreateConversation("auto", "plain", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_, _ = pg.Exec(`DELETE FROM findings WHERE report_conversation_id IN ($1,$2)`, reporter.ID, merged.ID)
|
|
_, _ = pg.Exec(`DELETE FROM conversations WHERE id IN ($1,$2,$3)`, reporter.ID, merged.ID, plain.ID)
|
|
_ = pg.DeleteTask(task.ID)
|
|
})
|
|
|
|
fid, err := pg.AddFinding(task.ID, 0, "xss", "name", "high", "summary", "evidence", "worker", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := pg.SetFindingReportConversationID(fid, reporter.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mergedFinding, err := pg.AddFinding(task.ID, 0, "xss", "name", "high", "summary", "evidence", "worker", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := pg.SetFindingReportConversationID(mergedFinding, merged.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := pg.SetConversationSandbox(merged.ID, "session", "conv-"+strconv.FormatInt(merged.ID, 10)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
w := retestRequest(s.pgListConversations, http.MethodGet, 0, "")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s", w.Code, w.Body)
|
|
}
|
|
var body struct {
|
|
Conversations []conversationListItem `json:"conversations"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found := map[int64]conversationListItem{}
|
|
for _, item := range body.Conversations {
|
|
if item.ID == reporter.ID || item.ID == merged.ID || item.ID == plain.ID {
|
|
found[item.ID] = item
|
|
}
|
|
}
|
|
if len(found) != 3 {
|
|
t.Fatalf("found %d conversations", len(found))
|
|
}
|
|
if found[reporter.ID].SandboxScope != "task" || found[reporter.ID].SandboxOwnerID != strconv.FormatInt(task.ID, 10) {
|
|
t.Fatalf("historical reporter = %+v", found[reporter.ID].Conversation)
|
|
}
|
|
if found[merged.ID].SandboxScope != "session" || found[merged.ID].SandboxOwnerID != "conv-"+strconv.FormatInt(merged.ID, 10) {
|
|
t.Fatalf("stored multi-task session = %+v", found[merged.ID].Conversation)
|
|
}
|
|
if found[plain.ID].SandboxScope != "session" || found[plain.ID].SandboxOwnerID != "conv-"+strconv.FormatInt(plain.ID, 10) {
|
|
t.Fatalf("plain conversation = %+v", found[plain.ID].Conversation)
|
|
}
|
|
}
|