def call(self): # Build the URL for the API (they are RESTful) kvFormat = '{}={}' params = '' # Now turn JSON like ``"a":"b"`` into URL parameters like ``a=b`` if self._payload is not {}: params = '&'.join([ kvFormat.format(key, str(self._payload[key])) for key in sorted(self._payload.keys()) ]) url = self.__host % (self._path, params) else: url = self.__host_no_params # Obtain the request signature timestamp = str(int((helpers.time.time() + helpers.timeOffset) * 1000)) signature = signer.sign(self._path, timestamp, params) # Generate the headers, fill with the request signature headers = { 'KC-API-KEY': signer.key, 'KC-API-NONCE': timestamp, 'KC-API-SIGNATURE': signature, 'Content-Type': 'application/x-www-form-urlencoded' } # Pass the actual request off to helpers, since they do error checking and more return helpers.request(self._method, url, headers, self)
def test_signature_missing(self, _process_signed_url, _validate_signature): """Should return 'Signature is missing' if there is no signature""" result = signer.sign(urlBase + "B02K_CUSTNAME=DEAN%20LE") self.assertEqual(result, "Signature is missing") self.assertFalse(_validate_signature.called) self.assertFalse(_process_signed_url.called)
def test_integration_sign_url(self): os.environ[signer.INPUT_SECRET] = "inputsecret" os.environ[signer.OUTPUT_SECRET] = "outputsecret" result = signer.sign(urlBase + "B02K_CUSTNAME=V%C4IN%D6%20M%C4KI&B02K_MAC=ebfa16b7dbbf887fd579099d5bbac83488fb34f6c824cc0f8d9d6e2f4d286d41") expected = urlBase + "firstname=V%C3%A4in%C3%B6&lastname=M%C3%A4ki&hash=6e6f22ec29ed8ec1f0be289a94dde36632720596499993b2cdfc1422b834934a" self.assertEqual(result, expected)
def test_sign_url_invalid_url(self, _process_signed_url, _validate_signature): """Should return 'Invalid URL' if cannot validate""" _validate_signature.return_value = False result = signer.sign(urlBase + "B02K_CUSTNAME=DEAN%20LE&B02K_MAC=xyz") self.assertEqual(result, "Invalid URL") args, _ = _validate_signature.call_args self.assertEqual(args[0], {"B02K_CUSTNAME": "DEAN LE", "B02K_MAC": "xyz", "input_secret": "inputsecret"}) self.assertFalse(_process_signed_url.called)
def test_sign_url_successfully_nonAscii_custname(self, _process_signed_url, _validate_signature): """Should output URL correctly even with non-ascii customer name""" # Setup _validate_signature.return_value = True _process_signed_url.return_value = urlBase + "firstname=V%C3%A4in%C3%B6&lastname=M%C3%A4ki&hash=abc123" # Call function result = signer.sign(urlBase + "B02K_CUSTNAME=V%C4IN%D6%20M%C4KI&B02K_MAC=xyz") # Asertion self.assertEqual(result, urlBase + "firstname=V%C3%A4in%C3%B6&lastname=M%C3%A4ki&hash=abc123") args, _ = _validate_signature.call_args self.assertEqual(args[0], {"B02K_CUSTNAME": "VÄINÖ MÄKI", "B02K_MAC": "xyz", "input_secret": "inputsecret"}) args, _ = _process_signed_url.call_args self.assertEqual(args[0], SplitResult("https", "fsecure.com", "/", "B02K_CUSTNAME=V%C4IN%D6%20M%C4KI&B02K_MAC=xyz", "")) self.assertEqual(args[1], "VÄINÖ MÄKI") self.assertEqual(args[2], "outputsecret")
def test_sign_url_successfully(self, _process_signed_url, _validate_signature): """Should output URL correctly""" # Setup _validate_signature.return_value = True _process_signed_url.return_value = urlBase + "firstname=Dean&lastname=Le&hash=abc123" # Call function result = signer.sign(urlBase + "B02K_CUSTNAME=DEAN%20LE&B02K_MAC=xyz") # Asertion self.assertEqual(result, urlBase + "firstname=Dean&lastname=Le&hash=abc123") self.assertTrue(_validate_signature.called) args, _ = _validate_signature.call_args self.assertEqual(args[0], {"B02K_CUSTNAME": "DEAN LE", "B02K_MAC": "xyz", "input_secret": "inputsecret"}) self.assertTrue(_process_signed_url.called) args, _ = _process_signed_url.call_args self.assertEqual(args[0], SplitResult("https", "fsecure.com", "/", "B02K_CUSTNAME=DEAN%20LE&B02K_MAC=xyz", "")) self.assertEqual(args[1], "DEAN LE") self.assertEqual(args[2], "outputsecret")