Exemple #1
0
    def test_web_utils_bad_requests(self):
        with self.assertRaises(ValueError):
            request = web_utils.CoinflexPerpetualRESTRequest(
                method=RESTMethod.GET,
                endpoint=CONSTANTS.ACCOUNT_INFO_URL,
                data="")

        request = web_utils.CoinflexPerpetualRESTRequest(
            method=RESTMethod.GET, url=CONSTANTS.ACCOUNT_INFO_URL)
        with self.assertRaises(ValueError):
            print(request.auth_path)
Exemple #2
0
    def test_web_utils_retries(self, mock_api, retry_sleep_time_mock):
        retry_sleep_time_mock.side_effect = lambda *args, **kwargs: 0
        url, regex_url = self._get_regex_url(CONSTANTS.ACCOUNT_INFO_URL,
                                             return_url=True)
        mock_response = "invalid"
        for i in range(CONSTANTS.API_MAX_RETRIES * 2):
            mock_api.get(regex_url, body=mock_response)
        request = web_utils.CoinflexPerpetualRESTRequest(
            method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNT_INFO_URL)
        with self.assertRaises(web_utils.CoinflexPerpetualAPIError):
            self.async_run_with_timeout(
                web_utils.api_call_with_retries(
                    request=request,
                    rest_assistant=self.rest_assistant,
                    throttler=self.throttler,
                    logger=self.logger))
        with self.assertRaises(web_utils.CoinflexPerpetualAPIError):
            self.async_run_with_timeout(
                web_utils.api_call_with_retries(
                    request=request,
                    rest_assistant=self.rest_assistant,
                    throttler=self.throttler))

        self.assertTrue(
            self._is_logged(
                "NETWORK",
                f"Error fetching data from {url}. HTTP status is 200. Retrying in 0s. invalid"
            ))
Exemple #3
0
    def test_coinflex_perpetual_rest_pre_processor_non_post_request(self):
        request: web_utils.CoinflexPerpetualRESTRequest = web_utils.CoinflexPerpetualRESTRequest(
            method=RESTMethod.GET,
            url="/TEST_URL",
        )

        result_request: web_utils.CoinflexPerpetualRESTRequest = self.async_run_with_timeout(
            self.pre_processor.pre_process(request))

        self.assertIn("Content-Type", result_request.headers)
        self.assertEqual(result_request.headers["Content-Type"],
                         "application/json")
Exemple #4
0
 def test_web_utils_error_request_data_success_false(self, mock_api):
     regex_url = self._get_regex_url(CONSTANTS.ACCOUNT_INFO_URL)
     mock_response = {"data": [{"success": "false", "message": "Error"}]}
     mock_api.get(regex_url, body=json.dumps(mock_response))
     request = web_utils.CoinflexPerpetualRESTRequest(
         method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNT_INFO_URL)
     with self.assertRaises(web_utils.CoinflexPerpetualAPIError):
         self.async_run_with_timeout(
             web_utils.api_call_with_retries(
                 request=request,
                 rest_assistant=self.rest_assistant,
                 throttler=self.throttler,
                 logger=self.logger))
Exemple #5
0
 def test_web_utils_error_request_truncated_string(self, mock_api,
                                                   retry_sleep_time_mock):
     retry_sleep_time_mock.side_effect = lambda *args, **kwargs: 0
     regex_url = self._get_regex_url(CONSTANTS.ACCOUNT_INFO_URL)
     mock_api.get(regex_url, body="a" * 101)
     request = web_utils.CoinflexPerpetualRESTRequest(
         method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNT_INFO_URL)
     with self.assertRaises(web_utils.CoinflexPerpetualAPIError):
         self.async_run_with_timeout(
             web_utils.api_call_with_retries(
                 request=request,
                 rest_assistant=self.rest_assistant,
                 throttler=self.throttler,
                 logger=self.logger))
Exemple #6
0
 def test_web_utils_error_request_error(self, mock_api):
     regex_url = self._get_regex_url(CONSTANTS.ACCOUNT_INFO_URL)
     mock_response = {"error": "Error"}
     mock_api.get(regex_url, body=json.dumps(mock_response))
     request = web_utils.CoinflexPerpetualRESTRequest(
         method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNT_INFO_URL)
     expected_error = {**mock_response, "errors": "Error"}
     with self.assertRaisesRegex(web_utils.CoinflexPerpetualAPIError,
                                 str(expected_error)):
         self.async_run_with_timeout(
             web_utils.api_call_with_retries(
                 request=request,
                 rest_assistant=self.rest_assistant,
                 throttler=self.throttler,
                 logger=self.logger))
Exemple #7
0
    def test_rest_authenticate(self, time_mock):
        now = 1234567890.000
        time_mock.return_value = now

        params = {
            "symbol": "LTCBTC",
            "side": "BUY",
            "type": "LIMIT",
            "timeInForce": "GTC",
            "quantity": 1,
            "price": "0.1",
        }
        full_params = copy(params)

        request = web_utils.CoinflexPerpetualRESTRequest(method=RESTMethod.GET,
                                                         endpoint="",
                                                         params=params,
                                                         is_auth_required=True)
        configured_request = self.async_run_with_timeout(
            self.auth.rest_authenticate(request))

        str_timestamp = datetime.utcfromtimestamp(now).isoformat()
        nonce = int(now * 1e3)

        encoded_params = "&".join(
            [f"{key}={value}" for key, value in full_params.items()])
        payload = '{}\n{}\n{}\n{}\n{}\n{}'.format(str_timestamp, nonce,
                                                  str(RESTMethod.GET),
                                                  request.auth_url,
                                                  request.auth_path,
                                                  encoded_params)

        expected_signature = b64encode(
            hmac.new(self.secret_key.encode("utf-8"), payload.encode("utf-8"),
                     hashlib.sha256).digest()).decode().strip()
        expected_headers = {
            "AccessKey": self.api_key,
            "Timestamp": str_timestamp,
            "Signature": expected_signature,
            "Nonce": str(nonce),
        }
        self.assertEqual(expected_headers, configured_request.headers)