def get_method_headers(self): logger = self.merchant_config.log try: auth = Authorization() authentication_type = self.merchant_config.authentication_type print("Request Type :" + self.request_type) print(GlobalLabelParameters.CONTENT_TYPE + " :" + GlobalLabelParameters.APPLICATION_JSON) if authentication_type.upper() == GlobalLabelParameters.HTTP.upper(): print(" " + GlobalLabelParameters.USER_AGENT + " : " + GlobalLabelParameters.USER_AGENT_VALUE) print(" MerchantID : " + self.merchant_config.merchant_id) print(" Date : " + self.merchant_config.get_time()) temp_sig = auth.get_token(self.merchant_config, self.date, logger) print("Signature Header :" + str(temp_sig)) print("Host :" + self.merchant_config.request_host) else: temp_sig = auth.get_token(self.merchant_config, self.date, logger) print("Authorization Bearer: " + str(temp_sig.decode("utf-8"))) if self.merchant_config.enable_log is True: logger.info("END> ======================================= ") logger.info("\n") except ApiException as e: authenticationsdk.util.ExceptionAuth.log_exception(logger, e, self.merchant_config) except Exception as e: authenticationsdk.util.ExceptionAuth.log_exception(logger, repr(e), self.merchant_config)
def call_authentication_header(self, method, header_params, body): time = mconfig.get_time() mconfig.request_type_method = method if method.upper() == GlobalLabelParameters.POST or method.upper() == GlobalLabelParameters.PUT or method.upper() == GlobalLabelParameters.PATCH: mconfig.request_json_path_data = body logger = mconfig.log auth = Authorization() token = auth.get_token(mconfig, mconfig.get_time(), logger) if mconfig.authentication_type.upper() == GlobalLabelParameters.HTTP.upper(): header_params['Accept-Encoding'] = '*' header_params['v-c-merchant-id'] = mconfig.merchant_id header_params["Date"] = time header_params["Host"] = mconfig.request_host header_params["User-Agent"] = GlobalLabelParameters.USER_AGENT_VALUE if method.upper() == GlobalLabelParameters.POST or method.upper() == GlobalLabelParameters.PUT or method.upper() == GlobalLabelParameters.PATCH: digest_header = self.set_digest((body)) header_params[ GlobalLabelParameters.DIGEST] = GlobalLabelParameters.DIGEST_PREFIX + digest_header.decode("utf-8") header_params["Signature"] = token elif mconfig.authentication_type.upper() == GlobalLabelParameters.JWT: token = "Bearer " + token.decode("utf-8") header_params['Authorization'] = str(token)
def post_method_headers(self): logger = LogFactory.setup_logger(self.__class__.__name__, self.merchant_config.log_config) try: auth = Authorization() digest = DigestAndPayload() authentication_type = self.merchant_config.authentication_type print("Request Type :" + self.request_type) print(GlobalLabelParameters.CONTENT_TYPE + " :" + GlobalLabelParameters.APPLICATION_JSON) if authentication_type.upper() == GlobalLabelParameters.HTTP.upper(): print(" " + GlobalLabelParameters.USER_AGENT + " : " + GlobalLabelParameters.USER_AGENT_VALUE) print(" MerchantID : " + self.merchant_config.merchant_id) print(" Date : " + self.merchant_config.get_time()) print("digest :" + GlobalLabelParameters.DIGEST_PREFIX + digest.string_digest_generation( self.merchant_config.request_json_path_data).encode("utf-8").decode("utf-8")) temp_sig = auth.get_token(self.merchant_config, self.date) print("Signature Header :" + str(temp_sig)) print("Host :" + self.merchant_config.request_host) else: temp_sig = auth.get_token(self.merchant_config, self.date) print("Authorization Bearer :" + str(temp_sig.encode("utf-8").decode("utf-8"))) if self.merchant_config.log_config.enable_log is True: logger.info("END> ======================================= ") logger.info("\n") except ApiException as e: authenticationsdk.util.ExceptionAuth.log_exception(logger, e, self.merchant_config) except Exception as e: authenticationsdk.util.ExceptionAuth.log_exception(logger, repr(e), self.merchant_config)
def set_signature(self, date_time): # This method calls the Authorization class which inturn decides whether to call HTTP_Signature # or JWT Signature based on the request type authorization = Authorization() temp = authorization.get_token(self.merchantconfig, self.date_time) temp_token = "Bearer " + temp authorization_headers = { GlobalLabelParameters.AUTHORIZATION_BEARER: str(temp_token) } return authorization_headers
def payment_get(self, mconfig): self.logger = LogFactory.setup_logger(self.__class__.__name__, mconfig.log_config) try: authorization = Authorization() authorization.validate_request_type_method(mconfig) # Calls PaymentRequestService class of service of sampleapiclient payment_req_obj = PaymentRequestService() payment_req_obj.payment_request_service(mconfig) except ApiException as e: authenticationsdk.util.ExceptionAuth.log_exception(self.logger, e, mconfig.log_config) except Exception as e: authenticationsdk.util.ExceptionAuth.log_exception(self.logger, repr(e), mconfig.log_config)
def set_signature(self, date_time): # This method calls the Authorization class which inturn decides whether to call HTTP_Signature # or JWT Signature based on the request type authorization = Authorization() signature_header_value = authorization.get_token( self.http_merchant_config, date_time) set_signature_header = { GlobalLabelParameters.SIGNATURE: str(signature_header_value) } return set_signature_header
def payment_get(self, mconfig): logger = mconfig.log try: authorization = Authorization() authorization.validate_request_type_method(mconfig) # Calls PaymentRequestService class of service of sampleapiclient payment_req_obj = PaymentRequestService() payment_req_obj.payment_request_service(mconfig, logger) except ApiException as e: authenticationsdk.util.ExceptionAuth.log_exception( logger, e, mconfig) except Exception as e: authenticationsdk.util.ExceptionAuth.log_exception( logger, repr(e), mconfig)
class TestBasicFunction(unittest.TestCase): def setUp(self): self.func = Authorization() self.merchant_config = MerchantConfiguration() self.date = self.merchant_config.get_time() logging.disable(logging.CRITICAL) def tearDown(self): logging.disable(logging.NOTSET) # This method checks the HTTP_Get token Generation Unit Testing def test_token_for_get_http(self): self.merchant_config.set_merchantconfig(MockData.HTTP_VALUES) self.merchant_config.request_type_method = "GET" self.get_id = "5246387105766473203529" self.merchant_config.request_target = "/pts/v2/payments/" + self.get_id self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the HTTP_Get token Generation when enable_log,Log_Maximum_Size,Log_Directory is None/Empty def test_get_http_enable_log(self): self.merchant_config.set_merchantconfig(MockData.HTTP_DEFAULT_VALUES) self.merchant_config.request_type_method = "GET" self.get_id = "5246387105766473203529" self.merchant_config.request_target = "/pts/v2/payments/" + self.get_id self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.merchant_config.validate_merchant_details( MockData.HTTP_DEFAULT_VALUES, self.merchant_config) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the HTTP_Get token Generation when the run_environment="CyberSource.Environment.PRODUCTION" def test_get_jwt_production_url(self): self.merchant_config.set_merchantconfig( MockData.JWT_VALUES_FOR_PRODUCTION) self.merchant_config.request_type_method = "GET" self.get_id = "5246387105766473203529" self.merchant_config.request_target = "/pts/v2/payments/" + self.get_id self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.merchant_config.validate_merchant_details( MockData.JWT_VALUES_FOR_PRODUCTION, self.merchant_config) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the HTTP_Post token Generation Unit Testing def test_token_for_post_http(self): self.merchant_config.set_merchantconfig(MockData.HTTP_VALUES) self.merchant_config.request_type_method = "POST" self.merchant_config.request_target = "/pts/v2/payments" self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.merchant_config.request_json_path_data = json.dumps( MockData.REQUEST_DATA) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the HTTP_Put token Generation Unit Testing def test_token_for_put_http(self): self.merchant_config.set_merchantconfig(MockData.HTTP_VALUES) self.merchant_config.request_type_method = "PUT" self.merchant_config.request_target = "/reporting/v2/reportSubscriptions/TRRReport?organizationId=testrest" self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.merchant_config.request_json_path_data = json.dumps( MockData.TRR_DATA) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the HTTP_Delete token Generation Unit Testing def test_token_for_delete_http(self): self.merchant_config.set_merchantconfig(MockData.HTTP_VALUES) self.merchant_config.request_type_method = "DELETE" self.merchant_config.request_target = "/reporting/v2/reportSubscriptions/TRRReport?organizationId=testrest/5246387105766473203529" self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the JWT_Get token Generation Unit Testing def test_token_for_get_jwt(self): self.merchant_config.set_merchantconfig(MockData.JWT_VALUES) self.merchant_config.request_type_method = "GET" self.get_id = "5246387105766473203529" self.merchant_config.request_target = "/pts/v2/payments/" + self.get_id self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the JWT_Post token Generation Unit Testing def test_token_for_post_jwt(self): self.merchant_config.set_merchantconfig(MockData.JWT_VALUES) self.merchant_config.request_type_method = "POST" self.merchant_config.request_target = "/pts/v2/payments/5246387105766473203529" self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.merchant_config.request_json_path_data = json.dumps( MockData.REQUEST_DATA) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the JWT_Put token Generation Unit Testing def test_token_for_put_jwt(self): self.merchant_config.set_merchantconfig(MockData.JWT_VALUES) self.merchant_config.request_type_method = "PUT" self.merchant_config.request_target = "/reporting/v2/reportSubscriptions/TRRReport?organizationId=testrest" self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.merchant_config.request_json_path_data = json.dumps( MockData.TRR_DATA) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger)) # This method checks the JWT_Delete token Generation Unit Testing def test_token_for_delete_jwt(self): self.merchant_config.set_merchantconfig(MockData.JWT_VALUES) self.merchant_config.request_type_method = "DELETE" self.merchant_config.request_target = "/reporting/v2/reportSubscriptions/TRRReport?organizationId=testrest/5246387105766473203529" self.logger = authenticationsdk.logger.Log.setup_logger( self.merchant_config) self.assertIsNotNone( self.func.get_token(self.merchant_config, self.date, self.logger))
def setUp(self): self.func = Authorization() self.merchant_config = MerchantConfiguration() self.date = self.merchant_config.get_time() logging.disable(logging.CRITICAL)