4
0

thresholds_hide_recovery_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // thresholds_hide_recovery_test.go - campo hideRecoveryEvents del endpoint de
  2. // umbrales (#1518): round-trip GET/PUT. El efecto del filtro sobre el feed lo
  3. // cubre TestHideRecoveryFiltersFeedAndUnread (paleta alerts, engine con BD
  4. // real): el adapter demo del server de test monta su engine SIN BD (dataset
  5. // sintético), así que aquí solo se valida el contrato del endpoint.
  6. package httpapi_test
  7. import (
  8. "encoding/json"
  9. "net/http"
  10. "testing"
  11. )
  12. func thresholdsBody(t *testing.T, res *http.Response) map[string]any {
  13. t.Helper()
  14. defer res.Body.Close()
  15. var body map[string]any
  16. if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
  17. t.Fatalf("json: %v", err)
  18. }
  19. return body
  20. }
  21. func TestThresholdsHideRecoveryRoundTrip(t *testing.T) {
  22. ts := makeTestServer(t)
  23. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  24. // Default: off.
  25. body := thresholdsBody(t, get(t, ts.URL, "/api/settings/thresholds", cookie))
  26. if body["hideRecoveryEvents"] != false {
  27. t.Fatalf("default hideRecoveryEvents = %v, esperaba false", body["hideRecoveryEvents"])
  28. }
  29. // PUT on: persiste y el GET lo devuelve.
  30. res := doJSON(t, "PUT", ts.URL, "/api/settings/thresholds", cookie, `{"hideRecoveryEvents":true}`)
  31. if res.StatusCode != 200 {
  32. t.Fatalf("PUT hideRecoveryEvents: status %d", res.StatusCode)
  33. }
  34. body = thresholdsBody(t, res)
  35. if body["hideRecoveryEvents"] != true {
  36. t.Fatalf("tras PUT, hideRecoveryEvents = %v, esperaba true", body["hideRecoveryEvents"])
  37. }
  38. body = thresholdsBody(t, get(t, ts.URL, "/api/settings/thresholds", cookie))
  39. if body["hideRecoveryEvents"] != true {
  40. t.Fatalf("GET tras PUT: hideRecoveryEvents = %v, esperaba true", body["hideRecoveryEvents"])
  41. }
  42. // PUT off: vuelve el comportamiento por defecto.
  43. res = doJSON(t, "PUT", ts.URL, "/api/settings/thresholds", cookie, `{"hideRecoveryEvents":false}`)
  44. if res.StatusCode != 200 {
  45. t.Fatalf("PUT hideRecoveryEvents=false: status %d", res.StatusCode)
  46. }
  47. body = thresholdsBody(t, get(t, ts.URL, "/api/settings/thresholds", cookie))
  48. if body["hideRecoveryEvents"] != false {
  49. t.Fatalf("GET tras PUT off: hideRecoveryEvents = %v, esperaba false", body["hideRecoveryEvents"])
  50. }
  51. }