def test_body_hash3(self): oauth_parameters = OAuthParameters() encoded_hash = Util.base64_encode( Util.sha256_encode("{\"foƵ\":\"bar\"}")) oauth_parameters.set_oauth_body_hash(encoded_hash) self.assertEqual("+Z+PWW2TJDnPvRcTgol+nKO3LT7xm8smnsg+//XMIyI=", encoded_hash)
def test_body_hash1(self): oauth_parameters = OAuthParameters() encoded_hash = Util.base64_encode( Util.sha256_encode(OAuth.EMPTY_STRING)) oauth_parameters.set_oauth_body_hash(encoded_hash) self.assertEqual("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", encoded_hash)
def test_body_hash2(self): oauth_parameters = OAuthParameters() encoded_hash = Util.base64_encode(Util.sha256_encode(None)) # print(encoded_hash) oauth_parameters.set_oauth_body_hash(encoded_hash) self.assertEqual("3JN7WYkmBPWoaslpNs1/8J4l8Yrmt1joAUokx/oDnpE=", encoded_hash)
def test_signature_base_string2(self): body = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><ns2:TerminationInquiryRequest xmlns:ns2=\"http://mastercard.com/termination\"><AcquirerId>1996</AcquirerId><TransactionReferenceNumber>1</TransactionReferenceNumber><Merchant><Name>TEST</Name><DoingBusinessAsName>TEST</DoingBusinessAsName><PhoneNumber>5555555555</PhoneNumber><NationalTaxId>1234567890</NationalTaxId><Address><Line1>5555 Test Lane</Line1><City>TEST</City><CountrySubdivision>XX</CountrySubdivision><PostalCode>12345</PostalCode><Country>USA</Country></Address><Principal><FirstName>John</FirstName><LastName>Smith</LastName><NationalId>1234567890</NationalId><PhoneNumber>5555555555</PhoneNumber><Address><Line1>5555 Test Lane</Line1><City>TEST</City><CountrySubdivision>XX</CountrySubdivision><PostalCode>12345</PostalCode><Country>USA</Country></Address><DriversLicense><Number>1234567890</Number><CountrySubdivision>XX</CountrySubdivision></DriversLicense></Principal></Merchant></ns2:TerminationInquiryRequest>" url = "https://sandbox.api.mastercard.com/fraud/merchant/v1/termination-inquiry?Format=XML&PageOffset=0&PageLength=10" method = "POST" oauth_parameters = OAuthParameters() oauth_parameters.set_oauth_consumer_key( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") oauth_parameters.set_oauth_nonce("1111111111111111111") oauth_parameters.set_oauth_timestamp("1111111111") oauth_parameters.set_oauth_version("1.0") oauth_parameters.set_oauth_body_hash("body/hash") encoded_hash = Util.base64_encode(Util.sha256_encode(body)) oauth_parameters.set_oauth_body_hash(encoded_hash) oauth_parameters_base = oauth_parameters.get_base_parameters_dict() merge_parameters = oauth_parameters_base.copy() norm_params = Util.normalize_params("", merge_parameters) # print(oauth_parameters_base) query_params = OAuth.get_query_params(url) # print(query_params) normalize_params = Util.normalize_params("", query_params) base_string = OAuth.get_base_string( url, method, oauth_parameters, oauth_parameters.get_base_parameters_dict()) expected = "POST&https%3A%2F%2Fsandbox.api.mastercard.com%2Ffraud%2Fmerchant%2Fv1%2Ftermination-inquiry&Format%3DXML%26PageLength%3D10%26PageOffset%3D0%26oauth_body_hash%3Dh2Pd7zlzEZjZVIKB4j94UZn%2FxxoR3RoCjYQ9%2FJdadGQ%253D%26oauth_consumer_key%3Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%26oauth_nonce%3D1111111111111111111%26oauth_timestamp%3D1111111111%26oauth_version%3D1.0" self.maxDiff = None self.assertEqual(expected, base_string)
def test_signature_base_string(self): uri = "https://api.mastercard.com" base_uri = Util.normalize_url(uri) oauth_parameters = OAuthParameters() oauth_parameters.set_oauth_body_hash("body/hash") oauth_parameters.set_oauth_nonce("randomnonce") base_string = OAuth.get_base_string(base_uri, "POST", oauth_parameters.get_base_parameters_dict()) self.assertEqual("POST&https%3A%2F%2Fapi.mastercard.com%2F&oauth_body_hash%3Dbody%2Fhash%26oauth_nonce%3Drandomnonce", base_string);
def test_sign_json_body(self): uri = "https://sandbox.api.mastercard.com/restservices/clients" consumer_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" method = "POST" body = { "clientBaseAddressData": { "addressLine1": "Testowa 5", "city": "Warszawa", "postalCode": "23-456" }, "clientContactData": { "phoneNumberMobile": "test" }, "clientNumber": "2019102301", "clientPersonalData": { "countryCode": "BEL", "shortName": "test" }, "embossedData": { "embossedFirstName": "Pavel", "embossedLastName": "TEST" }, "clientType": "PR" } oauth_parameters = OAuthParameters() oauth_parameters.set_oauth_consumer_key(consumer_key) oauth_parameters.set_oauth_nonce("1111111111111111111") oauth_parameters.set_oauth_timestamp('1111111111') oauth_parameters.set_oauth_signature_method("RSA-SHA256") oauth_parameters.set_oauth_version("1.0") oauth_parameters.set_oauth_body_hash( OAuth().get_encoded_body_hash(body)) base_string = OAuth.get_base_string( uri, method, oauth_parameters.get_base_parameters_dict()) signed_body = OAuth.sign_message(self, base_string, self.signing_key) self.assertEqual( "F3zw3Cqjqx3bsHM9BItsqwGkZx1esgsmyUIr8G1/ydbMSvnPzTJ6OeTBhlgln4R7MybyxErUbTaiuRRMD8z6P4WQ/QIRzZefqvcDBJ1e/jgmPIvGUZmM9FsQDRZ1EaTVNIGVfxZDbJS1b7114JtxeCWeAuM/O3Si3EzFNbzQSZr17Cma6qxojv63fKWqd8NqGmq3X5ngeA1/4bo8xveBZO3iSamFjJW9H6Gf8P++paP0+ORJ4YLQ1KQR5hmP53b53fPrXk5/06CmoMGltfHJvrUE8XUCBS/Y8bJehoCw4930VVCtCQ5FBmnX0W5kY/XEoWaHWiYmOIK7QmBztSd2zQ==", signed_body)