push_api_test.go 5.6 KB

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