This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gestion/db"
|
||||
"gestion/handlers"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Si le livreur est déjà en route quand le client accepte une proposition
|
||||
// d'adresse (ProposeAddressChange autorise ce cas même en "en_route" — voir
|
||||
// commands.go ~L236-240), il doit être notifié que la destination a changé,
|
||||
// sans quoi il continuerait à naviguer vers l'ancienne adresse alors que la
|
||||
// nouvelle est déjà enregistrée en base (voir RespondToAddressProposal).
|
||||
func TestRespondToAddressProposal_NotifiesLivreurWhenAlreadyEnRoute(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("appelle le vrai service Nominatim en réseau — sauté en mode -short")
|
||||
}
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "respond_addr_notif_client")
|
||||
livreur := newTestClient(t, "respond_addr_notif_livreur")
|
||||
productID := newTestProduct(t, "RespondAddrNotif", 10)
|
||||
cmdID := newTestCommandWithItem(t, username, "en_route", livreur, productID, 1, 10)
|
||||
|
||||
if err := testDB.ProposeAddressChange(cmdID, "12 rue Crebillon, 44000 Nantes", "admin_test"); err != nil {
|
||||
t.Fatalf("ProposeAddressChange: %v", err)
|
||||
}
|
||||
|
||||
notifKey := fmt.Sprintf("notifications:%s", livreur)
|
||||
db.Redis.Del(db.RedisCtx, notifKey)
|
||||
|
||||
body := []byte(`{"accepted":true}`)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/commands/address/respond", 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("geoService", ensureTestGeoService())
|
||||
c.Set("username", username)
|
||||
c.Set("role", "client")
|
||||
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", cmdID)}}
|
||||
|
||||
handlers.RespondToAddressProposal(c)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("RespondToAddressProposal doit réussir: got=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
entries, err := db.Redis.LRange(db.RedisCtx, notifKey, 0, -1).Result()
|
||||
if err != nil {
|
||||
t.Fatalf("lecture notifications livreur: %v", err)
|
||||
}
|
||||
var found bool
|
||||
for _, raw := range entries {
|
||||
var notif struct {
|
||||
CommandID int `json:"command_id"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(raw), ¬if); err == nil && notif.CommandID == cmdID && notif.Type == "address_changed" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("le livreur déjà en route doit être notifié du changement d'adresse (type=address_changed, command_id=%d): notifications=%v", cmdID, entries)
|
||||
}
|
||||
}
|
||||
|
||||
// Si aucun livreur n'est assigné, il ne doit évidemment pas y avoir de
|
||||
// notification livreur envoyée (pas de panique/erreur non plus).
|
||||
func TestRespondToAddressProposal_NoLivreurNotificationWhenNoneAssigned(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("appelle le vrai service Nominatim en réseau — sauté en mode -short")
|
||||
}
|
||||
cleanupStockTestData(t)
|
||||
username := newTestClient(t, "respond_addr_notif_nolivreur")
|
||||
productID := newTestProduct(t, "RespondAddrNotifNoLivreur", 10)
|
||||
cmdID := newTestCommandWithItem(t, username, "pending", "", productID, 1, 10)
|
||||
|
||||
if err := testDB.ProposeAddressChange(cmdID, "12 rue Crebillon, 44000 Nantes", "admin_test"); err != nil {
|
||||
t.Fatalf("ProposeAddressChange: %v", err)
|
||||
}
|
||||
|
||||
body := []byte(`{"accepted":true}`)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/commands/address/respond", 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("geoService", ensureTestGeoService())
|
||||
c.Set("username", username)
|
||||
c.Set("role", "client")
|
||||
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", cmdID)}}
|
||||
|
||||
handlers.RespondToAddressProposal(c)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("RespondToAddressProposal doit réussir: got=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user