diff --git a/awx/api/views.py b/awx/api/views.py index 0b6047be92..cf40687256 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2116,7 +2116,9 @@ class HostInsights(GenericAPIView): except requests.exceptions.RequestException as e: return (dict(error=_('Unkown exception {} while trying to GET {}').format(e, url)), status.HTTP_502_BAD_GATEWAY) - if res.status_code != 200: + if res.status_code == 401: + return (dict(error=_('Unauthorized access. Please check your Insights Credential username and password.')), status.HTTP_502_BAD_GATEWAY) + elif res.status_code != 200: return (dict(error=_('Failed to gather reports and maintenance plans from Insights API at URL {}. Server responded with {} status code and message {}').format(url, res.status_code, res.content)), status.HTTP_502_BAD_GATEWAY) try: diff --git a/awx/main/tests/unit/api/test_views.py b/awx/main/tests/unit/api/test_views.py index f926f4eade..2402186606 100644 --- a/awx/main/tests/unit/api/test_views.py +++ b/awx/main/tests/unit/api/test_views.py @@ -158,6 +158,14 @@ class TestHostInsights(): (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_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_malformed_json_content(self, patch_parent, mocker): view = HostInsights()