diff --git a/demisto_client/demisto_api/rest.py b/demisto_client/demisto_api/rest.py index 8bd2a4f..de2cd9b 100644 --- a/demisto_client/demisto_api/rest.py +++ b/demisto_client/demisto_api/rest.py @@ -18,6 +18,7 @@ import logging import re import ssl +import os import certifi # python 2 and python 3 compatibility library diff --git a/gen-code.sh b/gen-code.sh index a32d674..f5cecd0 100755 --- a/gen-code.sh +++ b/gen-code.sh @@ -64,6 +64,8 @@ echo -e "\n def generic_request(self, path, method, body=None, **kwargs): # # fix bug where binary data is decoded on py3 sed -i "${INPLACE[@]}" -e 's#if six\.PY3:#if six.PY3 and r.getheader("Content-Type") != "application/octet-stream":#' demisto_client/demisto_api/rest.py # Disable sensitive logging by default +sed -i "${INPLACE[@]}" -e 's/import ssl/import ssl\ +import os/g' demisto_client/demisto_api/rest.py sed -i "${INPLACE[@]}" -e 's/"""Custom error messages for exception"""/"""Custom error messages for exception"""\ sensitive_env = os.getenv("DEMISTO_EXCEPTION_HEADER_LOGGING")\ if sensitive_env:\ diff --git a/tests/mocks_test.py b/tests/mocks_test.py index 0a66ad1..b5af9b1 100644 --- a/tests/mocks_test.py +++ b/tests/mocks_test.py @@ -1,3 +1,5 @@ +import unittest + from urllib3_mock import Responses import demisto_client from datetime import datetime @@ -296,4 +298,87 @@ def test_import_layout_with_http_info_with_old_layout_format(mocker): mocker.patch.object(client.api_client, 'call_api', side_effect=[("{'demistoVersion': '5.0.0'}", 200, {'Content-type': 'application/json'}), {'test': 'test'}]) res = client.import_layout('tests_data/layout-details-test-V2.json') - assert res.get('test') == 'test' \ No newline at end of file + assert res.get('test') == 'test' + + +class TestFailedGenericRequestNoEnv(unittest.TestCase): + + def test_generic_request(self): + """ + Given: + - Request which should result in an ApiException + When: + - No environment variable has been set + Then: + - Return ApiException without the headers in the error + """ + from demisto_client.demisto_api.rest import ApiException + + @responses.activate + def run(): + responses.add('POST', '/test', + body="Not good", + status=400, + content_type='text/plain') + api_instance = demisto_client.configure(base_url=host, api_key=api_key, debug=False) + + with self.assertRaises(ApiException) as context: + (_, _, _) = api_instance.generic_request('/test', 'POST', + body="this is a test", + content_type='text/plain', + accept='text/plain') + self.assertTrue('HTTP response body' in str(context.exception)) + self.assertFalse('HTTP response headers' in str(context.exception)) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == '/test' + assert responses.calls[0].request.host == 'localhost' + assert responses.calls[0].request.scheme == 'http' + + run() + assert_reset() + + +class TestFailedGenericRequestWithEnv(unittest.TestCase): + + def test_generic_request(self): + """ + Given: + - Request which should result in an ApiException + When: + - Environment variable DEMISTO_EXCEPTION_HEADER_LOGGING has been set to true + Then: + - Return ApiException with the headers in the error + """ + import sys + # Error should be the same in both Py2 and Py3, but Py2 does not support unittest mock in + # the same way + if sys.version_info[0] > 2: + import os + from demisto_client.demisto_api.rest import ApiException + from unittest import mock + + @mock.patch.dict(os.environ, {"DEMISTO_EXCEPTION_HEADER_LOGGING": "true"}) + @responses.activate + def run(): + responses.add('POST', '/test', + body="Not good", + status=400, + content_type='text/plain') + api_instance = demisto_client.configure(base_url=host, api_key=api_key, debug=False) + + with self.assertRaises(ApiException) as context: + (_, _, _) = api_instance.generic_request('/test', 'POST', + body="this is a test", + content_type='text/plain', + accept='text/plain') + self.assertTrue('HTTP response body' in str(context.exception)) + self.assertTrue('HTTP response headers' in str(context.exception)) + assert len(responses.calls) == 1 + assert responses.calls[0].request.url == '/test' + assert responses.calls[0].request.host == 'localhost' + assert responses.calls[0].request.scheme == 'http' + else: + def run(): + assert 1 == 1 + run() + assert_reset()