From 97ececa8b459484bcfbec63f15a47be9be119241 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Wed, 2 Oct 2024 11:27:08 -0400 Subject: [PATCH] Fix 500 error due to None data in DAB response (#15562) * Fix 500 error due to None data in DAB response * NOQA for flake8 failures --- awx/api/generics.py | 3 +- awx/main/tests/functional/api/test_user.py | 32 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/awx/api/generics.py b/awx/api/generics.py index a059ca834ffb..f9b1e04bbc8c 100644 --- a/awx/api/generics.py +++ b/awx/api/generics.py @@ -244,7 +244,8 @@ def finalize_response(self, request, response, *args, **kwargs): if hasattr(self, '__init_request_error__'): response = self.handle_exception(self.__init_request_error__) if response.status_code == 401: - response.data['detail'] += _(' To establish a login session, visit') + ' /api/login/.' + if response.data and 'detail' in response.data: + response.data['detail'] += _(' To establish a login session, visit') + ' /api/login/.' logger.info(status_msg) else: logger.warning(status_msg) diff --git a/awx/main/tests/functional/api/test_user.py b/awx/main/tests/functional/api/test_user.py index f762990a55bc..87234c18baf0 100644 --- a/awx/main/tests/functional/api/test_user.py +++ b/awx/main/tests/functional/api/test_user.py @@ -4,6 +4,11 @@ import pytest from django.contrib.sessions.middleware import SessionMiddleware +from django.test.utils import override_settings +from django.contrib.auth.models import AnonymousUser + +from ansible_base.lib.utils.response import get_relative_url +from ansible_base.lib.testing.fixtures import settings_override_mutable # NOQA: F401 imported to be a pytest fixture from awx.main.models import User from awx.api.versioning import reverse @@ -16,6 +21,33 @@ EXAMPLE_USER_DATA = {"username": "affable", "first_name": "a", "last_name": "a", "email": "a@a.com", "is_superuser": False, "password": "r$TyKiOCb#ED"} +@pytest.mark.django_db +def test_validate_local_user(post, admin_user, settings, settings_override_mutable): # NOQA: F811 this is how you use a pytest fixture + "Copy of the test by same name in django-ansible-base for integration and compatibility testing" + url = get_relative_url('validate-local-account') + admin_user.set_password('password') + admin_user.save() + data = { + "username": admin_user.username, + "password": "password", + } + with override_settings(RESOURCE_SERVER={"URL": "https://foo.invalid", "SECRET_KEY": "foobar"}): + response = post(url=url, data=data, user=AnonymousUser(), expect=200) + + assert 'ansible_id' in response.data + assert response.data['auth_code'] is not None, response.data + + # No resource server, return coherent response but can not provide auth code + response = post(url=url, data=data, user=AnonymousUser(), expect=200) + assert 'ansible_id' in response.data + assert response.data['auth_code'] is None + + # wrong password + data['password'] = 'foobar' + response = post(url=url, data=data, user=AnonymousUser(), expect=401) + # response.data may be none here, this is just testing that we get no server error + + @pytest.mark.django_db def test_user_create(post, admin): response = post(reverse('api:user_list'), EXAMPLE_USER_DATA, admin, middleware=SessionMiddleware(mock.Mock()))