Example #1
0
    def test_get_server_metadata_headers_no_additional_headers(self):
        """Tests that the MetaDataProvider functions correctly without any additional headers as arguments"""
        meta_data_provider = MetaDataProvider("OnlinePayments")

        request_headers = meta_data_provider.meta_data_headers
        self.assertEqual(1, len(request_headers))
        self.assertServerMetaInfo(meta_data_provider, "OnlinePayments", None, request_headers[0])
Example #2
0
    def test_to_uri_with_request_parameters(self):
        """Tests if the communicator can correctly construct an url using a known base url, a relative url and a list of request parameters"""
        requestParams = [
            RequestParam("amount", "123"),
            RequestParam("source", "USD"),
            RequestParam("target", "EUR"),
            RequestParam("dummy", "é&%=")
        ]
        communicator = Communicator(
            api_endpoint=urlparse("https://example.com"),
            authenticator=DefaultAuthenticator("a_key", "a_secret"),
            meta_data_provider=MetaDataProvider("OnlinePayments"))

        uri1 = communicator._to_absolute_uri("v2/1/testconnection",
                                             requestParams)
        uri2 = communicator._to_absolute_uri("/v2/1/testconnection",
                                             requestParams)

        self.assertEqual(
            "https://example.com/v2/1/testconnection"
            "?amount=123&source=USD&target=EUR&dummy=%C3%A9%26%25%3D",
            uri1.geturl())
        self.assertEqual(
            "https://example.com/v2/1/testconnection"
            "?amount=123&source=USD&target=EUR&dummy=%C3%A9%26%25%3D",
            uri2.geturl())
Example #3
0
 def setUp(self):
     self.mock_connection = MagicMock(spec=Connection(), autospec=True)
     self.communicator = Communicator(
         api_endpoint="http://localhost",
         authenticator=DefaultAuthenticator("admin", "admin"),
         meta_data_provider=MetaDataProvider("OnlinePayments"),
         connection=self.mock_connection)
Example #4
0
    def test_server_meta_data_headers_full_no_shopping_cart_extension_id(self):
        """Tests that the MetaDataProvider can construct meta_data_headers when supplied with a full shopping cart"""
        shopping_cart_extension = ShoppingCartExtension("OnlinePayments.creator", "Extension", "1.0")
        meta_data_provider = MetaDataProvider("OnlinePayments", shopping_cart_extension)

        request_headers = meta_data_provider.meta_data_headers
        self.assertEqual(1, len(request_headers))
        self.assertServerMetaInfo(meta_data_provider, "OnlinePayments", shopping_cart_extension, request_headers[0])
Example #5
0
 def test_constructor_with_prohibited_headers(self):
     """Tests that the MetaDataProvider constructor does not accept any headers marked as prohibited"""
     for name in MetaDataProvider.prohibited_headers:
         additional_headers = [RequestHeader("Header1", "Value1"),
                               RequestHeader(name, "should be slashed and burnt"),
                               RequestHeader("Header3", "Value3")]
         with self.assertRaises(Exception) as error:
             MetaDataProvider("OnlinePayments", None, additional_headers)
         self.assertIn(name, str(error.exception))
Example #6
0
    def test_get_server_metadata_headers_additional_headers(self):
        """Tests that the MetaDataProvider can handle multiple additional headers"""
        additional_headers = [RequestHeader("Header1", "&=$%"), RequestHeader("Header2", "blah blah"),
                              RequestHeader("Header3", "foo")]
        meta_data_provider = MetaDataProvider("OnlinePayments", None, additional_headers)
        request_headers = meta_data_provider.meta_data_headers

        self.assertEqual(4, len(request_headers))

        for index in range(1, 4):
            self.assertEqual(additional_headers[index - 1].name, request_headers[index].name)
            self.assertEqual(additional_headers[index - 1].value, request_headers[index].value)
Example #7
0
def create_client(
        http_host,
        connect_timeout=0.500,
        socket_timeout=0.500,
        max_connections=EndpointConfiguration.DEFAULT_MAX_CONNECTIONS):
    connection = DefaultConnection(connect_timeout, socket_timeout,
                                   max_connections)
    authenticator = DefaultAuthenticator("apiKey", "secret")
    meta_data_provider = MetaDataProvider("the Online Payments")
    communicator = Communicator(api_endpoint=http_host,
                                authenticator=authenticator,
                                meta_data_provider=meta_data_provider,
                                connection=connection)
    return Factory.create_client_from_communicator(communicator)
Example #8
0
    def test_to_uri_without_request_parameters(self):
        """Tests if the communicator can correctly construct an url using a known base url and a relative url"""
        communicator = Communicator(
            api_endpoint=urlparse("https://example.com"),
            authenticator=DefaultAuthenticator("a_key", "a_secret"),
            meta_data_provider=MetaDataProvider("OnlinePayments"))

        uri1 = communicator._to_absolute_uri("v2/merchant/1/testconnection",
                                             [])
        uri2 = communicator._to_absolute_uri("/v2/merchant/1/testconnection",
                                             [])

        self.assertEqual("https://example.com/v2/merchant/1/testconnection",
                         uri1.geturl())
        self.assertEqual("https://example.com/v2/merchant/1/testconnection",
                         uri2.geturl())
Example #9
0
 def create_communicator_from_configuration(
     communicator_configuration,
     meta_data_provider=None,
     connection=None,
     authenticator=None,
     marshaller=DefaultMarshaller.INSTANCE()
 ) -> Communicator:
     """
     Creates a Communicator based on the configuration stored in the CommunicatorConfiguration argument
     """
     # If an authenticator is not given, api_key_id and secret_api_key are
     # used to create a DefaultAuthenticator used to create the communicator.
     if connection is None:
         connection = DefaultConnection(
             connect_timeout=communicator_configuration.connect_timeout,
             socket_timeout=communicator_configuration.socket_timeout,
             max_connections=communicator_configuration.max_connections,
             proxy_configuration=communicator_configuration.
             proxy_configuration)
     if authenticator is None:
         authenticator = DefaultAuthenticator(
             api_key_id=communicator_configuration.api_key_id,
             secret_api_key=communicator_configuration.secret_api_key,
             authorization_type=communicator_configuration.
             authorization_type)
     if meta_data_provider is None:
         meta_data_provider = MetaDataProvider(
             integrator=communicator_configuration.integrator,
             shopping_cart_extension=communicator_configuration.
             shopping_cart_extension)
     return Communicator(
         api_endpoint=communicator_configuration.api_endpoint,
         authenticator=authenticator,
         connection=connection,
         meta_data_provider=meta_data_provider,
         marshaller=marshaller)