<s> from __future__ import unicode_literals <EOL> import mock <EOL> import transaction as db_transaction <EOL> from freezegun import freeze_time <EOL> from billy . tests . functional . helper import ViewTestCase <EOL> from billy . errors import BillyError <EOL> from billy . utils . generic import utc_now <EOL> @ freeze_time ( '<STR_LIT>' ) <EOL> class TestInvoiceViews ( ViewTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestInvoiceViews , self ) . setUp ( ) <EOL> with db_transaction . manager : <EOL> self . company = self . company_model . create ( <EOL> processor_key = '<STR_LIT>' , <EOL> ) <EOL> self . customer = self . customer_model . create ( company = self . company ) <EOL> self . company2 = self . company_model . create ( <EOL> processor_key = '<STR_LIT>' , <EOL> ) <EOL> self . customer2 = self . customer_model . create ( company = self . company2 ) <EOL> self . api_key = str ( self . company . api_key ) <EOL> self . api_key2 = str ( self . company2 . api_key ) <EOL> def _encode_item_params ( self , items ) : <EOL> """<STR_LIT>""" <EOL> item_params = { } <EOL> for i , item in enumerate ( items ) : <EOL> item_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT:name>' ] <EOL> item_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in item : <EOL> item_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in item : <EOL> item_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in item : <EOL> item_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT>' ] <EOL> if '<STR_LIT:type>' in item : <EOL> item_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT:type>' ] <EOL> return item_params <EOL> def _encode_adjustment_params ( self , items ) : <EOL> """<STR_LIT>""" <EOL> adjustment_params = { } <EOL> for i , item in enumerate ( items ) : <EOL> adjustment_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in item : <EOL> adjustment_params [ '<STR_LIT>' . format ( i ) ] = item [ '<STR_LIT>' ] <EOL> return adjustment_params <EOL> def test_create_invoice ( self ) : <EOL> amount = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> external_id = '<STR_LIT>' <EOL> appears_on_statement_as = '<STR_LIT>' <EOL> now = utc_now ( ) <EOL> now_iso = now . isoformat ( ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> title = title , <EOL> external_id = external_id , <EOL> appears_on_statement_as = appears_on_statement_as , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , now_iso ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , now_iso ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , amount ) <EOL> self . assertEqual ( res . json [ '<STR_LIT:title>' ] , title ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , external_id ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , <EOL> appears_on_statement_as ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , self . customer . guid ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , None ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:0> ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_create_invoice_with_invalid_funding_instrument ( <EOL> self , <EOL> validate_funding_instrument_method , <EOL> ) : <EOL> validate_funding_instrument_method . side_effect = BillyError ( '<STR_LIT>' ) <EOL> self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> validate_funding_instrument_method . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_create_invoice_with_zero_amount ( self ) : <EOL> amount = <NUM_LIT:0> <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , amount ) <EOL> def test_create_invoice_with_external_id ( self ) : <EOL> amount = <NUM_LIT> <EOL> external_id = '<STR_LIT>' <EOL> self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> external_id = external_id , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> external_id = external_id , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> def test_create_invoice_with_items ( self ) : <EOL> items = [ <EOL> dict ( name = '<STR_LIT:foo>' , amount = <NUM_LIT> ) , <EOL> dict ( name = '<STR_LIT:bar>' , amount = <NUM_LIT> , unit = '<STR_LIT>' ) , <EOL> dict ( name = '<STR_LIT>' , amount = <NUM_LIT> , unit = '<STR_LIT>' ) , <EOL> ] <EOL> item_params = self . _encode_item_params ( items ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> item_namexxx = '<STR_LIT>' , <EOL> ** item_params <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , None ) <EOL> item_result = res . json [ '<STR_LIT>' ] <EOL> for item in item_result : <EOL> for key , value in list ( item . iteritems ( ) ) : <EOL> if value is None : <EOL> del item [ key ] <EOL> self . assertEqual ( item_result , items ) <EOL> def test_create_invoice_with_adjustments ( self ) : <EOL> adjustments = [ <EOL> dict ( amount = - <NUM_LIT:100> , reason = '<STR_LIT>' ) , <EOL> dict ( amount = <NUM_LIT:20> , reason = '<STR_LIT>' ) , <EOL> dict ( amount = <NUM_LIT:3> , reason = '<STR_LIT>' ) , <EOL> ] <EOL> adjustment_params = self . _encode_adjustment_params ( adjustments ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT:200> , <EOL> adjustment_amountxxx = '<STR_LIT>' , <EOL> ** adjustment_params <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , - <NUM_LIT:100> + <NUM_LIT:20> + <NUM_LIT:3> ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , <EOL> <NUM_LIT:200> + res . json [ '<STR_LIT>' ] ) <EOL> adjustment_result = res . json [ '<STR_LIT>' ] <EOL> for adjustment in adjustment_result : <EOL> for key , value in list ( adjustment . iteritems ( ) ) : <EOL> if value is None : <EOL> del adjustment [ key ] <EOL> self . assertEqual ( adjustment_result , adjustments ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_create_invoice_with_funding_instrument_uri ( self , debit_method ) : <EOL> amount = <NUM_LIT> <EOL> funding_instrument_uri = '<STR_LIT>' <EOL> now = utc_now ( ) <EOL> now_iso = now . isoformat ( ) <EOL> adjustments = [ <EOL> dict ( amount = - <NUM_LIT:100> , reason = '<STR_LIT>' ) , <EOL> ] <EOL> adjustment_params = self . _encode_adjustment_params ( adjustments ) <EOL> debit_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> funding_instrument_uri = funding_instrument_uri , <EOL> ** adjustment_params <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , now_iso ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , now_iso ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , amount ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , amount - <NUM_LIT:100> ) <EOL> self . assertEqual ( res . json [ '<STR_LIT:title>' ] , None ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , self . customer . guid ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , <EOL> funding_instrument_uri ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:1> ) <EOL> transaction = invoice . transactions [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction . amount , invoice . effective_amount ) <EOL> self . assertEqual ( transaction . processor_uri , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . DONE ) <EOL> self . assertEqual ( transaction . status , <EOL> self . transaction_model . statuses . SUCCEEDED ) <EOL> debit_method . assert_called_once_with ( transaction ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_create_invoice_with_funding_instrument_uri_with_zero_amount ( self , debit_method ) : <EOL> amount = <NUM_LIT:0> <EOL> funding_instrument_uri = '<STR_LIT>' <EOL> debit_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> funding_instrument_uri = funding_instrument_uri , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:0> ) <EOL> self . assertFalse ( debit_method . called ) <EOL> def test_create_invoice_with_bad_parameters ( self ) : <EOL> def assert_bad_parameters ( params ) : <EOL> self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> params , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> assert_bad_parameters ( { } ) <EOL> assert_bad_parameters ( dict ( <EOL> customer_guid = self . customer . guid , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) ) <EOL> assert_bad_parameters ( dict ( <EOL> customer_guid = self . customer . guid , <EOL> ) ) <EOL> assert_bad_parameters ( dict ( <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) ) <EOL> assert_bad_parameters ( dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = - <NUM_LIT:1> , <EOL> ) ) <EOL> assert_bad_parameters ( dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> title = '<STR_LIT:t>' * <NUM_LIT> , <EOL> ) ) <EOL> assert_bad_parameters ( dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> appears_on_statement_as = '<STR_LIT>' , <EOL> ) ) <EOL> assert_bad_parameters ( dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> appears_on_statement_as = '<STR_LIT>' , <EOL> ) ) <EOL> def test_create_invoice_to_other_company_customer ( self ) : <EOL> self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer2 . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> def test_create_invoice_with_bad_api ( self ) : <EOL> self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = b'<STR_LIT>' ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> def test_create_invoice_to_a_deleted_customer ( self ) : <EOL> with db_transaction . manager : <EOL> self . customer_model . delete ( self . customer ) <EOL> self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> def test_get_invoice ( self ) : <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> created_invoice = res . json <EOL> guid = created_invoice [ '<STR_LIT>' ] <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' . format ( guid ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . assertEqual ( res . json , created_invoice ) <EOL> def test_get_non_existing_invoice ( self ) : <EOL> self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> <EOL> ) <EOL> def test_get_invoice_with_bad_api_key ( self ) : <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> created_invoice = res . json <EOL> guid = created_invoice [ '<STR_LIT>' ] <EOL> self . testapp . get ( <EOL> '<STR_LIT>' . format ( guid ) , <EOL> extra_environ = dict ( REMOTE_USER = b'<STR_LIT>' ) , <EOL> status = <NUM_LIT> <EOL> ) <EOL> def test_get_invoice_of_other_company ( self ) : <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer2 . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key2 ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> other_guid = res . json [ '<STR_LIT>' ] <EOL> self . testapp . get ( <EOL> '<STR_LIT>' . format ( other_guid ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> def test_invoice_list ( self ) : <EOL> with db_transaction . manager : <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> with freeze_time ( '<STR_LIT>' . format ( i + <NUM_LIT:1> ) ) : <EOL> self . invoice_model . create ( <EOL> customer = self . customer2 , <EOL> amount = <NUM_LIT> , <EOL> ) <EOL> with db_transaction . manager : <EOL> guids = [ ] <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> with freeze_time ( '<STR_LIT>' . format ( i + <NUM_LIT:1> ) ) : <EOL> invoice = self . invoice_model . create ( <EOL> customer = self . customer , <EOL> amount = ( i + <NUM_LIT:1> ) * <NUM_LIT:1000> , <EOL> ) <EOL> guids . append ( invoice . guid ) <EOL> guids = list ( reversed ( guids ) ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> items = res . json [ '<STR_LIT>' ] <EOL> result_guids = [ item [ '<STR_LIT>' ] for item in items ] <EOL> self . assertEqual ( result_guids , guids ) <EOL> def test_invoice_transaction_list ( self ) : <EOL> with db_transaction . manager : <EOL> other_invoice = self . invoice_model . create ( <EOL> customer = self . customer2 , <EOL> amount = <NUM_LIT> , <EOL> ) <EOL> with db_transaction . manager : <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> self . transaction_model . create ( <EOL> invoice = other_invoice , <EOL> amount = other_invoice . effective_amount , <EOL> transaction_type = other_invoice . transaction_type , <EOL> funding_instrument_uri = other_invoice . funding_instrument_uri , <EOL> appears_on_statement_as = other_invoice . appears_on_statement_as , <EOL> ) <EOL> with db_transaction . manager : <EOL> invoice = self . invoice_model . create ( <EOL> customer = self . customer , <EOL> amount = <NUM_LIT> , <EOL> ) <EOL> with db_transaction . manager : <EOL> guids = [ ] <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> with freeze_time ( '<STR_LIT>' . format ( i + <NUM_LIT:1> ) ) : <EOL> transaction = self . transaction_model . create ( <EOL> invoice = invoice , <EOL> amount = invoice . effective_amount , <EOL> transaction_type = invoice . transaction_type , <EOL> funding_instrument_uri = invoice . funding_instrument_uri , <EOL> appears_on_statement_as = invoice . appears_on_statement_as , <EOL> ) <EOL> guids . append ( transaction . guid ) <EOL> guids = list ( reversed ( guids ) ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' . format ( invoice . guid ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> items = res . json [ '<STR_LIT>' ] <EOL> result_guids = [ item [ '<STR_LIT>' ] for item in items ] <EOL> self . assertEqual ( result_guids , guids ) <EOL> def test_invoice_list_with_external_id ( self ) : <EOL> with db_transaction . manager : <EOL> guids = [ ] <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> with freeze_time ( '<STR_LIT>' . format ( i + <NUM_LIT:1> ) ) : <EOL> external_id = i <EOL> if i >= <NUM_LIT:2> : <EOL> external_id = None <EOL> invoice = self . invoice_model . create ( <EOL> customer = self . customer , <EOL> amount = ( i + <NUM_LIT:1> ) * <NUM_LIT:1000> , <EOL> external_id = external_id , <EOL> ) <EOL> guids . append ( invoice . guid ) <EOL> guids = list ( reversed ( guids ) ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> dict ( external_id = <NUM_LIT:0> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> items = res . json [ '<STR_LIT>' ] <EOL> result_guids = [ item [ '<STR_LIT>' ] for item in items ] <EOL> self . assertEqual ( result_guids , [ guids [ - <NUM_LIT:1> ] ] ) <EOL> res = self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> dict ( external_id = <NUM_LIT:1> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> items = res . json [ '<STR_LIT>' ] <EOL> result_guids = [ item [ '<STR_LIT>' ] for item in items ] <EOL> self . assertEqual ( result_guids , [ guids [ - <NUM_LIT:2> ] ] ) <EOL> def test_invoice_list_with_bad_api_key ( self ) : <EOL> with db_transaction . manager : <EOL> invoice = self . invoice_model . create ( <EOL> customer = self . customer , <EOL> amount = <NUM_LIT> , <EOL> ) <EOL> self . testapp . get ( <EOL> '<STR_LIT>' , <EOL> extra_environ = dict ( REMOTE_USER = b'<STR_LIT>' ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> self . testapp . get ( <EOL> '<STR_LIT>' . format ( invoice . guid ) , <EOL> extra_environ = dict ( REMOTE_USER = b'<STR_LIT>' ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_update_invoice_funding_instrument_uri ( self , debit_method ) : <EOL> amount = <NUM_LIT> <EOL> funding_instrument_uri = '<STR_LIT>' <EOL> debit_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , None ) <EOL> guid = res . json [ '<STR_LIT>' ] <EOL> res = self . testapp . put ( <EOL> '<STR_LIT>' . format ( guid ) , <EOL> dict ( <EOL> funding_instrument_uri = funding_instrument_uri , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , funding_instrument_uri ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:1> ) <EOL> transaction = invoice . transactions [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction . processor_uri , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , self . transaction_model . submit_statuses . DONE ) <EOL> debit_method . assert_called_once_with ( transaction ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_update_invoice_funding_instrument_uri_for_settled_invoice ( <EOL> self , <EOL> debit_method , <EOL> ) : <EOL> debit_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . testapp . put ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> dict ( <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_update_invoice_funding_instrument_uri_with_mutiple_failures ( <EOL> self , <EOL> debit_method <EOL> ) : <EOL> self . model_factory . settings [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> debit_method . side_effect = RuntimeError ( '<STR_LIT>' ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( invoice . status , <EOL> self . invoice_model . statuses . FAILED ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:1> ) <EOL> transaction = invoice . transactions [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction . funding_instrument_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . FAILED ) <EOL> self . assertEqual ( transaction . status , None ) <EOL> def update_instrument_uri ( when , uri ) : <EOL> with freeze_time ( when ) : <EOL> self . testapp . put ( <EOL> '<STR_LIT>' . format ( invoice . guid ) , <EOL> dict ( <EOL> funding_instrument_uri = uri , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> update_instrument_uri ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( invoice . status , <EOL> self . invoice_model . statuses . FAILED ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:2> ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:1> ] <EOL> self . assertEqual ( transaction . funding_instrument_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . FAILED ) <EOL> self . assertEqual ( transaction . status , None ) <EOL> self . model_factory . settings [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> update_instrument_uri ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( invoice . status , <EOL> self . invoice_model . statuses . PROCESSING ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:3> ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:1> ] <EOL> self . assertEqual ( transaction . funding_instrument_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . RETRYING ) <EOL> self . assertEqual ( transaction . status , None ) <EOL> update_instrument_uri ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( invoice . status , <EOL> self . invoice_model . statuses . PROCESSING ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:4> ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:2> ] <EOL> self . assertEqual ( transaction . funding_instrument_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . CANCELED ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:1> ] <EOL> self . assertEqual ( transaction . funding_instrument_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . RETRYING ) <EOL> self . assertEqual ( transaction . status , None ) <EOL> debit_method . side_effect = None <EOL> debit_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> update_instrument_uri ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( invoice . status , <EOL> self . invoice_model . statuses . SETTLED ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:5> ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:2> ] <EOL> self . assertEqual ( transaction . funding_instrument_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . CANCELED ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:1> ] <EOL> self . assertEqual ( transaction . funding_instrument_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . processor_uri , '<STR_LIT>' ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . DONE ) <EOL> self . assertEqual ( transaction . status , <EOL> self . transaction_model . statuses . SUCCEEDED ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_update_invoice_funding_instrument_uri_with_zero_amount ( self , debit_method ) : <EOL> amount = <NUM_LIT:0> <EOL> funding_instrument_uri = '<STR_LIT>' <EOL> debit_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , None ) <EOL> self . assertFalse ( debit_method . called ) <EOL> guid = res . json [ '<STR_LIT>' ] <EOL> res = self . testapp . put ( <EOL> '<STR_LIT>' . format ( guid ) , <EOL> dict ( <EOL> funding_instrument_uri = funding_instrument_uri , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . assertEqual ( res . json [ '<STR_LIT>' ] , funding_instrument_uri ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:0> ) <EOL> self . assertFalse ( debit_method . called ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_invoice_refund ( self , refund_method ) : <EOL> refund_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> with freeze_time ( '<STR_LIT>' ) : <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> dict ( amount = <NUM_LIT> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( invoice . status , self . invoice_model . statuses . SETTLED ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:2> ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:1> ] <EOL> refund_method . assert_called_once_with ( transaction ) <EOL> self . assertEqual ( transaction . funding_instrument_uri , None ) <EOL> self . assertEqual ( transaction . amount , <NUM_LIT> ) <EOL> self . assertEqual ( transaction . status , <EOL> self . transaction_model . statuses . SUCCEEDED ) <EOL> self . assertEqual ( transaction . submit_status , self . transaction_model . submit_statuses . DONE ) <EOL> self . assertEqual ( transaction . transaction_type , <EOL> self . transaction_model . types . REFUND ) <EOL> self . assertEqual ( transaction . appears_on_statement_as , None ) <EOL> self . assertEqual ( transaction . processor_uri , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_invoice_mutiple_refund ( self , refund_method ) : <EOL> refund_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = self . transaction_model . statuses . SUCCEEDED , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> def refund ( when , amount ) : <EOL> with freeze_time ( when ) : <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> dict ( amount = amount ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . assertEqual ( invoice . status , self . invoice_model . statuses . SETTLED ) <EOL> transaction = invoice . transactions [ - <NUM_LIT:1> ] <EOL> refund_method . assert_called_with ( transaction ) <EOL> self . assertEqual ( transaction . funding_instrument_uri , None ) <EOL> self . assertEqual ( transaction . amount , amount ) <EOL> self . assertEqual ( transaction . status , <EOL> self . transaction_model . statuses . SUCCEEDED ) <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . DONE ) <EOL> self . assertEqual ( transaction . transaction_type , <EOL> self . transaction_model . types . REFUND ) <EOL> self . assertEqual ( transaction . appears_on_statement_as , None ) <EOL> self . assertEqual ( transaction . processor_uri , '<STR_LIT>' ) <EOL> refund ( '<STR_LIT>' , <NUM_LIT:1000> ) <EOL> refund ( '<STR_LIT>' , <NUM_LIT> ) <EOL> refund ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> dict ( amount = <NUM_LIT> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> def test_invoice_cancel ( self ) : <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> with freeze_time ( '<STR_LIT>' ) : <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( invoice . status , self . invoice_model . statuses . CANCELED ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:0> ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_invoice_cancel_while_processing ( self , debit_method ) : <EOL> debit_method . side_effect = RuntimeError ( '<STR_LIT>' ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> funding_instrument_uri = '<STR_LIT>' , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> with freeze_time ( '<STR_LIT>' ) : <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( invoice . status , self . invoice_model . statuses . CANCELED ) <EOL> self . assertEqual ( len ( invoice . transactions ) , <NUM_LIT:1> ) <EOL> transaction = invoice . transactions [ <NUM_LIT:0> ] <EOL> self . assertEqual ( transaction . submit_status , <EOL> self . transaction_model . submit_statuses . CANCELED ) <EOL> def test_invoice_cancel_already_canceled_invoice ( self ) : <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = <NUM_LIT> , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . testapp . post ( <EOL> '<STR_LIT>' . format ( res . json [ '<STR_LIT>' ] ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT> , <EOL> ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_invoice_create_status ( self , debit_method ) : <EOL> amount = <NUM_LIT> <EOL> funding_instrument_uri = '<STR_LIT>' <EOL> def assert_status ( tx_status , expected_status ) : <EOL> debit_method . return_value = dict ( <EOL> processor_uri = '<STR_LIT>' , <EOL> status = tx_status , <EOL> ) <EOL> res = self . testapp . post ( <EOL> '<STR_LIT>' , <EOL> dict ( <EOL> customer_guid = self . customer . guid , <EOL> amount = amount , <EOL> funding_instrument_uri = funding_instrument_uri , <EOL> ) , <EOL> extra_environ = dict ( REMOTE_USER = self . api_key ) , <EOL> status = <NUM_LIT:200> , <EOL> ) <EOL> self . failUnless ( '<STR_LIT>' in res . json ) <EOL> invoice = self . invoice_model . get ( res . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( invoice . status , expected_status ) <EOL> ts = self . transaction_model . statuses <EOL> ivs = self . invoice_model . statuses <EOL> assert_status ( ts . PENDING , ivs . PROCESSING ) <EOL> assert_status ( ts . SUCCEEDED , ivs . SETTLED ) <EOL> assert_status ( ts . FAILED , ivs . FAILED ) </s>
