@@ -0,0 +1,215 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"gestion/handlers"
|
||||
"gestion/models"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func statusAdminCtx(commandID int, status string) (*gin.Context, *httptest.ResponseRecorder) {
|
||||
body, _ := json.Marshal(map[string]string{"status": status})
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/v2/admin/protected/orders/x/status", bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = req
|
||||
c.Set("database", testDB)
|
||||
c.Set("role", "admin")
|
||||
c.Set("username", "admin_test")
|
||||
c.Params = gin.Params{{Key: "id", Value: strconv.Itoa(commandID)}}
|
||||
return c, rec
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// DeleteCommandByCabine / DeleteCommandAtomic — refus sur commande
|
||||
// déjà livrée/approuvée (plus de suppression physique d'une preuve de
|
||||
// transaction, quel que soit le rôle).
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
|
||||
func TestDeleteCommandByCabine_RejectsApprovedOrder(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "delcab_approved")
|
||||
productID := newTestProduct(t, "DelCabApproved", 10)
|
||||
cmdID := newTestCommandWithItem(t, username, "approved", "", productID, 1, 10)
|
||||
|
||||
c, rec := cmdContext(http.MethodDelete, testUserPrefix+"delcab_approved_actor", "cabine", nil, cmdID)
|
||||
handlers.DeleteCommandByCabine(c)
|
||||
|
||||
if rec.Code != http.StatusConflict {
|
||||
t.Fatalf("status HTTP: got=%d want=%d body=%s", rec.Code, http.StatusConflict, rec.Body.String())
|
||||
}
|
||||
var count int64
|
||||
testDB.GDB.Raw(`SELECT COUNT(*) FROM commandes WHERE id = ?`, cmdID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Errorf("la commande approuvée ne doit pas être supprimée: got=%d lignes want=1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteCommandByCabine_RejectsDeliveredOrder(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "delcab_livre")
|
||||
productID := newTestProduct(t, "DelCabLivre", 10)
|
||||
cmdID := newTestCommandWithItem(t, username, "livre", "", productID, 1, 10)
|
||||
|
||||
c, rec := cmdContext(http.MethodDelete, testUserPrefix+"delcab_livre_actor", "admin", nil, cmdID)
|
||||
handlers.DeleteCommandByCabine(c)
|
||||
|
||||
if rec.Code != http.StatusConflict {
|
||||
t.Fatalf("status HTTP: got=%d want=%d body=%s", rec.Code, http.StatusConflict, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Régression: le cas non-terminal (pending) doit toujours fonctionner.
|
||||
func TestDeleteCommandByCabine_StillWorksOnPendingOrder(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "delcab_pending_ok")
|
||||
productID := newTestProduct(t, "DelCabPendingOk", 10)
|
||||
cmdID := newTestCommandWithItem(t, username, "pending", "", productID, 1, 10)
|
||||
|
||||
c, rec := cmdContext(http.MethodDelete, testUserPrefix+"delcab_pending_actor", "cabine", nil, cmdID)
|
||||
handlers.DeleteCommandByCabine(c)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status HTTP: got=%d want=%d body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// UpdateCommandStatusAdmin — le passage à "approved" doit déléguer à
|
||||
// ApproveDeliveryAtomicByStaff (points crédités), pas un UPDATE brut.
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
|
||||
func TestUpdateCommandStatusAdmin_ApprovedDelegatesToStaffApproval(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "statusadmin_approve_ok")
|
||||
productID := newTestProduct(t, "StatusAdminApproveOk", 20)
|
||||
setPointsPoolsSettings(t, []models.PointsPool{
|
||||
{Key: "pool_0", Name: "Pool Test", Categories: []string{"test"}, Tiers: []models.PointsTier{{Min: 0, Max: 0, Points: 6}}},
|
||||
})
|
||||
cmdID := newTestCommandWithItem(t, username, "livre", "", productID, 1, 10)
|
||||
|
||||
c, rec := statusAdminCtx(cmdID, "approved")
|
||||
handlers.UpdateCommandStatusAdmin(c)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status HTTP: got=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if got := commandStatus(t, cmdID); got != "approved" {
|
||||
t.Errorf("statut: got=%s want=approved", got)
|
||||
}
|
||||
// Preuve que ça passe bien par le pipeline transactionnel et pas un
|
||||
// UPDATE brut : les points sont effectivement crédités.
|
||||
if got := clientPointsExtra(t, username)["pool_0"]; got != 6 {
|
||||
t.Errorf("points_extra après passage à 'approved' via ce endpoint: got=%d want=6 (doit passer par ApproveDeliveryAtomicByStaff)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Ne doit pas permettre de sauter l'étape "livre" depuis un statut trop en
|
||||
// amont (ApproveDeliveryAtomicByStaff a ses propres garde-fous de statut).
|
||||
func TestUpdateCommandStatusAdmin_ApprovedFromPendingIsRejected(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "statusadmin_approve_pending")
|
||||
productID := newTestProduct(t, "StatusAdminApprovePending", 20)
|
||||
setPointsPoolsSettings(t, []models.PointsPool{
|
||||
{Key: "pool_0", Name: "Pool Test", Categories: []string{"test"}, Tiers: []models.PointsTier{{Min: 0, Max: 0, Points: 6}}},
|
||||
})
|
||||
cmdID := newTestCommandWithItem(t, username, "cancelled", "", productID, 1, 10)
|
||||
|
||||
c, rec := statusAdminCtx(cmdID, "approved")
|
||||
handlers.UpdateCommandStatusAdmin(c)
|
||||
|
||||
if rec.Code == http.StatusOK {
|
||||
t.Fatalf("approuver une commande 'cancelled' ne doit pas réussir: body=%s", rec.Body.String())
|
||||
}
|
||||
if got := commandStatus(t, cmdID); got != "cancelled" {
|
||||
t.Errorf("le statut ne doit pas changer sur un rejet: got=%s want=cancelled", got)
|
||||
}
|
||||
if got := clientPointsExtra(t, username)["pool_0"]; got != 0 {
|
||||
t.Errorf("aucun point ne doit être crédité sur un rejet: got=%d want=0", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Régression: le passage à "cancelled" via ce même endpoint continue de
|
||||
// fonctionner (chemin déjà correct, CancelCommandByAdminAtomic).
|
||||
func TestUpdateCommandStatusAdmin_CancelledStillWorks(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "statusadmin_cancel_ok")
|
||||
productID := newTestProduct(t, "StatusAdminCancelOk", 20)
|
||||
cmdID := newTestCommandWithItem(t, username, "pending", "", productID, 1, 10)
|
||||
|
||||
c, rec := adminCancelCtx(cmdID)
|
||||
handlers.UpdateCommandStatusAdmin(c)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status HTTP: got=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if got := commandStatus(t, cmdID); got != "cancelled" {
|
||||
t.Errorf("statut: got=%s want=cancelled", got)
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// GetCommandPaymentStatus — IDOR : un client ne doit voir que le paiement
|
||||
// crypto de ses propres commandes.
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
|
||||
func newTestCryptoPayment(t *testing.T, commandID int) {
|
||||
t.Helper()
|
||||
if err := testDB.GDB.Exec(
|
||||
`INSERT INTO crypto_payments (command_id, nowpayment_id, status, price_amount, price_currency, pay_currency, pay_address, pay_amount)
|
||||
VALUES (?, 'np-test-id', 'waiting', 10.00, 'eur', 'btc', 'bc1qtestaddress', 0.0001)`,
|
||||
commandID,
|
||||
).Error; err != nil {
|
||||
t.Fatalf("newTestCryptoPayment: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCommandPaymentStatus_RejectsOtherClientsOrder(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
owner := newTestClient(t, "payment_idor_owner")
|
||||
intruder := newTestClient(t, "payment_idor_intruder")
|
||||
productID := newTestProduct(t, "PaymentIDORProduct", 10)
|
||||
cmdID := newTestCommandWithItem(t, owner, "pending_payment", "", productID, 1, 10)
|
||||
newTestCryptoPayment(t, cmdID)
|
||||
|
||||
c, rec := cmdContext(http.MethodGet, intruder, "client", nil, cmdID)
|
||||
handlers.GetCommandPaymentStatus(c)
|
||||
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("status HTTP: got=%d want=%d body=%s", rec.Code, http.StatusForbidden, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCommandPaymentStatus_AllowsOwner(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
owner := newTestClient(t, "payment_idor_owner_ok")
|
||||
productID := newTestProduct(t, "PaymentIDOROwnerOk", 10)
|
||||
cmdID := newTestCommandWithItem(t, owner, "pending_payment", "", productID, 1, 10)
|
||||
newTestCryptoPayment(t, cmdID)
|
||||
|
||||
c, rec := cmdContext(http.MethodGet, owner, "client", nil, cmdID)
|
||||
handlers.GetCommandPaymentStatus(c)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status HTTP: got=%d want=%d body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCommandPaymentStatus_UnknownCommandReturns404(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "payment_idor_unknown")
|
||||
|
||||
c, rec := cmdContext(http.MethodGet, username, "client", nil, 99999999)
|
||||
handlers.GetCommandPaymentStatus(c)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status HTTP: got=%d want=%d body=%s", rec.Code, http.StatusNotFound, rec.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user