Files
Charon/backend/internal/api/handlers/crowdsec_coverage_gap_test.go
akanealw eec8c28fb3
Some checks are pending
Go Benchmark / Performance Regression Check (push) Waiting to run
Cerberus Integration / Cerberus Security Stack Integration (push) Waiting to run
Upload Coverage to Codecov / Backend Codecov Upload (push) Waiting to run
Upload Coverage to Codecov / Frontend Codecov Upload (push) Waiting to run
CodeQL - Analyze / CodeQL analysis (go) (push) Waiting to run
CodeQL - Analyze / CodeQL analysis (javascript-typescript) (push) Waiting to run
CrowdSec Integration / CrowdSec Bouncer Integration (push) Waiting to run
Docker Build, Publish & Test / build-and-push (push) Waiting to run
Docker Build, Publish & Test / Security Scan PR Image (push) Blocked by required conditions
Quality Checks / Auth Route Protection Contract (push) Waiting to run
Quality Checks / Codecov Trigger/Comment Parity Guard (push) Waiting to run
Quality Checks / Backend (Go) (push) Waiting to run
Quality Checks / Frontend (React) (push) Waiting to run
Rate Limit integration / Rate Limiting Integration (push) Waiting to run
Security Scan (PR) / Trivy Binary Scan (push) Waiting to run
Supply Chain Verification (PR) / Verify Supply Chain (push) Waiting to run
WAF integration / Coraza WAF Integration (push) Waiting to run
changed perms
2026-04-22 18:19:14 +00:00

106 lines
2.9 KiB
Go
Executable File

package handlers
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/Wikid82/charon/backend/internal/crowdsec"
)
// MockCommandExecutor implements handlers.CommandExecutor and crowdsec.CommandExecutor
type MockCommandExecutor struct {
mock.Mock
}
func (m *MockCommandExecutor) Execute(ctx context.Context, name string, args ...string) ([]byte, error) {
call := m.Called(ctx, name, args)
return call.Get(0).([]byte), call.Error(1)
}
func (m *MockCommandExecutor) ExecuteWithEnv(ctx context.Context, name string, args []string, env map[string]string) ([]byte, error) {
call := m.Called(ctx, name, args, env)
return call.Get(0).([]byte), call.Error(1)
}
// TestConsoleEnrollMissingKey covers the "enrollment_key required" branch
func TestConsoleEnrollMissingKey(t *testing.T) {
mockExec := new(MockCommandExecutor)
// Create real service
consoleSvc := crowdsec.NewConsoleEnrollmentService(nil, mockExec, "/tmp", "")
h := &CrowdsecHandler{
Console: consoleSvc,
}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request, _ = http.NewRequest("POST", "/enroll", bytes.NewBufferString(`{"agent_name": "test-agent"}`))
c.Request.Header.Set("Content-Type", "application/json")
t.Setenv("FEATURE_CROWDSEC_CONSOLE_ENROLLMENT", "1")
h.ConsoleEnroll(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "enrollment_key required")
}
// TestGetCachedPreset_ValidationAndMiss covers path param validation empty check (if any) and cache miss
func TestGetCachedPreset_ValidationAndMiss(t *testing.T) {
tmpDir := t.TempDir()
cache, _ := crowdsec.NewHubCache(tmpDir, time.Hour)
mockExec := new(MockCommandExecutor)
hubSvc := crowdsec.NewHubService(mockExec, cache, tmpDir)
h := &CrowdsecHandler{
Hub: hubSvc,
Console: nil,
}
t.Setenv("FEATURE_CERBERUS_ENABLED", "1")
w := httptest.NewRecorder()
_, r := gin.CreateTestContext(w)
r.GET("/api/v1/presets/:slug", h.GetCachedPreset)
req, _ := http.NewRequest(http.MethodGet, "/api/v1/presets/valid-slug", nil)
r.ServeHTTP(w, req)
// Expect 404 on cache miss
assert.Equal(t, http.StatusNotFound, w.Code)
assert.Contains(t, w.Body.String(), "cache miss")
}
func TestGetCachedPreset_SlugRequired(t *testing.T) {
h := &CrowdsecHandler{}
t.Setenv("FEATURE_CERBERUS_ENABLED", "1")
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
// Manually set params with empty slug
c.Params = []gin.Param{{Key: "slug", Value: " "}}
c.Request = httptest.NewRequest("GET", "/api", nil)
tmpDir := t.TempDir()
cache, _ := crowdsec.NewHubCache(tmpDir, time.Hour)
h.Hub = crowdsec.NewHubService(&MockCommandExecutor{}, cache, tmpDir)
h.GetCachedPreset(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "slug required")
}