From 78427f730cf39dc839ed3e2478458b251c18f95a Mon Sep 17 00:00:00 2001 From: Veiko Aasa Date: Tue, 9 Mar 2021 22:16:08 +0200 Subject: [PATCH] users: Fix unit test failures when LDAP is empty When LDAP is set up but the first admin is not yet set up, some of the users module tests fail because known admin users is deleted before other users. A known admin user must exists to delete existing users. Fix this by deleting a known admin user only after deleting other users when cleaning up tests. Tests performed on Debian stable and testing: - All the users module unit tests pass: - when LDAP is not set up yet - after LDAP is set up but empty - after first admin user has set up using the web UI Signed-off-by: Veiko Aasa Reviewed-by: James Valleroy --- plinth/modules/users/tests/test_actions.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/plinth/modules/users/tests/test_actions.py b/plinth/modules/users/tests/test_actions.py index 4fd2cdbf7..5c038e814 100644 --- a/plinth/modules/users/tests/test_actions.py +++ b/plinth/modules/users/tests/test_actions.py @@ -21,6 +21,9 @@ from plinth.tests import config as test_config _cleanup_users = None _cleanup_groups = None +# Temporary admin user created if an admin doesn't already exist +PYTEST_ADMIN_USERNAME = 'pytest_admin' + def _is_ldap_set_up(): """Return whether LDAP is set up.""" @@ -115,12 +118,22 @@ def fixture_auto_cleanup_users_groups(needs_root, load_cfg): _cleanup_groups = set() yield + pytest_admin_exists = PYTEST_ADMIN_USERNAME in _cleanup_users + for user in _cleanup_users: + if user == PYTEST_ADMIN_USERNAME: + continue try: _delete_user(user) except Exception: pass + if pytest_admin_exists: + try: + _delete_user(PYTEST_ADMIN_USERNAME) + except Exception: + pass + for group in _cleanup_groups: _delete_group(group) @@ -169,8 +182,7 @@ def _create_admin_if_does_not_exist(): """Create a main admin user""" admin_user, _ = _get_admin_user_password() if not admin_user: - username = "pytest_admin" - _create_user(username, ['admin']) + _create_user(PYTEST_ADMIN_USERNAME, ['admin']) def _get_admin_user_password(): @@ -184,9 +196,8 @@ def _get_admin_user_password(): if test_config.admin_username in admin_users: return (test_config.admin_username, test_config.admin_password) - pytest_admin_username = 'pytest_admin' - if pytest_admin_username in admin_users: - return (pytest_admin_username, pytest_admin_username + '_passwd') + if PYTEST_ADMIN_USERNAME in admin_users: + return (PYTEST_ADMIN_USERNAME, PYTEST_ADMIN_USERNAME + '_passwd') return (admin_users[0], admin_users[0] + '_passwd')