<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import balanced
import requests
import unittest

from fixtures import cards, merchants


class AcceptanceUseCases(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        balanced.config.root_uri = 'http://127.0.0.1:5000/'
        if not balanced.config.api_key_secret:
            api_key = balanced.APIKey().save()
            balanced.configure(api_key.secret)
            cls.api_key = api_key
            cls.merchant = api_key.merchant
            balanced.Marketplace().save()

    def setUp(self):
        self.valid_international_address = {
            'street_address': '24 Grosvenor Square London, Mayfair, London',
            'country_code': 'GBR',
        }
        self.person = {
            'name': '<NAME>',
        }
        self.valid_us_address = {
            'street_address': '1 Large Prime St',
            'city': 'Gaussian',
            'state': 'CA',
            'postal_code': '99971',
        }
        self.us_card_payload = {
            'name': '<NAME>',
            'card_number': cards.TEST_CARDS['visa'][0],
            'expiration_month': 12,
            'expiration_year': 2013,
        }
        self.us_card_payload.update(self.valid_us_address)

        self.bank_account_payload = {
            'name': '<NAME>',
            'account_number': '28304871049',
            'bank_code': '121042882',
         }

    def _find_buyer_account(self):
        mp = balanced.Marketplace.query.one()
        accounts = list(mp.accounts)
        filtered_accounts = [account for account in accounts if account.roles == ['buyer']]
        if not filtered_accounts:
            card_payload = dict(self.us_card_payload)
            card = balanced.Card(**card_payload).save()
            card_uri = card.uri
            buyer = mp.create_buyer(
                email_address='<EMAIL>',
                card_uri=card_uri,
                meta={'foo': 'bar'},
            )
        else:
            buyer = filtered_accounts[0]
        return buyer

    def test_merchant_expectations(self):
        mps = balanced.Marketplace.query.all()
        self.assertEqual(len(mps), 1)

    @unittest.skip('need balanced fix')
    def test_valid_non_us_address_no_postal_code(self):
        card_number = cards.TEST_CARDS['visa'][0]
        card_payload = {
            'card_number': card_number,
            'expiration_month': 12,
            'expiration_year': 2013,
        }
        card_payload.update(self.valid_international_address)
        card_payload.update(self.person)
        balanced.Card(**card_payload).save()

    def test_valid_us_address(self):
        buyer = self._find_buyer_account()
        self.assertTrue(buyer.id.startswith('AC'), buyer.id)
        self.assertEqual(buyer.roles, ['buyer'])
        self.assertDictEqual(buyer.meta, {'foo': 'bar'})

    def test_fractional_debit(self):
        buyer_account = self._find_buyer_account()
        bad_amount = (3.14, 100.32)
        for amount in bad_amount:
            with self.assertRaises(requests.HTTPError) as exc:
                buyer_account.debit(
                    amount=amount,
                    appears_on_statement_as='pi',
                )
            the_exception = exc.exception
            self.assertEqual(the_exception.status_code, 400)

    def test_create_simple_credit(self):
        mp = balanced.Marketplace.query.one()
        payload = dict(self.bank_account_payload)
        bank_account = balanced.BankAccount(**payload).save()
        merchant = mp.create_merchant(
            '<EMAIL>',
            merchant=merchants.BUSINESS_MERCHANT,
            bank_account_uri=bank_account.uri,
        )
        self.assertItemsEqual(merchant.roles, ['buyer', 'merchant'])

        amount = 10000
        buyer_account = self._find_buyer_account()
        buyer_account.debit(amount=amount)
        merchant.credit(amount)

    def test_credit_lower_than_escrow(self):
        mp = balanced.Marketplace.query.one()
        escrow_balance = mp.in_escrow 
        credit_amount = escrow_balance + 10000
        merchants = mp.accounts
        merchant = merchants[0]
        with self.assertRaises(requests.HTTPError) as exc:
            merchant.credit(amount=credit_amount)
        the_exception = exc.exception
        self.assertEqual(the_exception.status_code, 409)

    def test_valid_international_address(self):
        for card_payload in cards.generate_international_card_payloads():
            card = balanced.Card(**card_payload).save()
            self.assertEqual(card.street_address,
                card_payload['street_address'])

    def test_transactions_invalid_funding_sources(self):
        mp = balanced.Marketplace.query.one()
        card_payload = dict(self.us_card_payload)<commit_msg>tests for associating bad cards<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import balanced
import requests
import unittest

from fixtures import cards, merchants


class AcceptanceUseCases(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        balanced.config.root_uri = 'http://127.0.0.1:5000/'
        if not balanced.config.api_key_secret:
            api_key = balanced.APIKey().save()
            balanced.configure(api_key.secret)
            cls.api_key = api_key
            cls.merchant = api_key.merchant
            balanced.Marketplace().save()

    def setUp(self):
        self.valid_international_address = {
            'street_address': '24 Grosvenor Square London, Mayfair, London',
            'country_code': 'GBR',
        }
        self.person = {
            'name': '<NAME>',
        }
        self.valid_us_address = {
            'street_address': '1 Large Prime St',
            'city': 'Gaussian',
            'state': 'CA',
            'postal_code': '99971',
        }
        self.us_card_payload = {
            'name': '<NAME>',
            'card_number': cards.TEST_CARDS['visa'][0],
            'expiration_month': 12,
            'expiration_year': 2013,
        }
        self.us_card_payload.update(self.valid_us_address)

        self.bank_account_payload = {
            'name': '<NAME>',
            'account_number': '28304871049',
            'bank_code': '121042882',
         }

    def _find_buyer_account(self):
        mp = balanced.Marketplace.query.one()
        accounts = list(mp.accounts)
        filtered_accounts = [account for account in accounts if account.roles == ['buyer']]
        if not filtered_accounts:
            card_payload = dict(self.us_card_payload)
            card = balanced.Card(**card_payload).save()
            card_uri = card.uri
            buyer = mp.create_buyer(
                email_address='<EMAIL>',
                card_uri=card_uri,
                meta={'foo': 'bar'},
            )
        else:
            buyer = filtered_accounts[0]
        return buyer

    def test_merchant_expectations(self):
        mps = balanced.Marketplace.query.all()
        self.assertEqual(len(mps), 1)

    @unittest.skip('need balanced fix')
    def test_valid_non_us_address_no_postal_code(self):
        card_number = cards.TEST_CARDS['visa'][0]
        card_payload = {
            'card_number': card_number,
            'expiration_month': 12,
            'expiration_year': 2013,
        }
        card_payload.update(self.valid_international_address)
        card_payload.update(self.person)
        balanced.Card(**card_payload).save()

    def test_valid_us_address(self):
        buyer = self._find_buyer_account()
        self.assertTrue(buyer.id.startswith('AC'), buyer.id)
        self.assertEqual(buyer.roles, ['buyer'])
        self.assertDictEqual(buyer.meta, {'foo': 'bar'})

    def test_fractional_debit(self):
        buyer_account = self._find_buyer_account()
        bad_amount = (3.14, 100.32)
        for amount in bad_amount:
            with self.assertRaises(requests.HTTPError) as exc:
                buyer_account.debit(
                    amount=amount,
                    appears_on_statement_as='pi',
                )
            the_exception = exc.exception
            self.assertEqual(the_exception.status_code, 400)

    def test_create_simple_credit(self):
        mp = balanced.Marketplace.query.one()
        payload = dict(self.bank_account_payload)
        bank_account = balanced.BankAccount(**payload).save()
        merchant = mp.create_merchant(
            '<EMAIL>',
            merchant=merchants.BUSINESS_MERCHANT,
            bank_account_uri=bank_account.uri,
        )
        self.assertItemsEqual(merchant.roles, ['buyer', 'merchant'])

        amount = 10000
        buyer_account = self._find_buyer_account()
        buyer_account.debit(amount=amount)
        merchant.credit(amount)

    def test_credit_lower_than_escrow(self):
        mp = balanced.Marketplace.query.one()
        escrow_balance = mp.in_escrow
        credit_amount = escrow_balance + 10000
        merchants = mp.accounts
        merchant = merchants[0]
        with self.assertRaises(requests.HTTPError) as exc:
            merchant.credit(amount=credit_amount)
        the_exception = exc.exception
        self.assertEqual(the_exception.status_code, 409)

    def test_valid_international_address(self):
        for card_payload in cards.generate_international_card_payloads():
            card = balanced.Card(**card_payload).save()
            self.assertEqual(card.street_address,
                card_payload['street_address'])

    def test_associate_bad_cards(self):
        mp = balanced.Marketplace.query.one()
        card_payload = dict(self.us_card_payload)
        card = balanced.Card(**card_payload).save()
        card_uri = card.uri
        buyer = mp.create_buyer(
            email_address='<EMAIL>',
            card_uri=card_uri,
            meta={'foo': 'bar'},
        )
        # Invalid card
        card_payload = dict(self.us_card_payload)
        card_payload['is_valid'] = False
        card = balanced.Card(**card_payload).save()
        card_uri = card.uri
        with self.assertRaises(requests.HTTPError) as exc:
            buyer.add_card(card_uri=card_uri)
        the_exception = exc.exception
        self.assertEqual(the_exception.status_code, 409)
        self.assertEqual(the_exception.category_code,
            'funding-source-not-valid')

        # Already-associated card
        card_payload = dict(self.us_card_payload)
        card = balanced.Card(**card_payload).save()
        card_uri = card.uri
        mp.create_buyer(
            email_address='<EMAIL>',
            card_uri=card_uri,
            meta={'foo': 'bar'},
        )
        with self.assertRaises(requests.HTTPError) as exc:
            buyer.add_card(card_uri=card_uri)
        the_exception = exc.exception
        self.assertEqual(the_exception.status_code, 409)
        self.assertEqual(the_exception.category_code,
            'card-already-funding-src')

        # Completely fake card uri
        with self.assertRaises(requests.HTTPError) as exc:
            buyer.add_card(card_uri='/completely/fake')
        the_exception = exc.exception
        self.assertEqual(the_exception.status_code, 400)

    def test_transactions_invalid_funding_sources(self):
        mp = balanced.Marketplace.query.one()
        card_payload = dict(self.us_card_payload)
{'fraction_non_alphanumeric': 0.10590201424927434, 'fraction_numerical': 0.014864983727680536, 'mean_word_length': 2.2569464336866227, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2942133', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3330, 'n_words': 539}