Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

test: cover ai patrol and run-command scopes

rcourtmanc68e386d01f2
diff --git a/internal/api/security_regression_test.go b/internal/api/security_regression_test.go
index 2752a2ec7f..3549460ff3 100644
--- a/internal/api/security_regression_test.go
+++ b/internal/api/security_regression_test.go
@@ -792,6 +792,60 @@ func TestAIDebugContextRequiresSettingsReadScope(t *testing.T) {
}
}
+func TestAIRunCommandRequiresAIExecuteScope(t *testing.T) {
+ rawToken := "ai-run-token-123.12345678"
+ record := newTokenRecord(t, rawToken, []string{config.ScopeAIChat}, nil)
+ cfg := newTestConfigWithTokens(t, record)
+ router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+
+ req := httptest.NewRequest(http.MethodPost, "/api/ai/run-command", strings.NewReader(`{}`))
+ req.Header.Set("X-API-Token", rawToken)
+ rec := httptest.NewRecorder()
+ router.Handler().ServeHTTP(rec, req)
+ if rec.Code != http.StatusForbidden {
+ t.Fatalf("expected 403 for missing ai:execute scope, got %d", rec.Code)
+ }
+ if !strings.Contains(rec.Body.String(), config.ScopeAIExecute) {
+ t.Fatalf("expected missing scope response to mention %q, got %q", config.ScopeAIExecute, rec.Body.String())
+ }
+}
+
+func TestAIPatrolRunRequiresAIExecuteScope(t *testing.T) {
+ rawToken := "ai-patrol-run-token-123.12345678"
+ record := newTokenRecord(t, rawToken, []string{config.ScopeAIChat}, nil)
+ cfg := newTestConfigWithTokens(t, record)
+ router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+
+ req := httptest.NewRequest(http.MethodPost, "/api/ai/patrol/run", strings.NewReader(`{}`))
+ req.Header.Set("X-API-Token", rawToken)
+ rec := httptest.NewRecorder()
+ router.Handler().ServeHTTP(rec, req)
+ if rec.Code != http.StatusForbidden {
+ t.Fatalf("expected 403 for missing ai:execute scope, got %d", rec.Code)
+ }
+ if !strings.Contains(rec.Body.String(), config.ScopeAIExecute) {
+ t.Fatalf("expected missing scope response to mention %q, got %q", config.ScopeAIExecute, rec.Body.String())
+ }
+}
+
+func TestAIPatrolAutonomyRequiresSettingsWriteScope(t *testing.T) {
+ rawToken := "ai-patrol-autonomy-token-123.12345678"
+ record := newTokenRecord(t, rawToken, []string{config.ScopeSettingsRead}, nil)
+ cfg := newTestConfigWithTokens(t, record)
+ router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+
+ req := httptest.NewRequest(http.MethodGet, "/api/ai/patrol/autonomy", nil)
+ req.Header.Set("X-API-Token", rawToken)
+ rec := httptest.NewRecorder()
+ router.Handler().ServeHTTP(rec, req)
+ if rec.Code != http.StatusForbidden {
+ t.Fatalf("expected 403 for missing settings:write scope, got %d", rec.Code)
+ }
+ if !strings.Contains(rec.Body.String(), config.ScopeSettingsWrite) {
+ t.Fatalf("expected missing scope response to mention %q, got %q", config.ScopeSettingsWrite, rec.Body.String())
+ }
+}
+
func TestInfraUpdateReadEndpointsRequireMonitoringReadScope(t *testing.T) {
rawToken := "infra-read-token-123.12345678"
record := newTokenRecord(t, rawToken, []string{config.ScopeSettingsRead}, nil)