@@ -204,22 +204,58 @@ func TestDeleteCommandAtomic_DoesNotRefundAlreadyCancelledOrApprovedOrder(t *tes
|
||||
}
|
||||
}
|
||||
|
||||
// Régression: une commande "livrée" (marchandise déjà sortie du stock au
|
||||
// checkout, remise au client) ne doit pas voir son stock remboursé lors
|
||||
// d'une suppression — symétrique à CancelCommandByAdminAtomic qui exclut
|
||||
// déjà "livre" de son remboursement.
|
||||
func TestDeleteCommandAtomic_DoesNotRefundDeliveredOrder(t *testing.T) {
|
||||
// Sécurité: une commande déjà "livre" ou "approved" (preuve de transaction)
|
||||
// ne doit jamais pouvoir être supprimée physiquement, quel que soit le rôle
|
||||
// appelant (admin ou cabine) — seule une annulation logique reste possible.
|
||||
// La commande et son stock restent donc intacts.
|
||||
func TestDeleteCommandAtomic_RejectsDeliveredOrder(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "delete_admin_livre")
|
||||
productID := newTestProduct(t, "DeleteAdminLivre", 5)
|
||||
cmdID := newTestCommandWithItem(t, username, "livre", "", productID, 4, 40)
|
||||
|
||||
if err := testDB.DeleteCommandAtomic(cmdID, "admin_test", "admin"); err != nil {
|
||||
t.Fatalf("DeleteCommandAtomic: %v", err)
|
||||
if err := testDB.DeleteCommandAtomic(cmdID, "admin_test", "admin"); err == nil {
|
||||
t.Fatal("la suppression d'une commande 'livre' doit être refusée")
|
||||
}
|
||||
|
||||
if got := productStock(t, productID); got != 5 {
|
||||
t.Errorf("stock ne doit pas être remboursé pour une commande déjà livrée: got=%.2f want=5", got)
|
||||
t.Errorf("stock ne doit pas bouger pour une suppression refusée: got=%.2f want=5", got)
|
||||
}
|
||||
if got := commandStatus(t, cmdID); got != "livre" {
|
||||
t.Errorf("la commande ne doit pas être touchée: got status=%s want=livre", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteCommandAtomic_RejectsApprovedOrder(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "delete_admin_approved")
|
||||
productID := newTestProduct(t, "DeleteAdminApproved", 5)
|
||||
cmdID := newTestCommandWithItem(t, username, "approved", "", productID, 4, 40)
|
||||
|
||||
if err := testDB.DeleteCommandAtomic(cmdID, "admin_test", "admin"); err == nil {
|
||||
t.Fatal("la suppression d'une commande 'approved' doit être refusée")
|
||||
}
|
||||
|
||||
if got := productStock(t, productID); got != 5 {
|
||||
t.Errorf("stock ne doit pas bouger pour une suppression refusée: got=%.2f want=5", got)
|
||||
}
|
||||
var count int64
|
||||
testDB.GDB.Raw(`SELECT COUNT(*) FROM commandes WHERE id = ?`, cmdID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Errorf("la commande ne doit pas être supprimée: got=%d lignes restantes want=1", count)
|
||||
}
|
||||
}
|
||||
|
||||
// Appelé par un rôle "cabine" : même refus qu'un admin, le contrôle est au
|
||||
// niveau DB et ne dépend pas du rôle.
|
||||
func TestDeleteCommandAtomic_RejectsApprovedOrder_EvenForCabine(t *testing.T) {
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "delete_cabine_approved")
|
||||
productID := newTestProduct(t, "DeleteCabineApproved", 5)
|
||||
cmdID := newTestCommandWithItem(t, username, "approved", "", productID, 4, 40)
|
||||
|
||||
if err := testDB.DeleteCommandAtomic(cmdID, "cabine_test", "cabine"); err == nil {
|
||||
t.Fatal("la suppression d'une commande 'approved' doit être refusée pour cabine aussi")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user