def test_to_data_to_sign(self):
        """Tests that the to_data_to_sign function correctly constructs a POST request for multiple headers"""
        authenticator = DefaultAuthenticator(
            AuthorizationType.get_authorization("v1HMAC"), "apiKeyId",
            "secretApiKey")
        http_headers = [
            RequestHeader(
                "X-GCS-ServerMetaInfo",
                "{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 (Oracle Corporation; "
                "Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\",\"sdkIdentifier\":\"1.0\"}"
            ),
            RequestHeader("Content-Type", "application/json"),
            RequestHeader("X-GCS-ClientMetaInfo", "{\"aap\",\"noot\"}"),
            RequestHeader("User-Agent", "Apache-HttpClient/4.3.4 (java 1.5)"),
            RequestHeader("Date", "Mon, 07 Jul 2014 12:12:40 GMT")
        ]
        expected_start = "POST\n" \
                         "application/json\n"
        expected_end = "x-gcs-clientmetainfo:{\"aap\",\"noot\"}\n" \
                       "x-gcs-servermetainfo:{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 " \
                       "(Oracle Corporation; Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\"," \
                       "\"sdkIdentifier\":\"1.0\"}\n" \
                       "/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen%20noot\n"

        url = urlparse.urlparse(
            "http://localhost:8080/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen%20noot"
        )
        data_to_sign = authenticator.to_data_to_sign("POST", url, http_headers)

        actual_start = data_to_sign[:22]
        actual_end = data_to_sign[52:]
        self.assertEqual(expected_start, actual_start)
        self.assertEqual(expected_end, actual_end)
 def test_unmarshal_no_secret_key_available(self):
     self.clear_public_key_cache()
     helper = self.__create_helper()
     body = self.__read_resource("valid-body")
     request_headers = [
         RequestHeader(self.__SIGNATURE_HEADER, self.__SIGNATURE),
         RequestHeader(self.__KEY_ID_HEADER, self.__KEY_ID)]
     self.assertRaises(SecretKeyNotAvailableException,
                       helper.unmarshal, body, request_headers)
 def test_unmarshal_string_invalid_signature(self):
     helper = self.__create_helper()
     InMemorySecretKeyStore.INSTANCE().store_secret_key(self.__KEY_ID,
                                                        self.__SECRET_KEY)
     body = self.__read_resource("valid-body")
     request_headers = [
         RequestHeader(self.__SIGNATURE_HEADER, "1" + self.__SIGNATURE),
         RequestHeader(self.__KEY_ID_HEADER, self.__KEY_ID)]
     self.assertRaises(SignatureValidationException, helper.unmarshal,
                       body, request_headers)
 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("Ingenico", None, additional_headers)
         self.assertIn(name, str(error.exception))
 def test_unmarshal_api_version_mismatch(self):
     marshaller = mock(Marshaller)
     event = WebhooksEvent()
     event.api_version = "v0"
     body = self.__read_resource("valid-body")
     when(marshaller).unmarshal(body, WebhooksEvent).thenReturn(event)
     helper = self.__create_helper(marshaller)
     InMemorySecretKeyStore.INSTANCE().store_secret_key(
         self.__KEY_ID, self.__SECRET_KEY)
     request_headers = [
         RequestHeader(self.__SIGNATURE_HEADER, self.__SIGNATURE),
         RequestHeader(self.__KEY_ID_HEADER, self.__KEY_ID)
     ]
     self.assertRaises(ApiVersionMismatchException, helper.unmarshal, body,
                       request_headers)
 def to_data_to_sign(self, http_method, resource_uri, http_headers):
     content_type = None
     date = None
     canonicalized_headers = ""
     canonicalized_resource = self.__to_canonicalized_resource(resource_uri)
     xgcs_http_headers = []
     if http_headers is not None:
         for http_header in http_headers:
             if "Content-Type".lower() == http_header.name.lower():
                 content_type = http_header.value
             elif "Date".lower() == http_header.name.lower():
                 date = http_header.value
             else:
                 name = self.__to_canonicalize_header_name(http_header.name)
                 if name.startswith("x-gcs"):
                     value = self.to_canonicalize_header_value(
                         http_header.value)
                     xgcs_http_header = RequestHeader(name, value)
                     xgcs_http_headers.append(xgcs_http_header)
     xgcs_http_headers.sort(key=attrgetter('name'))
     for xgcs_http_header in xgcs_http_headers:
         canonicalized_headers += xgcs_http_header.name + ":" + xgcs_http_header.value + "\n"
     string = http_method.upper() + "\n"
     if content_type is not None:
         string += content_type + "\n"
     else:
         string += "\n"
     string += date + "\n"
     string += str(canonicalized_headers)
     string += canonicalized_resource + "\n"
     return str(string)
    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("Ingenico", 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],
                             request_headers[index])
 def test_get_header_list(self):
     """Tests get_header using a list of RequestHeader objects"""
     headers = [RequestHeader("Content-Type", "application/json")]
     self.assertEqual("Content-Type:application/json",
                      str(get_header(headers, "Content-Type")))
     self.assertEqual("Content-Type:application/json",
                      str(get_header(headers, "content-type")))
     self.assertIsNone(get_header_value(headers, "Content-Length"))
