def __init__(self,
                 integrator,
                 shopping_cart_extension=None,
                 additional_request_headers=()):
        MetaDataProvider.__validate_additional_request_headers(
            additional_request_headers)
        for i in additional_request_headers:
            i.name = re.sub(r'\r?\n(?:(?![\r\n])\s)*', " ", i.name)
            i.name = i.name.strip()
            i.value = re.sub(r'\r?\n(?:(?![\r\n])\s)*', " ", i.value)
            i.value = i.value.strip()
        server_meta_info = self.ServerMetaInfo()
        server_meta_info.platform_identifier = self._platform_identifier
        server_meta_info.sdk_identifier = self._sdk_identifier
        server_meta_info.sdk_creator = "Ingenico"
        server_meta_info.integrator = integrator
        server_meta_info.shopping_cart_extension = shopping_cart_extension

        server_meta_info_string = DefaultMarshaller.INSTANCE().marshal(
            server_meta_info)
        server_meta_info_header = RequestHeader(
            self.__SERVER_META_INFO_HEADER,
            b64encode(server_meta_info_string.encode('utf-8')))
        if not additional_request_headers:
            self.__meta_data_headers = tuple([server_meta_info_header])
        else:
            request_headers = [server_meta_info_header]
            request_headers.extend(additional_request_headers)
            self.__meta_data_headers = tuple(request_headers)
    def assertServerMetaInfo(self,
                             meta_data_provider,
                             integrator,
                             shopping_cart_extension=None,
                             request_header=None):
        """Assert that checks that the request_header is the default header "X-GCS-ServerMetaInfo",
        that the server_meta_data_info of the meta_data_provider is correct
        and that the shopping cart extension is consistent with the extension stored in meta_data_provider
        """
        self.assertEqual("X-GCS-ServerMetaInfo", request_header.name)
        self.assertIsNotNone(request_header.value)

        # server_meta_info is stored in json format and encoded using utf-8 and base64 encoding, decode it
        server_meta_info_json = base64.b64decode(
            request_header.value).decode('utf-8')
        server_meta_info = DefaultMarshaller.INSTANCE().unmarshal(
            server_meta_info_json, MetaDataProvider.ServerMetaInfo)
        self.assertEqual(meta_data_provider._platform_identifier,
                         server_meta_info.platform_identifier)
        self.assertEqual(meta_data_provider._sdk_identifier,
                         server_meta_info.sdk_identifier)
        self.assertEqual("Ingenico", server_meta_info.sdk_creator)
        self.assertEqual(integrator, server_meta_info.integrator)
        if shopping_cart_extension is None:
            self.assertIsNone(server_meta_info.shopping_cart_extension)
        else:
            self.assertEqual(shopping_cart_extension.creator,
                             server_meta_info.shopping_cart_extension.creator)
            self.assertEqual(shopping_cart_extension.name,
                             server_meta_info.shopping_cart_extension.name)
            self.assertEqual(shopping_cart_extension.version,
                             server_meta_info.shopping_cart_extension.version)
            self.assertEqual(
                shopping_cart_extension.extension_id,
                server_meta_info.shopping_cart_extension.extension_id)
Exemplo n.º 3
0
 def create_helper_builder(secret_key_store):
     """
     Creates a WebhooksHelperBuilder that will use the given SecretKeyStore.
     """
     return WebhooksHelperBuilder().with_marshaller(
         DefaultMarshaller.INSTANCE()).with_secret_key_store(
             secret_key_store)
    def test_create_communicator(self):
        """Tests that the factory is correctly able to create a communicator"""
        communicator = Factory.create_communicator_from_file(
            PROPERTIES_URI, API_KEY_ID, SECRET_API_KEY)

        self.assertIs(communicator.marshaller, DefaultMarshaller.INSTANCE())

        session = communicator._Communicator__session
        connection = session.connection
        self.assertIsInstance(connection, DefaultConnection)
        DefaultConnectionTest.assertConnection(self, connection, None, None,
                                               100, None)

        authenticator = session.authenticator
        self.assertIsInstance(authenticator, DefaultAuthenticator)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.assertEqual(
                AuthorizationType.V1HMAC,
                authenticator._DefaultAuthenticator__authorization_type)
            self.assertEqual(API_KEY_ID,
                             authenticator._DefaultAuthenticator__api_id_key)
            self.assertEqual(
                SECRET_API_KEY,
                authenticator._DefaultAuthenticator__secret_api_key)

        meta_data_provider = session.meta_data_provider
        self.assertIsInstance(meta_data_provider, MetaDataProvider)
        request_headers = meta_data_provider.meta_data_headers
        self.assertEqual(1, len(request_headers))
        self.assertEqual("X-GCS-ServerMetaInfo", request_headers[0].name)
    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", "é&%=")
        ]
        patcher = patch(
            'ingenico.connect.sdk.session.Session',
            api_endpoint=urlparse("https://api-sandbox.globalcollect.com"))
        session = patcher.start()
        communicator = Communicator(session, DefaultMarshaller.INSTANCE())

        uri1 = communicator._to_absolute_uri("v1/merchant/20000/convertamount",
                                             requestparams)
        uri2 = communicator._to_absolute_uri(
            "/v1/merchant/20000/convertamount", requestparams)

        self.assertEqual(
            "https://api-sandbox.globalcollect.com/v1/merchant/20000/convertamount"
            "?amount=123&source=USD&target=EUR&dummy=%C3%A9%26%25%3D",
            uri1.geturl())
        self.assertEqual(
            "https://api-sandbox.globalcollect.com/v1/merchant/20000/convertamount"
            "?amount=123&source=USD&target=EUR&dummy=%C3%A9%26%25%3D",
            uri2.geturl())
