<commit_before>from __future__ import absolute_import, division, print_function, unicode_literals

import balanced
import mock

from gratipay.testing.billing import BillingHarness
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.participant import Participant


class TestRoutes(BillingHarness):

    def hit(self, username, action, network, address, expected=200):
        r =  self.client.POST('/%s/routes/%s.json' % (username, action),
                              data=dict(network=network, address=address),
                              auth_as=username, raise_immediately=False)
        assert r.code == expected
        return r

    def test_associate_and_delete_valid_card(self):
        card = balanced.Card(
            number='4242424242424242',
            expiration_year=2020,
            expiration_month=12
        ).save()
        customer = self.david.get_balanced_account()
        self.hit('david', 'associate', 'balanced-cc', card.href)

        cards = customer.cards.all()
        assert len(cards) == 1
        assert cards[0].href == card.href

        assert self.david.get_credit_card_error() == ''

        self.hit('david', 'delete', 'balanced-cc', card.href)

        david = Participant.from_username('david')
        assert david.get_credit_card_error() == 'invalidated'
        assert david.balanced_customer_href<commit_msg>Add test for associate a new balanced card (should fail)<commit_after>from __future__ import absolute_import, division, print_function, unicode_literals

import balanced
import mock
import pytest

from gratipay.testing.billing import BillingHarness
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.participant import Participant


class TestRoutes(BillingHarness):

    def hit(self, username, action, network, address, expected=200):
        r =  self.client.POST('/%s/routes/%s.json' % (username, action),
                              data=dict(network=network, address=address),
                              auth_as=username, raise_immediately=False)
        assert r.code == expected
        return r

    # Remove this once we've dumped 'balanced-cc' altogether
    def test_associate_balanced_card_should_fail(self):
        card = balanced.Card(
            number='4242424242424242',
            expiration_year=2020,
            expiration_month=12
        ).save()
        customer = self.david.get_balanced_account()
        self.hit('david', 'associate', 'balanced-cc', card.href, expected=400)

        cards = customer.cards.all()
        assert len(cards) == 0

    def test_associate_and_delete_valid_card(self):
        card = balanced.Card(
            number='4242424242424242',
            expiration_year=2020,
            expiration_month=12
        ).save()
        customer = self.david.get_balanced_account()
        self.hit('david', 'associate', 'balanced-cc', card.href)

        cards = customer.cards.all()
        assert len(cards) == 1
        assert cards[0].href == card.href

        assert self.david.get_credit_card_error() == ''

        self.hit('david', 'delete', 'balanced-cc', card.href)

        david = Participant.from_username('david')
        assert david.get_credit_card_error() == 'invalidated'
        assert david.balanced_customer_href
{'fraction_non_alphanumeric': 0.10217654171704958, 'fraction_numerical': 0.02418379685610641, 'mean_word_length': 2.829861111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6683854', 'n_tokens_mistral': 1019, 'n_tokens_neox': 930, 'n_words': 197}