示例#9
0
 def assertClientHeaders(self, client, client_meta_info):
     """Checks that the 'ClientMetaInfo' header with client_meta_info is stored properly in the client"""
     headers = client._client_headers
     header_value = base64.b64encode(client_meta_info.encode("utf-8"))
     expected = RequestHeader("X-GCS-ClientMetaInfo", header_value)
     found = False
     for header in headers:
         if str(expected) == str(header):
             found = True
     self.assertTrue(found, "header {0} was not found in {1}".format(expected, headers))
    def test_multiline_header(self):
        """Test if the products service can function with a multiline header"""
        multi_line_header = " some value  \r\n \n with        a liberal amount     of \r\n  spaces    "
        configuration = init_utils.create_communicator_configuration()
        meta_data_provider = MetaDataProvider(
            integrator="Ingenico",
            additional_request_headers=(RequestHeader("X-GCS-MultiLineHeader",
                                                      multi_line_header), ))
        params = DirectoryParams()
        params.country_code = "NL"
        params.currency_code = "EUR"
        session = Factory.create_session_from_configuration(
            configuration, meta_data_provider=meta_data_provider)

        with Factory.create_client_from_session(session) as client:
            response = client.merchant(MERCHANT_ID).products().directory(
                809, params)

        self.assertGreater(len(response.entries), 0)
    def test_unmarshal_string_success(self):
        helper = self.__create_helper()
        InMemorySecretKeyStore.INSTANCE().store_secret_key(
            self.__KEY_ID, self.__SECRET_KEY)
        body_stream = self.__read_resource("valid-body")
        request_headers = [
            RequestHeader(self.__SIGNATURE_HEADER, self.__SIGNATURE),
            RequestHeader(self.__KEY_ID_HEADER, self.__KEY_ID)
        ]
        event = helper.unmarshal(body_stream, request_headers)
        self.assertEqual("v1", event.api_version)
        self.assertEqual("8ee793f6-4553-4749-85dc-f2ef095c5ab0", event.id)
        self.assertEqual("2017-02-02T11:24:14.040+0100", event.created)
        self.assertEqual("20000", event.merchant_id)
        self.assertEqual("payment.paid", event.type)

        self.assertIsNone(event.refund)
        self.assertIsNone(event.payout)
        self.assertIsNone(event.token)

        self.assertIsNotNone(event.payment)
        self.assertEqual("00000200000143570012", event.payment.id)
        self.assertIsNotNone(event.payment.payment_output)
        self.assertIsNotNone(event.payment.payment_output.amount_of_money)
        self.assertEqual(1000,
                         event.payment.payment_output.amount_of_money.amount)
        self.assertEqual(
            "EUR", event.payment.payment_output.amount_of_money.currency_code)
        self.assertIsNotNone(
            event.payment.payment_output.amount_of_money.currency_code)
        self.assertIsNotNone(event.payment.payment_output.references)
        self.assertEqual(
            "200001681810",
            event.payment.payment_output.references.payment_reference)
        self.assertEqual("bankTransfer",
                         event.payment.payment_output.payment_method)

        self.assertIsNone(
            event.payment.payment_output.card_payment_method_specific_output)
        self.assertIsNone(
            event.payment.payment_output.cash_payment_method_specific_output)
        self.assertIsNone(event.payment.payment_output.
                          direct_debit_payment_method_specific_output)
        self.assertIsNone(event.payment.payment_output.
                          invoice_payment_method_specific_output)
        self.assertIsNone(event.payment.payment_output.
                          redirect_payment_method_specific_output)
        self.assertIsNone(event.payment.payment_output.
                          sepa_direct_debit_payment_method_specific_output)
        self.assertIsNotNone(event.payment.payment_output.
                             bank_transfer_payment_method_specific_output)
        self.assertEqual(
            11, event.payment.payment_output.
            bank_transfer_payment_method_specific_output.payment_product_id)

        self.assertEqual("PAID", event.payment.status)
        self.assertIsNotNone(event.payment.status_output)
        self.assertEqual(False, event.payment.status_output.is_cancellable)
        self.assertEqual("COMPLETED",
                         event.payment.status_output.status_category)
        self.assertEqual(1000, event.payment.status_output.status_code)
        self.assertEqual(
            "20170202112414",
            event.payment.status_output.status_code_change_date_time)
        self.assertEqual(True, event.payment.status_output.is_authorized)