152 lines
4.3 KiB
Go
152 lines
4.3 KiB
Go
package ldmod_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm/schema"
|
|
|
|
"backend/internal/modules/ldmod"
|
|
)
|
|
|
|
// fakeRepository garde la ligne unique en mémoire. Comme le vrai dépôt,
|
|
// Update remplace TOUS les champs.
|
|
type fakeRepository struct {
|
|
mu sync.Mutex
|
|
row *ldmod.LDMod
|
|
}
|
|
|
|
func newFakeRepository() *fakeRepository {
|
|
return &fakeRepository{row: &ldmod.LDMod{ID: 1, ToggleEnabled: true, DefaultTheme: ldmod.ThemeDark}}
|
|
}
|
|
|
|
func (r *fakeRepository) Get(context.Context) (*ldmod.LDMod, error) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
cp := *r.row
|
|
return &cp, nil
|
|
}
|
|
|
|
func (r *fakeRepository) Update(_ context.Context, m *ldmod.LDMod) (*ldmod.LDMod, error) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
cp := *m
|
|
cp.ID = 1
|
|
r.row = &cp
|
|
out := cp
|
|
return &out, nil
|
|
}
|
|
|
|
func newRouter() *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
h := ldmod.NewHandler(ldmod.NewService(newFakeRepository()))
|
|
api := r.Group("/api")
|
|
ldmod.RegisterPublicRoutes(api, h)
|
|
ldmod.RegisterAdminRoutes(api, h, func(c *gin.Context) { c.Next() })
|
|
return r
|
|
}
|
|
|
|
func do(r *gin.Engine, method, path, body string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
return w
|
|
}
|
|
|
|
func decode(t *testing.T, w *httptest.ResponseRecorder) map[string]any {
|
|
t.Helper()
|
|
var m map[string]any
|
|
if err := json.Unmarshal(w.Body.Bytes(), &m); err != nil {
|
|
t.Fatalf("réponse JSON invalide: %v (%s)", err, w.Body)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestGet_ReturnsCurrentSettings(t *testing.T) {
|
|
w := do(newRouter(), "GET", "/api/theme-settings", "")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("HTTP %d, attendu 200", w.Code)
|
|
}
|
|
m := decode(t, w)
|
|
if m["toggle_enabled"] != true || m["default_theme"] != "dark" {
|
|
t.Errorf("valeurs par défaut inattendues: %v", m)
|
|
}
|
|
}
|
|
|
|
func TestUpdate_CanDisableToggleAndForceLight(t *testing.T) {
|
|
r := newRouter()
|
|
w := do(r, "PUT", "/api/admin/theme-settings", `{"toggle_enabled":false,"default_theme":"light"}`)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("HTTP %d: %s", w.Code, w.Body)
|
|
}
|
|
m := decode(t, w)
|
|
if m["toggle_enabled"] != false || m["default_theme"] != "light" {
|
|
t.Errorf("valeurs = %v", m)
|
|
}
|
|
// toggle_enabled=false doit bien persister (pas ignoré comme un zéro de struct Go).
|
|
if got := decode(t, do(r, "GET", "/api/theme-settings", "")); got["toggle_enabled"] != false {
|
|
t.Errorf("le GET public ne reflète pas la désactivation: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestUpdate_RejectsInvalidTheme(t *testing.T) {
|
|
r := newRouter()
|
|
w := do(r, "PUT", "/api/admin/theme-settings", `{"toggle_enabled":true,"default_theme":"purple"}`)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("HTTP %d, attendu 400", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestUpdate_RejectsInvalidBody(t *testing.T) {
|
|
w := do(newRouter(), "PUT", "/api/admin/theme-settings", `not json`)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("HTTP %d, attendu 400", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminRoute_RequiresAdminMiddleware(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
h := ldmod.NewHandler(ldmod.NewService(newFakeRepository()))
|
|
api := r.Group("/api")
|
|
ldmod.RegisterPublicRoutes(api, h)
|
|
ldmod.RegisterAdminRoutes(api, h, func(c *gin.Context) { c.AbortWithStatus(http.StatusUnauthorized) })
|
|
|
|
if w := do(r, "PUT", "/api/admin/theme-settings", `{"toggle_enabled":true,"default_theme":"dark"}`); w.Code != http.StatusUnauthorized {
|
|
t.Errorf("PUT admin sans droit : HTTP %d, attendu 401", w.Code)
|
|
}
|
|
if w := do(r, "GET", "/api/theme-settings", ""); w.Code != http.StatusOK {
|
|
t.Errorf("GET public : HTTP %d, attendu 200", w.Code)
|
|
}
|
|
}
|
|
|
|
// Les noms de colonnes de la migration 000035 doivent être ceux que GORM
|
|
// déduit des champs de la struct.
|
|
func TestModelColumnsMatchMigration(t *testing.T) {
|
|
want := []string{"id", "toggle_enabled", "default_theme"}
|
|
s, err := schema.Parse(&ldmod.LDMod{}, &sync.Map{}, schema.NamingStrategy{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]bool{}
|
|
for _, f := range s.Fields {
|
|
got[f.DBName] = true
|
|
}
|
|
for _, col := range want {
|
|
if !got[col] {
|
|
t.Errorf("colonne %q absente du modèle (colonnes GORM: %v)", col, s.DBNames)
|
|
}
|
|
}
|
|
if s.Table != "ld_mod" {
|
|
t.Errorf("table %q, attendu ld_mod", s.Table)
|
|
}
|
|
}
|