<s> import random <EOL> import sys <EOL> from datetime import datetime <EOL> from decimal import Decimal <EOL> from django . conf import settings <EOL> from django . test import TestCase <EOL> from django . utils . unittest import skipIf <EOL> from billing . tests . utils import BetterXMLCompareMixin <EOL> from billing . gateway import get_gateway <EOL> from billing . gateways . global_iris_gateway import GlobalIrisGateway , CARD_NAMES <EOL> from billing . integrations . global_iris_real_mpi_integration import GlobalIrisRealMpiIntegration <EOL> from billing . signals import transaction_was_unsuccessful , transaction_was_successful <EOL> from billing . utils . credit_card import CreditCard , CardNotSupported , Visa <EOL> class Dummy200Response ( object ) : <EOL> def __init__ ( self , content ) : <EOL> self . status_code = <NUM_LIT:200> <EOL> self . content = content <EOL> class GlobalIrisTestBase ( object ) : <EOL> def mk_gateway ( self ) : <EOL> return GlobalIrisGateway ( <EOL> config = { '<STR_LIT>' : dict ( SHARED_SECRET = "<STR_LIT>" , <EOL> MERCHANT_ID = "<STR_LIT>" , <EOL> ACCOUNT = "<STR_LIT>" ) <EOL> } , <EOL> test_mode = True ) <EOL> def mk_integration ( self ) : <EOL> return GlobalIrisRealMpiIntegration ( <EOL> config = { '<STR_LIT>' : dict ( SHARED_SECRET = "<STR_LIT>" , <EOL> MERCHANT_ID = "<STR_LIT>" , <EOL> ACCOUNT = "<STR_LIT>" ) <EOL> } , <EOL> test_mode = True ) <EOL> def get_visa_card ( self ) : <EOL> return CreditCard ( first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> month = <NUM_LIT:7> , <EOL> year = <NUM_LIT> , <EOL> number = '<STR_LIT>' , <EOL> verification_value = '<STR_LIT>' , <EOL> ) <EOL> def get_amex_card ( self ) : <EOL> return CreditCard ( first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> month = <NUM_LIT:8> , <EOL> year = <NUM_LIT> , <EOL> number = '<STR_LIT>' , <EOL> verification_value = '<STR_LIT>' , <EOL> ) <EOL> def get_order_id ( self ) : <EOL> return str ( datetime . now ( ) ) . replace ( '<STR_LIT::>' , '<STR_LIT:_>' ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) + str ( random . randint ( <NUM_LIT:0> , sys . maxint ) ) <EOL> @ skipIf ( not settings . MERCHANT_SETTINGS . get ( "<STR_LIT>" , None ) , "<STR_LIT>" ) <EOL> class GlobalIrisGatewayTestCase ( BetterXMLCompareMixin , GlobalIrisTestBase , TestCase ) : <EOL> def test_request_xml ( self ) : <EOL> gateway = self . mk_gateway ( ) <EOL> card = CreditCard ( first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> month = <NUM_LIT:7> , <EOL> year = <NUM_LIT> , <EOL> number = '<STR_LIT>' , <EOL> verification_value = '<STR_LIT>' , <EOL> ) <EOL> gateway . validate_card ( card ) <EOL> data = { <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : card , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> } <EOL> xml = gateway . build_xml ( data ) <EOL> self . assertXMLEqual ( u"""<STR_LIT>""" . encode ( '<STR_LIT:utf-8>' ) , xml ) <EOL> mpi_data = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:5>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } } <EOL> data . update ( mpi_data ) <EOL> xml2 = gateway . build_xml ( data ) <EOL> self . assertXMLEqual ( u"""<STR_LIT>""" . encode ( '<STR_LIT:utf-8>' ) , xml2 ) <EOL> def test_signature ( self ) : <EOL> gateway = self . mk_gateway ( ) <EOL> card = CreditCard ( number = '<STR_LIT>' , <EOL> first_name = '<STR_LIT:x>' , <EOL> last_name = '<STR_LIT:x>' , <EOL> year = '<STR_LIT:1>' , month = '<STR_LIT:1>' , <EOL> verification_value = '<STR_LIT>' ) <EOL> gateway . validate_card ( card ) <EOL> config = gateway . get_config ( card ) <EOL> sig = gateway . get_standard_signature ( <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : card , <EOL> } , config ) <EOL> self . assertEqual ( sig , "<STR_LIT>" ) <EOL> def test_parse_fail_xml ( self ) : <EOL> gateway = self . mk_gateway ( ) <EOL> fail_resp = Dummy200Response ( '<STR_LIT>' ) <EOL> retval = gateway . handle_response ( fail_resp , "<STR_LIT>" ) <EOL> self . assertEqual ( retval [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( retval [ '<STR_LIT:message>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( retval [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> self . assertEqual ( retval [ '<STR_LIT>' ] , fail_resp ) <EOL> def test_parse_success_xml ( self ) : <EOL> gateway = self . mk_gateway ( ) <EOL> success_resp = Dummy200Response ( '<STR_LIT>' ) <EOL> retval = gateway . handle_response ( success_resp , "<STR_LIT>" ) <EOL> self . assertEqual ( retval [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( retval [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( retval [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( retval [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( retval [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( retval [ '<STR_LIT>' ] , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> def test_config_for_card_type ( self ) : <EOL> """<STR_LIT>""" <EOL> gateway = GlobalIrisGateway ( config = { <EOL> '<STR_LIT>' : dict ( SHARED_SECRET = "<STR_LIT>" , <EOL> MERCHANT_ID = "<STR_LIT>" , <EOL> ACCOUNT = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : dict ( SHARED_SECRET = "<STR_LIT>" , <EOL> MERCHANT_ID = "<STR_LIT>" , <EOL> ACCOUNT = "<STR_LIT>" ) , <EOL> } , test_mode = False ) <EOL> vc = self . get_visa_card ( ) <EOL> self . assertTrue ( gateway . validate_card ( vc ) ) <EOL> ac = self . get_amex_card ( ) <EOL> self . assertTrue ( gateway . validate_card ( ac ) ) <EOL> self . assertEqual ( "<STR_LIT>" , gateway . get_config ( vc ) . account ) <EOL> self . assertEqual ( "<STR_LIT>" , gateway . get_config ( ac ) . account ) <EOL> def test_purchase_fail ( self ) : <EOL> received_signals = [ ] <EOL> def receive ( sender , ** kwargs ) : <EOL> received_signals . append ( kwargs . get ( "<STR_LIT>" ) ) <EOL> transaction_was_unsuccessful . connect ( receive ) <EOL> gateway = get_gateway ( '<STR_LIT>' ) <EOL> card = self . get_visa_card ( ) <EOL> gateway . validate_card ( card ) <EOL> response = gateway . purchase ( Decimal ( "<STR_LIT>" ) , card , options = { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( response [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( received_signals , [ transaction_was_unsuccessful ] ) <EOL> def _get_test_cards ( self ) : <EOL> cards = [ ] <EOL> card_dicts = settings . MERCHANT_SETTINGS [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> for card_dict in card_dicts : <EOL> card_type = card_dict [ '<STR_LIT>' ] <EOL> d = dict ( first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> month = <NUM_LIT:1> , <EOL> year = datetime . now ( ) . year + <NUM_LIT:2> , <EOL> number = card_dict [ '<STR_LIT>' ] , <EOL> verification_value = "<STR_LIT>" if card_type == "<STR_LIT>" else "<STR_LIT>" ) <EOL> card = CreditCard ( ** d ) <EOL> card . expected_response_code = card_dict [ '<STR_LIT>' ] <EOL> cards . append ( card ) <EOL> return cards <EOL> @ skipIf ( '<STR_LIT>' not in settings . MERCHANT_SETTINGS . get ( '<STR_LIT>' , { } ) <EOL> or '<STR_LIT>' not in settings . MERCHANT_SETTINGS . get ( '<STR_LIT>' , { } ) , <EOL> "<STR_LIT>" ) <EOL> def test_purchase_with_test_cards ( self ) : <EOL> gateway = GlobalIrisGateway ( ) <EOL> if not gateway . test_mode : <EOL> self . fail ( "<STR_LIT>" ) <EOL> for card in self . _get_test_cards ( ) : <EOL> received_signals = [ ] <EOL> def success ( sender , ** kwargs ) : <EOL> received_signals . append ( kwargs . get ( "<STR_LIT>" ) ) <EOL> transaction_was_successful . connect ( success ) <EOL> def fail ( sender , ** kwargs ) : <EOL> received_signals . append ( kwargs . get ( "<STR_LIT>" ) ) <EOL> transaction_was_unsuccessful . connect ( fail ) <EOL> try : <EOL> gateway . validate_card ( card ) <EOL> except CardNotSupported : <EOL> self . assertNotEqual ( card . expected_response_code , "<STR_LIT>" ) <EOL> continue <EOL> response = gateway . purchase ( Decimal ( "<STR_LIT>" ) , card , <EOL> options = { '<STR_LIT>' : self . get_order_id ( ) , <EOL> } ) <EOL> actual_rc = response [ '<STR_LIT>' ] <EOL> expected_rc = card . expected_response_code <EOL> self . assertEqual ( actual_rc , expected_rc , <EOL> "<STR_LIT>" % ( actual_rc , expected_rc , card . number , response [ '<STR_LIT:message>' ] ) ) <EOL> if card . expected_response_code == "<STR_LIT>" : <EOL> self . assertEqual ( response [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( received_signals , [ transaction_was_successful ] ) <EOL> else : <EOL> self . assertEqual ( response [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( received_signals , [ transaction_was_unsuccessful ] ) <EOL> def test_address_to_code ( self ) : <EOL> gateway = GlobalIrisGateway ( ) <EOL> self . assertEqual ( gateway . address_to_code ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> @ skipIf ( not settings . MERCHANT_SETTINGS . get ( "<STR_LIT>" , None ) , "<STR_LIT>" ) <EOL> class GlobalIrisRealMpiIntegrationTestCase ( BetterXMLCompareMixin , GlobalIrisTestBase , TestCase ) : <EOL> def test_3ds_verifyenrolled_xml ( self ) : <EOL> expected = """<STR_LIT>""" <EOL> integration = self . mk_integration ( ) <EOL> gateway = self . mk_gateway ( ) <EOL> card = CreditCard ( first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> month = <NUM_LIT:7> , <EOL> year = <NUM_LIT> , <EOL> number = '<STR_LIT>' , <EOL> verification_value = '<STR_LIT>' , <EOL> ) <EOL> gateway . validate_card ( card ) <EOL> actual_xml = integration . build_3ds_verifyenrolled_xml ( <EOL> { '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : card , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:5> ) , <EOL> } ) <EOL> self . assertXMLEqual ( actual_xml , expected ) <EOL> def test_parse_3ds_verifyenrolled_response ( self ) : <EOL> example_xml = """<STR_LIT>""" <EOL> integration = self . mk_integration ( ) <EOL> retval = integration . handle_3ds_verifyenrolled_response ( Dummy200Response ( example_xml ) ) <EOL> self . assertEqual ( retval . enrolled , True ) <EOL> self . assertEqual ( retval . response_code , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . message , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . url , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . pareq , "<STR_LIT>" ) <EOL> def test_parse_3ds_verifyenrolled_response_not_enrolled ( self ) : <EOL> example_xml = """<STR_LIT>""" <EOL> integration = self . mk_integration ( ) <EOL> retval = integration . handle_3ds_verifyenrolled_response ( Dummy200Response ( example_xml ) ) <EOL> self . assertEqual ( retval . enrolled , False ) <EOL> self . assertEqual ( retval . response_code , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . message , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . url , None ) <EOL> gateway = self . mk_gateway ( ) <EOL> card = self . get_visa_card ( ) <EOL> gateway . validate_card ( card ) <EOL> proceed , extra = retval . proceed_with_auth ( card ) <EOL> self . assertEqual ( proceed , True ) , <EOL> self . assertEqual ( extra , { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:6> } } ) <EOL> def test_send_3ds_verifyenrolled ( self ) : <EOL> integration = self . mk_integration ( ) <EOL> gateway = self . mk_gateway ( ) <EOL> card = self . get_visa_card ( ) <EOL> gateway . validate_card ( card ) <EOL> response = integration . send_3ds_verifyenrolled ( { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : card , <EOL> } ) <EOL> self . assertEqual ( response . error , True ) <EOL> def test_encode ( self ) : <EOL> card = self . get_visa_card ( ) <EOL> integration = self . mk_integration ( ) <EOL> gateway = self . mk_gateway ( ) <EOL> gateway . validate_card ( card ) <EOL> d = { '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : card , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> } <EOL> encoded = integration . encode_merchant_data ( d ) <EOL> decoded = integration . decode_merchant_data ( encoded ) <EOL> self . assertEqual ( decoded [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( decoded [ '<STR_LIT>' ] . number , card . number ) <EOL> self . assertEqual ( decoded [ '<STR_LIT>' ] . card_type , Visa ) <EOL> self . assertEqual ( decoded [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] ) <EOL> def test_3ds_verifysig_xml ( self ) : <EOL> expected = """<STR_LIT>""" <EOL> integration = self . mk_integration ( ) <EOL> gateway = self . mk_gateway ( ) <EOL> card = CreditCard ( first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> month = <NUM_LIT:7> , <EOL> year = <NUM_LIT> , <EOL> number = '<STR_LIT>' , <EOL> verification_value = '<STR_LIT>' , <EOL> ) <EOL> gateway . validate_card ( card ) <EOL> actual_xml = integration . build_3ds_verifysig_xml ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : card , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:5> ) , <EOL> } ) <EOL> self . assertXMLEqual ( actual_xml , expected ) <EOL> def test_parse_3ds_verifysig_response_no_auth ( self ) : <EOL> example_xml = """<STR_LIT>""" <EOL> integration = self . mk_integration ( ) <EOL> gateway = self . mk_gateway ( ) <EOL> card = self . get_visa_card ( ) <EOL> gateway . validate_card ( card ) <EOL> retval = integration . handle_3ds_verifysig_response ( Dummy200Response ( example_xml ) ) <EOL> self . assertEqual ( retval . response_code , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . message , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . status , "<STR_LIT:N>" ) <EOL> self . assertEqual ( retval . proceed_with_auth ( card ) [ <NUM_LIT:0> ] , False ) <EOL> def test_parse_3ds_verifysig_response_yes_auth ( self ) : <EOL> example_xml = """<STR_LIT>""" <EOL> integration = self . mk_integration ( ) <EOL> gateway = self . mk_gateway ( ) <EOL> card = self . get_visa_card ( ) <EOL> gateway . validate_card ( card ) <EOL> retval = integration . handle_3ds_verifysig_response ( Dummy200Response ( example_xml ) ) <EOL> self . assertEqual ( retval . response_code , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . message , "<STR_LIT>" ) <EOL> self . assertEqual ( retval . status , "<STR_LIT:Y>" ) <EOL> proceed , data = retval . proceed_with_auth ( card ) <EOL> self . assertTrue ( proceed ) <EOL> self . assertEqual ( data , { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:5>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } } ) <EOL> def test_send_3ds_verifysig ( self ) : <EOL> integration = self . mk_integration ( ) <EOL> gateway = self . mk_gateway ( ) <EOL> card = self . get_visa_card ( ) <EOL> gateway . validate_card ( card ) <EOL> response = integration . send_3ds_verifysig ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : card , <EOL> } ) <EOL> self . assertEqual ( response . error , True ) </s>
