Skip to content

Commit

Permalink
expand some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Sep 17, 2023
1 parent a0a3cbf commit c23ca66
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/py/test_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pando.utils import utcnow

from liberapay.constants import PAYIN_AMOUNTS
from liberapay.cron import Daily, Weekly
from liberapay.i18n.currencies import fetch_currency_exchange_rates
from liberapay.models.participant import (
Expand Down Expand Up @@ -110,13 +111,16 @@ def test_disabled_job_is_not_run(self):
job.period = period

def test_fetch_currency_exchange_rates(self):
assert PAYIN_AMOUNTS['paypal']['min_acceptable']['HUF']
assert 'HUF' in PAYIN_AMOUNTS['paypal']['min_acceptable']
currency_exchange_rates = self.client.website.currency_exchange_rates.copy()
try:
with self.allow_changes_to('currency_exchange_rates'), self.db.get_cursor() as cursor:
fetch_currency_exchange_rates(cursor)
cursor.connection.rollback()
finally:
self.client.website.currency_exchange_rates = currency_exchange_rates
assert 'HUF' not in PAYIN_AMOUNTS['paypal']['min_acceptable']

def test_send_account_disabled_notifications(self):
admin = self.make_participant('admin', privileges=1)
Expand Down
28 changes: 28 additions & 0 deletions tests/py/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from liberapay.billing.payday import compute_next_payday_date
from liberapay.i18n.base import LOCALE_EN
from liberapay.models.participant import Participant
from liberapay.payin.common import update_payin_transfer
from liberapay.payin.cron import (
execute_scheduled_payins,
Expand Down Expand Up @@ -742,6 +743,17 @@ def test_late_manual_payment_switched_to_automatic_is_scheduled_a_week_away(self

class TestScheduledPayins(EmailHarness):

def setUp(self):
super().setUp()
schedule_renewals = Participant.schedule_renewals
self.sr_patch = patch.object(Participant, 'schedule_renewals', autospec=True)
self.sr_mock = self.sr_patch.__enter__()
self.sr_mock.side_effect = schedule_renewals

def tearDown(self):
self.sr_patch.__exit__(None, None, None)
super().tearDown()

def test_no_scheduled_payins(self):
self.make_participant('alice')
send_upcoming_debit_notifications()
Expand Down Expand Up @@ -783,6 +795,7 @@ def test_one_scheduled_payin(self):
SET execution_date = current_date
, last_notif_ts = (last_notif_ts - interval '14 days')
""")
self.sr_mock.reset_mock()
execute_scheduled_payins()
payins = self.db.all("SELECT * FROM payins ORDER BY ctime")
assert len(payins) == 2
Expand All @@ -798,6 +811,7 @@ def test_one_scheduled_payin(self):
assert len(emails) == 1
assert emails[0]['to'] == ['alice <[email protected]>']
assert emails[0]['subject'] == "Your payment has succeeded"
assert self.sr_mock.call_count == 1

scheduled_payins = self.db.all(
"SELECT * FROM scheduled_payins ORDER BY execution_date"
Expand Down Expand Up @@ -859,6 +873,7 @@ def test_multiple_scheduled_payins(self):
SET execution_date = %(execution_date)s
WHERE id = %(id)s
""", manual_payin.__dict__)
self.sr_mock.reset_mock()
execute_scheduled_payins()
payins = self.db.all("SELECT * FROM payins ORDER BY ctime")
assert len(payins) == 4
Expand All @@ -882,6 +897,7 @@ def test_multiple_scheduled_payins(self):
assert len(emails) == 1
assert emails[0]['to'] == ['alice <[email protected]>']
assert emails[0]['subject'] == "Your payment has succeeded"
assert self.sr_mock.call_count == 1

def test_early_manual_renewal_of_automatic_donations(self):
alice = self.make_participant('alice', email='[email protected]')
Expand Down Expand Up @@ -969,6 +985,7 @@ def test_canceled_and_impossible_transfers(self):
, last_notif_ts = (last_notif_ts - interval '14 days')
, ctime = (ctime - interval '12 hours')
""")
self.sr_mock.reset_mock()
execute_scheduled_payins()
payins = self.db.all("SELECT * FROM payins ORDER BY ctime")
assert len(payins) == 3
Expand All @@ -978,6 +995,7 @@ def test_canceled_and_impossible_transfers(self):
assert emails[0]['subject'] == 'Liberapay donation renewal: payment aborted'
assert emails[1]['to'] == ['alice <[email protected]>']
assert emails[1]['subject'] == 'Liberapay donation renewal: payment aborted'
assert self.sr_mock.call_count == 0

def test_canceled_scheduled_payin(self):
alice = self.make_participant('alice', email='[email protected]')
Expand Down Expand Up @@ -1036,6 +1054,7 @@ def test_scheduled_payin_suspended_payer(self):
, last_notif_ts = (last_notif_ts - interval '14 days')
, ctime = (ctime - interval '12 hours')
""")
self.sr_mock.reset_mock()
execute_scheduled_payins()
payins = self.db.all("SELECT * FROM payins ORDER BY ctime")
assert len(payins) == 1
Expand All @@ -1044,6 +1063,7 @@ def test_scheduled_payin_suspended_payer(self):
assert payins[0].off_session is False
emails = self.get_emails()
assert len(emails) == 0
assert self.sr_mock.call_count == 0

def test_missing_route(self):
alice = self.make_participant('alice', email='[email protected]')
Expand Down Expand Up @@ -1074,6 +1094,7 @@ def test_missing_route(self):
SET execution_date = current_date
, last_notif_ts = (last_notif_ts - interval '14 days')
""")
self.sr_mock.reset_mock()
execute_scheduled_payins()
payins = self.db.all("SELECT * FROM payins ORDER BY ctime")
assert len(payins) == 1
Expand All @@ -1082,6 +1103,7 @@ def test_missing_route(self):
assert payins[0].off_session is False
emails = self.get_emails()
assert len(emails) == 0
assert self.sr_mock.call_count == 0

def test_scheduled_payin_requiring_authentication(self):
alice = self.make_participant('alice', email='[email protected]')
Expand Down Expand Up @@ -1114,6 +1136,7 @@ def test_scheduled_payin_requiring_authentication(self):
, last_notif_ts = (last_notif_ts - interval '14 days')
, ctime = (ctime - interval '12 hours')
""")
self.sr_mock.reset_mock()
execute_scheduled_payins()
payins = self.db.all("SELECT * FROM payins ORDER BY ctime")
assert len(payins) == 2
Expand All @@ -1130,6 +1153,7 @@ def test_scheduled_payin_requiring_authentication(self):
scheduled_payins = self.db.all("SELECT * FROM scheduled_payins ORDER BY id")
assert len(scheduled_payins) == 1
assert scheduled_payins[0].payin == payins[1].id
assert self.sr_mock.call_count == 0
# Test the payin page, it should redirect to the 3DSecure page
r = self.client.GET(payin_page_path, auth_as=alice, raise_immediately=False)
assert r.code == 200
Expand Down Expand Up @@ -1168,9 +1192,11 @@ def test_scheduled_automatic_payin_currency_unaccepted_before_reminder(self):
, last_notif_ts = (last_notif_ts - interval '14 days')
, ctime = (ctime - interval '12 hours')
""")
self.sr_mock.reset_mock()
execute_scheduled_payins()
emails = self.get_emails()
assert len(emails) == 0
assert self.sr_mock.call_count == 0
send_donation_reminder_notifications()
emails = self.get_emails()
assert len(emails) == 1
Expand Down Expand Up @@ -1220,6 +1246,7 @@ def test_scheduled_automatic_payin_currency_unaccepted_after_reminder(self):
, last_notif_ts = (last_notif_ts - interval '14 days')
, ctime = (ctime - interval '12 hours')
""")
self.sr_mock.reset_mock()
execute_scheduled_payins()
emails = self.get_emails()
assert len(emails) == 1
Expand All @@ -1229,6 +1256,7 @@ def test_scheduled_automatic_payin_currency_unaccepted_after_reminder(self):
assert len(scheduled_payins) == 1
assert scheduled_payins[0].payin is None
assert scheduled_payins[0].notifs_count == 2
assert self.sr_mock.call_count == 0

def test_cancelling_a_scheduled_payin(self):
alice = self.make_participant('alice', email='[email protected]')
Expand Down

0 comments on commit c23ca66

Please sign in to comment.