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_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_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_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
HostGroupsList, HostAllGroupsList, HostJobEventsList, HostJobHostSummariesList, HostActivityStreamList, HostInventorySourcesList, HostSmartInventoriesList, HostAdHocCommandsList, HostAdHocCommandEventsList, HostInsights, ) urls = [ url(r'^$', HostList.as_view(), name='host_list'), url(r'^(?P<pk>[0-9]+)/$', HostDetail.as_view(), name='host_detail'), url(r'^(?P<pk>[0-9]+)/variable_data/$', HostVariableData.as_view(), name='host_variable_data'), url(r'^(?P<pk>[0-9]+)/groups/$', HostGroupsList.as_view(), name='host_groups_list'), url(r'^(?P<pk>[0-9]+)/all_groups/$', HostAllGroupsList.as_view(), name='host_all_groups_list'), url(r'^(?P<pk>[0-9]+)/job_events/', HostJobEventsList.as_view(), name='host_job_events_list'), url(r'^(?P<pk>[0-9]+)/job_host_summaries/$', HostJobHostSummariesList.as_view(), name='host_job_host_summaries_list'), url(r'^(?P<pk>[0-9]+)/activity_stream/$', HostActivityStreamList.as_view(), name='host_activity_stream_list'), url(r'^(?P<pk>[0-9]+)/inventory_sources/$', HostInventorySourcesList.as_view(), name='host_inventory_sources_list'), url(r'^(?P<pk>[0-9]+)/smart_inventories/$', HostSmartInventoriesList.as_view(), name='host_smart_inventories_list'), url(r'^(?P<pk>[0-9]+)/ad_hoc_commands/$', HostAdHocCommandsList.as_view(), name='host_ad_hoc_commands_list'), url(r'^(?P<pk>[0-9]+)/ad_hoc_command_events/$', HostAdHocCommandEventsList.as_view(), name='host_ad_hoc_command_events_list'), url(r'^(?P<pk>[0-9]+)/insights/$', HostInsights.as_view(), name='host_insights'), ] __all__ = ['urls']
url(r'^(?P<pk>[0-9]+)/job_host_summaries/$', HostJobHostSummariesList.as_view(), name='host_job_host_summaries_list'), url(r'^(?P<pk>[0-9]+)/activity_stream/$', HostActivityStreamList.as_view(), name='host_activity_stream_list'), url(r'^(?P<pk>[0-9]+)/inventory_sources/$', HostInventorySourcesList.as_view(), name='host_inventory_sources_list'), url(r'^(?P<pk>[0-9]+)/smart_inventories/$', HostSmartInventoriesList.as_view(), name='host_smart_inventories_list'), url(r'^(?P<pk>[0-9]+)/ad_hoc_commands/$', HostAdHocCommandsList.as_view(), name='host_ad_hoc_commands_list'), url(r'^(?P<pk>[0-9]+)/ad_hoc_command_events/$', HostAdHocCommandEventsList.as_view(), name='host_ad_hoc_command_events_list'), url(r'^(?P<pk>[0-9]+)/fact_versions/$', HostFactVersionsList.as_view(), name='host_fact_versions_list'), url(r'^(?P<pk>[0-9]+)/fact_view/$', HostFactCompareView.as_view(), name='host_fact_compare_view'), url(r'^(?P<pk>[0-9]+)/insights/$', HostInsights.as_view(), name='host_insights'), ] __all__ = ['urls']
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'])