agent_url_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // agent_url_test.go — #1335: NETPULSE_AGENT_URL como URL base preferente con
  2. // la que los agentes alcanzan al server. Cadena: AGENT_URL > PUBLIC_URL >
  3. // Host de la petición. Antes de #1335 el install line ignoraba ambas
  4. // variables, así que tras un reverse proxy con dominio público el comando
  5. // copiado apuntaba al dominio en vez de a la LAN.
  6. package httpapi_test
  7. import (
  8. "net/http"
  9. "net/http/httptest"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/gnacho/netpulse/server-go/internal/adapters"
  14. "github.com/gnacho/netpulse/server-go/internal/auth"
  15. "github.com/gnacho/netpulse/server-go/internal/config"
  16. "github.com/gnacho/netpulse/server-go/internal/db"
  17. "github.com/gnacho/netpulse/server-go/internal/httpapi"
  18. "github.com/gnacho/netpulse/server-go/internal/sse"
  19. )
  20. // makeURLTestServer levanta el handler real con un env extra (las variables
  21. // NETPULSE_* del caso) para observar cómo queda el install line.
  22. func makeURLTestServer(t *testing.T, extraEnv map[string]string) *agentTestServer {
  23. t.Helper()
  24. auth.SetTrustProxy(true)
  25. t.Cleanup(func() { auth.SetTrustProxy(false) })
  26. dataDir := t.TempDir()
  27. env := map[string]string{
  28. "AUTH_USER": "admin", "AUTH_PASS": "test123456",
  29. "DEMO_MODE": "0", "DATA_DIR": dataDir, "NODE_ENV": "test",
  30. }
  31. for k, v := range extraEnv {
  32. env[k] = v
  33. }
  34. cfg, err := config.Load(env, dataDir)
  35. if err != nil {
  36. t.Fatalf("config: %v", err)
  37. }
  38. d, err := db.Open(dataDir)
  39. if err != nil {
  40. t.Fatalf("db: %v", err)
  41. }
  42. secret, err := auth.EnsureSessionSecret(d, cfg)
  43. if err != nil {
  44. t.Fatalf("secret: %v", err)
  45. }
  46. if err := auth.EnsureUsers(d, cfg); err != nil {
  47. t.Fatalf("users: %v", err)
  48. }
  49. reg := adapters.NewAgentRegistry(90 * time.Second)
  50. hub := sse.NewHub(d, cfg.MaxSSEClients, func() any { return nil })
  51. agentHub := sse.NewAgentHub(func(_, _ string) bool { return false })
  52. handler := httpapi.NewHandler(httpapi.Deps{
  53. Config: cfg, DB: d, Adapter: adapters.NewDemo(), Hub: hub, Secret: secret,
  54. Agents: reg, Started: time.Now(), AgentHub: agentHub,
  55. })
  56. srv := httptest.NewServer(handler)
  57. status, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  58. if status != 204 {
  59. t.Fatalf("login: %d", status)
  60. }
  61. ts := &agentTestServer{Server: srv, db: d, agents: reg, cookie: cookie}
  62. t.Cleanup(func() {
  63. srv.Close()
  64. d.Close()
  65. })
  66. return ts
  67. }
  68. func TestAgentInstallLinePrefiereAgentURL(t *testing.T) {
  69. ts := makeURLTestServer(t, map[string]string{
  70. "NETPULSE_AGENT_URL": "http://192.168.1.50:9999",
  71. "NETPULSE_PUBLIC_URL": "https://netpulse.example.com",
  72. })
  73. status, _, install := createAgentToken(t, ts, "rt1")
  74. if status != http.StatusCreated {
  75. t.Fatalf("POST /api/agents: %d", status)
  76. }
  77. if !strings.Contains(install, "--server=http://192.168.1.50:9999") {
  78. t.Fatalf("install line debe usar AGENT_URL: %s", install)
  79. }
  80. if strings.Contains(install, "netpulse.example.com") {
  81. t.Fatalf("install line no debe usar PUBLIC_URL cuando hay AGENT_URL: %s", install)
  82. }
  83. }
  84. func TestAgentInstallLinePrefierePublicURL(t *testing.T) {
  85. ts := makeURLTestServer(t, map[string]string{
  86. "NETPULSE_PUBLIC_URL": "https://netpulse.example.com",
  87. })
  88. _, _, install := createAgentToken(t, ts, "rt1")
  89. if !strings.Contains(install, "--server=https://netpulse.example.com") {
  90. t.Fatalf("install line debe usar PUBLIC_URL: %s", install)
  91. }
  92. if strings.Contains(install, "--server="+ts.URL) {
  93. t.Fatalf("install line no debe caer al Host de la petición cuando hay PUBLIC_URL: %s", install)
  94. }
  95. }
  96. func TestAgentInstallLineFallbackHostPeticion(t *testing.T) {
  97. ts := makeURLTestServer(t, nil)
  98. _, _, install := createAgentToken(t, ts, "rt1")
  99. if !strings.Contains(install, "--server="+ts.URL) {
  100. t.Fatalf("install line debe caer al Host de la petición: %s", install)
  101. }
  102. }