def main(override_payday_checks=False): from liberapay.billing.transactions import sync_with_mangopay from liberapay.main import website # https://github.com/liberapay/salon/issues/19#issuecomment-191230689 from liberapay.billing.payday import Payday if not website.env.override_payday_checks and not override_payday_checks: # Check that payday hasn't already been run this week r = website.db.one(""" SELECT id FROM paydays WHERE ts_start >= now() - INTERVAL '6 days' AND ts_end >= ts_start """) assert not r, "payday has already been run this week" # Prevent a race condition, by acquiring a DB lock conn = website.db.get_connection().__enter__() cursor = conn.cursor() lock = cursor.one("SELECT pg_try_advisory_lock(1)") assert lock, "failed to acquire the payday lock" try: sync_with_mangopay(website.db) Payday.start().run(website.env.log_dir, website.env.keep_payday_logs) except KeyboardInterrupt: # pragma: no cover pass except Exception as e: # pragma: no cover website.tell_sentry(e, {}, allow_reraise=False) raise finally: conn.close()
def main(override_payday_checks=False): from liberapay.billing.transactions import sync_with_mangopay from liberapay.main import website # https://github.com/liberapay/salon/issues/19#issuecomment-191230689 from liberapay.billing.payday import Payday if not website.env.override_payday_checks and not override_payday_checks: # Check that payday hasn't already been run this week r = website.db.one(""" SELECT id FROM paydays WHERE ts_start >= now() - INTERVAL '6 days' AND ts_end >= ts_start """) assert not r, "payday has already been run this week" # Prevent a race condition, by acquiring a DB lock with website.db.lock('payday', blocking=False): try: sync_with_mangopay(website.db) Payday.start().run(website.env.log_dir, website.env.keep_payday_logs) except KeyboardInterrupt: # pragma: no cover pass except Exception as e: # pragma: no cover website.tell_sentry(e, {}, allow_reraise=False) raise
def main(override_payday_checks=False): from liberapay.billing.transactions import sync_with_mangopay from liberapay.main import website # https://github.com/liberapay/salon/issues/19#issuecomment-191230689 from liberapay.billing.payday import Payday if not website.env.override_payday_checks and not override_payday_checks: # Check that payday hasn't already been run this week r = website.db.one(""" SELECT id FROM paydays WHERE ts_start >= now() - INTERVAL '6 days' AND ts_end >= ts_start """) assert not r, "payday has already been run this week" # Prevent a race condition, by acquiring a DB lock conn = website.db.get_connection().__enter__() cursor = conn.cursor() lock = cursor.one("SELECT pg_try_advisory_lock(1)") assert lock, "failed to acquire the payday lock" try: sync_with_mangopay(website.db) Payday.start().run(website.env.log_dir, website.env.keep_payday_logs) except KeyboardInterrupt: # pragma: no cover pass except Exception as e: # pragma: no cover website.tell_sentry(e, {}, allow_reraise=False) raise finally: conn.close()
def test_1_sync_with_mangopay_records_exchange_success(self): with mock.patch('liberapay.billing.transactions.record_exchange_result') as rer: rer.side_effect = Foobar() with self.assertRaises(Foobar): charge(self.db, self.janet_route, PAYIN_CARD_MIN, 'http://localhost/') exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'pre' sync_with_mangopay(self.db) exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'succeeded' assert Participant.from_username('janet').balance == PAYIN_CARD_MIN
def test_1_sync_with_mangopay_records_exchange_success(self): amount = PAYIN_CARD_MIN['EUR'].amount with mock.patch('liberapay.billing.transactions.record_exchange_result') as rer: rer.side_effect = Foobar() with self.assertRaises(Foobar): charge(self.db, self.janet_route, amount, 'http://localhost/') exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'pre' sync_with_mangopay(self.db) exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'succeeded' assert Participant.from_username('janet').balance == amount
def test_4_sync_with_mangopay_records_transfer_success(self): self.make_exchange('mango-cc', 10, 0, self.janet) with mock.patch('liberapay.billing.transactions.record_transfer_result') as rtr: rtr.side_effect = Foobar() with self.assertRaises(Foobar): transfer(self.db, self.janet.id, self.david.id, EUR('10.00'), 'tip') t = self.db.one("SELECT * FROM transfers") assert t.status == 'pre' sync_with_mangopay(self.db) t = self.db.one("SELECT * FROM transfers") assert t.status == 'succeeded' assert Participant.from_username('david').balance == 10 assert Participant.from_username('janet').balance == 0
def test_4_sync_with_mangopay_records_transfer_success(self): self.make_exchange('mango-cc', 10, 0, self.janet) with mock.patch('liberapay.billing.transactions.record_transfer_result') as rtr: rtr.side_effect = Foobar() with self.assertRaises(Foobar): transfer(self.db, self.janet.id, self.david.id, EUR('10.00'), 'tip') t = self.db.one("SELECT * FROM transfers") assert t.status == 'pre' sync_with_mangopay(self.db) t = self.db.one("SELECT * FROM transfers") assert t.status == 'succeeded' assert Participant.from_username('david').balance == 10 assert Participant.from_username('janet').balance == 0
def test_5_sync_with_mangopay_reverts_payouts_that_didnt_happen(self): self.make_exchange('mango-cc', 41, 0, self.homer) with mock.patch('liberapay.billing.transactions.record_exchange_result') as rer, \ mock.patch('liberapay.billing.transactions.test_hook') as test_hook: rer.side_effect = test_hook.side_effect = Foobar with self.assertRaises(Foobar): payout(self.db, self.homer_route, EUR('35.00')) exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0") assert exchange.status == 'pre' self.throw_transactions_back_in_time() sync_with_mangopay(self.db) exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0") assert exchange.status == 'failed' homer = self.homer.refetch() assert homer.balance == homer.get_withdrawable_amount('EUR') == 41
def test_2_sync_with_mangopay_handles_payins_that_didnt_happen(self): pass # this is for pep8 with mock.patch('liberapay.billing.transactions.record_exchange_result') as rer, \ mock.patch('liberapay.billing.transactions.DirectPayIn.save', autospec=True) as save: rer.side_effect = save.side_effect = Foobar with self.assertRaises(Foobar): charge(self.db, self.janet_route, EUR('33.67'), 'http://localhost/') exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'pre' self.throw_transactions_back_in_time() sync_with_mangopay(self.db) exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'failed' assert exchange.note == 'interrupted' assert Participant.from_username('janet').balance == 0
def test_5_sync_with_mangopay_reverts_payouts_that_didnt_happen(self): self.make_exchange('mango-cc', 41, 0, self.homer) with mock.patch('liberapay.billing.transactions.record_exchange_result') as rer, \ mock.patch('liberapay.billing.transactions.test_hook') as test_hook: rer.side_effect = test_hook.side_effect = Foobar with self.assertRaises(Foobar): payout(self.db, self.homer_route, D('35.00')) exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0") assert exchange.status == 'pre' self.throw_transactions_back_in_time() sync_with_mangopay(self.db) exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0") assert exchange.status == 'failed' homer = self.homer.refetch() assert homer.balance == homer.get_withdrawable_amount('EUR') == 41
def test_2_sync_with_mangopay_handles_payins_that_didnt_happen(self): pass # this is for pep8 with mock.patch('liberapay.billing.transactions.record_exchange_result') as rer, \ mock.patch('liberapay.billing.transactions.DirectPayIn.save', autospec=True) as save: rer.side_effect = save.side_effect = Foobar with self.assertRaises(Foobar): charge(self.db, self.janet_route, D('33.67'), 'http://localhost/') exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'pre' self.throw_transactions_back_in_time() sync_with_mangopay(self.db) exchange = self.db.one("SELECT * FROM exchanges") assert exchange.status == 'failed' assert exchange.note == 'interrupted' assert Participant.from_username('janet').balance == 0
def test_3_sync_with_mangopay_handles_transfers_that_didnt_happen(self): self.make_exchange('mango-cc', 10, 0, self.janet) with mock.patch('liberapay.billing.transactions._record_transfer_result') as rtr, \ mock.patch('liberapay.billing.transactions.Transfer.save', autospec=True) as save: rtr.side_effect = save.side_effect = Foobar with self.assertRaises(Foobar): transfer(self.db, self.janet.id, self.david.id, EUR('10.00'), 'tip') t = self.db.one("SELECT * FROM transfers") assert t.status == 'pre' self.throw_transactions_back_in_time() sync_with_mangopay(self.db) t = self.db.one("SELECT * FROM transfers") assert t.status == 'failed' assert t.error == 'interrupted' assert Participant.from_username('david').balance == 0 assert Participant.from_username('janet').balance == 10
def test_3_sync_with_mangopay_handles_transfers_that_didnt_happen(self): self.make_exchange('mango-cc', 10, 0, self.janet) with mock.patch('liberapay.billing.transactions._record_transfer_result') as rtr, \ mock.patch('liberapay.billing.transactions.Transfer.save', autospec=True) as save: rtr.side_effect = save.side_effect = Foobar with self.assertRaises(Foobar): transfer(self.db, self.janet.id, self.david.id, EUR('10.00'), 'tip') t = self.db.one("SELECT * FROM transfers") assert t.status == 'pre' self.throw_transactions_back_in_time() sync_with_mangopay(self.db) t = self.db.one("SELECT * FROM transfers") assert t.status == 'failed' assert t.error == 'interrupted' assert Participant.from_username('david').balance == 0 assert Participant.from_username('janet').balance == 10