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 <veiko17@disroot.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Veiko Aasa 2021-03-09 22:16:08 +02:00 committed by James Valleroy
parent c1bcf30e44
commit 78427f730c
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -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')