From da085483ce6697e3495b12b7297ad866a902fdbf Mon Sep 17 00:00:00 2001 From: Pyrox Date: Tue, 3 Sep 2024 17:33:44 -0400 Subject: [PATCH] move eventutil from urllib to requests (#700) This resolves the 2018 TODO comment that suggests moving to requests. I've preserved exact compatibility with the previous version here by ensuring that the body is exactly the same, and I've re-enabled the test for eventutil since it can now be mocked with responses. --- tests/test_eventutil.py | 29 ++++++++++++++++++++--------- tracker/eventutil.py | 11 ++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/tests/test_eventutil.py b/tests/test_eventutil.py index 67ea21f40..af75af032 100644 --- a/tests/test_eventutil.py +++ b/tests/test_eventutil.py @@ -1,8 +1,8 @@ # coding: utf-8 import datetime +import json from decimal import Decimal -from unittest import skip import responses from django.test import TransactionTestCase @@ -22,13 +22,13 @@ def setUp(self): datetime=datetime.datetime(2018, 1, 1), ) self.postback = PostbackURL.objects.create( - event=self.event, url='https://example.com' + event=self.event, + url='https://example.com', ) - @skip("This test only works with requests :')") @responses.activate def test_request_made(self): - responses.add(responses.GET, 'https://example.com', status=200) + responses.post('https://example.com', status=200) donation = Donation.objects.create( timereceived=datetime.datetime(2018, 1, 1), @@ -37,13 +37,24 @@ def test_request_made(self): domain='PAYPAL', donor=self.donor, event=self.event, + transactionstate='COMPLETED', ) eventutil.post_donation_to_postbacks(donation) assert len(responses.calls) == 1 - assert ( - responses.calls[0].request.url - == 'https://example.com/?comment=&amount=1.5&timereceived=2018-01-01+00%3A00%3A00&donor__visibility=FIRST&domain=PAYPAL&id=1&donor__visiblename=%28No+Name%29' - ) - assert responses.calls[0].response.status_code == 200 + resp = responses.calls[0] + assert resp.request.url == 'https://example.com/' + assert json.loads(resp.request.body) == { + 'id': donation.id, + 'event': donation.event_id, + 'timereceived': '2018-01-01 00:00:00', + 'comment': '', + 'amount': 1.5, + 'donor__visibility': 'FIRST', + 'donor__visiblename': '(No Name)', + 'new_total': 1.5, + 'domain': 'PAYPAL', + 'bids': [], + } + assert resp.response.status_code == 200 diff --git a/tracker/eventutil.py b/tracker/eventutil.py index 8b7dccbaa..2817152d9 100644 --- a/tracker/eventutil.py +++ b/tracker/eventutil.py @@ -1,7 +1,7 @@ import json import traceback -import urllib.request +import requests from asgiref.sync import async_to_sync from channels.layers import get_channel_layer from django.core import serializers @@ -12,8 +12,6 @@ import tracker.viewutil as viewutil from tracker.consumers.processing import broadcast_new_donation_to_processors -# TODO: this is 2018, we ought to be using requests - def post_donation_to_postbacks(donation): event_donations = filters.run_model_query('donation', {'event': donation.event.id}) @@ -57,13 +55,12 @@ def post_donation_to_postbacks(donation): postbacks = models.PostbackURL.objects.filter(event=donation.event) for postback in postbacks: - opener = urllib.request.build_opener() - req = urllib.request.Request( + requests.post( postback.url, - data_json, + data=data_json, headers={'Content-Type': 'application/json; charset=utf-8'}, + timeout=5, ) - opener.open(req, timeout=5) except Exception: viewutil.tracker_log( 'postback_url', traceback.format_exc(), event=donation.event