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

100 lines
2.7 KiB
Go

package handlers
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"github.com/Wikid82/charon/backend/internal/models"
)
func setupFlagsDB(t *testing.T) *gorm.DB {
db := OpenTestDB(t)
if err := db.AutoMigrate(&models.Setting{}); err != nil {
t.Fatalf("auto migrate failed: %v", err)
}
return db
}
func TestFeatureFlags_GetAndUpdate(t *testing.T) {
db := setupFlagsDB(t)
h := NewFeatureFlagsHandler(db)
gin.SetMode(gin.TestMode)
r := gin.New()
r.GET("/api/v1/feature-flags", h.GetFlags)
r.PUT("/api/v1/feature-flags", h.UpdateFlags)
// 1) GET should return all default flags (as keys)
req := httptest.NewRequest(http.MethodGet, "/api/v1/feature-flags", http.NoBody)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 got %d body=%s", w.Code, w.Body.String())
}
var flags map[string]bool
if err := json.Unmarshal(w.Body.Bytes(), &flags); err != nil {
t.Fatalf("invalid json: %v", err)
}
// ensure keys present
for _, k := range defaultFlags {
if _, ok := flags[k]; !ok {
t.Fatalf("missing default flag key: %s", k)
}
}
// 2) PUT update a single flag
payload := map[string]bool{
defaultFlags[0]: true,
}
b, _ := json.Marshal(payload)
req2 := httptest.NewRequest(http.MethodPut, "/api/v1/feature-flags", bytes.NewReader(b))
req2.Header.Set("Content-Type", "application/json")
w2 := httptest.NewRecorder()
r.ServeHTTP(w2, req2)
if w2.Code != http.StatusOK {
t.Fatalf("expected 200 on update got %d body=%s", w2.Code, w2.Body.String())
}
// confirm DB persisted
var s models.Setting
if err := db.Where("key = ?", defaultFlags[0]).First(&s).Error; err != nil {
t.Fatalf("expected setting persisted, db error: %v", err)
}
if s.Value != "true" {
t.Fatalf("expected stored value 'true' got '%s'", s.Value)
}
}
func TestFeatureFlags_EnvFallback(t *testing.T) {
// Ensure env fallback is used when DB not present
t.Setenv("FEATURE_CERBERUS_ENABLED", "true")
db := OpenTestDB(t)
// Do not write any settings so DB lookup fails and env is used
h := NewFeatureFlagsHandler(db)
gin.SetMode(gin.TestMode)
r := gin.New()
r.GET("/api/v1/feature-flags", h.GetFlags)
req := httptest.NewRequest(http.MethodGet, "/api/v1/feature-flags", http.NoBody)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 got %d body=%s", w.Code, w.Body.String())
}
var flags map[string]bool
if err := json.Unmarshal(w.Body.Bytes(), &flags); err != nil {
t.Fatalf("invalid json: %v", err)
}
if !flags["feature.cerberus.enabled"] {
t.Fatalf("expected feature.cerberus.enabled to be true via env fallback")
}
}