<s> from __future__ import unicode_literals <EOL> import datetime <EOL> import json <EOL> import urlparse <EOL> import balanced <EOL> from billy . tests . integration . helper import IntegrationTestCase <EOL> class TestBasicScenarios ( IntegrationTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestBasicScenarios , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestBasicScenarios , self ) . tearDown ( ) <EOL> def create_company ( self , processor_key = None ) : <EOL> if processor_key is None : <EOL> processor_key = self . processor_key <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( processor_key = self . processor_key ) , <EOL> ) <EOL> company = res . json <EOL> return company <EOL> def test_simple_subscription_and_cancel ( self ) : <EOL> balanced . configure ( self . processor_key ) <EOL> card = balanced . Card ( <EOL> name = '<STR_LIT>' , <EOL> number = '<STR_LIT>' , <EOL> expiration_month = '<STR_LIT>' , <EOL> expiration_year = '<STR_LIT>' , <EOL> security_code = '<STR_LIT>' , <EOL> ) . save ( ) <EOL> company = self . create_company ( ) <EOL> api_key = str ( company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> customer = res . json <EOL> self . assertEqual ( customer [ '<STR_LIT>' ] , company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> plan_type = '<STR_LIT>' , <EOL> amount = <NUM_LIT> , <EOL> frequency = '<STR_LIT>' , <EOL> ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> plan = res . json <EOL> self . assertEqual ( plan [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( plan [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( plan [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( plan [ '<STR_LIT>' ] , company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = customer [ '<STR_LIT>' ] , <EOL> plan_guid = plan [ '<STR_LIT>' ] , <EOL> funding_instrument_uri = card . href , <EOL> appears_on_statement_as = '<STR_LIT>' , <EOL> ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> subscription = res . json <EOL> self . assertEqual ( subscription [ '<STR_LIT>' ] , customer [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( subscription [ '<STR_LIT>' ] , plan [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( subscription [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' . format ( subscription [ '<STR_LIT>' ] ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> invoices = res . json <EOL> self . assertEqual ( len ( invoices [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> invoice = res . json [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( invoice [ '<STR_LIT>' ] , subscription [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( invoice [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> transactions = res . json <EOL> self . assertEqual ( len ( transactions [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> transaction = res . json [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , invoice [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> debit = balanced . Debit . fetch ( transaction [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( debit . meta [ '<STR_LIT>' ] , transaction [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( debit . amount , <NUM_LIT> ) <EOL> self . assertEqual ( debit . status , '<STR_LIT>' ) <EOL> self . assertEqual ( debit . appears_on_statement_as , '<STR_LIT>' ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' . format ( subscription [ '<STR_LIT>' ] ) , <EOL> dict ( <EOL> refund_amount = <NUM_LIT> , <EOL> ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> subscription = res . json <EOL> self . assertEqual ( subscription [ '<STR_LIT>' ] , True ) <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( invoice [ '<STR_LIT>' ] ) , <EOL> dict ( <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> transactions = res . json <EOL> self . assertEqual ( len ( transactions [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> transaction = res . json [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , invoice [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> refund = balanced . Refund . fetch ( transaction [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( refund . meta [ '<STR_LIT>' ] , <EOL> transaction [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( refund . amount , <NUM_LIT> ) <EOL> self . assertEqual ( refund . status , '<STR_LIT>' ) <EOL> res = self . testapp . delete ( <EOL> '<STR_LIT>' . format ( plan [ '<STR_LIT>' ] ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> plan = res . json <EOL> self . assertEqual ( plan [ '<STR_LIT>' ] , True ) <EOL> res = self . testapp . delete ( <EOL> '<STR_LIT>' . format ( customer [ '<STR_LIT>' ] ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> customer = res . json <EOL> self . assertEqual ( customer [ '<STR_LIT>' ] , True ) <EOL> def test_invoicing ( self ) : <EOL> balanced . configure ( self . processor_key ) <EOL> card = balanced . Card ( <EOL> name = '<STR_LIT>' , <EOL> number = '<STR_LIT>' , <EOL> expiration_month = '<STR_LIT>' , <EOL> expiration_year = '<STR_LIT>' , <EOL> security_code = '<STR_LIT>' , <EOL> ) . save ( ) <EOL> company = self . create_company ( ) <EOL> api_key = str ( company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> customer = res . json <EOL> self . assertEqual ( customer [ '<STR_LIT>' ] , company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = customer [ '<STR_LIT>' ] , <EOL> amount = <NUM_LIT> , <EOL> title = '<STR_LIT>' , <EOL> item_name1 = '<STR_LIT>' , <EOL> item_amount1 = <NUM_LIT:200> , <EOL> adjustment_amount1 = '<STR_LIT>' , <EOL> adjustment_reason1 = '<STR_LIT>' , <EOL> funding_instrument_uri = card . href , <EOL> appears_on_statement_as = '<STR_LIT>' , <EOL> ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> invoice = res . json <EOL> self . assertEqual ( invoice [ '<STR_LIT:title>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( invoice [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( invoice [ '<STR_LIT>' ] , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertEqual ( invoice [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( invoice [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> transactions = res . json <EOL> self . assertEqual ( len ( transactions [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> transaction = res . json [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , invoice [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> debit = balanced . Debit . fetch ( transaction [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( debit . meta [ '<STR_LIT>' ] , transaction [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( debit . amount , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertEqual ( debit . status , '<STR_LIT>' ) <EOL> self . assertEqual ( debit . appears_on_statement_as , '<STR_LIT>' ) <EOL> def test_invalid_funding_instrument ( self ) : <EOL> balanced . configure ( self . processor_key ) <EOL> card = balanced . Card ( <EOL> name = '<STR_LIT>' , <EOL> number = '<STR_LIT>' , <EOL> expiration_month = '<STR_LIT>' , <EOL> expiration_year = '<STR_LIT>' , <EOL> security_code = '<STR_LIT>' , <EOL> ) . save ( ) <EOL> card_uri = card . href + '<STR_LIT>' <EOL> company = self . create_company ( ) <EOL> api_key = str ( company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> customer = res . json <EOL> self . assertEqual ( customer [ '<STR_LIT>' ] , company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = customer [ '<STR_LIT>' ] , <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = card_uri , <EOL> ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT> <EOL> ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def _to_json ( self , input_obj ) : <EOL> def dt_handler ( obj ) : <EOL> if isinstance ( obj , ( datetime . datetime , datetime . date ) ) : <EOL> return obj . isoformat ( ) <EOL> if isinstance ( obj , balanced . Resource ) : <EOL> return obj . __dict__ <EOL> return json . dumps ( input_obj , default = dt_handler ) <EOL> def test_register_callback ( self ) : <EOL> balanced . configure ( self . processor_key ) <EOL> company = self . create_company ( ) <EOL> guid = company [ '<STR_LIT>' ] <EOL> callback_key = str ( company [ '<STR_LIT>' ] ) <EOL> callbacks = balanced . Callback . query . all ( ) <EOL> callback_urls = set ( ) <EOL> for callback in callbacks : <EOL> callback_urls . add ( callback . url ) <EOL> expected_url = urlparse . urljoin ( <EOL> self . target_url , <EOL> '<STR_LIT>' . format ( guid , callback_key ) <EOL> ) <EOL> self . assertIn ( expected_url , callback_urls ) <EOL> def test_callback ( self ) : <EOL> balanced . configure ( self . processor_key ) <EOL> card = balanced . Card ( <EOL> name = '<STR_LIT>' , <EOL> number = '<STR_LIT>' , <EOL> expiration_month = '<STR_LIT>' , <EOL> expiration_year = '<STR_LIT>' , <EOL> security_code = '<STR_LIT>' , <EOL> ) . save ( ) <EOL> company = self . create_company ( ) <EOL> api_key = str ( company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> status = <NUM_LIT:200> <EOL> ) <EOL> customer = res . json <EOL> self . assertEqual ( customer [ '<STR_LIT>' ] , company [ '<STR_LIT>' ] ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = customer [ '<STR_LIT>' ] , <EOL> amount = <NUM_LIT> , <EOL> title = '<STR_LIT>' , <EOL> funding_instrument_uri = card . href , <EOL> ) , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> ) <EOL> transactions = res . json <EOL> self . assertEqual ( len ( transactions [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> transaction = res . json [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> callback_uri = ( <EOL> '<STR_LIT>' <EOL> . format ( company [ '<STR_LIT>' ] , company [ '<STR_LIT>' ] ) <EOL> ) <EOL> debit = balanced . Debit . fetch ( transaction [ '<STR_LIT>' ] ) <EOL> for event in debit . events : <EOL> res = self . testapp . post ( <EOL> callback_uri , <EOL> self . _to_json ( event . __dict__ ) , <EOL> headers = [ <EOL> self . make_auth ( api_key ) , <EOL> ( b'<STR_LIT>' , b'<STR_LIT:application/json>' ) <EOL> ] , <EOL> ) <EOL> entity = getattr ( event , '<STR_LIT>' , None ) <EOL> if entity is not None : <EOL> entity = entity . copy ( ) <EOL> del entity [ '<STR_LIT>' ] <EOL> entity = entity . popitem ( ) [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if ( <EOL> entity is None or <EOL> '<STR_LIT>' in entity [ '<STR_LIT>' ] <EOL> ) : <EOL> self . assertEqual ( res . json [ '<STR_LIT:code>' ] , '<STR_LIT>' ) <EOL> else : <EOL> self . assertEqual ( res . json [ '<STR_LIT:code>' ] , '<STR_LIT:ignore>' ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> headers = [ self . make_auth ( api_key ) ] , <EOL> ) <EOL> transactions = res . json <EOL> self . assertEqual ( len ( transactions [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> transaction = res . json [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction [ '<STR_LIT:status>' ] , '<STR_LIT>' ) </s>
