orchestr_plans_body_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package httpapi_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/gnacho/netpulse/agent/executor"
  9. "github.com/gnacho/netpulse/server-go/internal/orchestr"
  10. )
  11. // TestPlansCreateRespondsWithBody (#500): el POST /api/plans con diff
  12. // explícito debe devolver el plan serializado (se vio un 201 con body vacío
  13. // en producción que rompía a los clientes JSON).
  14. func TestPlansCreateRespondsWithBody(t *testing.T) {
  15. ts := makeTestServer(t)
  16. defer ts.Close()
  17. cookie := adminCookie(t, ts)
  18. body := `{"routerId":"rt-test","resource":"wireless","diff":[{"kind":"uci_set","args":{"config":"wireless","section":"radio1","option":"channel","value":"1"},"desc":"channel"}]}`
  19. req, _ := http.NewRequest("POST", ts.URL+"/api/plans", strings.NewReader(body))
  20. req.Header.Set("Cookie", "session="+cookie)
  21. req.Header.Set("Content-Type", "application/json")
  22. res, err := http.DefaultClient.Do(req)
  23. if err != nil {
  24. t.Fatalf("post plans: %v", err)
  25. }
  26. defer res.Body.Close()
  27. if res.StatusCode != http.StatusCreated {
  28. t.Fatalf("expected 201, got %d", res.StatusCode)
  29. }
  30. var plan map[string]any
  31. if err := json.NewDecoder(res.Body).Decode(&plan); err != nil {
  32. t.Fatalf("response body is not JSON (regression: empty body): %v", err)
  33. }
  34. if plan["id"] == nil || plan["status"] != "pending" {
  35. t.Fatalf("unexpected plan body: %+v", plan)
  36. }
  37. }
  38. // TestPlanMarshalDirect aísla el problema de serialización: un Plan con el
  39. // diff que manda la UI del channel plan debe serializar sin error.
  40. func TestPlanMarshalDirect(t *testing.T) {
  41. p := orchestr.Plan{
  42. ID: "p1",
  43. RouterID: "rt2",
  44. Resource: "wireless",
  45. Diff: []executor.Op{
  46. {Kind: "uci_set", Args: map[string]string{"config": "wireless", "section": "radio1", "option": "channel", "value": "1"}, Desc: "channel radio1 -> 1"},
  47. {Kind: "uci_commit", Args: map[string]string{"config": "wireless"}, Desc: "commit"},
  48. {Kind: "service", Args: map[string]string{"name": "network", "action": "restart"}, Desc: "reload"},
  49. },
  50. Status: "pending",
  51. CreatedBy: "admin",
  52. CreatedAt: time.Now().UnixMilli(),
  53. }
  54. b, err := json.Marshal(p)
  55. if err != nil {
  56. t.Fatalf("marshal plan: %v", err)
  57. }
  58. t.Logf("plan json: %.200s", b)
  59. }