82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package tests
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"gestion/handlers"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func deliveryDetailsContext(username string, commandID int) (*gin.Context, *httptest.ResponseRecorder) {
|
|
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/livreur/deliveries/%d", commandID), nil)
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
c.Request = req
|
|
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", commandID)}}
|
|
c.Set("database", testDB)
|
|
c.Set("username", username)
|
|
c.Set("role", "livreur")
|
|
return c, rec
|
|
}
|
|
|
|
// GetDeliveryDetails doit exposer is_reward par item, au même titre que
|
|
// GetMyDeliveries (la liste) — sans quoi le modal "détails" côté livreur ne
|
|
// peut pas signaler un article récompense (gratuit ou -50%), ni afficher son
|
|
// prix effectif correctement.
|
|
func TestGetDeliveryDetails_ExposesIsRewardPerItem(t *testing.T) {
|
|
cleanupStockTestData(t)
|
|
client := newTestClient(t, "delivdetails_client")
|
|
livreur := newTestClient(t, "delivdetails_livreur")
|
|
paidProductID := newTestProduct(t, "DelivDetailsPaid", 20)
|
|
rewardProductID := newTestProduct(t, "DelivDetailsReward", 5)
|
|
|
|
cmdID := newTestCommandWithItem(t, client, "en_route", livreur, paidProductID, 1, 10)
|
|
insertRewardCommandItem(t, cmdID, rewardProductID, 1, 5, "pool_0")
|
|
|
|
c, rec := deliveryDetailsContext(livreur, cmdID)
|
|
handlers.GetDeliveryDetails(c)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status HTTP: got=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
var resp struct {
|
|
Success bool `json:"success"`
|
|
Delivery struct {
|
|
Items []struct {
|
|
Produit string `json:"produit"`
|
|
IsReward bool `json:"is_reward"`
|
|
} `json:"items"`
|
|
} `json:"delivery"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("décodage réponse: %v body=%s", err, rec.Body.String())
|
|
}
|
|
if !resp.Success {
|
|
t.Fatalf("réponse non successful: body=%s", rec.Body.String())
|
|
}
|
|
if len(resp.Delivery.Items) != 2 {
|
|
t.Fatalf("nombre d'items: got=%d want=2", len(resp.Delivery.Items))
|
|
}
|
|
|
|
var sawReward, sawPaid bool
|
|
for _, it := range resp.Delivery.Items {
|
|
if it.IsReward {
|
|
sawReward = true
|
|
} else {
|
|
sawPaid = true
|
|
}
|
|
}
|
|
if !sawReward {
|
|
t.Errorf("l'item récompense doit avoir is_reward=true dans la réponse: %+v", resp.Delivery.Items)
|
|
}
|
|
if !sawPaid {
|
|
t.Errorf("l'item payant doit avoir is_reward=false dans la réponse: %+v", resp.Delivery.Items)
|
|
}
|
|
}
|