independencia del provider
This commit is contained in:
+75
-5
@@ -93,13 +93,17 @@ func TestHandleBook(t *testing.T) {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
w.Write([]byte(`{"id":5}`))
|
||||
})
|
||||
mux.HandleFunc("/index.php/api/v1/providers", func(w http.ResponseWriter, r *http.Request) {
|
||||
hits = append(hits, "GET providers")
|
||||
json.NewEncoder(w).Encode([]eaProvider{{ID: 7}})
|
||||
})
|
||||
srv := httptest.NewServer(mux)
|
||||
defer srv.Close()
|
||||
|
||||
ea := &eaClient{base: srv.URL, http: srv.Client()}
|
||||
|
||||
body := `{"nombre":"Ana","apellidos":"López Gil","email":"ana@x.com","telefono":"600","mensaje":"hola",
|
||||
"providerId":1,"serviceIds":[1,2],"date":"2026-07-01","time":"10:00"}`
|
||||
"serviceIds":[1,2],"date":"2026-07-01","time":"10:00"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/book", strings.NewReader(body))
|
||||
rec := httptest.NewRecorder()
|
||||
handleBook(ea)(rec, req)
|
||||
@@ -115,7 +119,8 @@ func TestHandleBook(t *testing.T) {
|
||||
if gotAppt.Start != "2026-07-01 10:00:00" || gotAppt.End != "2026-07-01 11:15:00" {
|
||||
t.Errorf("start/end = %q/%q, want 10:00:00/11:15:00", gotAppt.Start, gotAppt.End)
|
||||
}
|
||||
if gotAppt.CustomerID != 99 || gotAppt.ProviderID != 1 || gotAppt.Status != "booked" {
|
||||
// El provider lo resuelve el servidor contra EA, no el navegador.
|
||||
if gotAppt.CustomerID != 99 || gotAppt.ProviderID != 7 || gotAppt.Status != "booked" {
|
||||
t.Errorf("appt = %+v", gotAppt)
|
||||
}
|
||||
// Nombre y apellidos viajan por separado hasta EA, sin adivinar cuál es cuál.
|
||||
@@ -128,7 +133,7 @@ func TestHandleBook(t *testing.T) {
|
||||
}
|
||||
// Toda la gestión pasó por EA y nada más.
|
||||
sort.Strings(hits)
|
||||
want := []string{"GET customers", "GET services", "POST appointments", "POST customers"}
|
||||
want := []string{"GET customers", "GET providers", "GET services", "POST appointments", "POST customers"}
|
||||
if strings.Join(hits, ",") != strings.Join(want, ",") {
|
||||
t.Errorf("llamadas a EA = %v, want %v", hits, want)
|
||||
}
|
||||
@@ -148,7 +153,7 @@ func TestHandleBookRejectsUnknownService(t *testing.T) {
|
||||
defer srv.Close()
|
||||
ea := &eaClient{base: srv.URL, http: srv.Client()}
|
||||
|
||||
body := `{"nombre":"Ana","apellidos":"López","email":"a@x.com","telefono":"600","providerId":1,"serviceIds":[999],"date":"2026-07-01","time":"10:00"}`
|
||||
body := `{"nombre":"Ana","apellidos":"López","email":"a@x.com","telefono":"600","serviceIds":[999],"date":"2026-07-01","time":"10:00"}`
|
||||
rec := httptest.NewRecorder()
|
||||
handleBook(ea)(rec, httptest.NewRequest(http.MethodPost, "/api/book", strings.NewReader(body)))
|
||||
|
||||
@@ -160,6 +165,71 @@ func TestHandleBookRejectsUnknownService(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// El id del provider se resuelve contra EA (autodescubierto) o vía
|
||||
// EA_PROVIDER_ID, nunca hardcodeado: es distinto en cada entorno.
|
||||
func TestProviderID(t *testing.T) {
|
||||
calls := 0
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/index.php/api/v1/providers", func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
json.NewEncoder(w).Encode([]eaProvider{{ID: 5}, {ID: 9}})
|
||||
})
|
||||
srv := httptest.NewServer(mux)
|
||||
defer srv.Close()
|
||||
ea := &eaClient{base: srv.URL, http: srv.Client()}
|
||||
|
||||
t.Run("autodescubre el primero de EA", func(t *testing.T) {
|
||||
id, err := ea.providerID()
|
||||
if err != nil || id != 5 {
|
||||
t.Errorf("providerID() = %d, %v; want 5, nil", id, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EA_PROVIDER_ID tiene prioridad y no llama a EA", func(t *testing.T) {
|
||||
t.Setenv("EA_PROVIDER_ID", "9")
|
||||
before := calls
|
||||
id, err := ea.providerID()
|
||||
if err != nil || id != 9 {
|
||||
t.Errorf("providerID() = %d, %v; want 9, nil", id, err)
|
||||
}
|
||||
if calls != before {
|
||||
t.Error("con EA_PROVIDER_ID definido no debería consultar EA")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EA_PROVIDER_ID inválido es error", func(t *testing.T) {
|
||||
t.Setenv("EA_PROVIDER_ID", "mayra")
|
||||
if _, err := ea.providerID(); err == nil {
|
||||
t.Error("EA_PROVIDER_ID no numérico debería dar error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("sin providers en EA es error", func(t *testing.T) {
|
||||
empty := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`[]`))
|
||||
}))
|
||||
defer empty.Close()
|
||||
ea := &eaClient{base: empty.URL, http: empty.Client()}
|
||||
if _, err := ea.providerID(); err == nil {
|
||||
t.Error("sin providers debería dar error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("/api/config lo expone al frontend", func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
handleConfig(ea)(rec, httptest.NewRequest(http.MethodGet, "/api/config", nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
var got struct {
|
||||
ProviderID int `json:"providerId"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil || got.ProviderID != 5 {
|
||||
t.Errorf("body = %s (err %v), want providerId 5", rec.Body.String(), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Sin apellidos ⇒ 400 sin llegar a llamar a EA.
|
||||
func TestHandleBookRequiresApellidos(t *testing.T) {
|
||||
called := false
|
||||
@@ -169,7 +239,7 @@ func TestHandleBookRequiresApellidos(t *testing.T) {
|
||||
defer srv.Close()
|
||||
ea := &eaClient{base: srv.URL, http: srv.Client()}
|
||||
|
||||
body := `{"nombre":"Ana","email":"a@x.com","telefono":"600","providerId":1,"serviceIds":[1],"date":"2026-07-01","time":"10:00"}`
|
||||
body := `{"nombre":"Ana","email":"a@x.com","telefono":"600","serviceIds":[1],"date":"2026-07-01","time":"10:00"}`
|
||||
rec := httptest.NewRecorder()
|
||||
handleBook(ea)(rec, httptest.NewRequest(http.MethodPost, "/api/book", strings.NewReader(body)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user