def test_get_insights_401(self, patch_parent, mocker): view = HostInsights() Response = namedtuple('Response', 'status_code content') mocker.patch.object(view, '_get_insights', return_value=Response(401, '')) (msg, code) = view.get_insights('https://myexample.com/whocares/me/', 'ignore', 'ignore') assert msg['error'] == 'Unauthorized access. Please check your Insights Credential username and password.'
def test_get_insights_request_exception(self, patch_parent, mocker, status_code, exception, error, message): view = HostInsights() mocker.patch.object(view, '_get_insights', side_effect=exception(error)) (msg, code) = view.get_insights('https://myexample.com/whocares/me/', 'ignore', 'ignore') assert code == status_code assert msg['error'] == message or error
def test_get_insights_user_agent(self, patch_parent, mocker): with mock.patch.object(requests.Session, 'get') as get: HostInsights()._get_insights('https://example.org', 'joe', 'example') assert get.call_count == 1 args, kwargs = get.call_args_list[0] assert args == ('https://example.org',) assert re.match(r'AWX [^\s]+ \(open\)', kwargs['headers']['User-Agent'])
def test_get_insights_non_200(self, patch_parent, mocker): view = HostInsights() Response = namedtuple('Response', 'status_code content') mocker.patch.object(view, '_get_insights', return_value=Response(500, 'mock 500 err msg')) (msg, code) = view.get_insights('https://myexample.com/whocares/me/', 'ignore', 'ignore') assert msg['error'] == ( 'Failed to gather reports and maintenance plans from Insights API at URL' ' https://myexample.com/whocares/me/. Server responded with 500 status code ' 'and message mock 500 err msg')
def test_get_not_insights_host(self, mocker): view = HostInsights() host = Host() host.insights_system_id = None mocker.patch.object(view, 'get_object', return_value=host) resp = view.get(None) assert resp.data['error'] == 'This host is not recognized as an Insights host.' assert resp.status_code == 404
def test_get_insights_malformed_json_content(self, patch_parent, mocker): view = HostInsights() class Response(): status_code = 200 content = 'booo!' def json(self): raise ValueError('we do not care what this is') mocker.patch.object(view, '_get_insights', return_value=Response()) (msg, code) = view.get_insights('https://myexample.com/whocares/me/', 'ignore', 'ignore') assert msg['error'] == 'Expected JSON response from Insights but instead got booo!' assert code == 502
def test_get_no_credential(self, patch_parent, mocker): view = HostInsights() class MockInventory(): insights_credential = None name = 'inventory_name_here' class MockHost(): insights_system_id = 'insights_system_id_value' inventory = MockInventory() mocker.patch.object(view, 'get_object', return_value=MockHost()) resp = view.get(None) assert resp.data['error'] == 'The Insights Credential for "inventory_name_here" was not found.' assert resp.status_code == 404
def test_get_insights_user_agent(self, patch_parent, mocker): resp = HostInsights()._get_insights('https://example.org', 'joe', 'example') assert re.match(r'AWX [^\s]+ \(open\)', resp.request.headers['User-Agent'])