def test_is_valid_ok_codes_empty(self): from requests import Response from CommonServerPython import BaseClient new_client = BaseClient('http://example.com/api/v2/') response = Response() response.status_code = 200 assert new_client._is_status_code_valid(response, None)
class TestBaseClient: from CommonServerPython import BaseClient text = {"status": "ok"} client = BaseClient('http://example.com/api/v2/', ok_codes=(200, 201)) def test_http_request_json(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', text=json.dumps(self.text)) res = self.client._http_request('get', 'event') assert res == self.text def test_http_request_json_negative(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', text='notjson') with raises(DemistoException, match="Failed to parse json"): self.client._http_request('get', 'event') def test_http_request_text(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', text=json.dumps(self.text)) res = self.client._http_request('get', 'event', resp_type='text') assert res == json.dumps(self.text) def test_http_request_content(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', content=str.encode(json.dumps(self.text))) res = self.client._http_request('get', 'event', resp_type='content') assert json.loads(res) == self.text def test_http_request_response(self, requests_mock): requests_mock.get('http://example.com/api/v2/event') res = self.client._http_request('get', 'event', resp_type='response') assert isinstance(res, requests.Response) def test_http_request_not_ok(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', status_code=500) with raises(DemistoException, match="[500]"): self.client._http_request('get', 'event') def test_http_request_not_ok_but_ok(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', status_code=500) res = self.client._http_request('get', 'event', resp_type='response', ok_codes=(500, )) assert res.status_code == 500 def test_http_request_not_ok_with_json(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', status_code=500, content=str.encode(json.dumps(self.text))) with raises(DemistoException, match="Error in API call"): self.client._http_request('get', 'event') def test_http_request_not_ok_with_json_parsing(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', status_code=500, content=str.encode(json.dumps(self.text))) with raises(DemistoException) as exception: self.client._http_request('get', 'event') message = str(exception.value) response_json_error = json.loads(message.split('\n')[1]) assert response_json_error == self.text def test_http_request_timeout(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.ConnectTimeout) with raises(DemistoException, match="Connection Timeout Error"): self.client._http_request('get', 'event') def test_http_request_ssl_error(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.SSLError) with raises(DemistoException, match="SSL Certificate Verification Failed"): self.client._http_request('get', 'event', resp_type='response') def test_http_request_proxy_error(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.ProxyError) with raises(DemistoException, match="Proxy Error"): self.client._http_request('get', 'event', resp_type='response') def test_http_request_connection_error(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.ConnectionError) with raises(DemistoException, match="Verify that the server URL parameter"): self.client._http_request('get', 'event', resp_type='response') def test_is_valid_ok_codes_empty(self): from requests import Response from CommonServerPython import BaseClient new_client = BaseClient('http://example.com/api/v2/') response = Response() response.status_code = 200 assert new_client._is_status_code_valid(response, None) def test_is_valid_ok_codes_from_function(self): from requests import Response response = Response() response.status_code = 200 assert self.client._is_status_code_valid(response, (200, 201)) def test_is_valid_ok_codes_from_self(self): from requests import Response response = Response() response.status_code = 200 assert self.client._is_status_code_valid(response, None) def test_is_valid_ok_codes_empty_false(self): from requests import Response response = Response() response.status_code = 400 assert not self.client._is_status_code_valid(response, None) def test_is_valid_ok_codes_from_function_false(self): from requests import Response response = Response() response.status_code = 400 assert not self.client._is_status_code_valid(response, (200, 201)) def test_is_valid_ok_codes_from_self_false(self): from requests import Response response = Response() response.status_code = 400 assert not self.client._is_status_code_valid(response)
class TestBuildDBotEntry(object): def test_build_dbot_entry(self): from CommonServerPython import build_dbot_entry res = build_dbot_entry('*****@*****.**', 'Email', 'Vendor', 1) assert res == { 'DBotScore': { 'Indicator': '*****@*****.**', 'Type': 'email', 'Vendor': 'Vendor', 'Score': 1 } } def test_build_dbot_entry_no_malicious(self): from CommonServerPython import build_dbot_entry res = build_dbot_entry('*****@*****.**', 'Email', 'Vendor', 3, build_malicious=False) assert res == { 'DBotScore': { 'Indicator': '*****@*****.**', 'Type': 'email', 'Vendor': 'Vendor', 'Score': 3 } } def test_build_dbot_entry_malicious(self): from CommonServerPython import build_dbot_entry, outputPaths res = build_dbot_entry('*****@*****.**', 'Email', 'Vendor', 3, 'Malicious email') assert res == { "DBotScore": { "Vendor": "Vendor", "Indicator": "*****@*****.**", "Score": 3, "Type": "email" }, outputPaths['email']: { "Malicious": { "Vendor": "Vendor", "Description": "Malicious email" }, "Address": "*****@*****.**" } } def test_build_malicious_dbot_entry_file(self): from CommonServerPython import build_malicious_dbot_entry, outputPaths res = build_malicious_dbot_entry('md5hash', 'MD5', 'Vendor', 'Google DNS') assert res == { outputPaths['file']: { "Malicious": { "Vendor": "Vendor", "Description": "Google DNS" }, "MD5": "md5hash" } } def test_build_malicious_dbot_entry(self): from CommonServerPython import build_malicious_dbot_entry, outputPaths res = build_malicious_dbot_entry('8.8.8.8', 'ip', 'Vendor', 'Google DNS') assert res == { outputPaths['ip']: { 'Address': '8.8.8.8', 'Malicious': { 'Vendor': 'Vendor', 'Description': 'Google DNS' } } } def test_build_malicious_dbot_entry_wrong_indicator_type(self): from CommonServerPython import build_malicious_dbot_entry, DemistoException with raises(DemistoException, match='Wrong indicator type'): build_malicious_dbot_entry('8.8.8.8', 'notindicator', 'Vendor', 'Google DNS') def test_illegal_dbot_score(self): from CommonServerPython import build_dbot_entry, DemistoException with raises(DemistoException, match='illegal DBot score'): build_dbot_entry('1', 'ip', 'Vendor', 8) def test_illegal_indicator_type(self): from CommonServerPython import build_dbot_entry, DemistoException with raises(DemistoException, match='illegal indicator type'): build_dbot_entry('1', 'NOTHING', 'Vendor', 2) def test_file_indicators(self): from CommonServerPython import build_dbot_entry, outputPaths res = build_dbot_entry('md5hash', 'md5', 'Vendor', 3) assert res == { "DBotScore": { "Indicator": "md5hash", "Type": "file", "Vendor": "Vendor", "Score": 3 }, outputPaths['file']: { "MD5": "md5hash", "Malicious": { "Vendor": "Vendor", "Description": None } } } from CommonServerPython import BaseClient text = {"status": "ok"} client = BaseClient('http://example.com/api/v2/', ok_codes=(200, 201)) def test_http_request_json(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', text=json.dumps(self.text)) res = self.client._http_request('get', 'event') assert res == self.text def test_http_request_json_negative(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', text='notjson') with raises(DemistoException, match="Failed to parse json"): self.client._http_request('get', 'event') def test_http_request_text(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', text=json.dumps(self.text)) res = self.client._http_request('get', 'event', resp_type='text') assert res == json.dumps(self.text) def test_http_request_content(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', content=str.encode(json.dumps(self.text))) res = self.client._http_request('get', 'event', resp_type='content') assert json.loads(res) == self.text def test_http_request_response(self, requests_mock): requests_mock.get('http://example.com/api/v2/event') res = self.client._http_request('get', 'event', resp_type='response') assert isinstance(res, requests.Response) def test_http_request_not_ok(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', status_code=500) with raises(DemistoException, match="[500]"): self.client._http_request('get', 'event') def test_http_request_not_ok_but_ok(self, requests_mock): requests_mock.get('http://example.com/api/v2/event', status_code=500) res = self.client._http_request('get', 'event', resp_type='response', ok_codes=(500, )) assert res.status_code == 500 def test_http_request_not_ok_with_json(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', status_code=500, content=str.encode(json.dumps(self.text))) with raises(DemistoException, match="Error in API call"): self.client._http_request('get', 'event') def test_http_request_not_ok_with_json_parsing(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', status_code=500, content=str.encode(json.dumps(self.text))) with raises(DemistoException) as exception: self.client._http_request('get', 'event') message = str(exception.value) response_json_error = json.loads(message.split('\n')[1]) assert response_json_error == self.text def test_http_request_timeout(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.ConnectTimeout) with raises(DemistoException, match="Connection Timeout Error"): self.client._http_request('get', 'event') def test_http_request_ssl_error(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.SSLError) with raises(DemistoException, match="SSL Certificate Verification Failed"): self.client._http_request('get', 'event', resp_type='response') def test_http_request_proxy_error(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.ProxyError) with raises(DemistoException, match="Proxy Error"): self.client._http_request('get', 'event', resp_type='response') def test_http_request_connection_error(self, requests_mock): from CommonServerPython import DemistoException requests_mock.get('http://example.com/api/v2/event', exc=requests.exceptions.ConnectionError) with raises(DemistoException, match="Verify that the server URL parameter"): self.client._http_request('get', 'event', resp_type='response') def test_text_exception_parsing(self, requests_mock): from CommonServerPython import DemistoException reason = 'Bad Request' text = 'additional text' requests_mock.get('http://example.com/api/v2/event', status_code=400, reason=reason, text=text) with raises(DemistoException, match='- {}\n{}'.format(reason, text)): self.client._http_request('get', 'event', resp_type='text') def test_json_exception_parsing(self, requests_mock): from CommonServerPython import DemistoException reason = 'Bad Request' json_response = {'error': 'additional text'} requests_mock.get('http://example.com/api/v2/event', status_code=400, reason=reason, json=json_response) with raises(DemistoException, match='- {}\n.*{}'.format(reason, json_response["error"])): self.client._http_request('get', 'event', resp_type='text') def test_is_valid_ok_codes_empty(self): from requests import Response from CommonServerPython import BaseClient new_client = BaseClient('http://example.com/api/v2/') response = Response() response.status_code = 200 assert new_client._is_status_code_valid(response, None) def test_is_valid_ok_codes_from_function(self): from requests import Response response = Response() response.status_code = 200 assert self.client._is_status_code_valid(response, (200, 201)) def test_is_valid_ok_codes_from_self(self): from requests import Response response = Response() response.status_code = 200 assert self.client._is_status_code_valid(response, None) def test_is_valid_ok_codes_empty_false(self): from requests import Response response = Response() response.status_code = 400 assert not self.client._is_status_code_valid(response, None) def test_is_valid_ok_codes_from_function_false(self): from requests import Response response = Response() response.status_code = 400 assert not self.client._is_status_code_valid(response, (200, 201)) def test_is_valid_ok_codes_from_self_false(self): from requests import Response response = Response() response.status_code = 400 assert not self.client._is_status_code_valid(response)