ntfy_settings_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // ntfy_settings_test.go - contrato del canal ntfy (#766): GET sin token,
  2. // PUT con validación y conservación, test que publica contra un server falso.
  3. package httpapi_test
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/gnacho/netpulse/server-go/internal/httpapi"
  9. )
  10. func TestNtfySettingsContract(t *testing.T) {
  11. // Server ntfy falso: registra publicaciones.
  12. var published int
  13. fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  14. if r.URL.Path != "/mi-topic" {
  15. w.WriteHeader(404)
  16. return
  17. }
  18. published++
  19. w.WriteHeader(200)
  20. }))
  21. defer fake.Close()
  22. ts, _ := makeOwutTestServer(t, nil)
  23. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  24. // GET inicial: defaults, sin token.
  25. res := getJSONPath(t, ts.URL, "/api/settings/ntfy", cookie)
  26. body := readJSON(t, res)
  27. if body["server"] != "https://ntfy.sh" || body["tokenSet"] != false {
  28. t.Fatalf("GET inicial: %v", body)
  29. }
  30. if _, present := body["token"]; present {
  31. t.Fatalf("el token jamas sale: %v", body)
  32. }
  33. // PUT valido apuntando al server falso.
  34. res = putJSONPath(t, ts.URL, "/api/settings/ntfy",
  35. `{"enabled":true,"topic":"mi-topic","server":"`+fake.URL+`","token":"tk1"}`, cookie)
  36. if res.StatusCode != 200 {
  37. t.Fatalf("PUT: %d %v", res.StatusCode, readJSON(t, res))
  38. }
  39. // GET: topic/server visibles, tokenSet true, sin token.
  40. res = getJSONPath(t, ts.URL, "/api/settings/ntfy", cookie)
  41. body = readJSON(t, res)
  42. if body["topic"] != "mi-topic" || body["tokenSet"] != true || body["enabled"] != true {
  43. t.Fatalf("GET tras PUT: %v", body)
  44. }
  45. // Test: publica contra el falso.
  46. res = postJSON(t, ts.URL, "/api/settings/ntfy/test", "{}", cookie)
  47. if res.StatusCode != 200 {
  48. t.Fatalf("test: %d %v", res.StatusCode, readJSON(t, res))
  49. }
  50. if published != 1 {
  51. t.Fatalf("el falso debia recibir 1 publicacion: %d", published)
  52. }
  53. // PUT invalido (topic con espacios): 400.
  54. res = putJSONPath(t, ts.URL, "/api/settings/ntfy", `{"enabled":true,"topic":"tema con espacios"}`, cookie)
  55. if res.StatusCode != 400 {
  56. t.Fatalf("topic invalido: %d", res.StatusCode)
  57. }
  58. // Clear: limpia el canal.
  59. res = putJSONPath(t, ts.URL, "/api/settings/ntfy", `{"clear":true}`, cookie)
  60. if res.StatusCode != 200 {
  61. t.Fatalf("clear: %d", res.StatusCode)
  62. }
  63. res = getJSONPath(t, ts.URL, "/api/settings/ntfy", cookie)
  64. body = readJSON(t, res)
  65. if body["topic"] != "" || body["tokenSet"] != false || body["enabled"] != false {
  66. t.Fatalf("tras clear: %v", body)
  67. }
  68. }
  69. // TestNtfySettingsTestFailsVisibly: un server caido devuelve el error con
  70. // mensaje (boton de prueba honesto).
  71. func TestNtfySettingsTestFailsVisibly(t *testing.T) {
  72. ts, _ := makeOwutTestServer(t, nil)
  73. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  74. _ = putJSONPath(t, ts.URL, "/api/settings/ntfy",
  75. `{"enabled":true,"topic":"x","server":"http://127.0.0.1:1"}`, cookie)
  76. res := postJSON(t, ts.URL, "/api/settings/ntfy/test", "{}", cookie)
  77. if res.StatusCode == 200 {
  78. t.Fatalf("server caido debe fallar el test")
  79. }
  80. body := readJSON(t, res)
  81. if msg, _ := body["message"].(string); msg == "" {
  82. t.Fatalf("el fallo debe llevar mensaje: %v", body)
  83. }
  84. }
  85. // TestNtfySettingsTestRequiresEnabled: el endpoint de test no publica con el
  86. // canal desactivado (misma decisión que el worker).
  87. func TestNtfySettingsTestRequiresEnabled(t *testing.T) {
  88. ts, _ := makeOwutTestServer(t, nil)
  89. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  90. res := putJSONPath(t, ts.URL, "/api/settings/ntfy",
  91. `{"enabled":false,"topic":"mi-topic"}`, cookie)
  92. if res.StatusCode != 200 {
  93. t.Fatalf("PUT desactivado: %d", res.StatusCode)
  94. }
  95. res = postJSON(t, ts.URL, "/api/settings/ntfy/test", "{}", cookie)
  96. if res.StatusCode == 200 {
  97. t.Fatalf("test con canal desactivado debe fallar")
  98. }
  99. body := readJSON(t, res)
  100. if msg, _ := body["message"].(string); msg == "" {
  101. t.Fatalf("el fallo debe llevar mensaje: %v", body)
  102. }
  103. }
  104. var _ = httpapi.Version