From 7d49351adf2df258ecbea20a2de333a2efafbfb1 Mon Sep 17 00:00:00 2001 From: Julien Enselme Date: Tue, 7 May 2019 16:46:10 +0200 Subject: [PATCH] Fallback to 0.0.0.0 if REMOTE_ADDR is an empty string It complements 5ebc785543136479d7062523fb67ff8d4796c161 which fixes the issue when REMOTE_ADDR evaluates to None. It appears that on some environment it can also be an empty string. With this change, both cases are supported. See also #640 --- HISTORY.rst | 1 + djstripe/models/webhooks.py | 2 +- tests/test_webhooks.py | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index f79936d4c6..e4e082a44f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -9,6 +9,7 @@ History This is a bugfix-only version: - Don't save event objects if the webhook processing fails (#832). +- Fixed IntegrityError when ``REMOTE_ADDR`` is an empty string. 2.0.1 (2019-04-29) ------------------ diff --git a/djstripe/models/webhooks.py b/djstripe/models/webhooks.py index 8c1df2b90b..252df38551 100644 --- a/djstripe/models/webhooks.py +++ b/djstripe/models/webhooks.py @@ -80,7 +80,7 @@ def from_request(cls, request): body = "(error decoding body)" ip = request.META.get("REMOTE_ADDR") - if ip is None: + if not ip: warnings.warn( "Could not determine remote IP (missing REMOTE_ADDR). " "This is likely an issue with your wsgi/server setup." diff --git a/tests/test_webhooks.py b/tests/test_webhooks.py index 3fcc2de529..a5f809510f 100644 --- a/tests/test_webhooks.py +++ b/tests/test_webhooks.py @@ -169,7 +169,7 @@ def test_webhook_no_signature(self): self.assertEqual(resp.status_code, 400) self.assertEqual(WebhookEventTrigger.objects.count(), 0) - def test_webhook_no_remote_addr(self): + def test_webhook_remote_addr_is_none(self): self.assertEqual(WebhookEventTrigger.objects.count(), 0) with warnings.catch_warnings(): warnings.simplefilter("ignore") @@ -185,6 +185,22 @@ def test_webhook_no_remote_addr(self): event_trigger = WebhookEventTrigger.objects.first() self.assertEqual(event_trigger.remote_ip, "0.0.0.0") + def test_webhook_remote_addr_is_empty_string(self): + self.assertEqual(WebhookEventTrigger.objects.count(), 0) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + Client().post( + reverse("djstripe:webhook"), + "{}", + content_type="application/json", + HTTP_STRIPE_SIGNATURE="PLACEHOLDER", + REMOTE_ADDR="", + ) + + self.assertEqual(WebhookEventTrigger.objects.count(), 1) + event_trigger = WebhookEventTrigger.objects.first() + self.assertEqual(event_trigger.remote_ip, "0.0.0.0") + @patch( "djstripe.models.WebhookEventTrigger.validate", return_value=True, autospec=True )