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_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_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_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_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
Example #6
0
    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
Example #7
0
 def test_taking_is_zero_for_team(self):
     team = self.make_team()
     alice = self.make_participant('alice', claimed_time='now')
     team.add_member(alice)
     team = Participant.from_id(team.id)
     assert team.taking == 0
     assert team.receiving == 100
    def test_capture_card_hold_amount_under_minimum(self):
        hold, error = create_card_hold(self.db, self.janet, D('20.00'))
        assert error == ''  # sanity check

        capture_card_hold(self.db, self.janet, D('0.01'), hold)
        janet = Participant.from_id(self.janet.id)
        assert self.janet.balance == janet.balance == D('9.41')
        assert self.janet.last_bill_result == janet.last_bill_result == ''
 def test_create_card_hold_no_card(self):
     customer_href = self.make_balanced_customer()
     bob = self.make_participant('bob', balanced_customer_href=customer_href,
                                 is_suspicious=False)
     hold, error = create_card_hold(self.db, bob, D('10.00'))
     bob2 = Participant.from_id(bob.id)
     assert error == 'NoResultFound()'
     assert bob.last_bill_result == bob2.last_bill_result == None
Example #10
0
    def test_but_team_can_take_from_other_team(self):
        a_team = self.make_team('A Team', claimed_time='now')
        b_team = self.make_team('B Team', claimed_time='now')
        a_team.add_member(b_team)
        a_team.set_take_for(b_team, D('1.00'), b_team)

        b_team = Participant.from_id(b_team.id)
        assert b_team.taking == 1
        assert b_team.receiving == 101
 def test_take_over_during_payin(self):
     alice = self.make_participant('alice', claimed_time='now', balance=50)
     bob = self.make_participant('bob', claimed_time='now', elsewhere='twitter')
     alice.set_tip_to(bob, 18)
     payday = Payday.start()
     with self.db.get_cursor() as cursor:
         payday.prepare(cursor, payday.ts_start)
         bruce = self.make_participant('bruce', claimed_time='now')
         bruce.take_over(('twitter', str(bob.id)), have_confirmation=True)
         payday.transfer_tips(cursor)
         bruce.delete_elsewhere('twitter', str(bob.id))
         billy = self.make_participant('billy', claimed_time='now')
         billy.take_over(('github', str(bruce.id)), have_confirmation=True)
         payday.update_balances(cursor)
     payday.take_over_balances()
     assert Participant.from_id(bob.id).balance == 0
     assert Participant.from_id(bruce.id).balance == 0
     assert Participant.from_id(billy.id).balance == 18
    def test_receiving_is_zero_for_patrons(self):
        alice = self.make_participant('alice', claimed_time='now', last_bill_result='')
        bob = self.make_participant('bob', claimed_time='now')
        alice.set_tip_to(bob, '3.00')

        bob.update_goal(Decimal('-1'))
        assert bob.receiving == 0
        alice = Participant.from_id(alice.id)
        assert alice.giving == 0
 def test_ach_credit_withhold(self):
     bob = self.make_participant('bob', last_ach_result="failure", balance=20,
                                 balanced_customer_href=self.homer_href,
                                 is_suspicious=False)
     withhold = D('1.00')
     error = ach_credit(self.db, bob, withhold)
     assert error == ''
     bob2 = Participant.from_id(bob.id)
     assert bob.balance == bob2.balance == 1
    def test_capture_card_hold_full_amount(self):
        hold, error = create_card_hold(self.db, self.janet, D('20.00'))
        assert error == ''  # sanity check
        assert hold.meta['state'] == 'new'

        capture_card_hold(self.db, self.janet, D('20.00'), hold)
        janet = Participant.from_id(self.janet.id)
        assert self.janet.balance == janet.balance == D('20.00')
        assert self.janet.last_bill_result == janet.last_bill_result == ''
        assert hold.meta['state'] == 'captured'
    def test_ach_credit_failure(self, Customer):
        Customer.side_effect = Foobar
        bob = self.make_participant('bob', last_ach_result="failure", balance=20,
                                    balanced_customer_href=self.homer_href,
                                    is_suspicious=False)

        error = ach_credit(self.db, bob, D('1.00'))
        bob2 = Participant.from_id(bob.id)
        assert bob.last_ach_result == bob2.last_ach_result == error == "Foobar()"
        assert bob.balance == bob2.balance == 20
    def test_create_card_hold_success(self):
        hold, error = create_card_hold(self.db, self.janet, D('1.00'))
        janet = Participant.from_id(self.janet.id)
        assert isinstance(hold, balanced.CardHold)
        assert hold.failure_reason is None
        assert hold.amount == 1000
        assert hold.meta['state'] == 'new'
        assert error == ''
        assert self.janet.balance == janet.balance == 0

        # Clean up
        cancel_card_hold(hold)
    def test_capture_card_hold_too_high_amount(self):
        hold, error = create_card_hold(self.db, self.janet, D('20.00'))
        assert error == ''  # sanity check

        with self.assertRaises(balanced.exc.HTTPError):
            capture_card_hold(self.db, self.janet, D('20.01'), hold)

        janet = Participant.from_id(self.janet.id)
        assert self.janet.balance == janet.balance == 0

        # Clean up
        cancel_card_hold(hold)
Example #18
0
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)
Example #19
0
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)
Example #20
0
    def setUp(self):
        Harness.setUp(self)
        now = utcnow()
        hour_ago = now - datetime.timedelta(hours=1)
        for i, username in enumerate(['alice', 'bob', 'carl']):
            p = self.make_participant( username
                                     , claimed_time=hour_ago
                                     , last_bill_result=''
                                     , balance=Decimal(i)
                                      )
            setattr(self, username, p)

        deadbeef = self.make_participant('deadbeef', balance=Decimal('18.03'), elsewhere='twitter')
        self.expected_new_balance = self.bob.balance + deadbeef.balance
        deadbeef_twitter = AccountElsewhere.from_user_name('twitter', 'deadbeef')

        self.carl.set_tip_to(self.bob, '1.00')
        self.alice.set_tip_to(deadbeef, '1.00')
        self.bob.take_over(deadbeef_twitter, have_confirmation=True)
        self.deadbeef_archived = Participant.from_id(deadbeef.id)
Example #21
0
    def test_associate_valid_card(self):
        self.david.set_tip_to(self.homer, 10)
        card = balanced.Card(
            number='4242424242424242',
            expiration_year=2020,
            expiration_month=12
        ).save()
        billing.associate( self.db
                         , 'credit card'
                         , 'david'
                         , self.david_href
                         , unicode(card.href)
                          )

        customer = balanced.Customer.fetch(self.david_href)
        cards = customer.cards.all()
        assert len(cards) == 1
        assert cards[0].href == card.href

        homer = Participant.from_id(self.homer.id)
        assert homer.receiving == 10