mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/intercept"
|
|
)
|
|
|
|
func TestInterceptTaskPendingSummaryHTTP(t *testing.T) {
|
|
dsn, _, err := db.DSN()
|
|
if err != nil {
|
|
db.SkipPGUnavailable(t, err)
|
|
}
|
|
d, err := db.Open(dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = d.Close() })
|
|
task := "pending-summary-http"
|
|
t.Cleanup(func() { _, _ = d.Exec("DELETE FROM intercept_pending WHERE task_id IN ($1,$2)", task, task+"-other") })
|
|
s := &Server{m: &Manager{pg: d, interceptor: intercept.New(d)}, jwtKey: []byte("summary-test-signing-key")}
|
|
h := s.Handler()
|
|
token, err := signJWT(s.jwtKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, fixture := range []struct{ task, status string }{{task, "pending"}, {task, "allowed"}, {task + "-other", "pending"}} {
|
|
if _, err := d.CreateDecidedIntercept(0, 0, fixture.task, "mainagent", "Bash", []byte(`{"command":"private fixture"}`), fixture.status, "private reason"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
do := func(task string, auth bool) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(http.MethodGet, "/api/intercept/task/"+task+"/pending-summary", nil)
|
|
if auth {
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, r)
|
|
return rec
|
|
}
|
|
if rec := do(task, false); rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("unprotected summary: %d", rec.Code)
|
|
}
|
|
for _, tc := range []struct {
|
|
task string
|
|
total int64
|
|
}{{task, 1}, {task + "-empty", 0}} {
|
|
rec := do(tc.task, true)
|
|
var summary db.InterceptPendingSummary
|
|
if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &summary) != nil || summary.Total != tc.total || summary.Agents == nil {
|
|
t.Fatalf("summary: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
if rec.Body.Len() > 150 {
|
|
t.Fatalf("summary unexpectedly includes request data: %d bytes", rec.Body.Len())
|
|
}
|
|
}
|
|
// The existing history API still includes settled rows; it is not repurposed.
|
|
req := httptest.NewRequest(http.MethodGet, "/api/intercept/task/"+task, nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
var history struct {
|
|
Total int `json:"total"`
|
|
}
|
|
if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &history) != nil || history.Total != 2 {
|
|
t.Fatalf("history contract changed: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|