def scenario_1(test_func):
    """Scenarios! :D

    We've got three live Gittip accounts: foo, bar, and baz.

    Each has a GitHub account connected.

    Foo has a tip pledged to deadbeef, which is a stub participant with a
    Twitter account connected.

    Bar claims deadbeef's Twitter account.

    What happens?!

    """
    tips = [ ("foo", "deadbeef", 1, True, False, False)
           , ("bar", None, "na", True, False, "na", "Twitter", "2345")
           , ("baz", "bar", 1)
            ]
    with tip_graph(*tips) as context:
        platform, user_id = context.resolve_elsewhere('deadbeef')[0]
        Participant('bar').take_over(StubAccount(platform, user_id))

        p = context.dump()['participants'].keys()
        context.deadbeef_archived_as = [x for x in p if len(x) > 3][0]

        test_func(context)
def test_get_chart_of_receiving_handles_a_non_standard_amount():
    tip = ("foo", "bar", "5.37", True)
    expected = ( [[-1, 1, Decimal('5.37'), 1.0, Decimal('1')]]
               , 1.0, Decimal('5.37')
                )
    with tip_graph(tip):
        actual = check_chart_of_receiving(u'bar')
        assert actual == expected, actual
def test_get_chart_of_receiving_handles_a_tip():
    tip = ("foo", "bar", "3.00", True)
    expected = ( [[Decimal('3.00'), 1, Decimal('3.00'), 1.0, Decimal('1')]]
               , 1.0, Decimal('3.00')
                )
    with tip_graph(tip):
        actual = check_chart_of_receiving(u'bar')
        assert actual == expected, actual
def test_get_chart_of_receiving_ignores_missing_cc():
    tips = [ ("foo", "bar", "1.00", True)
           , ("baz", "bar", "3.00", None)
            ]
    expected = ( [[Decimal('1.00'), 1L, Decimal('1.00'), 1, Decimal('1')]]
               , 1.0, Decimal('1.00')
                )
    with tip_graph(*tips):
        actual = check_chart_of_receiving(u'bar')
        assert actual == expected, actual
def test_bob_ends_up_tipping_alice_two_dollars():
    tips = [ ('bob', 'alice', 1)
           , ('carl', 'alice', 1)
            ]
    with tip_graph(*tips) as context:
        platform, user_id = context.resolve_elsewhere('carl')[0]
        Participant('bob').take_over(StubAccount(platform, user_id), True)
        expected = Decimal('2.00')
        actual = context.diff()['tips']['inserts'][0]['amount']
        assert actual == expected, actual
def test_connecting_unknown_account_fails():
    tips = [ ('alice', 'bob', 1)
           , ('alice', 'carl', 1)
            ]
    with tip_graph(*tips) as context:
        assert_raises( AssertionError
                     , Participant('bob').take_over
                     , StubAccount('github', 'jim')
                      )
        actual = context.diff()
        assert actual == {}, actual
def test_get_chart_of_receiving_handles_multiple_tips():
    tips = [ ("foo", "bar", "1.00", True)
           , ("baz", "bar", "3.00", True)
            ]
    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')
                )
    with tip_graph(*tips):
        actual = check_chart_of_receiving(u'bar')
        assert actual == expected, actual
def test_taking_over_yourself_sets_all_to_zero():
    with tip_graph(("foo", "bar", 1)) as context:
        platform, user_id = context.resolve_elsewhere("bar")[0]
        Participant('foo').take_over(StubAccount(platform, user_id), have_confirmation=True)

        expected = { 'amount': Decimal('0.00')
                   , 'tipper': 'foo'
                   #, 'tippee': 'bar'  becomes a random id
                    }
        actual = context.diff()['tips']['inserts'][0]
        del actual['ctime'], actual['mtime'], actual['id'], actual['tippee']
        assert actual == expected, actual
def test_ctime_comes_from_the_older_tip():
    tips = [ ('alice', 'bob', 1)
           , ('alice', 'carl', 1)
            ]
    with tip_graph(*tips) as context:
        platform, user_id = context.resolve_elsewhere('carl')[0]
        Participant('bob').take_over(StubAccount(platform, user_id), True)

        tips = sorted(context.dump()['tips'].items())
        first, second = tips[0][1], tips[1][1]

        # sanity checks (these don't count :)
        assert len(tips) == 4
        assert first['ctime'] < second['ctime']
        assert first['tipper'], first['tippee'] == ('alice', 'bob')
        assert second['tipper'], second['tippee'] == ('alice', 'carl')

        expected = first['ctime']
        actual = context.diff()['tips']['inserts'][0]['ctime']
        assert actual == expected, actual
def test_profile():
    with tip_graph(("cheese", "puffs", 0)):
        expected = "I&rsquo;m grateful for tips"
        actual = serve_request('/cheese/').body
        assert expected in actual, actual
def test_github_resolve_resolves():
    with tip_graph(('alice', 'bob', 1)):
        expected = 'alice'
        actual = github.resolve(u'alice')
        assert actual == expected, actual
def test_twitter_resolve_resolves():
    with tip_graph(('alice', 'bob', 1, True, False, False, "twitter", "2345")):
        expected = 'alice'
        actual = twitter.resolve(u'alice')
        assert actual == expected, actual
def test_devnet_resolve_resolves():
    with tip_graph(('alice', 'bob', 1, True, False, False, "devnet", "7890")):
        expected = 'alice'
        actual = devnet.resolve(u'alice')
        assert actual == expected, actual
def test_widget():
    with tip_graph(("cheese", "puffs", 0)):
        expected = "javascript: window.open"
        actual = serve_request('/cheese/widget.html').body
        assert expected in actual, actual
def test_get_chart_of_receiving_handles_no_tips():
    expected = ([], 0.0, Decimal('0.00'))
    with tip_graph():
        actual = check_chart_of_receiving(u'foo')
        assert actual == expected, actual
def test_public_json_something():
    with tip_graph(('alice', 'bob', 1)):
        expected = '{"receiving": "1.00"}'
        actual = serve_request('/bob/public.json').body
        assert expected in actual, actual
def test_cant_take_over_claimed_participant_without_confirmation():
    with tip_graph(("foo", "bar", 1)) as context:
        platform, user_id = context.resolve_elsewhere("bar")[0]
        func = Participant('foo').take_over
        assert_raises(NeedConfirmation, func, StubAccount(platform, user_id))