From 8c64fa2fe1f318116ac213f22590f863edf67c71 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 5 Dec 2016 12:05:00 -0500 Subject: [PATCH] handle is_system_auditor case with unsaved users --- awx/main/models/__init__.py | 8 +++- awx/main/tests/functional/api/test_user.py | 56 +++++++--------------- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/awx/main/models/__init__.py b/awx/main/models/__init__.py index d8b206e18a..0475da8eb7 100644 --- a/awx/main/models/__init__.py +++ b/awx/main/models/__init__.py @@ -76,8 +76,12 @@ User.add_to_class('auditor_of_organizations', user_get_auditor_of_organizations) @property def user_is_system_auditor(user): if not hasattr(user, '_is_system_auditor'): - user._is_system_auditor = user.roles.filter( - singleton_name='system_auditor', role_field='system_auditor').exists() + if user.pk: + user._is_system_auditor = user.roles.filter( + singleton_name='system_auditor', role_field='system_auditor').exists() + else: + # Odd case where user is unsaved, this should never be relied on + user._is_system_auditor = False return user._is_system_auditor diff --git a/awx/main/tests/functional/api/test_user.py b/awx/main/tests/functional/api/test_user.py index fb4c7a33e0..e3b7b4145c 100644 --- a/awx/main/tests/functional/api/test_user.py +++ b/awx/main/tests/functional/api/test_user.py @@ -7,65 +7,41 @@ from django.core.urlresolvers import reverse # user creation # +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_user_create(post, admin): - response = post(reverse('api:user_list'), { - "username": "affable", - "first_name": "a", - "last_name": "a", - "email": "a@a.com", - "is_superuser": False, - "password": "fo0m4nchU" - }, admin) + response = post(reverse('api:user_list'), EXAMPLE_USER_DATA, admin) assert response.status_code == 201 + assert not response.data['is_superuser'] + assert not response.data['is_system_auditor'] @pytest.mark.django_db def test_fail_double_create_user(post, admin): - response = post(reverse('api:user_list'), { - "username": "affable", - "first_name": "a", - "last_name": "a", - "email": "a@a.com", - "is_superuser": False, - "password": "fo0m4nchU" - }, admin) + response = post(reverse('api:user_list'), EXAMPLE_USER_DATA, admin) assert response.status_code == 201 - response = post(reverse('api:user_list'), { - "username": "affable", - "first_name": "a", - "last_name": "a", - "email": "a@a.com", - "is_superuser": False, - "password": "fo0m4nchU" - }, admin) + response = post(reverse('api:user_list'), EXAMPLE_USER_DATA, admin) assert response.status_code == 400 @pytest.mark.django_db def test_create_delete_create_user(post, delete, admin): - response = post(reverse('api:user_list'), { - "username": "affable", - "first_name": "a", - "last_name": "a", - "email": "a@a.com", - "is_superuser": False, - "password": "fo0m4nchU" - }, admin) + response = post(reverse('api:user_list'), EXAMPLE_USER_DATA, admin) assert response.status_code == 201 response = delete(reverse('api:user_detail', args=(response.data['id'],)), admin) assert response.status_code == 204 - response = post(reverse('api:user_list'), { - "username": "affable", - "first_name": "a", - "last_name": "a", - "email": "a@a.com", - "is_superuser": False, - "password": "fo0m4nchU" - }, admin) + response = post(reverse('api:user_list'), EXAMPLE_USER_DATA, admin) print(response.data) assert response.status_code == 201