ARTEX/db/task_scope_egress_test.go
c4nc c620807fdc fix: review minors: propagate company egress reads, stale comment, changelog
CompanyEgressForTask errors no longer skip a company's grants: the
caller returns TargetGateUnavailable instead of a silently narrowed set.
The TestTaskEgressGrants doc no longer claims allow rules participate
(grants-only by design since the gate test that pinned that invariant),
and the orphan Security line under the Unreleased heading is gone.
2026-12-02 17:19:17 +01:00

87 lines
3.3 KiB
Go

package db
import (
"strings"
"testing"
)
// TestTaskEgressGrants checks the gate-facing view of a task's approved
// egress: the task's own operator-approved unexpanded rows and the entries
// frozen by approved company grants, normalized as the sandbox proxy builds
// them. Unapproved and expanded rows grant nothing, and allow rules are not
// part of the set.
func TestTaskEgressGrants(t *testing.T) {
dsn := scratchDSN(t, "egress_grants")
d, err := Open(dsn)
if err != nil {
t.Fatalf("open: %v", err)
}
defer d.Close()
var companyID, expID, taskID int64
if err := d.QueryRow(`INSERT INTO companies(name, nkey) VALUES ('Grant Corp', 'grant corp') RETURNING id`).Scan(&companyID); err != nil {
t.Fatalf("insert company: %v", err)
}
if err := d.QueryRow(`INSERT INTO explorations(goal) VALUES ('grant fixture') RETURNING id`).Scan(&expID); err != nil {
t.Fatalf("insert exploration: %v", err)
}
if err := d.QueryRow(`INSERT INTO tasks(name, description, goal, exploration_id) VALUES ('grants', 'grants', 'grants', $1) RETURNING id`, expID).Scan(&taskID); err != nil {
t.Fatalf("insert task: %v", err)
}
seeds := []struct {
stmt string
args []any
}{
{`INSERT INTO task_scope(task_id, kind, net, source, approved_by_operator) VALUES ($1, 'ip', '192.0.2.5/32', 'manual', true)`, []any{taskID}},
{`INSERT INTO task_scope(task_id, kind, domain, source, approved_by_operator) VALUES ($1, 'root_domain', 'unapproved.test', 'agent', false)`, []any{taskID}},
{`INSERT INTO task_scope(task_id, kind, domain, source, approved_by_operator, expanded) VALUES ($1, 'subdomain', 'expanded.test', 'manual', true, true)`, []any{taskID}},
{`INSERT INTO task_scope(task_id, kind, company_id, source, approved_by_operator) VALUES ($1, 'company', $2, 'manual', true)`, []any{taskID, companyID}},
{`INSERT INTO task_scope_egress(task_id, kind, value, company_id) VALUES ($1, 'domain', '*.Lab.test', $2), ($1, 'cidr', '198.51.100.0/24', $2)`, []any{taskID, companyID}},
}
for _, s := range seeds {
if _, err := d.Exec(s.stmt, s.args...); err != nil {
t.Fatalf("seed (%s): %v", s.stmt, err)
}
}
grants, err := d.TaskEgressGrants(taskID)
if err != nil {
t.Fatalf("TaskEgressGrants: %v", err)
}
byKind := map[string]map[string]bool{}
for _, g := range grants {
if byKind[g.Kind] == nil {
byKind[g.Kind] = map[string]bool{}
}
byKind[g.Kind][g.Value] = true
}
for _, want := range []string{"lab.test"} {
if !byKind["domain"][want] {
t.Errorf("domains %v, want %s", byKind["domain"], want)
}
}
for _, bad := range []string{"unapproved.test", "expanded.test", "*.lab.test", "*.Lab.test"} {
if byKind["domain"][bad] {
t.Errorf("domains %v, must not hold %s", byKind["domain"], bad)
}
}
// net::text may render a /32 with or without the prefix length.
cidrOK := false
for v := range byKind["cidr"] {
if strings.HasPrefix(v, "192.0.2.5") {
cidrOK = true
}
}
if !cidrOK {
t.Errorf("cidrs %v, want the approved 192.0.2.5", byKind["cidr"])
}
for _, want := range []string{"198.51.100.0/24"} {
if !byKind["cidr"][want] {
t.Errorf("cidrs %v, want %s", byKind["cidr"], want)
}
}
if len(byKind["cidr"]) != 2 {
t.Errorf("cidrs %v, want exactly the approved ip and the frozen company range", byKind["cidr"])
}
if got, err := d.TaskEgressGrants(0); err != nil || got != nil {
t.Errorf("TaskEgressGrants(0) = %v, %v, want nil, nil", got, err)
}
}