push_api_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.
  55. body := `{"endpoint":"https://push.example/abc","keys":{"auth":"a1","p256dh":"p1"}}`
  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. // Upsert del mismo endpoint (claves nuevas): 200, sin duplicar.
  65. body = `{"endpoint":"https://push.example/abc","keys":{"auth":"a2","p256dh":"p2"}}`
  66. res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, body)
  67. if res.StatusCode != 200 {
  68. t.Fatalf("subscribe upsert: %d, esperaba 200", res.StatusCode)
  69. }
  70. res.Body.Close()
  71. if count() != 1 {
  72. t.Fatalf("upsert duplicó: %d filas", count())
  73. }
  74. var auth string
  75. if err := ts.db.QueryRow("SELECT keys_auth FROM push_subscriptions WHERE endpoint = ?", "https://push.example/abc").Scan(&auth); err != nil || auth != "a2" {
  76. t.Fatalf("upsert no actualizó claves: auth=%q err=%v", auth, err)
  77. }
  78. // Cuerpos inválidos → 400.
  79. for _, bad := range []string{
  80. `nope`,
  81. `{"endpoint":"","keys":{"auth":"a","p256dh":"p"}}`,
  82. `{"endpoint":"https://x/y"}`,
  83. `{"endpoint":"https://x/y","keys":{"auth":"","p256dh":"p"}}`,
  84. } {
  85. res = doJSON(t, "POST", ts.URL, "/api/push/subscribe", cookie, bad)
  86. if res.StatusCode != 400 {
  87. t.Fatalf("subscribe body %s: %d, esperaba 400", bad, res.StatusCode)
  88. }
  89. res.Body.Close()
  90. }
  91. // POST unsubscribe: 204 y la fila desaparece; idempotente.
  92. res = doJSON(t, "POST", ts.URL, "/api/push/unsubscribe", cookie, `{"endpoint":"https://push.example/abc"}`)
  93. if res.StatusCode != 204 {
  94. t.Fatalf("unsubscribe: %d, esperaba 204", res.StatusCode)
  95. }
  96. res.Body.Close()
  97. if count() != 0 {
  98. t.Fatalf("tras unsubscribe: %d filas", count())
  99. }
  100. res = doJSON(t, "POST", ts.URL, "/api/push/unsubscribe", cookie, `{"endpoint":"https://push.example/abc"}`)
  101. if res.StatusCode != 204 {
  102. t.Fatalf("unsubscribe idempotente: %d, esperaba 204", res.StatusCode)
  103. }
  104. res.Body.Close()
  105. // Unsubscribe sin endpoint → 400.
  106. res = doJSON(t, "POST", ts.URL, "/api/push/unsubscribe", cookie, `{"endpoint":""}`)
  107. if res.StatusCode != 400 {
  108. t.Fatalf("unsubscribe sin endpoint: %d, esperaba 400", res.StatusCode)
  109. }
  110. res.Body.Close()
  111. }