60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package tests
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"gestion/handlers"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func statsSectionContext(section string) (*gin.Context, *httptest.ResponseRecorder) {
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/stats/reset", nil)
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
c.Request = req
|
|
c.Set("database", testDB)
|
|
c.Params = gin.Params{{Key: "section", Value: section}}
|
|
return c, rec
|
|
}
|
|
|
|
func TestResetAdminStats_RejectsInvalidSection(t *testing.T) {
|
|
c, rec := statsSectionContext("section-inexistante")
|
|
handlers.ResetAdminStats(c)
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Errorf("section invalide doit retourner 400: got=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestResetAdminStats_SuccessResetsSection(t *testing.T) {
|
|
c, rec := statsSectionContext("commandes")
|
|
handlers.ResetAdminStats(c)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("reset d'une section valide doit réussir: got=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
if !bytes.Contains(rec.Body.Bytes(), []byte(`"success":true`)) {
|
|
t.Errorf("réponse doit indiquer success: body=%s", rec.Body.String())
|
|
}
|
|
resetAt := testDB.ReadResetAt("stats_reset_commandes_at")
|
|
if resetAt.IsZero() {
|
|
t.Error("le timestamp de reset doit être renseigné après ResetAdminStats")
|
|
}
|
|
}
|
|
|
|
func TestGetMyDeliveryStats_RejectsNonLivreurRole(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/livreur/stats", nil)
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
c.Request = req
|
|
c.Set("database", testDB)
|
|
c.Set("username", testUserPrefix+"stats_role")
|
|
c.Set("role", "client")
|
|
handlers.GetMyDeliveryStats(c)
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Errorf("role client doit être refusé: got=%d want=403", rec.Code)
|
|
}
|
|
}
|