Files
Charon/backend/internal/api/tests/integration_test.go
GitHub Actions 63cebf07ab Refactor services and improve error handling
- Updated file permissions in certificate_service_test.go and log_service_test.go to use octal notation.
- Added a new doc.go file to document the services package.
- Enhanced error handling in docker_service.go, log_service.go, notification_service.go, proxyhost_service.go, remoteserver_service.go, update_service.go, and uptime_service.go by logging errors when closing resources.
- Improved log_service.go to simplify log file processing and deduplication.
- Introduced CRUD tests for notification templates in notification_service_template_test.go.
- Removed the obsolete python_compile_check.sh script.
- Updated notification_service.go to improve template management functions.
- Added tests for uptime service notifications in uptime_service_notification_test.go.
2025-12-08 05:55:17 +00:00

72 lines
2.1 KiB
Go

package tests
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"github.com/Wikid82/charon/backend/internal/api/routes"
"github.com/Wikid82/charon/backend/internal/config"
)
// TestIntegration_WAF_BlockAndMonitor exercises middleware behavior and metrics exposure.
func TestIntegration_WAF_BlockAndMonitor(t *testing.T) {
gin.SetMode(gin.TestMode)
// Helper to spin server with given WAF mode
newServer := func(mode string) (*gin.Engine, *gorm.DB) {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("db open: %v", err)
}
cfg, err := config.Load()
if err != nil {
t.Fatalf("load cfg: %v", err)
}
cfg.Security.WAFMode = mode
r := gin.New()
if err := routes.Register(r, db, cfg); err != nil {
t.Fatalf("register: %v", err)
}
return r, db
}
// Block mode should reject suspicious payload on an API route covered by middleware
rBlock, _ := newServer("block")
req := httptest.NewRequest(http.MethodGet, "/api/v1/remote-servers?test=<script>", http.NoBody)
w := httptest.NewRecorder()
rBlock.ServeHTTP(w, req)
if w.Code == http.StatusOK {
t.Fatalf("expected block in block mode, got 200: body=%s", w.Body.String())
}
// Monitor mode should allow request but still evaluate (log-only)
rMon, _ := newServer("monitor")
req2 := httptest.NewRequest(http.MethodGet, "/api/v1/remote-servers?test=<script>", http.NoBody)
w2 := httptest.NewRecorder()
rMon.ServeHTTP(w2, req2)
if w2.Code != http.StatusOK {
t.Fatalf("unexpected status in monitor mode: %d", w2.Code)
}
// Metrics should be exposed
reqM := httptest.NewRequest(http.MethodGet, "/metrics", http.NoBody)
wM := httptest.NewRecorder()
rMon.ServeHTTP(wM, reqM)
if wM.Code != http.StatusOK {
t.Fatalf("metrics not served: %d", wM.Code)
}
body := wM.Body.String()
required := []string{"charon_waf_requests_total", "charon_waf_blocked_total", "charon_waf_monitored_total"}
for _, k := range required {
if !strings.Contains(body, k) {
t.Fatalf("missing metric %s in /metrics output", k)
}
}
}