Skip to content

Commit

Permalink
move eventutil from urllib to requests (#700)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pyrox0 authored Sep 3, 2024
1 parent b8d7228 commit da08548
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
29 changes: 20 additions & 9 deletions tests/test_eventutil.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
Expand All @@ -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
11 changes: 4 additions & 7 deletions tracker/eventutil.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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})
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit da08548

Please sign in to comment.