def test_get_tip_distribution_handles_a_tip(self): Participant.from_username('alice').set_tip_to('bob', '3.00') expected = ([[Decimal('3.00'), 1, Decimal('3.00'), 1.0, Decimal('1')]], 1.0, Decimal('3.00')) actual = Participant.from_username('bob').get_tip_distribution() assert actual == expected
def setUp(self): super(Harness, self).setUp() now = utcnow() for idx, username in enumerate(['alice', 'bob', 'carl'], start=1): self.make_participant(username, claimed_time=now) twitter_account = TwitterAccount(idx, {'screen_name': username}) Participant.from_username(username).take_over(twitter_account)
def test_taking_over_yourself_sets_all_to_zero(self): bob_twitter = self.make_elsewhere("twitter", "2", "bob") Participant.from_username("alice").set_tip_to("bob", "1.00") Participant.from_username("alice").take_over(bob_twitter, have_confirmation=True) expected = Decimal("0.00") actual = Participant.from_username("alice").get_dollars_giving() assert actual == expected
def test_taking_over_yourself_sets_all_to_zero(self): bob_twitter = StubAccount('twitter', '2') Participant.from_username('alice').set_tip_to('bob', '1.00') Participant.from_username('alice').take_over(bob_twitter, have_confirmation=True) expected = Decimal('0.00') actual = Participant.from_username('alice').get_dollars_giving() assert actual == expected
def test_cpi_clears_personal_information(self): alice = self.make_participant( 'alice' , statement='not forgetting to be awesome!' , goal=100 , anonymous_giving=True , anonymous_receiving=True , number='plural' , avatar_url='img-url' , email=('*****@*****.**', True) , claimed_time='now' , session_token='deadbeef' , session_expires='2000-01-01' , giving=20 , pledging=30 , receiving=40 ) assert Participant.from_username('alice').number == 'plural' # sanity check with self.db.get_cursor() as cursor: alice.clear_personal_information(cursor) new_alice = Participant.from_username('alice') assert alice.statement == new_alice.statement == '' assert alice.goal == new_alice.goal == None assert (alice.anonymous_giving, new_alice.anonymous_giving) == (False, False) assert (alice.anonymous_receiving, new_alice.anonymous_giving) == (False, False) assert alice.number == new_alice.number == 'singular' assert alice.avatar_url == new_alice.avatar_url == None assert alice.email == new_alice.email == None assert alice.claimed_time == new_alice.claimed_time == None assert alice.giving == new_alice.giving == 0 assert alice.pledging == new_alice.pledging == 0 assert alice.receiving == new_alice.receiving == 0 assert alice.session_token == new_alice.session_token == None assert alice.session_expires.year == new_alice.session_expires.year == date.today().year
def test_get_tip_distribution_ignores_missing_cc(self): self.make_participant("missing_cc", last_bill_result=None) Participant.from_username("alice").set_tip_to("bob", "1.00") Participant.from_username("missing_cc").set_tip_to("bob", "3.00") expected = ([[Decimal("1.00"), 1L, Decimal("1.00"), 1, Decimal("1")]], 1.0, Decimal("1.00")) actual = Participant.from_username("bob").get_tip_distribution() assert actual == expected
def test_uic_uses_supplied_cursor(self): alice = self.make_participant('alice') with self.db.get_cursor() as cursor: alice.update_is_closed(True, cursor) assert alice.is_closed assert not Participant.from_username('alice').is_closed assert Participant.from_username('alice').is_closed
def test_update_receiving_amounts_updates_receiving_amounts(self): A = self.make_participant("A") B = self.make_participant("B", claimed_time="now", last_bill_result="") B.set_tip_to(A, D("10.00"), update_tippee=False) assert Participant.from_username("A").receiving == 0 self.payday.update_receiving_amounts() assert Participant.from_username("A").receiving == 10
def test_update_receiving_amounts_updates_receiving_amounts(self): A = self.make_participant('A') B = self.make_participant('B', claimed_time='now', last_bill_result='') B.set_tip_to(A, D('10.00'), update_tippee=False) assert Participant.from_username('A').receiving == 0 self.payday.update_receiving_amounts() assert Participant.from_username('A').receiving == 10
def test_get_tip_distribution_ignores_bad_cc(self): self.make_participant('bad_cc', last_bill_result='Failure!') Participant.from_username('alice').set_tip_to('bob', '1.00') Participant.from_username('bad_cc').set_tip_to('bob', '3.00') expected = ([[Decimal('1.00'), 1L, Decimal('1.00'), 1, Decimal('1')]], 1.0, Decimal('1.00')) actual = Participant.from_username('bob').get_tip_distribution() assert_equals(actual, expected)
def test_get_tip_distribution_ignores_missing_cc(self): self.make_participant('missing_cc', last_bill_result=None) Participant.from_username('alice').set_tip_to('bob', '1.00') Participant.from_username('missing_cc').set_tip_to('bob', '3.00') expected = ([[Decimal('1.00'), 1L, Decimal('1.00'), 1, Decimal('1')]], 1.0, Decimal('1.00')) actual = Participant.from_username('bob').get_tip_distribution() assert actual == expected
def test_payday_moves_money(self, charge_on_balanced): charge_on_balanced.return_value = (D('10.00'), D('0.68'), "") self.janet.set_tip_to(self.homer, '6.00') # under $10! self.payday.run() janet = Participant.from_username('janet') homer = Participant.from_username('homer') assert homer.balance == D('6.00') assert janet.balance == D('3.32')
def test_get_tip_distribution_handles_multiple_tips(self): self.make_participant('carl', last_bill_result='') Participant.from_username('alice').set_tip_to('bob', '1.00') Participant.from_username('carl').set_tip_to('bob', '3.00') expected = ([ [Decimal('1.00'), 1L, Decimal('1.00'), 0.5, Decimal('0.25')], [Decimal('3.00'), 1L, Decimal('3.00'), 0.5, Decimal('0.75')] ], 2.0, Decimal('4.00')) actual = Participant.from_username('bob').get_tip_distribution() assert actual == expected
def test_payday_moves_money(self, fch): self.janet.set_tip_to(self.homer, '6.00') # under $10! fch.return_value = {} Payday.start().run() janet = Participant.from_username('janet') homer = Participant.from_username('homer') assert homer.balance == D('6.00') assert janet.balance == D('3.41')
def test_dbafg_favors_highest_tippee_in_rounding_errors(self): alice = self.make_participant('alice', claimed_time='now', balance=D('10.00')) bob = self.make_participant('bob', claimed_time='now') carl = self.make_participant('carl', claimed_time='now') alice.set_tip_to(bob, D('3.00')) alice.set_tip_to(carl, D('6.00')) with self.db.get_cursor() as cursor: alice.distribute_balance_as_final_gift(cursor) assert Participant.from_username('bob').balance == D('3.33') assert Participant.from_username('carl').balance == D('6.67') assert Participant.from_username('alice').balance == D('0.00')
def test_update_receiving_amounts_includes_taking(self): A = self.make_participant('A', claimed_time='now', taking=3) B = self.make_participant('B', claimed_time='now', last_bill_result='') B.set_tip_to(A, D('10.00'), update_tippee=False) assert Participant.from_username('A').receiving == 0 assert Participant.from_username('A').taking == 3 Payday.start().update_receiving_amounts() assert Participant.from_username('A').receiving == 13 assert Participant.from_username('A').taking == 3
def test_dbafg_gives_all_to_claimed(self): alice = self.make_participant('alice', claimed_time='now', balance=D('10.00')) bob = self.make_participant('bob', claimed_time='now') carl = self.make_participant('carl') alice.set_tip_to(bob, D('3.00')) alice.set_tip_to(carl, D('2.00')) with self.db.get_cursor() as cursor: alice.distribute_balance_as_final_gift(cursor) assert Participant.from_username('bob').balance == D('10.00') assert Participant.from_username('carl').balance == D('0.00') assert Participant.from_username('alice').balance == D('0.00')
def test_can_post_to_close_page(self): alice = self.make_participant('alice', claimed_time='now', balance=7) bob = self.make_participant('bob', claimed_time='now') alice.set_tip_to(bob, D('10.00')) data = {'disbursement_strategy': 'downstream'} response = self.client.PxST('/alice/account/close', auth_as='alice', data=data) assert response.code == 302 assert response.headers['Location'] == '/alice/' assert Participant.from_username('alice').balance == 0 assert Participant.from_username('bob').balance == 7
def test_dbafg_distributes_balance_as_final_gift(self): alice = self.make_participant('alice', balance=D('10.00')) bob = self.make_participant('bob', claimed_time='now') carl = self.make_participant('carl', claimed_time='now') alice.set_tip_to(bob, D('3.00')) alice.set_tip_to(carl, D('2.00')) with self.db.get_cursor() as cursor: alice.distribute_balance_as_final_gift(cursor) assert Participant.from_username('bob').balance == D('6.00') assert Participant.from_username('carl').balance == D('4.00') assert Participant.from_username('alice').balance == D('0.00')
def test_dbafg_with_zero_balance_is_a_noop(self): alice = self.make_participant('alice', claimed_time='now', balance=D('0.00')) bob = self.make_participant('bob', claimed_time='now') carl = self.make_participant('carl', claimed_time='now') alice.set_tip_to(bob, D('3.00')) alice.set_tip_to(carl, D('6.00')) with self.db.get_cursor() as cursor: alice.distribute_balance_as_final_gift(cursor) assert self.db.one("SELECT count(*) FROM tips") == 2 assert Participant.from_username('bob').balance == D('0.00') assert Participant.from_username('carl').balance == D('0.00') assert Participant.from_username('alice').balance == D('0.00')
def test_dbafg_needs_claimed_tips(self): alice = self.make_participant('alice', balance=D('10.00')) bob = self.make_participant('bob') carl = self.make_participant('carl') alice.set_tip_to(bob, D('3.00')) alice.set_tip_to(carl, D('2.00')) with self.db.get_cursor() as cursor: with pytest.raises(alice.NoOneToGiveFinalGiftTo): alice.distribute_balance_as_final_gift(cursor) assert Participant.from_username('bob').balance == D('0.00') assert Participant.from_username('carl').balance == D('0.00') assert Participant.from_username('alice').balance == D('10.00')
def test_transfer_takes_doesnt_make_negative_transfers(self, fch): hold = balanced.CardHold(amount=1500, meta={'participant_id': self.janet.id}) hold.capture = lambda *a, **kw: None hold.save = lambda *a, **kw: None fch.return_value = {self.janet.id: hold} self.janet.update_number('plural') self.janet.set_tip_to(self.homer, 10) self.janet.add_member(self.david) Payday.start().payin() assert Participant.from_id(self.david.id).balance == 0 assert Participant.from_id(self.homer.id).balance == 10 assert Participant.from_id(self.janet.id).balance == 0
def test_pachinko_pachinkos(self): a_team = self.make_participant("a_team", claimed_time="now", number="plural", balance=20, pending=0) a_team.add_member(self.make_participant("alice", claimed_time="now", balance=0, pending=0)) a_team.add_member(self.make_participant("bob", claimed_time="now", balance=0, pending=0)) ts_start = self.payday.start() participants = self.payday.genparticipants(ts_start, LOOP_PACHINKO) self.payday.pachinko(ts_start, participants) assert Participant.from_username("alice").pending == D("0.01") assert Participant.from_username("bob").pending == D("0.01")
def test_dbafg_with_zero_balance_is_a_noop(self): alice = self.make_participant('alice', balance=D('0.00')) bob = self.make_participant('bob', claimed_time='now') carl = self.make_participant('carl', claimed_time='now') alice.set_tip_to(bob, D('3.00')) alice.set_tip_to(carl, D('6.00')) with self.db.get_cursor() as cursor: alice.distribute_balance_as_final_gift(cursor) assert self.db.one("SELECT count(*) FROM tips") == 2 assert Participant.from_username('bob').balance == D('0.00') assert Participant.from_username('carl').balance == D('0.00') assert Participant.from_username('alice').balance == D('0.00')
def test_payin_cancels_uncaptured_holds(self, log): self.janet.set_tip_to(self.homer, 42) alice = self.make_participant('alice', claimed_time='now', is_suspicious=False) self.make_exchange('bill', 50, 0, alice) alice.set_tip_to(self.janet, 50) Payday.start().payin() assert log.call_args_list[-3][0] == ("Captured 0 card holds.",) assert log.call_args_list[-2][0] == ("Canceled 1 card holds.",) assert Participant.from_id(alice.id).balance == 0 assert Participant.from_id(self.janet.id).balance == 8 assert Participant.from_id(self.homer.id).balance == 42
def test_dbafg_needs_claimed_tips(self): alice = self.make_participant('alice', claimed_time='now', balance=D('10.00')) bob = self.make_participant('bob') carl = self.make_participant('carl') alice.set_tip_to(bob, D('3.00')) alice.set_tip_to(carl, D('2.00')) with self.db.get_cursor() as cursor: with pytest.raises(alice.NoOneToGiveFinalGiftTo): alice.distribute_balance_as_final_gift(cursor) assert Participant.from_username('bob').balance == D('0.00') assert Participant.from_username('carl').balance == D('0.00') assert Participant.from_username('alice').balance == D('10.00')
def test_payin_doesnt_process_tips_when_goal_is_negative(self): alice = self.make_participant('alice', claimed_time='now', balance=20) bob = self.make_participant('bob', claimed_time='now') alice.set_tip_to(bob, 13) self.db.run("UPDATE participants SET goal = -1 WHERE username='******'") payday = Payday.start() with self.db.get_cursor() as cursor: payday.prepare(cursor, payday.ts_start) payday.transfer_tips(cursor) payday.update_balances(cursor) assert Participant.from_id(alice.id).balance == 20 assert Participant.from_id(bob.id).balance == 0
def test_pachinko_pachinkos(self): a_team = self.make_participant('a_team', claimed_time='now', number='plural', balance=20, \ pending=0) a_team.add_member(self.make_participant('alice', claimed_time='now', balance=0, pending=0)) a_team.add_member(self.make_participant('bob', claimed_time='now', balance=0, pending=0)) ts_start = self.payday.start() participants = self.payday.genparticipants(ts_start, LOOP_PACHINKO) self.payday.pachinko(ts_start, participants) assert Participant.from_username('alice').pending == D('0.01') assert Participant.from_username('bob').pending == D('0.01')
def test_get_tip_distribution_handles_multiple_tips(self): self.make_participant('carl', last_bill_result='') Participant.from_username('alice').set_tip_to('bob', '1.00') Participant.from_username('carl').set_tip_to('bob', '3.00') expected = ([[ Decimal('1.00'), 1L, Decimal('1.00'), 0.5, Decimal('0.25') ], [Decimal('3.00'), 1L, Decimal('3.00'), 0.5, Decimal('0.75')]], 2.0, Decimal('4.00')) actual = Participant.from_username('bob').get_tip_distribution() assert actual == expected
def test_get_tip_distribution_handles_multiple_tips(self): self.make_participant("carl", last_bill_result="") Participant.from_username("alice").set_tip_to("bob", "1.00") Participant.from_username("carl").set_tip_to("bob", "3.00") expected = ( [ [Decimal("1.00"), 1L, Decimal("1.00"), 0.5, Decimal("0.25")], [Decimal("3.00"), 1L, Decimal("3.00"), 0.5, Decimal("0.75")], ], 2.0, Decimal("4.00"), ) actual = Participant.from_username("bob").get_tip_distribution() assert actual == expected
def test_transfer_tips(self): alice = self.make_participant('alice', claimed_time='now', balance=1, last_bill_result='') alice.set_tip_to(self.janet, D('0.51')) alice.set_tip_to(self.homer, D('0.50')) payday = Payday.start() with self.db.get_cursor() as cursor: payday.prepare(cursor, payday.ts_start) payday.transfer_tips(cursor) payday.update_balances(cursor) alice = Participant.from_id(alice.id) assert Participant.from_id(alice.id).balance == D('0.49') assert Participant.from_id(self.janet.id).balance == D('0.51') assert Participant.from_id(self.homer.id).balance == 0
def test_iter_payday_events(self): Payday.start().run() team = self.make_participant('team', number='plural', claimed_time='now') alice = self.make_participant('alice', claimed_time='now') self.make_exchange('bill', 10000, 0, team) self.make_exchange('bill', 10000, 0, alice) self.make_exchange('bill', -5000, 0, alice) self.db.run(""" UPDATE transfers SET timestamp = "timestamp" - interval '1 month' """) bob = self.make_participant('bob', claimed_time='now') carl = self.make_participant('carl', claimed_time='now') team.add_member(bob) team.set_take_for(bob, Decimal('1.00'), team) alice.set_tip_to(bob, Decimal('5.00')) assert bob.balance == 0 for i in range(2): with patch.object(Payday, 'fetch_card_holds') as fch: fch.return_value = {} Payday.start().run() self.db.run(""" UPDATE paydays SET ts_start = ts_start - interval '1 week' , ts_end = ts_end - interval '1 week'; UPDATE transfers SET timestamp = "timestamp" - interval '1 week'; """) bob = Participant.from_id(bob.id) assert bob.balance == 12 Payday().start() events = list(iter_payday_events(self.db, bob)) assert len(events) == 8 assert events[0]['kind'] == 'day-open' assert events[0]['payday_number'] == 2 assert events[1]['balance'] == 12 assert events[-1]['kind'] == 'day-close' assert events[-1]['balance'] == '0.00' alice = Participant.from_id(alice.id) assert alice.balance == 4990 events = list(iter_payday_events(self.db, alice)) assert len(events) == 10 carl = Participant.from_id(carl.id) assert carl.balance == 0 events = list(iter_payday_events(self.db, carl)) assert len(events) == 0
def test_payday_doesnt_move_money_to_a_suspicious_account(self, charge_on_balanced): charge_on_balanced.return_value = (D('10.00'), D('0.68'), "") self.db.run(""" UPDATE participants SET is_suspicious = true WHERE username = '******' """) self.janet.set_tip_to(self.homer, '6.00') # under $10! self.payday.run() janet = Participant.from_username('janet') homer = Participant.from_username('homer') assert janet.balance == D('0.00') assert homer.balance == D('0.00')
def test_payday_doesnt_move_money_to_a_suspicious_account(self, fch): self.db.run(""" UPDATE participants SET is_suspicious = true WHERE username = '******' """) self.janet.set_tip_to(self.homer, '6.00') # under $10! fch.return_value = {} Payday.start().run() janet = Participant.from_username('janet') homer = Participant.from_username('homer') assert janet.balance == D('0.00') assert homer.balance == D('0.00')
def test_iter_payday_events(self): Payday(self.db).run() team = self.make_participant('team', number='plural', claimed_time='now', balance=10000) alice = self.make_participant('alice', claimed_time='now', balance=5000) self.db.run(""" INSERT INTO exchanges (amount, fee, participant, timestamp) VALUES (10000, 0, 'team', now() - interval '1 month') , (10000, 0, 'alice', now() - interval '1 month') , (-5000, 0, 'alice', now() - interval '1 month'); """) bob = self.make_participant('bob', claimed_time='now') carl = self.make_participant('carl', claimed_time='now') team.add_member(bob) team.set_take_for(bob, Decimal('1.00'), team) alice.set_tip_to(bob, Decimal('5.00')) assert bob.balance == 0 for i in range(2): Payday(self.db).run() self.db.run(""" UPDATE paydays SET ts_start = ts_start - interval '1 week' , ts_end = ts_end - interval '1 week'; UPDATE transfers SET timestamp = "timestamp" - interval '1 week'; """) bob = Participant.from_id(bob.id) assert bob.balance == 12 Payday(self.db).start() events = list(iter_payday_events(self.db, bob)) assert len(events) == 8 assert events[0]['kind'] == 'day-open' assert events[0]['payday_number'] == 2 assert events[1]['balance'] == 12 assert events[-1]['kind'] == 'day-close' assert events[-1]['balance'] == '0.00' alice = Participant.from_id(alice.id) assert alice.balance == 4990 events = list(iter_payday_events(self.db, alice)) assert len(events) == 10 carl = Participant.from_id(carl.id) assert carl.balance == 0 events = list(iter_payday_events(self.db, carl)) assert len(events) == 0
def test_record_credit_updates_balance(self): self.payday.record_credit(amount=D("-1.00"), fee=D("0.41"), error="", username="******") alice = Participant.from_username('alice') assert alice.balance == D("0.59")
def make_participant(self, username, **kw): # At this point wireup.db() has been called, but not ... wireup.username_restrictions(self.client.website) participant = Participant.with_random_username() participant.change_username(username) if 'elsewhere' in kw or 'claimed_time' in kw: username = participant.username platform = kw.pop('elsewhere', 'github') self.seq += 1 self.db.run( """ INSERT INTO elsewhere (platform, user_id, user_name, participant) VALUES (%s,%s,%s,%s) """, (platform, self.seq, username, username)) # Update participant if kw: if kw.get('claimed_time') == 'now': kw['claimed_time'] = utcnow() cols, vals = zip(*kw.items()) cols = ', '.join(cols) placeholders = ', '.join(['%s'] * len(vals)) participant = self.db.one( """ UPDATE participants SET ({0}) = ({1}) WHERE username=%s RETURNING participants.*::participants """.format(cols, placeholders), vals + (participant.username, )) return participant
def test_uic_updates_is_closed_False(self): alice = self.make_participant('alice') alice.update_is_closed(True) alice.update_is_closed(False) assert not alice.is_closed assert not Participant.from_username('alice').is_closed
def test_record_credit_doesnt_update_balance_if_error(self): self.payday.record_credit(amount=Decimal("-1.00"), fee=Decimal("0.41"), error="SOME ERROR", username="******") alice = Participant.from_username('alice') assert_equals(alice.balance, Decimal("0.00"))
def make_participant(self, username, **kw): # At this point wireup.db() has been called, but not ... wireup.username_restrictions(self.client.website) participant = Participant.with_random_username() participant.change_username(username) if 'elsewhere' in kw or 'claimed_time' in kw: username = participant.username platform = kw.pop('elsewhere', 'github') self.seq += 1 self.db.run( """ INSERT INTO elsewhere (platform, user_id, user_name, participant) VALUES (%s,%s,%s,%s) """, (platform, self.seq, username, username)) # brute force update for use in testing for k, v in kw.items(): if k == 'claimed_time': if v == 'now': v = datetime.datetime.now(pytz.utc) self.db.run("UPDATE participants SET {}=%s WHERE username=%s" \ .format(k), (v, participant.username)) participant.set_attributes(**kw) return participant
def fake_participant(db, number="singular", is_admin=False): """Create a fake User. """ username = faker.first_name() + fake_text_id(3) _fake_thing( db, "participants", id=fake_int_id(), username=username, username_lower=username.lower(), statement=fake_sentence(), ctime=faker.date_time_this_year(), is_admin=is_admin, balance=fake_balance(), anonymous_giving=(random.randrange(5) == 0), anonymous_receiving=(random.randrange(5) == 0), goal=fake_balance(), balanced_customer_href=faker.uri(), last_ach_result='', is_suspicious=False, last_bill_result='' # Needed to not be suspicious , claimed_time=faker.date_time_this_year(), number=number) #Call participant constructor to perform other DB initialization return Participant.from_username(username)
def fake_participant(db, number="singular", is_admin=False, anonymous=False): """Create a fake User. """ username = faker.firstName() + fake_text_id(3) d = _fake_thing( db, "participants", id=fake_int_id(), username=username, username_lower=username.lower(), statement=fake_sentence(), ctime=faker.dateTimeThisYear(), is_admin=is_admin, balance=fake_balance(), anonymous=anonymous, goal=fake_balance(), balanced_account_uri=faker.uri(), last_ach_result='', is_suspicious=False, last_bill_result='' # Needed to not be suspicious , claimed_time=faker.dateTimeThisYear(), number=number) #Call participant constructor to perform other DB initialization return Participant.from_username(username)
def make_participant(self, username, number='singular', **kw): participant = Participant(username=username, username_lower=username.lower(), number=number, **kw) self.session.add(participant) self.session.commit() return participant
def make_participant(self, username, **kw): participant = Participant(username=username, username_lower=username.lower(), type='individual', **kw) self.session.add(participant) self.session.commit() return participant
def test_delete_elsewhere_nonsignin(self): g = self.make_elsewhere('bountysource', 1, 'alice') alice = Participant.from_username('alice') alice.take_over(g) accounts = alice.get_accounts_elsewhere() assert accounts['twitter'] and accounts['bountysource'] alice.delete_elsewhere('bountysource', 1) accounts = alice.get_accounts_elsewhere() assert accounts['twitter'] and accounts.get('bountysource') is None
def test_bob_ends_up_tipping_alice_two_dollars(self): carl_twitter = StubAccount('twitter', '3') Participant.from_username('bob').set_tip_to('alice', '1.00') Participant.from_username('carl').set_tip_to('alice', '1.00') Participant.from_username('bob').take_over(carl_twitter, have_confirmation=True) expected = Decimal('2.00') actual = Participant.from_username('bob').get_tip_to('alice') assert actual == expected
def test_session_cookie_set_in_auth_response(self): self.make_participant('alice') # Make a normal authenticated request. normal = self.client.GET("/", auth_as='alice', HTTP_X_FORWARDED_PROTO='https', HTTP_HOST='www.gittip.com') alice = Participant.from_username('alice') assert normal.headers.cookie['session'].value == alice.session_token
def test_associate_bank_account_invalid(self, find): ex = balanced.exc.HTTPError('errrrrror') find.return_value.add_bank_account.side_effect = ex find.return_value.uri = self.balanced_account_uri billing.associate(u"bank account", 'alice', self.balanced_account_uri, self.balanced_destination_uri) # participant in db should be updated alice = Participant.from_username('alice') assert alice.last_ach_result == 'errrrrror'
def test_payday_moves_money(self, charge_on_balanced): charge_on_balanced.return_value = (D('10.00'), D('0.68'), "") day_ago = utcnow() - timedelta(days=1) bob = self.make_participant('bob', claimed_time=day_ago, last_bill_result='', is_suspicious=False) carl = self.make_participant( 'carl', claimed_time=day_ago, balanced_customer_href=self.balanced_customer_href, last_bill_result='', is_suspicious=False) carl.set_tip_to(bob, '6.00') # under $10! self.payday.run() bob = Participant.from_username('bob') carl = Participant.from_username('carl') assert bob.balance == D('6.00') assert carl.balance == D('3.32')
def test_pachinko_pachinkos(self): a_team = self.make_participant('a_team', claimed_time='now', number='plural', balance=20, \ pending=0) a_team.add_member( self.make_participant('alice', claimed_time='now', balance=0, pending=0)) a_team.add_member( self.make_participant('bob', claimed_time='now', balance=0, pending=0)) ts_start = self.payday.start() participants = self.payday.genparticipants(ts_start, LOOP_PACHINKO) self.payday.pachinko(ts_start, participants) assert Participant.from_username('alice').pending == D('0.01') assert Participant.from_username('bob').pending == D('0.01')
def test_delete_elsewhere(self): g = self.make_elsewhere('github', 1, 'alice') alice = Participant.from_username('alice') alice.take_over(g) # test preconditions accounts = alice.get_accounts_elsewhere() assert accounts['twitter'] and accounts['github'] # do the thing alice.delete_elsewhere('twitter', 1) # unit test accounts = alice.get_accounts_elsewhere() assert accounts.get('twitter') is None and accounts['github']
def test_payday_moves_money_with_balanced(self): day_ago = utcnow() - timedelta(days=1) paying_customer = balanced.Customer().save() balanced.Card.fetch(self.card_href)\ .associate_to_customer(paying_customer) balanced.BankAccount.fetch(self.bank_account_href)\ .associate_to_customer(self.balanced_customer_href) bob = self.make_participant( 'bob', claimed_time=day_ago, balanced_customer_href=self.balanced_customer_href, last_bill_result='', is_suspicious=False) carl = self.make_participant( 'carl', claimed_time=day_ago, balanced_customer_href=paying_customer.href, last_bill_result='', is_suspicious=False) carl.set_tip_to(bob, '15.00') self.payday.run() bob = Participant.from_username('bob') carl = Participant.from_username('carl') assert bob.balance == D('0.00') assert carl.balance == D('0.00') bob_customer = balanced.Customer.fetch(bob.balanced_customer_href) carl_customer = balanced.Customer.fetch(carl.balanced_customer_href) bob_credits = bob_customer.credits.all() assert len(bob_credits) == 1 assert bob_credits[0].amount == 1500 assert bob_credits[0].description == 'bob' carl_debits = carl_customer.debits.all() assert len(carl_debits) == 1 assert carl_debits[0].amount == 1576 # base amount + fee assert carl_debits[0].description == 'carl'
def test_associate_bank_account_invalid(self): billing.associate( self.db, u"bank account", 'alice', self.balanced_customer_href, '/bank_accounts/BA123123123123123123' # invalid href ) # participant in db should be updated alice = Participant.from_username('alice') assert alice.last_ach_result == '404 Client Error: NOT FOUND'
def get_participant_via_access_token(access_token): """From a Gittip access token, attempt to find an external account :param access_token: access token generated by Gittip on account link redirect :returns: the participant, if found """ if access_token_valid(access_token): parts = access_token.split('.') participant_id = parts[0] return Participant.from_id(participant_id)