summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Rominger <arominge@redhat.com>2024-10-02 17:27:08 +0200
committerGitHub <noreply@github.com>2024-10-02 17:27:08 +0200
commit97ececa8b459484bcfbec63f15a47be9be119241 (patch)
tree7f2ecca0b03ddd55993fde69ec1772449aabea21
parentRemoved docs associated with SAML auth. (#15563) (diff)
downloadawx-97ececa8b459484bcfbec63f15a47be9be119241.tar.xz
awx-97ececa8b459484bcfbec63f15a47be9be119241.zip
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
-rw-r--r--awx/api/generics.py3
-rw-r--r--awx/main/tests/functional/api/test_user.py32
2 files changed, 34 insertions, 1 deletions
diff --git a/awx/api/generics.py b/awx/api/generics.py
index a059ca834f..f9b1e04bbc 100644
--- a/awx/api/generics.py
+++ b/awx/api/generics.py
@@ -244,7 +244,8 @@ class APIView(views.APIView):
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 f762990a55..87234c18ba 100644
--- a/awx/main/tests/functional/api/test_user.py
+++ b/awx/main/tests/functional/api/test_user.py
@@ -4,6 +4,11 @@ from unittest import mock
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
@@ -17,6 +22,33 @@ EXAMPLE_USER_DATA = {"username": "affable", "first_name": "a", "last_name": "a",
@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()))
assert response.status_code == 201