Files
Charon/backend/internal/api/handlers/crowdsec_cache_verification_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

92 lines
2.5 KiB
Go
Executable File

package handlers
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/Wikid82/charon/backend/internal/crowdsec"
)
// TestListPresetsShowsCachedStatus verifies the /presets endpoint marks cached presets.
func TestListPresetsShowsCachedStatus(t *testing.T) {
cacheDir := t.TempDir()
dataDir := t.TempDir()
cache, err := crowdsec.NewHubCache(cacheDir, time.Hour)
require.NoError(t, err)
// Cache a preset
ctx := context.Background()
archive := []byte("archive")
_, err = cache.Store(ctx, "test/cached", "etag", "hub", "preview", archive)
require.NoError(t, err)
// Setup handler
hub := crowdsec.NewHubService(nil, cache, dataDir)
db := OpenTestDB(t)
handler := newTestCrowdsecHandler(t, db, &fakeExec{}, "/bin/false", dataDir)
handler.Hub = hub
r := gin.New()
g := r.Group("/api/v1")
handler.RegisterRoutes(g)
// List presets
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/crowdsec/presets", http.NoBody)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)
var result map[string]any
err = json.Unmarshal(resp.Body.Bytes(), &result)
require.NoError(t, err)
presets := result["presets"].([]any)
require.NotEmpty(t, presets, "Should have at least one preset")
// Find our cached preset
found := false
for _, p := range presets {
preset := p.(map[string]any)
if preset["slug"] == "test/cached" {
found = true
require.True(t, preset["cached"].(bool), "Preset should be marked as cached")
require.NotEmpty(t, preset["cache_key"], "Should have cache_key")
}
}
require.True(t, found, "Cached preset should appear in list")
}
// TestCacheKeyPersistence verifies cache keys are consistent and retrievable.
func TestCacheKeyPersistence(t *testing.T) {
cacheDir := t.TempDir()
cache, err := crowdsec.NewHubCache(cacheDir, time.Hour)
require.NoError(t, err)
// Store a preset
ctx := context.Background()
archive := []byte("test archive")
meta, err := cache.Store(ctx, "test/preset", "etag123", "hub", "preview text", archive)
require.NoError(t, err)
originalCacheKey := meta.CacheKey
require.NotEmpty(t, originalCacheKey, "Cache key should be generated")
// Load it back
loaded, err := cache.Load(ctx, "test/preset")
require.NoError(t, err)
require.Equal(t, originalCacheKey, loaded.CacheKey, "Cache key should persist")
require.Equal(t, "test/preset", loaded.Slug)
require.Equal(t, "etag123", loaded.Etag)
}