A way to test webhooks locally? #232
-
I'm trying to test if my "tracking" signals get called and function correctly without having to deploy my project. Is there any way to do so? I've tried what is said here but it doesn't seem to work; the signals never get called. Does the test backend not support webhooks or am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Sorry, that docs page should probably be updated to clarify that's how you test sending, and to add sections for testing delivery tracking and receiving mail. (I think it was written back before Anymail supported webhooks.) Anymail's Test EmailBackend doesn't have any way to usefully simulate tracking webhooks, so it doesn't try. Webhooks are always called asynchronously by your ESP, which isn't really possible during tests. Also, the specific tracking events will vary depending on who you're emailing and what they do with the message. There's no way for the Test EmailBackend to know which tracking events to send. Anymail's own tests attempt to thoroughly cover that every ESP webhook event is correctly converted to a normalized AnymailTrackingEvent and delivered to registered signal receiver functions. See the various That leaves testing your code. Probably the easiest way to do this is build an AnymailTrackingEvent and use Django's from anymail.signals import AnymailTrackingEvent, tracking
from django.test import TestCase
class EmailTrackingTests(TestCase):
def test_delivered_event(self):
# Build an AnymailTrackingEvent with event_type (required)
# and any other fields your receiver cares about. E.g.:
event = AnymailTrackingEvent(
event_type="delivered",
recipient="[email protected]",
message_id="test-message-id",
)
# Invoke all registered Anymail tracking signal receivers:
tracking.send(sender=object(), event=event, esp_name="YourESP")
# Verify expected behavior of your receiver.
# E.g., if you create a Django model to store the event:
from myapp.models import MyTrackingModel
self.assertTrue(MyTrackingModel.objects.filter(
email="[email protected]", event="delivered", message_id="test-message-id"
).exists()) (For simplicity, this just sets Django's required |
Beta Was this translation helpful? Give feedback.
Sorry, that docs page should probably be updated to clarify that's how you test sending, and to add sections for testing delivery tracking and receiving mail. (I think it was written back before Anymail supported webhooks.)
Anymail's Test EmailBackend doesn't have any way to usefully simulate tracking webhooks, so it doesn't try. Webhooks are always called asynchronously by your ESP, which isn't really possible during tests. Also, the specific tracking events will vary depending on who you're emailing and what they do with the message. There's no way for the Test EmailBackend to know which tracking events to send.
Anymail's own tests attempt to thoroughly cover that every ESP webhook even…