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_query_parser_encoding(self): uri = "https://sandbox.api.mastercard.com?param1=plus+value¶m2=colon:value" oauth_parameters = OAuthParameters() oauth_parameters_base = oauth_parameters.get_base_parameters_dict() merge_parameters = oauth_parameters_base.copy() query_params = Util.normalize_params(uri, merge_parameters) self.assertEqual(query_params, "param1=plus%20value¶m2=colon%3Avalue")
def test_query_parser(self): uri = "https://sandbox.api.mastercard.com/audiences/v1/getcountries?offset=0&offset=1&length=10&empty&odd=" oauth_parameters = OAuthParameters() oauth_parameters_base = oauth_parameters.get_base_parameters_dict() merge_parameters = oauth_parameters_base.copy() query_params = Util.normalize_params(uri, merge_parameters) self.assertEqual(query_params, "empty=&length=10&odd=&offset=0&offset=1")
def test_params_string_ascending_byte_value_ordering(self): url = "https://localhost?b=b&A=a&A=A&B=B&a=A&a=a&0=0" oauth_parameters = OAuthParameters() oauth_parameters_base = oauth_parameters.get_base_parameters_dict() merge_parameters = oauth_parameters_base.copy() norm_params = Util.normalize_params(url, merge_parameters) self.assertEqual("0=0&A=A&A=a&B=B&a=A&a=a&b=b", norm_params)
def test_params_string_rfc_example_2(self): uri = "https://sandbox.api.mastercard.com?b5=%3D%253D&a3=a&a3=2%20q&c%40=&a2=r%20b&c2=" oauth_parameters2 = OAuthParameters() oauth_parameters_base2 = oauth_parameters2.get_base_parameters_dict() merge_parameters2 = oauth_parameters_base2.copy() query_params2 = Util.normalize_params(uri, merge_parameters2) self.assertEqual("a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=", query_params2)
def not_consistent_test_query_parser_not_encoded_params(self): uri = "https://api.mastercard.com/audiences?param1=plus+value¶m2=colon:value¶m3=a space~" oauth_parameters = OAuthParameters() oauth_parameters_base = oauth_parameters.get_base_parameters_dict() merge_parameters = oauth_parameters_base.copy() query_params = Util.normalize_params(uri, merge_parameters) self.assertEqual( query_params, "param1=plus%20value¶m2=colon%3Avalue¶m3=a%20space~")
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_params_string_rfc_example_1(self): uri = "https://sandbox.api.mastercard.com" oauth_parameters1 = OAuthParameters() oauth_parameters1.set_oauth_consumer_key("9djdj82h48djs9d2") oauth_parameters1.set_oauth_signature_method("HMAC-SHA1") oauth_parameters1.set_oauth_timestamp("137131201") oauth_parameters1.set_oauth_nonce("7d8f3e4a") oauth_parameters_base1 = oauth_parameters1.get_base_parameters_dict() merge_parameters1 = oauth_parameters_base1.copy() query_params1 = Util.normalize_params(uri, merge_parameters1) self.assertEqual("oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201", query_params1);
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)