chore: build

This commit is contained in:
Xor290
2026-09-08 17:33:32 +02:00
parent 42ed11bc22
commit 624df79974
14 changed files with 1066 additions and 60 deletions
@@ -1,9 +1,16 @@
package tests
import (
"bytes"
"encoding/json"
"gestion/db"
"gestion/handlers"
"gestion/models"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
// resetSettingsAfterTest restaure les settings par défaut à la fin du test —
@@ -304,3 +311,80 @@ func TestUpdateSettings_ColorAndGradientFieldsRoundTrip(t *testing.T) {
t.Errorf("client_title_gradient: got from=%q to=%q", loaded.ClientTitleGradientFrom, loaded.ClientTitleGradientTo)
}
}
// Reproduit exactement le flux réel de l'admin : PUT /settings avec le JSON
// tel qu'envoyé par le frontend (category_configs[].products, en mode
// sélection), puis GET /settings pour vérifier ce qui revient — contrairement
// aux autres tests de ce fichier qui appellent testDB.UpdateSettings /
// GetSettings directement en Go, en contournant le binding JSON HTTP réel.
func TestUpdateSettingsHTTP_CategoryConfigProductsSurviveSaveReload(t *testing.T) {
resetSettingsAfterTest(t)
s := db.DefaultSettings()
s.PointsReward = &models.PointsReward{
Threshold: 20,
CategoryConfigs: []models.RewardCategoryConfig{
{
Category: "test",
Type: "free_product",
AllProducts: false,
Products: []models.RewardProductQuantity{
{ProductID: 111, Quantity: 2},
{ProductID: 222, Quantity: 1},
},
},
},
}
body, err := json.Marshal(s)
if err != nil {
t.Fatalf("json.Marshal: %v", err)
}
putReq := httptest.NewRequest(http.MethodPut, "/api/v2/admin/protected/settings", bytes.NewReader(body))
putReq.Header.Set("Content-Type", "application/json")
putRec := httptest.NewRecorder()
putCtx, _ := gin.CreateTestContext(putRec)
putCtx.Request = putReq
putCtx.Set("database", testDB)
handlers.UpdateSettings(putCtx)
if putRec.Code != http.StatusOK {
t.Fatalf("PUT /settings: status=%d body=%s", putRec.Code, putRec.Body.String())
}
getReq := httptest.NewRequest(http.MethodGet, "/api/v2/admin/protected/settings", nil)
getRec := httptest.NewRecorder()
getCtx, _ := gin.CreateTestContext(getRec)
getCtx.Request = getReq
getCtx.Set("database", testDB)
handlers.GetSettings(getCtx)
if getRec.Code != http.StatusOK {
t.Fatalf("GET /settings: status=%d body=%s", getRec.Code, getRec.Body.String())
}
var resp struct {
Settings models.AppSettings `json:"settings"`
}
if err := json.Unmarshal(getRec.Body.Bytes(), &resp); err != nil {
t.Fatalf("décodage réponse GET: %v body=%s", err, getRec.Body.String())
}
if resp.Settings.PointsReward == nil {
t.Fatalf("points_reward est nil après reload")
}
if len(resp.Settings.PointsReward.CategoryConfigs) != 1 {
t.Fatalf("category_configs: got=%d want=1: %+v", len(resp.Settings.PointsReward.CategoryConfigs), resp.Settings.PointsReward.CategoryConfigs)
}
cfg := resp.Settings.PointsReward.CategoryConfigs[0]
if len(cfg.Products) != 2 {
t.Fatalf("products: got=%d want=2 (produits sélectionnés non persistés): %+v", len(cfg.Products), cfg.Products)
}
if cfg.Products[0].ProductID != 111 || cfg.Products[0].Quantity != 2 {
t.Errorf("products[0]: got=%+v want={ProductID:111 Quantity:2}", cfg.Products[0])
}
if cfg.Products[1].ProductID != 222 || cfg.Products[1].Quantity != 1 {
t.Errorf("products[1]: got=%+v want={ProductID:222 Quantity:1}", cfg.Products[1])
}
}