Exemplo n.º 6
0
    def test_unmarshal_extended_token_response(self):
        """Tests if the marshaller is able to marshal an object that inherits from token_response
        in such a way that it can be interpreted as a token_response
        """
        token_response = TokenResponseWithExtraField()
        billing_address = Address()
        billing_address.country_code = "NL"
        customer = CustomerToken()
        customer.billing_address = billing_address
        card = TokenCard()
        card_data = TokenCardData()
        card.customer = customer
        card.data = card_data
        token_response.card = card
        token_response.extraField = "a random string"
        marshaller = DefaultMarshaller.INSTANCE()
        # Marshal the extended token response and unmarshal as a regular token response
        json = marshaller.marshal(token_response)
        unmarshalled = marshaller.unmarshal(json, TokenResponse)

        self.assertIsNotNone(unmarshalled.card)
        self.assertIsNotNone(unmarshalled.card.customer)
        self.assertIsNotNone(unmarshalled.card.customer.billing_address)
        self.assertEqual(
            "NL", unmarshalled.card.customer.billing_address.country_code)
        self.assertIsNotNone(unmarshalled.card.data)
Exemplo n.º 7
0
 def create_communicator_from_configuration(communicator_configuration):
     """
     Create a Communicator based on the configuration stored in the
     CommunicatorConfiguration argument
     """
     session = Factory.create_session_from_configuration(
         communicator_configuration)
     return Communicator(session, DefaultMarshaller.INSTANCE())
Exemplo n.º 8
0
 def create_communicator_from_file(configuration_file_name, api_key_id,
                                   secret_api_key):
     """
     Creates a Communicator based on the configuration values in
     configuration_file_name, api_key_id and secret_api_key.
     """
     configuration = Factory.create_configuration(configuration_file_name,
                                                  api_key_id, secret_api_key)
     session = Factory.create_session_from_configuration(configuration)
     return Communicator(session, DefaultMarshaller.INSTANCE())
Exemplo n.º 9
0
    def test_with_client_meta_info(self):
        """Tests if the function withClientMetaInfo alters a client when it needs to and does nothing if not required"""

        client1 = Factory.create_client_from_file(PROPERTIES_URI, API_KEY_ID, SECRET_API_KEY)
        # client2 = client1.with_client_meta_info(None)
        client2 = client1.with_client_meta_info(None)
        client_meta_info = DefaultMarshaller.INSTANCE().marshal({"test": "test"})
        client3 = client1.with_client_meta_info(client_meta_info)
        client4 = client3.with_client_meta_info(client_meta_info)
        client5 = client3.with_client_meta_info(None)

        self.assertIsNone(client1._client_headers)
        self.assertIs(client1, client2)
        self.assertIsNot(client1, client3)
        self.assertClientHeaders(client3, client_meta_info)
        self.assertIs(client3, client4)
        self.assertIsNot(client3, client5)
        self.assertIsNone(client5._client_headers)
    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"""
        patcher = patch(
            'ingenico.connect.sdk.session.Session',
            api_endpoint=urlparse("https://api-sandbox.globalcollect.com"))
        session = patcher.start()
        communicator = Communicator(session, DefaultMarshaller.INSTANCE())

        uri1 = communicator._to_absolute_uri("v1/merchant/20000/convertamount",
                                             [])
        uri2 = communicator._to_absolute_uri(
            "/v1/merchant/20000/convertamount", [])

        self.assertEqual(
            "https://api-sandbox.globalcollect.com/v1/merchant/20000/convertamount",
            uri1.geturl())
        self.assertEqual(
            "https://api-sandbox.globalcollect.com/v1/merchant/20000/convertamount",
            uri2.geturl())
Exemplo n.º 11
0
    def test_unmarshal_with_extra_fields(self):
        """Tests if the marshaller is able to marshal an object and unmarshal it as an instance of a parent class"""
        dummy_object = JsonDummyExtended()
        mini_dummy = JsonMiniDummy()
        mini_mini_dummy = JsonMiniMiniDummy()
        mini_mini_dummy.foo = "hiddenfoo"
        mini_dummy.foo = mini_mini_dummy
        dummy_object.foo = mini_dummy
        dummy_object.bar = True
        dummy_object.boo = 0o1
        dummy_object.far = "close"
        dummy_object.extra_field = "something else"
        marshaller = DefaultMarshaller.INSTANCE()

        json = marshaller.marshal(dummy_object)
        unmarshalled = marshaller.unmarshal(json, JsonDummy)

        self.assertEqual(True, unmarshalled.bar)
        self.assertEqual(0o1, unmarshalled.boo)
        self.assertEqual("close", unmarshalled.far)
        self.assertEqual("hiddenfoo", unmarshalled.foo.foo.foo)
Exemplo n.º 12
0
 def create_communicator_from_session(session):
     """
     Create a Communicator based on the settings stored in the Session
     argument
     """
     return Communicator(session, DefaultMarshaller.INSTANCE())
 def __create_helper(self, marshaller=DefaultMarshaller.INSTANCE()):
     return WebhooksHelper(marshaller, InMemorySecretKeyStore.INSTANCE())
 def test_create_helper(self):
     helper = Webhooks.create_helper(InMemorySecretKeyStore.INSTANCE())
     self.assertIs(DefaultMarshaller.INSTANCE(), helper.marshaller)
     self.assertIs(InMemorySecretKeyStore.INSTANCE(),
                   helper.secret_key_store)