From 80023017a23c2c5a33d2c3da01fb3a72c34e93b1 Mon Sep 17 00:00:00 2001 From: Nikhil Jain Date: Thu, 11 Mar 2021 19:14:50 +0530 Subject: [PATCH 1/4] fix the tower_user module to update the fields properly --- awx_collection/plugins/modules/tower_user.py | 14 ++++++------- awx_collection/test/awx/test_user.py | 21 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/awx_collection/plugins/modules/tower_user.py b/awx_collection/plugins/modules/tower_user.py index 8f32c42666..b57266dbc0 100644 --- a/awx_collection/plugins/modules/tower_user.py +++ b/awx_collection/plugins/modules/tower_user.py @@ -149,19 +149,19 @@ def main(): # Create the data that gets sent for create and update new_fields = {} - if username: + if username is not None: new_fields['username'] = module.get_item_name(existing_item) if existing_item else username - if first_name: + if first_name is not None: new_fields['first_name'] = first_name - if last_name: + if last_name is not None: new_fields['last_name'] = last_name - if email: + if email is not None: new_fields['email'] = email - if is_superuser: + if is_superuser is not None: new_fields['is_superuser'] = is_superuser - if is_system_auditor: + if is_system_auditor is not None: new_fields['is_system_auditor'] = is_system_auditor - if password: + if password is not None: new_fields['password'] = password # If the state was present and we can let the module build or update the existing item, this will return on its own diff --git a/awx_collection/test/awx/test_user.py b/awx_collection/test/awx/test_user.py index 6c49f04121..1877d39df7 100644 --- a/awx_collection/test/awx/test_user.py +++ b/awx_collection/test/awx/test_user.py @@ -57,3 +57,24 @@ def test_update_password_on_create(run_module, admin_user, mock_auth_stuff): assert not result.get('failed', False), result.get('msg', result) assert not result.get('changed') + + +@pytest.mark.django_db +def test_update_user(run_module, admin_user, mock_auth_stuff): +    result = run_module('tower_user', dict( +        username='Bob', +        password='pass4word', +        is_system_auditor=True +    ), admin_user) +    assert not result.get('failed', False), result.get('msg', result) +    assert result.get('changed'), result + +    update_result =     result = run_module('tower_user', dict( +        username='Bob', +        password='pass4word', +        is_system_auditor=False +    ), admin_user) + +    assert update_result.get('changed') +    user = User.objects.get(id=result['id']) +    assert not user.is_system_auditor From 8e53453737fafa459849d0ee74105252078146c1 Mon Sep 17 00:00:00 2001 From: Nikhil Jain Date: Thu, 11 Mar 2021 19:35:53 +0530 Subject: [PATCH 2/4] remove the extra result in test --- awx_collection/test/awx/test_user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/test/awx/test_user.py b/awx_collection/test/awx/test_user.py index 1877d39df7..34adfaa6e9 100644 --- a/awx_collection/test/awx/test_user.py +++ b/awx_collection/test/awx/test_user.py @@ -69,7 +69,7 @@ def test_update_user(run_module, admin_user, mock_auth_stuff):     assert not result.get('failed', False), result.get('msg', result)     assert result.get('changed'), result -    update_result =     result = run_module('tower_user', dict( +    update_result = run_module('tower_user', dict(         username='Bob',         password='pass4word',         is_system_auditor=False From 53da8e0775c0aeea1cb2b2b64c426ba907064e7b Mon Sep 17 00:00:00 2001 From: Nikhil Jain Date: Thu, 11 Mar 2021 19:50:11 +0530 Subject: [PATCH 3/4] removing some invalid chars --- awx_collection/test/awx/test_user.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/awx_collection/test/awx/test_user.py b/awx_collection/test/awx/test_user.py index 34adfaa6e9..a530e63d90 100644 --- a/awx_collection/test/awx/test_user.py +++ b/awx_collection/test/awx/test_user.py @@ -61,7 +61,7 @@ def test_update_password_on_create(run_module, admin_user, mock_auth_stuff): @pytest.mark.django_db def test_update_user(run_module, admin_user, mock_auth_stuff): -    result = run_module('tower_user', dict( + result = run_module('tower_user', dict(         username='Bob',         password='pass4word',         is_system_auditor=True @@ -69,9 +69,8 @@ def test_update_user(run_module, admin_user, mock_auth_stuff):     assert not result.get('failed', False), result.get('msg', result)     assert result.get('changed'), result -    update_result = run_module('tower_user', dict( + update_result = run_module('tower_user', dict(         username='Bob', -        password='pass4word',         is_system_auditor=False     ), admin_user) From 2aa30226f43fd64a3278b0d33e870293bac6520f Mon Sep 17 00:00:00 2001 From: Nikhil Jain Date: Thu, 11 Mar 2021 20:07:31 +0530 Subject: [PATCH 4/4] removing some invalid chars --- awx_collection/test/awx/test_user.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/awx_collection/test/awx/test_user.py b/awx_collection/test/awx/test_user.py index a530e63d90..6a0dfa123d 100644 --- a/awx_collection/test/awx/test_user.py +++ b/awx_collection/test/awx/test_user.py @@ -62,18 +62,18 @@ def test_update_password_on_create(run_module, admin_user, mock_auth_stuff): @pytest.mark.django_db def test_update_user(run_module, admin_user, mock_auth_stuff): result = run_module('tower_user', dict( -        username='Bob', -        password='pass4word', -        is_system_auditor=True -    ), admin_user) -    assert not result.get('failed', False), result.get('msg', result) -    assert result.get('changed'), result + username='Bob', + password='pass4word', + is_system_auditor=True + ), admin_user) + assert not result.get('failed', False), result.get('msg', result) + assert result.get('changed'), result update_result = run_module('tower_user', dict( -        username='Bob', -        is_system_auditor=False -    ), admin_user) + username='Bob', + is_system_auditor=False + ), admin_user) -    assert update_result.get('changed') -    user = User.objects.get(id=result['id']) -    assert not user.is_system_auditor + assert update_result.get('changed') + user = User.objects.get(id=result['id']) + assert not user.is_system_auditor