push_test_1973_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // push_test_1973_test.go - el envio de prueba usa el camino real: mismo
  2. // cifrado, mismo VAPID, y devuelve status y reason (#1973).
  3. package push_test
  4. import (
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/gnacho/netpulse/server-go/internal/push"
  12. )
  13. func TestSendTestReachesTheService(t *testing.T) {
  14. d, _ := openDB(t)
  15. ua := newBrowserSub(t)
  16. got := make(chan capturedPush, 1)
  17. srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. body, _ := io.ReadAll(r.Body)
  19. got <- capturedPush{method: r.Method, header: r.Header.Clone(), body: body}
  20. w.WriteHeader(http.StatusCreated)
  21. }))
  22. defer srv.Close()
  23. n := newNotifier(t, d, testPushClient(srv))
  24. defer n.Close()
  25. res := n.SendTest(push.Subscription{
  26. Endpoint: "https://push.example/send", Auth: ua.authB, P256dh: ua.pubB, Language: "es",
  27. })
  28. if res.Error != "" || res.Status != http.StatusCreated {
  29. t.Fatalf("resultado = %+v", res)
  30. }
  31. var sent capturedPush
  32. select {
  33. case sent = <-got:
  34. case <-time.After(15 * time.Second):
  35. t.Fatal("el push service simulado no recibio nada en 15 s")
  36. }
  37. if !strings.HasPrefix(sent.header.Get("Authorization"), "vapid t=") {
  38. t.Fatalf("Authorization VAPID: %q", sent.header.Get("Authorization"))
  39. }
  40. plain := string(decryptAES128GCM(t, ua, sent.body))
  41. if !strings.Contains(plain, "Notificacion de prueba") {
  42. t.Fatalf("payload sin el texto de prueba: %q", plain)
  43. }
  44. }
  45. func TestSendTestReportsTheReason(t *testing.T) {
  46. d, _ := openDB(t)
  47. ua := newBrowserSub(t)
  48. srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  49. w.WriteHeader(http.StatusForbidden)
  50. _, _ = w.Write([]byte(`{"reason":"BadJwtToken"}`))
  51. }))
  52. defer srv.Close()
  53. if _, err := push.UpsertSubscription(d, push.Subscription{
  54. Endpoint: "https://push.example/send", Auth: ua.authB, P256dh: ua.pubB, UserID: 1,
  55. }); err != nil {
  56. t.Fatalf("upsert: %v", err)
  57. }
  58. n := newNotifier(t, d, testPushClient(srv))
  59. defer n.Close()
  60. res := n.SendTest(push.Subscription{Endpoint: "https://push.example/send", Auth: ua.authB, P256dh: ua.pubB})
  61. if res.Status != http.StatusForbidden {
  62. t.Fatalf("status = %d, esperaba 403", res.Status)
  63. }
  64. if !strings.Contains(res.Reason, "BadJwtToken") {
  65. t.Fatalf("reason = %q, esperaba el motivo del push service", res.Reason)
  66. }
  67. if res.Expired {
  68. t.Fatal("un 403 no caduca la suscripcion")
  69. }
  70. if _, ok, _ := push.SubscriptionForUser(d, "https://push.example/send", 1); !ok {
  71. t.Fatal("la suscripcion no debe borrarse con un 403")
  72. }
  73. }
  74. func TestSendTestPurgesExpiredSubscription(t *testing.T) {
  75. d, _ := openDB(t)
  76. ua := newBrowserSub(t)
  77. srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  78. w.WriteHeader(http.StatusGone)
  79. }))
  80. defer srv.Close()
  81. if _, err := push.UpsertSubscription(d, push.Subscription{
  82. Endpoint: "https://push.example/send", Auth: ua.authB, P256dh: ua.pubB, UserID: 7,
  83. }); err != nil {
  84. t.Fatalf("upsert: %v", err)
  85. }
  86. n := newNotifier(t, d, testPushClient(srv))
  87. defer n.Close()
  88. res := n.SendTest(push.Subscription{Endpoint: "https://push.example/send", Auth: ua.authB, P256dh: ua.pubB})
  89. if !res.Expired || res.Status != http.StatusGone {
  90. t.Fatalf("resultado = %+v", res)
  91. }
  92. if _, ok, _ := push.SubscriptionForUser(d, "https://push.example/send", 7); ok {
  93. t.Fatal("la suscripcion caducada debe purgarse")
  94. }
  95. }
  96. func TestSendTestRejectsInvalidEndpoint(t *testing.T) {
  97. d, _ := openDB(t)
  98. n := newNotifier(t, d)
  99. defer n.Close()
  100. res := n.SendTest(push.Subscription{Endpoint: "http://plain.example/x"})
  101. if res.Error == "" {
  102. t.Fatalf("un endpoint http debe rechazarse: %+v", res)
  103. }
  104. }