push_api_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // push_api_test.go — contrato de los endpoints Web Push (SPEC-PUSH §1):
  2. // auth de sesión obligatoria en los 3, vapid-key estable, subscribe 201/200
  3. // (upsert por endpoint) y unsubscribe 204 idempotente.
  4. package httpapi_test
  5. import (
  6. "io"
  7. "strings"
  8. "testing"
  9. )
  10. func TestPushAuthRequired(t *testing.T) {
  11. ts := makeTestServer(t)
  12. for _, tc := range []struct{ method, path, body string }{
  13. {"GET", "/api/push/vapid-key", ""},
  14. {"POST", "/api/push/subscribe", `{"endpoint":"https://x/y","keys":{"auth":"a","p256dh":"p"}}`},
  15. {"POST", "/api/push/unsubscribe", `{"endpoint":"https://x/y"}`},
  16. } {
  17. res := doJSON(t, tc.method, ts.URL, tc.path, "", tc.body)
  18. if res.StatusCode != 401 {
  19. t.Fatalf("%s %s sin sesión: %d, esperaba 401", tc.method, tc.path, res.StatusCode)
  20. }
  21. res.Body.Close()
  22. }
  23. }
  24. func TestPushEndpointsContract(t *testing.T) {
  25. ts := makeTestServer(t)
  26. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  27. // GET vapid-key: {"key":...} no vacía y ESTABLE entre llamadas; sin '\n'.
  28. res := get(t, ts.URL, "/api/push/vapid-key", cookie)
  29. if res.StatusCode != 200 {
  30. t.Fatalf("vapid-key: %d", res.StatusCode)
  31. }
  32. raw, _ := io.ReadAll(res.Body)
  33. res.Body.Close()
  34. if strings.HasSuffix(string(raw), "\n") {
  35. t.Fatal("writeJSON no debe terminar en \\n")
  36. }
  37. s := string(raw)
  38. if !strings.HasPrefix(s, `{"key":"`) || !strings.HasSuffix(s, `"}`) || len(s) <= len(`{"key":""}`) {
  39. t.Fatalf("vapid-key body inesperado: %s", s)
  40. }
  41. res = get(t, ts.URL, "/api/push/vapid-key", cookie)
  42. raw2, _ := io.ReadAll(res.Body)
  43. res.Body.Close()
  44. if string(raw) != string(raw2) {
  45. t.Fatalf("vapid-key no estable: %s vs %s", raw, raw2)
  46. }
  47. count := func() int {
  48. var n int
  49. if err := ts.db.QueryRow("SELECT COUNT(*) FROM push_subscriptions").Scan(&n); err != nil {
  50. t.Fatalf("count: %v", err)
  51. }
  52. return n
  53. }
  54. // POST subscribe: 201 la primera vez. #1334: language (es|en) obligatorio.
  55. body := `{"endpoint":"https://push.example/abc","keys":{"auth":"a1","p256dh":"p1"},"language":"en"}`
  56. res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, body)
  57. if res.StatusCode != 201 {
  58. t.Fatalf("subscribe nuevo: %d, esperaba 201", res.StatusCode)
  59. }
  60. res.Body.Close()
  61. if count() != 1 {
  62. t.Fatalf("suscripciones: %d, esperaba 1", count())
  63. }
  64. // #1334: dueño e idioma persistidos.
  65. var uid int64
  66. var lang string
  67. if err := ts.db.QueryRow("SELECT user_id, language FROM push_subscriptions WHERE endpoint = ?", "https://push.example/abc").Scan(&uid, &lang); err != nil || uid == 0 || lang != "en" {
  68. t.Fatalf("subscribe no persistió user_id/language: uid=%d lang=%q err=%v", uid, lang, err)
  69. }
  70. // Upsert del mismo endpoint (claves nuevas): 200, sin duplicar.
  71. body = `{"endpoint":"https://push.example/abc","keys":{"auth":"a2","p256dh":"p2"},"language":"es"}`
  72. res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, body)
  73. if res.StatusCode != 200 {
  74. t.Fatalf("subscribe upsert: %d, esperaba 200", res.StatusCode)
  75. }
  76. res.Body.Close()
  77. if count() != 1 {
  78. t.Fatalf("upsert duplicó: %d filas", count())
  79. }
  80. var auth string
  81. if err := ts.db.QueryRow("SELECT keys_auth FROM push_subscriptions WHERE endpoint = ?", "https://push.example/abc").Scan(&auth); err != nil || auth != "a2" {
  82. t.Fatalf("upsert no actualizó claves: auth=%q err=%v", auth, err)
  83. }
  84. // Cuerpos inválidos → 400.
  85. for _, bad := range []string{
  86. `nope`,
  87. `{"endpoint":"","keys":{"auth":"a","p256dh":"p"}}`,
  88. `{"endpoint":"https://x/y"}`,
  89. `{"endpoint":"https://x/y","keys":{"auth":"","p256dh":"p"}}`,
  90. `{"endpoint":"https://x/y","keys":{"auth":"a","p256dh":"p"}}`, // sin language
  91. `{"endpoint":"https://x/y","keys":{"auth":"a","p256dh":"p"},"language":""}`, // language vacío
  92. `{"endpoint":"https://x/y","keys":{"auth":"a","p256dh":"p"},"language":"fr"}`, // idioma no soportado
  93. } {
  94. res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, bad)
  95. if res.StatusCode != 400 {
  96. t.Fatalf("subscribe body %s: %d, esperaba 400", bad, res.StatusCode)
  97. }
  98. res.Body.Close()
  99. }
  100. // POST unsubscribe: 204 y la fila desaparece; idempotente.
  101. res = doJSON(t, "POST", ts.URL, "/api/push/unsubscribe", cookie, `{"endpoint":"https://push.example/abc"}`)
  102. if res.StatusCode != 204 {
  103. t.Fatalf("unsubscribe: %d, esperaba 204", res.StatusCode)
  104. }
  105. res.Body.Close()
  106. if count() != 0 {
  107. t.Fatalf("tras unsubscribe: %d filas", count())
  108. }
  109. res = doJSON(t, "POST", ts.URL, "/api/push/unsubscribe", cookie, `{"endpoint":"https://push.example/abc"}`)
  110. if res.StatusCode != 204 {
  111. t.Fatalf("unsubscribe idempotente: %d, esperaba 204", res.StatusCode)
  112. }
  113. res.Body.Close()
  114. // Unsubscribe sin endpoint → 400.
  115. res = doJSON(t, "POST", ts.URL, "/api/push/unsubscribe", cookie, `{"endpoint":""}`)
  116. if res.StatusCode != 400 {
  117. t.Fatalf("unsubscribe sin endpoint: %d, esperaba 400", res.StatusCode)
  118. }
  119. res.Body.Close()
  120. }