def test__verifyResult(self): tests = [ ({ "requestreference": "Ahc6uwqq6" }, "Ahc6uwqq6", None, None, None, None), ({}, "Ahc6uwqq6", securetrading.SecureTradingError, [ 'Different request reference: sent \ \(Ahc6uwqq6\) received \(None\)' ], "9 Different request reference: sent (Ahc6uwqq6) received \ (None)", "9"), ({ "requestreference": "Arckthaau" }, "Ahc6uwqq6", securetrading.SecureTradingError, [ 'Different request reference: sent \(Ahc6uwqq6\) received\ \(Arckthaau\)' ], "9 Different request reference: sent (Ahc6uwqq6) \ received (Arckthaau)", "9"), ] config = self.get_config() api = securetrading.Api(config) for result, ref, exp_exception, exp_data, exp_eng, exp_code in tests: verify_args = (result, ref) if exp_exception is None: api._verify_result(*verify_args) else: self.check_st_exception(exp_exception, exp_data, exp_eng, exp_code, api._verify_result, func_args=verify_args)
def test__generateError(self): tests = [ (KeyError("A Key Error Occured"), "Ahc6uwqq6", { "requestreference": "Ahc6uwqq6", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Ahc6uwqq6", "errorcode": "9", "errordata": ["A Key Error Occured"] }] }), (AssertionError("An Assertion Error Occured"), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "9", "errordata": ["An Assertion Error Occured"] }] }), (ValueError("A Value Error Occured"), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "9", "errordata": ["A Value Error Occured"] }] }), ] for exception, request_reference, exp_update in tests: config = self.get_config() api = securetrading.Api(config) actual = api._generate_error(exception, request_reference) exp_response = securetrading.Response() exp_response.update(exp_update) self.assertEqual(actual, exp_response)
def test__verify_request(self): request = securetrading.Request() tests = [ ({}, securetrading.SecureTradingError, ["Incorrect type of request specified" ], "10 Incorrect type of request specified", "10"), (request, None, None, None, None), ] config = self.get_config() api = securetrading.Api(config) for request, exp_exception, exp_data, exp_eng, exp_code in tests: if exp_exception is None: api._verify_request(request) else: self.check_st_exception(exp_exception, exp_data, exp_eng, exp_code, api._verify_request, func_args=(request, ))
#!/usr/bin/env python import logging import pprint import securetrading logging.basicConfig(filename='example.log', level=logging.INFO) # Configure global settings st_config = securetrading.Config() st_config.username = "******" st_config.password = "******" # create the Api object with the config st_api = securetrading.Api(st_config) # Create the request dictionary. AUTH_example = { "pan": "4111111111111111", "expirydate": "11/2031", "securitycode": "123", "requesttypedescription": "AUTH", "accounttypedescription": "ECOM", "sitereference": "YOURSITEREF", "paymenttypedescription": "VISA", "currencyiso3a": "GBP", "baseamount": "100", "billingfirstname": "first", } # Process the request or more requests st_response = st_api.process(AUTH_example)
def test__generateStError(self): sterror = securetrading.SecureTradingError apierror = securetrading.ApiError connecterror = securetrading.ConnectionError sendrecverror = securetrading.SendReceiveError tests = [ ({}, sterror("9", data=["Different request reference"]), "Ahc6uwqq6", { "requestreference": "Ahc6uwqq6", "responses": [{ "requestreference": "Ahc6uwqq6", "errorcode": "9", "errordata": ["Different request reference"], "requesttypedescription": "ERROR" }] }), ({}, sterror("9", data=["Different Issue"]), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "9", "errordata": ["Different Issue"] }] }), ({}, sterror("1", data=["Generic Issue"]), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "1", "errordata": ["Generic Issue"] }] }), ({}, sterror("2", data=["An Issue"]), "Ahc6uwqq6", { "requestreference": "Ahc6uwqq6", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Ahc6uwqq6", "errorcode": "2", "errordata": ["An Issue"] }] }), ({}, sendrecverror("4", data=["Send Issue"]), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "4", "errordata": ["Send Issue"] }] }), ({}, sendrecverror("5", data=["Receive Issue"]), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "5", "errordata": ["Receive Issue"] }] }), ({}, connecterror("7", data=["Connection Issue"]), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "7", "errordata": ["Connection Issue"] }] }), ({}, connecterror("8", data=["Unexpected Connection Issue"]), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "8", "errordata": ["Unexpected Connection Issue"] }] }), ({ "locale": "fr_fr" }, apierror("10", data=["Api Issue"]), "Ahc6uwqq6", { u'requestreference': u'Ahc6uwqq6', u'responses': [{ 'errorcode': u'10', 'requesttypedescription': u'ERROR', 'requestreference': u'Ahc6uwqq6', 'errordata': [u'Api Issue'], }] }), ] for configData, exception, request_reference, exp_update in tests: config = self.get_config(data=configData) api = securetrading.Api(config) actual = api._generate_st_error(exception, request_reference) exp_response = securetrading.Response() exp_response.update(exp_update) self.assertEqual(actual, exp_response)
def test_process(self): sterror = securetrading.SecureTradingError apierror = securetrading.ApiError connecterror = securetrading.ConnectionError sendrecverror = securetrading.SendReceiveError request1 = self.get_securetrading_request({}) request4 = self.get_securetrading_request({}) default_config = self.get_config() config1 = self.get_config({ "username": "******", "jsonversion": "2.00", "datacenterurl": "https://test.com", "http_response_headers": ["headers"], }) french_config = self.get_config({ "locale": "fr_fr", }) german_config = self.get_config({ "locale": "de_de", }) request2 = self.get_securetrading_request( {"datacenterurl": "https://somewhere.com"}) request1_str = '{{"requestreference":"{0}",\ "version":"1.00", "response":[{{"errorcode" : "0"}}]}}' request1_str2 = '{{"requestreference":"{0}",\ "version":"1.00", "response":[]}}' request1_dict = { "requestreference": request1["requestreference"], "versioninfo": self.version_info } request2_str = '{{"requestreference": "{0}",\ "version": "1.00", "response": [{{"errorcode" : "0"}}]}}' msg = "{0} Maximum time reached whilst trying to connect to {1}\ ".format(request2["requestreference"], request2["datacenterurl"]) connection_error = connecterror("7", data=[msg]) python_version = self.get_python_version() jsonMessage = ['No JSON object could be decoded'] request3 = self.get_securetrading_request( {"requestreference": "requestreference"}) request4_dict = { "requestreference": request4["requestreference"], "versioninfo": self.version_info } request4_str = '{{"requestreference": "{0}",\ "version": "1.00", "response": [{{"errormessage": "GATEWAYERRMSG",\ "errorcode" : "99"}}]}}' if python_version >= 3: jsonMessage = ["Expecting value: line 1 column 21 (char 20)"] test_cases = [ (request1, default_config, request1_str.format(request1["requestreference"]), { "Headers": "data" }, None, "https://webservices.securetrading.net/json/", { "alias": "", "libraryversion": self.lib_version, "request": [request1_dict], "version": "1.00" }, None, None, { "requestreference": request1["requestreference"], "version": "1.00", "responses": [{ "errormessage": "Ok", "errorcode": "0" }] }, None), # config with new values instead of defaults (request1, config1, request1_str.format(request1["requestreference"]), { "Headers": "data" }, None, "https://test.com/json/", { "alias": "test", "libraryversion": self.lib_version, "request": [request1_dict], "version": "2.00" }, None, None, { "requestreference": request1["requestreference"], "version": "1.00", "responses": [ { "errormessage": "Ok", "errorcode": "0" }, ] }, { "Headers": "data" }), # config with new values instead of defaults (request1, config1, request1_str2.format(request1["requestreference"]), { "Accept": "*/*" }, None, "https://test.com/json/", { "alias": "test", "libraryversion": self.lib_version, "request": [request1_dict], "version": "2.00" }, None, None, { "requestreference": request1["requestreference"], 'version': '1.00', "responses": [] }, { "Accept": "*/*" }), # Invalid response example (request2, config1, request2_str.format(request2["requestreference"]), { "Headers": "data" }, None, "https://somewhere.com/json/", { "alias": "test", "libraryversion": self.lib_version, "request": [{ "requestreference": request2["requestreference"], "datacenterurl": "https://somewhere.com", "versioninfo": self.version_info, }], "version": "2.00" }, None, None, { 'requestreference': request2["requestreference"], 'version': '1.00', 'responses': [{ "errormessage": "Ok", "errorcode": "0" }] }, { "Headers": "data" }), # config with new values and request overrides # datacenterurl ([], default_config, None, { "Headers": "data" }, None, None, None, None, None, { 'requestreference': '', 'responses': [ { 'errorcode': '10', 'errordata': ['Incorrect type of \ request specified'], 'requesttypedescription': 'ERROR', 'requestreference': '', 'errormessage': 'Incorrect usage of the \ Secure Trading API' }, ] }, None), ([], french_config, None, { "Headers": "data" }, None, None, None, None, None, { 'requestreference': '', 'responses': [ { 'errorcode': '10', 'errordata': ['Incorrect type of \ request specified'], 'requesttypedescription': 'ERROR', 'requestreference': '', 'errormessage': "Utilisation incorrecte de \ l'API Secure Trading" }, ] }, None), # French version of the config ([], german_config, None, { "Headers": "data" }, None, None, None, None, None, { 'requestreference': '', 'responses': [ { 'errorcode': '10', 'errordata': ['Incorrect type of \ request specified'], 'requesttypedescription': 'ERROR', 'requestreference': '', 'errormessage': 'Fehlerhafte Verwendung der Secure \ Trading API' }, ] }, None), # German version of the config (request2, default_config, None, { "Headers": "data" }, connecterror("7"), None, None, None, None, { "requestreference": request2["requestreference"], "responses": [{ "requesttypedescription": "ERROR", "requestreference": request2["requestreference"], "errorcode": "7", "errormessage": "An issue occured whilst trying to \ connect to Secure Trading servers", "errordata": [] }] }, None), (request2, default_config, None, { "Headers": "data" }, sendrecverror("4", connection_error), None, None, None, None, { "requestreference": request2["requestreference"], "responses": [{ "requesttypedescription": "ERROR", "requestreference": request2["requestreference"], 'errorcode': "4", 'errormessage': 'Send error', "errordata": [ '7 {0} Maximum time reached whilst trying to \ connect to https://somewhere.com'.format(request2["requestreference"]) ] }] }, None), (request1, default_config, '{"requestreference": \ "NEWREFERENCE-866-98", "version": "1.00", "response": [{"errorcode" : "0" }]}', { "Headers": "data" }, None, "https://webservices.securetrading.net/json/", { "alias": "", "libraryversion": self.lib_version, "request": [{ "requestreference": request1["requestreference"], "versioninfo": self.version_info }], "version": "1.00" }, None, None, { "requestreference": request1["requestreference"], "responses": [{ "requesttypedescription": "ERROR", "requestreference": request1["requestreference"], 'errorcode': "9", 'errormessage': 'Unknown error. If this persists\ please contact Secure Trading', 'errordata': [ 'Different request reference: sent ({0}) received\ (NEWREFERENCE-866-98)'.format(request1["requestreference"]) ] }] }, None), (request1, default_config, '{"requestreference":: \ "NEWREFERENCE-866-98", "version": "1.00", "response": []}', { "Headers": "data" }, None, "https://webservices.securetrading.net/json/", { "alias": "", "libraryversion": self.lib_version, "request": [{ "requestreference": request1["requestreference"], "versioninfo": self.version_info }], "version": "1.00" }, None, None, { "requestreference": request1["requestreference"], "responses": [{ "requesttypedescription": "ERROR", "requestreference": request1["requestreference"], 'errorcode': "5", 'errormessage': 'Receive error', 'errordata': jsonMessage, }] }, None), ({ "requestreference": "myref" }, default_config, request1_str.format("myref"), { "Headers": "data" }, None, "https://webservices.securetrading.net/json/", { "alias": "", "libraryversion": self.lib_version, "request": [{ "requestreference": "myref", "versioninfo": self.version_info, }], "version": "1.00" }, None, None, { "requestreference": "myref", "version": "1.00", "responses": [{ "errormessage": "Ok", "errorcode": "0" }] }, None), (request4, default_config, request4_str.format(request4["requestreference"]), { "Headers": "data" }, None, "https://webservices.securetrading.net/json/", { "alias": "", "libraryversion": self.lib_version, "request": [request4_dict], "version": "1.00" }, None, None, { "requestreference": request4["requestreference"], "version": "1.00", "responses": [{ "errormessage": "GATEWAYERRMSG", "errorcode": "99" }] }, None), ] http_main = st_httpclient.GenericHTTPClient._main try: for (request, config, http_result, http_headers, http_raises, http_url, http_json_dict, exp_raises, exp_message, exp_result, exp_headers) in test_cases: st_httpclient.GenericHTTPClient._main = self.mock_method( result=(http_result, http_headers), exception=http_raises) api = securetrading.Api(config=config) if exp_raises is None: actual = api.process(request) if http_result is not None: sent_to_mock = self.mock_receive actual_url = sent_to_mock[0][0][1] actual_json_dict = json.loads(sent_to_mock[0][0][2]) self.assertEqual(actual_url, http_url) self.assertEqual(actual_json_dict, http_json_dict) self.assertEqual(actual.pop("headers", None), exp_headers) self.assertEqual(actual, exp_result) else: self.assertRaisesRegexp(exp_raises, exp_message, api.process, request) finally: securetrading.httpclient.GenericHTTPClient._main = http_main
def __init__(self, *args, **kwargs): super(Module_Test_Api, self).__init__(*args, **kwargs) self.sitereference = passed_args.sitereference # Please contact Secure Trading support to set up a test site # The following options are required. # A currency rate account for USD Visa # test account with the following: # GBP ECOM VISA # GBP MOTO VISA # dcc USD ECOM VISA # GBP ECOM PAYPAL # EUR ECOM SOFORT # USD ECOM ACH # Fraud Control # Identity Check # GBP CFT VISA # GBP RECUR VISA # The site also needs a webservices user set up via MySt. username = passed_args.username password = passed_args.password datacenterurl = passed_args.datacenterurl ssl_cert_file = passed_args.overridecacerts base_path = self.get_package_path() # st_api valid credentials st_config = securetrading.Config() st_config.username = username st_config.password = password st_config.ssl_certificate_file = ssl_cert_file st_config.datacenterurl = datacenterurl # st_api2 is intentionally using bad credentials. st_config2 = securetrading.Config() st_config2.username = "******" st_config2.password = "******" st_config2.ssl_certificate_file = ssl_cert_file st_config2.datacenterurl = datacenterurl # st_api3 is intentionally using a corrupt cacerts file. st_config3 = securetrading.Config() st_config3.username = username st_config3.password = password st_config3.ssl_certificate_file = os.path.join(base_path, "test/badcacert.pem") st_config3.datacenterurl = datacenterurl # st_api4 is intentionally using a cacerts file that is not our ca. st_config4 = securetrading.Config() st_config4.username = username st_config4.password = password st_config4.ssl_certificate_file = os.path.join(base_path, "test/testcacert.pem") st_config4.datacenterurl = datacenterurl # st_api valid credentials fr setup st_config_fr = securetrading.Config() st_config_fr.username = username st_config_fr.password = password st_config_fr.ssl_certificate_file = ssl_cert_file st_config_fr.locale = "fr_fr" st_config_fr.datacenterurl = datacenterurl # st_api valid credentials de setup st_config_de = securetrading.Config() st_config_de.username = username st_config_de.password = password st_config_de.ssl_certificate_file = ssl_cert_file st_config_de.locale = "de_de" st_config_de.datacenterurl = datacenterurl # st_api valid credentials with Content-type response header st_config_content_type = securetrading.Config() st_config_content_type.username = username st_config_content_type.password = password st_config_content_type.ssl_certificate_file = ssl_cert_file st_config_content_type.http_response_headers = ["content-type"] st_config_content_type.datacenterurl = datacenterurl # initialise all the st_api objects with their respective config self.st_api = securetrading.Api(st_config) self.st_api2 = securetrading.Api(st_config2) self.st_api3 = securetrading.Api(st_config3) self.st_api4 = securetrading.Api(st_config4) self.st_api_fr = securetrading.Api(st_config_fr) self.st_api_de = securetrading.Api(st_config_de) self.st_api_content_type = securetrading.Api(st_config_content_type) if self.PARENT_RESPONSES is None: # Singleton so that it doenst get set always Module_Test_Api.PARENT_RESPONSES = self.set_up_parents() # Sleeping to allow parent transactions to be fully processed transfersleeptime = passed_args.transfersleeptime print( "Sleeping for {0:d} seconds to allow parent transactions to be\ fully processed".format(transfersleeptime)) time.sleep(transfersleeptime)