<s> """<STR_LIT>""" <EOL> import json <EOL> from mock import patch <EOL> import requests <EOL> import unittest <EOL> import itunesiap . legacy as itunesiap <EOL> from itunesiap . legacy import Request , Receipt , set_verification_mode <EOL> from itunesiap . legacy import exceptions <EOL> from tests . v2_test import LEGACY_RAW_RECEIPT <EOL> class TestsIAP ( unittest . TestCase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( TestsIAP , self ) . __init__ ( * args , ** kwargs ) <EOL> self . iap_response = { <EOL> u'<STR_LIT:status>' : <NUM_LIT:0> , <EOL> u'<STR_LIT>' : { <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT:1.0>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT:1>' <EOL> } <EOL> } <EOL> self . iap_response_in_app = { <EOL> u'<STR_LIT:status>' : <NUM_LIT:0> , <EOL> u'<STR_LIT>' : { <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : <NUM_LIT:0> , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : [ <EOL> { <EOL> u'<STR_LIT>' : u'<STR_LIT:false>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT:1>' <EOL> } , <EOL> { <EOL> u'<STR_LIT>' : u'<STR_LIT:false>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT:1>' <EOL> } <EOL> ] <EOL> } <EOL> } <EOL> def test_global_mode ( self ) : <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> assert Request ( '<STR_LIT>' ) . use_production is True <EOL> assert Request ( '<STR_LIT>' ) . use_sandbox is False <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> assert Request ( '<STR_LIT>' ) . use_production is False <EOL> assert Request ( '<STR_LIT>' ) . use_sandbox is True <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> assert Request ( '<STR_LIT>' ) . use_production is False <EOL> assert Request ( '<STR_LIT>' ) . use_sandbox is False <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> assert Request ( '<STR_LIT>' ) . use_production is True <EOL> assert Request ( '<STR_LIT>' ) . use_sandbox is True <EOL> def test_request ( self ) : <EOL> sandbox_receipt = LEGACY_RAW_RECEIPT <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> request = Request ( sandbox_receipt ) <EOL> try : <EOL> receipt = request . verify ( ) <EOL> assert False <EOL> except exceptions . InvalidReceipt as e : <EOL> assert e . status == <NUM_LIT> <EOL> assert e . description == e . _descriptions [ <NUM_LIT> ] <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> request = Request ( sandbox_receipt ) <EOL> receipt = request . verify ( ) <EOL> assert receipt <EOL> def test_responses ( self ) : <EOL> with patch . object ( requests , '<STR_LIT>' ) as mock_post : <EOL> iap_status_21007 = self . iap_response . copy ( ) <EOL> iap_status_21007 [ '<STR_LIT:status>' ] = <NUM_LIT> <EOL> mock_post . return_value . content = json . dumps ( iap_status_21007 ) . encode ( '<STR_LIT:utf-8>' ) <EOL> mock_post . return_value . status_code = <NUM_LIT:200> <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> request = Request ( '<STR_LIT>' ) <EOL> try : <EOL> request . verify ( ) <EOL> except exceptions . InvalidReceipt as e : <EOL> assert e . status == <NUM_LIT> <EOL> assert e . description == e . _descriptions [ <NUM_LIT> ] <EOL> with patch . object ( requests , '<STR_LIT>' ) as mock_post : <EOL> mock_post . return_value . content = '<STR_LIT>' <EOL> mock_post . return_value . status_code = <NUM_LIT> <EOL> set_verification_mode ( '<STR_LIT>' ) <EOL> request = Request ( '<STR_LIT>' ) <EOL> try : <EOL> request . verify ( ) <EOL> except exceptions . ItunesServerNotAvailable as e : <EOL> assert e . args [ <NUM_LIT:0> ] == <NUM_LIT> <EOL> assert e . args [ <NUM_LIT:1> ] == '<STR_LIT>' <EOL> def test_context ( self ) : <EOL> sandbox_receipt = LEGACY_RAW_RECEIPT <EOL> request = Request ( sandbox_receipt , verify_ssl = True ) <EOL> configs = request . use_production , request . use_sandbox <EOL> with request . verification_mode ( '<STR_LIT>' ) : <EOL> try : <EOL> request . verify ( ) <EOL> assert False <EOL> except exceptions . InvalidReceipt as e : <EOL> assert e . status == <NUM_LIT> <EOL> with request . verification_mode ( '<STR_LIT>' ) : <EOL> request . verify ( ) <EOL> try : <EOL> request . verify ( ) <EOL> assert False <EOL> except exceptions . InvalidReceipt as e : <EOL> assert e . status == <NUM_LIT> <EOL> assert configs == ( request . use_production , request . use_sandbox ) <EOL> def test_receipt ( self ) : <EOL> receipt = Receipt ( self . iap_response ) <EOL> assert receipt . status == <NUM_LIT:0> <EOL> assert receipt . product_id == u'<STR_LIT>' <EOL> assert receipt . original_transaction_id == u'<STR_LIT>' <EOL> assert receipt . quantity == u'<STR_LIT:1>' <EOL> assert receipt . unique_identifier == u'<STR_LIT>' <EOL> def test_shortcut ( self ) : <EOL> sandbox_receipt = LEGACY_RAW_RECEIPT <EOL> mode = itunesiap . get_verification_mode ( ) <EOL> itunesiap . set_verification_mode ( '<STR_LIT>' ) <EOL> itunesiap . verify ( sandbox_receipt ) <EOL> itunesiap . set_verification_mode ( mode ) <EOL> def test_extract_receipt ( self ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( '<STR_LIT>' , use_production = True ) <EOL> ext_receipt = request . _extract_receipt ( self . iap_response ) <EOL> assert ext_receipt [ '<STR_LIT:status>' ] == <NUM_LIT:0> <EOL> assert ext_receipt [ '<STR_LIT>' ] [ '<STR_LIT>' ] == u'<STR_LIT>' <EOL> assert ext_receipt [ '<STR_LIT>' ] [ '<STR_LIT>' ] == u'<STR_LIT>' <EOL> assert ext_receipt [ '<STR_LIT>' ] [ '<STR_LIT>' ] == u'<STR_LIT:1>' <EOL> request = Request ( '<STR_LIT>' , use_production = True ) <EOL> ext_receipt = request . _extract_receipt ( self . iap_response_in_app ) <EOL> assert ext_receipt [ '<STR_LIT:status>' ] == <NUM_LIT:0> <EOL> assert ext_receipt [ '<STR_LIT>' ] [ '<STR_LIT>' ] == u'<STR_LIT>' <EOL> assert ext_receipt [ '<STR_LIT>' ] [ '<STR_LIT>' ] == u'<STR_LIT>' <EOL> assert ext_receipt [ '<STR_LIT>' ] [ '<STR_LIT>' ] == u'<STR_LIT:1>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
