settings_integrations_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // settings_integrations_test.go - GET/PUT /api/settings/integrations (#968):
  2. // toggles server-side de ntfy/telegram/proxmox (ausente = activo) y mqtt
  3. // (reutiliza la clave existente mqtt.enabled). El PUT es partial-safe: solo
  4. // se escriben las claves presentes en el body.
  5. package httpapi_test
  6. import (
  7. "net/http"
  8. "testing"
  9. )
  10. func TestIntegrationsDefaultsOn(t *testing.T) {
  11. srv := makeTestServer(t)
  12. _, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  13. if cookie == "" {
  14. t.Fatal("login no devolvió cookie")
  15. }
  16. res, body := wanSpeedRequest(t, "GET", srv.URL, "/api/settings/integrations", cookie, "")
  17. if res.StatusCode != http.StatusOK {
  18. t.Fatalf("GET: status %d, esperado 200", res.StatusCode)
  19. }
  20. for _, k := range []string{"ntfy", "telegram", "proxmox"} {
  21. if body[k] != true {
  22. t.Fatalf("%s default: %v, esperado true (ausente = activo)", k, body[k])
  23. }
  24. }
  25. // MQTT por defecto apagado (la clave mqtt.enabled no existe aún).
  26. if body["mqtt"] != false {
  27. t.Fatalf("mqtt default: %v, esperado false", body["mqtt"])
  28. }
  29. // Sin sesión → 401.
  30. res, _ = wanSpeedRequest(t, "GET", srv.URL, "/api/settings/integrations", "", "")
  31. if res.StatusCode != http.StatusUnauthorized {
  32. t.Fatalf("GET sin auth: status %d, esperado 401", res.StatusCode)
  33. }
  34. }
  35. func TestIntegrationsPutIsPartialSafe(t *testing.T) {
  36. srv := makeTestServer(t)
  37. _, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  38. if cookie == "" {
  39. t.Fatal("login no devolvió cookie")
  40. }
  41. // Apagar ntfy no toca telegram ni proxmox.
  42. res, body := wanSpeedRequest(t, "PUT", srv.URL, "/api/settings/integrations", cookie, `{"ntfy":false}`)
  43. if res.StatusCode != http.StatusOK {
  44. t.Fatalf("PUT ntfy=false: status %d (%v)", res.StatusCode, body)
  45. }
  46. if body["ntfy"] != false || body["telegram"] != true || body["proxmox"] != true {
  47. t.Fatalf("echo tras PUT parcial: %v", body)
  48. }
  49. // Apagar telegram tampoco reactiva ntfy.
  50. res, body = wanSpeedRequest(t, "PUT", srv.URL, "/api/settings/integrations", cookie, `{"telegram":false}`)
  51. if res.StatusCode != http.StatusOK {
  52. t.Fatalf("PUT telegram=false: status %d (%v)", res.StatusCode, body)
  53. }
  54. if body["ntfy"] != false || body["telegram"] != false || body["proxmox"] != true {
  55. t.Fatalf("echo tras segundo PUT parcial: %v", body)
  56. }
  57. // La vista persiste en kv (GET lo relee).
  58. _, body = wanSpeedRequest(t, "GET", srv.URL, "/api/settings/integrations", cookie, "")
  59. if body["ntfy"] != false || body["telegram"] != false || body["proxmox"] != true {
  60. t.Fatalf("GET tras PUTs: %v", body)
  61. }
  62. // Body vacío → 400.
  63. res, _ = wanSpeedRequest(t, "PUT", srv.URL, "/api/settings/integrations", cookie, `{}`)
  64. if res.StatusCode != http.StatusBadRequest {
  65. t.Fatalf("PUT vacío: status %d, esperado 400", res.StatusCode)
  66. }
  67. }
  68. // TestIntegrationsMirrorChannelEnabled (#1016/#1017): el toggle de la fila es
  69. // la única fuente de verdad - el PUT espeja ntfy.enabled/telegram.enabled
  70. // para que el path de envío y el integrationGate nunca desincronicen.
  71. func TestIntegrationsMirrorChannelEnabled(t *testing.T) {
  72. srv := makeTestServer(t)
  73. _, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  74. if cookie == "" {
  75. t.Fatal("login no devolvió cookie")
  76. }
  77. kvVal := func(key string) string {
  78. var v string
  79. if err := srv.db.QueryRow("SELECT value FROM kv WHERE key = ?", key).Scan(&v); err != nil {
  80. return ""
  81. }
  82. return v
  83. }
  84. res, _ := wanSpeedRequest(t, "PUT", srv.URL, "/api/settings/integrations", cookie, `{"ntfy":false,"telegram":true}`)
  85. if res.StatusCode != http.StatusOK {
  86. t.Fatalf("PUT: status %d", res.StatusCode)
  87. }
  88. if v := kvVal("ntfy.enabled"); v != "false" {
  89. t.Fatalf("ntfy.enabled espejo: %q, esperado false", v)
  90. }
  91. if v := kvVal("telegram.enabled"); v != "true" {
  92. t.Fatalf("telegram.enabled espejo: %q, esperado true", v)
  93. }
  94. // Proxmox NO tiene clave de canal que espejar.
  95. res, _ = wanSpeedRequest(t, "PUT", srv.URL, "/api/settings/integrations", cookie, `{"proxmox":false}`)
  96. if res.StatusCode != http.StatusOK {
  97. t.Fatalf("PUT proxmox: status %d", res.StatusCode)
  98. }
  99. if v := kvVal("ntfy.enabled"); v != "false" {
  100. t.Fatalf("ntfy.enabled tras PUT de proxmox: %q, esperado false intacto", v)
  101. }
  102. }