Skip to content

Commit

Permalink
Merge pull request #57 from demisto/fix-rest-import
Browse files Browse the repository at this point in the history
Fix for import issue. Added unit test to catch in the future.
  • Loading branch information
amshamah419 authored Dec 21, 2020
2 parents 240ad35 + 037c2da commit 4491333
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions demisto_client/demisto_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
import re
import ssl
import os

import certifi
# python 2 and python 3 compatibility library
Expand Down
2 changes: 2 additions & 0 deletions gen-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:\
Expand Down
87 changes: 86 additions & 1 deletion tests/mocks_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from urllib3_mock import Responses
import demisto_client
from datetime import datetime
Expand Down Expand Up @@ -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'
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()

0 comments on commit 4491333

Please sign in to comment.