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
117 lines
3.3 KiB
Go
Executable File
117 lines
3.3 KiB
Go
Executable File
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// ============================================
|
|
// Additional Coverage Tests for Quick Wins
|
|
// Target: Boost handlers coverage from 83.1% to 85%+
|
|
// ============================================
|
|
|
|
func TestUpdateAcquisitionConfigMissingContent(t *testing.T) {
|
|
h := newTestCrowdsecHandler(t, OpenTestDB(t), &fakeExec{}, "/bin/false", t.TempDir())
|
|
r := gin.New()
|
|
g := r.Group("/api/v1")
|
|
h.RegisterRoutes(g)
|
|
|
|
// Send empty JSON
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPut, "/api/v1/admin/crowdsec/acquisition", strings.NewReader("{}"))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusBadRequest, w.Code)
|
|
require.Contains(t, w.Body.String(), "content is required")
|
|
}
|
|
|
|
func TestUpdateAcquisitionConfigInvalidJSON(t *testing.T) {
|
|
h := newTestCrowdsecHandler(t, OpenTestDB(t), &fakeExec{}, "/bin/false", t.TempDir())
|
|
r := gin.New()
|
|
g := r.Group("/api/v1")
|
|
h.RegisterRoutes(g)
|
|
|
|
// Send invalid JSON
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPut, "/api/v1/admin/crowdsec/acquisition", strings.NewReader("invalid json"))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestGetLAPIDecisionsWithIPFilter(t *testing.T) {
|
|
mockExec := &mockCommandExecutor{output: []byte(`[]`), err: nil}
|
|
h := &CrowdsecHandler{
|
|
CmdExec: mockExec,
|
|
DataDir: t.TempDir(),
|
|
}
|
|
r := gin.New()
|
|
r.GET("/decisions", h.GetLAPIDecisions)
|
|
|
|
// Test with IP query parameter
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/decisions?ip=1.2.3.4", http.NoBody)
|
|
r.ServeHTTP(w, req)
|
|
|
|
// Should fallback to cscli-based ListDecisions
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
func TestGetLAPIDecisionsWithScopeFilter(t *testing.T) {
|
|
mockExec := &mockCommandExecutor{output: []byte(`[]`), err: nil}
|
|
h := &CrowdsecHandler{
|
|
CmdExec: mockExec,
|
|
DataDir: t.TempDir(),
|
|
}
|
|
r := gin.New()
|
|
r.GET("/decisions", h.GetLAPIDecisions)
|
|
|
|
// Test with scope query parameter
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/decisions?scope=ip", http.NoBody)
|
|
r.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
func TestGetLAPIDecisionsWithTypeFilter(t *testing.T) {
|
|
mockExec := &mockCommandExecutor{output: []byte(`[]`), err: nil}
|
|
h := &CrowdsecHandler{
|
|
CmdExec: mockExec,
|
|
DataDir: t.TempDir(),
|
|
}
|
|
r := gin.New()
|
|
r.GET("/decisions", h.GetLAPIDecisions)
|
|
|
|
// Test with type query parameter
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/decisions?type=ban", http.NoBody)
|
|
r.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
func TestGetLAPIDecisionsWithMultipleFilters(t *testing.T) {
|
|
mockExec := &mockCommandExecutor{output: []byte(`[]`), err: nil}
|
|
h := &CrowdsecHandler{
|
|
CmdExec: mockExec,
|
|
DataDir: t.TempDir(),
|
|
}
|
|
r := gin.New()
|
|
r.GET("/decisions", h.GetLAPIDecisions)
|
|
|
|
// Test with multiple query parameters
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/decisions?ip=1.2.3.4&scope=ip&type=ban", http.NoBody)
|
|
r.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
}
|