feat(checkout): let the admin choose the details asked at checkout
The admin enables or disables each option (phone, address, Snapchat, Instagram, Telegram, SimpleX, WhatsApp, Signal, notes, preferred time) from a new "Checkout fields" page; the customer's name is always asked. Backend - checkoutfields module: single-row settings (CHECK id = 1), public GET and admin GET/PUT. - orders: answers stored as JSONB (contacts, info_sup); the customer phone moves from customer_phone to contacts.phone (migration 000034 copies existing values before dropping the column). - Orders are validated against the live settings: disabled options and unknown keys are dropped, enabled phone/address are required, and format, length and control characters are checked. - The Telegram notification includes the answers. Frontend - Checkout form generated from the enabled options; admin settings page; answers shown in the order list and detail. - API calls live in src/api (checkoutFields.ts, orders.ts); ApiError keeps the response body so the rejected field can be reported. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
178652ef14
commit
f9bfdc9d3c
@@ -0,0 +1,255 @@
|
||||
package orders_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"backend/internal/modules/checkoutfields"
|
||||
"backend/internal/modules/orders"
|
||||
)
|
||||
|
||||
// create passe une commande valide (un article) avec les réponses données.
|
||||
func (f *testFixture) create(in orders.CreateInput) (*orders.Order, error) {
|
||||
in.Items = []orders.ItemInput{{ProductID: f.productID, PriceTierID: f.tierID, Multiplier: 1}}
|
||||
order, _, err := f.service().Create(context.Background(), in)
|
||||
return order, err
|
||||
}
|
||||
|
||||
func requireFieldError(t *testing.T, err error, field, reason string) {
|
||||
t.Helper()
|
||||
if !errors.Is(err, orders.ErrInvalidField) {
|
||||
t.Fatalf("erreur = %v, attendu ErrInvalidField", err)
|
||||
}
|
||||
var fe *orders.FieldError
|
||||
if !errors.As(err, &fe) || fe.Field != field || fe.Reason != reason {
|
||||
t.Fatalf("erreur = %v, attendu champ %q raison %q", err, field, reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_NameIsAlwaysRequired(t *testing.T) {
|
||||
for _, name := range []string{"", " "} {
|
||||
_, err := newFixture().create(orders.CreateInput{CustomerName: name})
|
||||
requireFieldError(t, err, "customer_name", orders.ReasonRequired)
|
||||
}
|
||||
_, err := newFixture().create(orders.CreateInput{CustomerName: strings.Repeat("a", 101)})
|
||||
requireFieldError(t, err, "customer_name", orders.ReasonTooLong)
|
||||
}
|
||||
|
||||
func TestCheckout_DisabledFieldsAreDroppedNotStored(t *testing.T) {
|
||||
f := newFixture() // aucune option activée
|
||||
order, err := f.create(orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
Notes: "sonnez deux fois",
|
||||
Contacts: orders.Contacts{orders.ContactPhone: "+33612345678", orders.ContactSnap: "alice"},
|
||||
InfoSup: orders.InfoSup{orders.ContactAddress: "12 rue de Paris", orders.ContactHour: "18:30"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v (un champ désactivé doit être ignoré, pas refusé)", err)
|
||||
}
|
||||
if len(order.Contacts) != 0 || len(order.InfoSup) != 0 || order.Notes != "" {
|
||||
t.Errorf("réponses à des options désactivées conservées: contacts=%v info_sup=%v notes=%q",
|
||||
order.Contacts, order.InfoSup, order.Notes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_UnknownKeysAreDropped(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutSignal = true
|
||||
order, err := f.create(orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
Contacts: orders.Contacts{orders.ContactSignal: "alice.01", "admin": "true", "<script>": "x"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(order.Contacts) != 1 || order.Contacts[orders.ContactSignal] != "alice.01" {
|
||||
t.Errorf("contacts = %v, attendu seulement signal", order.Contacts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_EnabledPhoneAndAddressAreRequired(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutPhone = true
|
||||
f.settings.fields.CheckoutAddress = true
|
||||
|
||||
_, err := f.create(orders.CreateInput{CustomerName: "Alice", InfoSup: orders.InfoSup{orders.ContactAddress: "1 rue X"}})
|
||||
requireFieldError(t, err, "phone", orders.ReasonRequired)
|
||||
|
||||
_, err = f.create(orders.CreateInput{CustomerName: "Alice", Contacts: orders.Contacts{orders.ContactPhone: "0612345678"}})
|
||||
requireFieldError(t, err, "address", orders.ReasonRequired)
|
||||
|
||||
// Une valeur d'espaces ne compte pas comme une réponse.
|
||||
_, err = f.create(orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
Contacts: orders.Contacts{orders.ContactPhone: "0612345678"},
|
||||
InfoSup: orders.InfoSup{orders.ContactAddress: " "},
|
||||
})
|
||||
requireFieldError(t, err, "address", orders.ReasonRequired)
|
||||
|
||||
order, err := f.create(orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
Contacts: orders.Contacts{orders.ContactPhone: " 06 12 34 56 78 "},
|
||||
InfoSup: orders.InfoSup{orders.ContactAddress: " 1 rue X "},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if order.Contacts[orders.ContactPhone] != "06 12 34 56 78" || order.InfoSup[orders.ContactAddress] != "1 rue X" {
|
||||
t.Errorf("valeurs non nettoyées: %v %v", order.Contacts, order.InfoSup)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_MessengersAreOptionalWhenEnabled(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutSnap = true
|
||||
f.settings.fields.CheckoutTelegram = true
|
||||
f.settings.fields.CheckoutHour = true
|
||||
order, err := f.create(orders.CreateInput{CustomerName: "Alice", Contacts: orders.Contacts{orders.ContactTelegram: "@alice"}})
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v (messageries et heure sont facultatives)", err)
|
||||
}
|
||||
if len(order.Contacts) != 1 || order.Contacts[orders.ContactTelegram] != "@alice" {
|
||||
t.Errorf("contacts = %v", order.Contacts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_PhoneFormat(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutPhone = true
|
||||
for _, ok := range []string{"+33612345678", "06 12 34 56 78", "(01) 23-45-67-89", "0612.34.56.78"} {
|
||||
if _, err := f.create(orders.CreateInput{CustomerName: "A", Contacts: orders.Contacts{orders.ContactPhone: ok}}); err != nil {
|
||||
t.Errorf("téléphone %q refusé: %v", ok, err)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"abc", "12345", "1234567890123456", "06+12345678", "0612345678; DROP TABLE"} {
|
||||
_, err := f.create(orders.CreateInput{CustomerName: "A", Contacts: orders.Contacts{orders.ContactPhone: bad}})
|
||||
if !errors.Is(err, orders.ErrInvalidField) {
|
||||
t.Errorf("téléphone %q accepté (erreur = %v)", bad, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_HourFormat(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutHour = true
|
||||
for _, ok := range []string{"00:00", "09:05", "18:30", "23:59"} {
|
||||
if _, err := f.create(orders.CreateInput{CustomerName: "A", InfoSup: orders.InfoSup{orders.ContactHour: ok}}); err != nil {
|
||||
t.Errorf("heure %q refusée: %v", ok, err)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"24:00", "12:60", "9:30", "1830", "18h30", "ab:cd"} {
|
||||
_, err := f.create(orders.CreateInput{CustomerName: "A", InfoSup: orders.InfoSup{orders.ContactHour: bad}})
|
||||
requireFieldError(t, err, "hour", orders.ReasonFormat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_LimitsAndControlCharacters(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutAddress = true
|
||||
f.settings.fields.CheckoutSnap = true
|
||||
f.settings.fields.CheckoutNotes = true
|
||||
|
||||
_, err := f.create(orders.CreateInput{CustomerName: "A", InfoSup: orders.InfoSup{orders.ContactAddress: strings.Repeat("a", 301)}})
|
||||
requireFieldError(t, err, "address", orders.ReasonTooLong)
|
||||
|
||||
addr := orders.InfoSup{orders.ContactAddress: "1 rue X"}
|
||||
_, err = f.create(orders.CreateInput{CustomerName: "A", InfoSup: addr, Contacts: orders.Contacts{orders.ContactSnap: strings.Repeat("a", 101)}})
|
||||
requireFieldError(t, err, "snap", orders.ReasonTooLong)
|
||||
|
||||
// Un retour à la ligne dans un pseudo permettrait de falsifier la notification.
|
||||
_, err = f.create(orders.CreateInput{CustomerName: "A", InfoSup: addr, Contacts: orders.Contacts{orders.ContactSnap: "alice\nTotal: 0.00"}})
|
||||
requireFieldError(t, err, "snap", orders.ReasonCharacters)
|
||||
|
||||
_, err = f.create(orders.CreateInput{CustomerName: "Alice\nTotal: 0.00", InfoSup: addr})
|
||||
requireFieldError(t, err, "customer_name", orders.ReasonCharacters)
|
||||
|
||||
_, err = f.create(orders.CreateInput{CustomerName: "A", InfoSup: addr, Notes: strings.Repeat("n", 1001)})
|
||||
requireFieldError(t, err, "notes", orders.ReasonTooLong)
|
||||
|
||||
// Les notes et l'adresse peuvent contenir des retours à la ligne.
|
||||
order, err := f.create(orders.CreateInput{CustomerName: "A", InfoSup: orders.InfoSup{orders.ContactAddress: "1 rue X\nBât. B"}, Notes: "ligne 1\nligne 2"})
|
||||
if err != nil || order.Notes != "ligne 1\nligne 2" {
|
||||
t.Errorf("notes multi-lignes refusées ou altérées: %v %q", err, order.Notes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_RejectsOversizedMaps(t *testing.T) {
|
||||
big := orders.Contacts{}
|
||||
for i := 0; i < 21; i++ {
|
||||
big[strings.Repeat("k", i+1)] = "v"
|
||||
}
|
||||
_, err := newFixture().create(orders.CreateInput{CustomerName: "A", Contacts: big})
|
||||
requireFieldError(t, err, "contacts", orders.ReasonTooMany)
|
||||
}
|
||||
|
||||
func TestCheckout_NotesOnlyKeptWhenEnabled(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutNotes = true
|
||||
order, err := f.create(orders.CreateInput{CustomerName: "A", Notes: " sans oignons "})
|
||||
if err != nil || order.Notes != "sans oignons" {
|
||||
t.Errorf("notes = %q, err = %v", order.Notes, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_SettingsFailureFailsTheOrder(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.err = errors.New("db down")
|
||||
if _, err := f.create(orders.CreateInput{CustomerName: "A"}); err == nil || errors.Is(err, orders.ErrInvalidField) {
|
||||
t.Errorf("erreur = %v, attendu une erreur serveur (pas une erreur de champ)", err)
|
||||
}
|
||||
if len(f.repo.orders) != 0 {
|
||||
t.Error("une commande a été créée alors que les réglages n'ont pas pu être lus")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckout_NotificationIncludesAnswersWithoutInjection(t *testing.T) {
|
||||
f := newFixture()
|
||||
f.settings.fields.CheckoutPhone = true
|
||||
f.settings.fields.CheckoutHour = true
|
||||
if _, err := f.create(orders.CreateInput{
|
||||
CustomerName: "Alice",
|
||||
Contacts: orders.Contacts{orders.ContactPhone: "0612345678"},
|
||||
InfoSup: orders.InfoSup{orders.ContactHour: "18:30"},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(f.notifier.newOrderCalls) != 1 {
|
||||
t.Fatalf("%d notifications, attendu 1", len(f.notifier.newOrderCalls))
|
||||
}
|
||||
msg := f.notifier.newOrderCalls[0]
|
||||
for _, want := range []string{"New order from Alice", "phone: 0612345678", "hour: 18:30"} {
|
||||
if !strings.Contains(msg, want) {
|
||||
t.Errorf("notification sans %q :\n%s", want, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Garde-fou : le contrat avec le module checkoutfields (une option activée
|
||||
// d'un côté doit avoir sa clé de l'autre).
|
||||
func TestCheckout_EveryOptionHasAKey(t *testing.T) {
|
||||
all := &checkoutfields.CheckoutField{
|
||||
CheckoutPhone: true, CheckoutAddress: true, CheckoutSnap: true, CheckoutInsta: true,
|
||||
CheckoutTelegram: true, CheckoutSimpleX: true, CheckoutWhatsapp: true, CheckoutSignal: true,
|
||||
CheckoutHour: true,
|
||||
}
|
||||
f := newFixture()
|
||||
f.settings.fields = all
|
||||
contacts := orders.Contacts{}
|
||||
for _, k := range []string{orders.ContactPhone, orders.ContactSnap, orders.ContactInsta, orders.ContactTelegram,
|
||||
orders.ContactSimpleX, orders.ContactWhatsapp, orders.ContactSignal} {
|
||||
contacts[k] = "0612345678"
|
||||
}
|
||||
order, err := f.create(orders.CreateInput{
|
||||
CustomerName: "A",
|
||||
Contacts: contacts,
|
||||
InfoSup: orders.InfoSup{orders.ContactAddress: "1 rue X", orders.ContactHour: "10:00"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(order.Contacts) != 7 || len(order.InfoSup) != 2 {
|
||||
t.Errorf("contacts=%d (attendu 7) info_sup=%d (attendu 2) : une option n'a pas de clé", len(order.Contacts), len(order.InfoSup))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user