<s> __author__ = '<STR_LIT>' <EOL> import base64 <EOL> import datetime <EOL> import functools <EOL> import json <EOL> import time <EOL> from tornado import options <EOL> from viewfinder . backend . base import base_options , secrets <EOL> from viewfinder . backend . base . testing import BaseTestCase , MockAsyncHTTPClient <EOL> from viewfinder . backend . services . itunes_store import ITunesStoreClient , ITunesStoreError <EOL> kProductId = '<STR_LIT>' <EOL> kTransactionId = '<STR_LIT>' <EOL> kTransactionId2 = '<STR_LIT>' <EOL> kExpirationTime = <NUM_LIT> <EOL> kExpirationTime2 = <NUM_LIT> <EOL> kReceiptData = """<STR_LIT>""" <EOL> kVerifyResponseRenewedExpired = """<STR_LIT>""" <EOL> def MakeRenewedExpiredResponse ( ) : <EOL> """<STR_LIT>""" <EOL> return kVerifyResponseRenewedExpired <EOL> def MakeNewResponse ( ) : <EOL> """<STR_LIT>""" <EOL> reference = json . loads ( kVerifyResponseRenewedExpired ) <EOL> return json . dumps ( { '<STR_LIT:status>' : <NUM_LIT:0> , '<STR_LIT>' : reference [ '<STR_LIT>' ] } ) <EOL> def MakeFreshResponse ( ) : <EOL> """<STR_LIT>""" <EOL> reference = json . loads ( kVerifyResponseRenewedExpired ) <EOL> new = { '<STR_LIT:status>' : <NUM_LIT:0> , '<STR_LIT>' : reference [ '<STR_LIT>' ] } <EOL> new [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT> * ( time . time ( ) + datetime . timedelta ( days = <NUM_LIT> ) . total_seconds ( ) ) <EOL> return json . dumps ( new ) <EOL> def MakeRenewedResponse ( ) : <EOL> """<STR_LIT>""" <EOL> reference = json . loads ( kVerifyResponseRenewedExpired ) <EOL> return json . dumps ( { <EOL> '<STR_LIT:status>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : reference [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : base64 . b64encode ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : reference [ '<STR_LIT>' ] , <EOL> } ) <EOL> def MakeExpiredResponse ( ) : <EOL> """<STR_LIT>""" <EOL> reference = json . loads ( kVerifyResponseRenewedExpired ) <EOL> return json . dumps ( { '<STR_LIT:status>' : reference [ '<STR_LIT:status>' ] , '<STR_LIT>' : reference [ '<STR_LIT>' ] } ) <EOL> def MakeBadSignatureResponse ( ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def MakeServerErrorResponse ( ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def MakeSandboxOnProdResponse ( ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def MakeOtherAppResponse ( ) : <EOL> """<STR_LIT>""" <EOL> reference = json . loads ( kVerifyResponseRenewedExpired ) <EOL> new = { '<STR_LIT:status>' : <NUM_LIT:0> , '<STR_LIT>' : reference [ '<STR_LIT>' ] } <EOL> new [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return json . dumps ( new ) <EOL> class ITunesStoreTest ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( ITunesStoreTest , self ) . setUp ( ) <EOL> options . options . domain = '<STR_LIT>' <EOL> secrets . InitSecretsForTest ( ) <EOL> def VerifyReceipt ( self , response , request ) : <EOL> request_data = json . loads ( request . body ) <EOL> self . assertEqual ( sorted ( request_data . keys ( ) ) , [ '<STR_LIT:password>' , '<STR_LIT>' ] ) <EOL> return response <EOL> def GetResponse ( self , raw_response ) : <EOL> mock_http = MockAsyncHTTPClient ( ) <EOL> mock_http . map ( r"<STR_LIT>" , <EOL> functools . partial ( self . VerifyReceipt , raw_response ) ) <EOL> client = ITunesStoreClient ( http_client = mock_http ) <EOL> client . VerifyReceipt ( kReceiptData , self . stop ) <EOL> response = self . wait ( ) <EOL> return response <EOL> def test_verify_renewed_expired ( self ) : <EOL> response = self . GetResponse ( MakeRenewedExpiredResponse ( ) ) <EOL> self . assertTrue ( response . IsValid ( ) ) <EOL> self . assertEqual ( response . GetProductId ( ) , kProductId ) <EOL> self . assertEqual ( response . GetExpirationTime ( ) , kExpirationTime2 ) <EOL> self . assertTrue ( response . IsExpired ( ) ) <EOL> self . assertFalse ( response . IsRenewable ( ) ) <EOL> self . assertEqual ( response . GetOriginalTransactionId ( ) , kTransactionId ) <EOL> self . assertEqual ( response . GetRenewalTransactionId ( ) , kTransactionId2 ) <EOL> def test_verify_new ( self ) : <EOL> response = self . GetResponse ( MakeNewResponse ( ) ) <EOL> self . assertTrue ( response . IsValid ( ) ) <EOL> self . assertEqual ( response . GetProductId ( ) , kProductId ) <EOL> self . assertEqual ( response . GetExpirationTime ( ) , kExpirationTime ) <EOL> self . assertTrue ( response . IsRenewable ( ) ) <EOL> self . assertEqual ( response . GetRenewalData ( ) , kReceiptData ) <EOL> self . assertEqual ( response . GetOriginalTransactionId ( ) , kTransactionId ) <EOL> self . assertEqual ( response . GetRenewalTransactionId ( ) , kTransactionId ) <EOL> def test_verify_renewed ( self ) : <EOL> response = self . GetResponse ( MakeRenewedResponse ( ) ) <EOL> self . assertTrue ( response . IsValid ( ) ) <EOL> self . assertEqual ( response . GetProductId ( ) , kProductId ) <EOL> self . assertEqual ( response . GetExpirationTime ( ) , kExpirationTime2 ) <EOL> self . assertTrue ( response . IsRenewable ( ) ) <EOL> self . assertEqual ( response . GetRenewalData ( ) , "<STR_LIT>" ) <EOL> self . assertEqual ( response . GetOriginalTransactionId ( ) , kTransactionId ) <EOL> self . assertEqual ( response . GetRenewalTransactionId ( ) , kTransactionId2 ) <EOL> def test_verify_expired ( self ) : <EOL> response = self . GetResponse ( MakeExpiredResponse ( ) ) <EOL> self . assertTrue ( response . IsValid ( ) ) <EOL> self . assertEqual ( response . GetProductId ( ) , kProductId ) <EOL> self . assertEqual ( response . GetExpirationTime ( ) , kExpirationTime ) <EOL> self . assertTrue ( response . IsExpired ( ) ) <EOL> self . assertFalse ( response . IsRenewable ( ) ) <EOL> self . assertEqual ( response . GetOriginalTransactionId ( ) , kTransactionId ) <EOL> self . assertEqual ( response . GetRenewalTransactionId ( ) , kTransactionId ) <EOL> def test_verify_bad_signature ( self ) : <EOL> response = self . GetResponse ( MakeBadSignatureResponse ( ) ) <EOL> self . assertFalse ( response . IsValid ( ) ) <EOL> def test_verify_server_error ( self ) : <EOL> response = self . GetResponse ( MakeServerErrorResponse ( ) ) <EOL> self . assertRaises ( ITunesStoreError , response . IsValid ) <EOL> def test_verify_other_app ( self ) : <EOL> response = self . GetResponse ( MakeOtherAppResponse ( ) ) <EOL> self . assertFalse ( response . IsValid ( ) ) </s>
