From 87b2799e3bc36291628e97751a0bc3e74f89fd0b Mon Sep 17 00:00:00 2001 From: Veiko Aasa Date: Sat, 2 Jan 2021 10:31:28 +0200 Subject: [PATCH] users: Skip action script tests if LDAP is not set up - Also, make sure an admin account exists before some tests. Tests performed: - The users module test_actions unit tests are skipped if LDAP is not set up. - The users module unit tests pass if LDAP is set up. Signed-off-by: Veiko Aasa --- plinth/modules/users/tests/test_actions.py | 65 +++++++++++++++++----- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/plinth/modules/users/tests/test_actions.py b/plinth/modules/users/tests/test_actions.py index b88404476..44de66169 100644 --- a/plinth/modules/users/tests/test_actions.py +++ b/plinth/modules/users/tests/test_actions.py @@ -21,7 +21,19 @@ from plinth.tests import config as test_config _cleanup_users = None _cleanup_groups = None -pytestmark = pytest.mark.usefixtures('needs_root', 'load_cfg') + +def _is_ldap_set_up(): + """Return whether LDAP is set up.""" + return subprocess.call([ + 'ldapsearch', '-Y', 'EXTERNAL', '-H', 'ldapi:///', '-b', + 'ou=groups,dc=thisbox' + ]) == 0 + + +pytestmark = [ + pytest.mark.usefixtures('needs_root', 'load_cfg'), + pytest.mark.skipif(not _is_ldap_set_up(), reason="LDAP is not configured") +] def _random_string(length=8): @@ -150,6 +162,14 @@ def _delete_user(username): _call_action(['remove-user', username], input=process_input) +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']) + + def _get_admin_user_password(): """Return an admin username and password.""" @@ -161,6 +181,10 @@ 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') + return (admin_users[0], admin_users[0] + '_passwd') @@ -200,8 +224,10 @@ def _delete_group(groupname): def test_create_user(): """Test whether creating a new user works.""" - username, password = _create_user(groups=['admin', _random_string()]) - # assert_can_login_to_console(username, password) + _create_admin_if_does_not_exist() + + username, password = _create_user(groups=[_random_string()]) + assert _try_login_to_ssh(username, password) assert username in _get_samba_users() with pytest.raises(subprocess.CalledProcessError): @@ -210,12 +236,13 @@ def test_create_user(): def test_change_user_password(): """Test changing user password.""" - username, old_password = _create_user(groups=['admin']) + _create_admin_if_does_not_exist() + admin_user, admin_password = _get_admin_user_password() + + username, old_password = _create_user() old_password_hash = _get_password_hash(username) new_password = 'pass $123' - admin_user, admin_password = _get_admin_user_password() - process_input = "{0}\n{1}".format(new_password, admin_password).encode() _call_action(['set-user-password', username, '--auth-user', admin_user], input=process_input) @@ -231,6 +258,8 @@ def test_change_user_password(): def test_change_password_as_non_admin_user(): """Test changing user password as a non-admin user.""" + _create_admin_if_does_not_exist() + username, old_password = _create_user() old_password_hash = _get_password_hash(username) new_password = 'pass $123' @@ -250,6 +279,8 @@ def test_change_password_as_non_admin_user(): def test_change_other_users_password_as_non_admin(): """Test that changing other user's password as a non-admin user fails.""" + _create_admin_if_does_not_exist() + username1, password1 = _create_user() username2, _ = _create_user() new_password = 'pass $123' @@ -263,17 +294,22 @@ def test_change_other_users_password_as_non_admin(): def test_set_password_for_non_existent_user(): """Test setting password for a non-existent user.""" + _create_admin_if_does_not_exist() + admin_user, admin_password = _get_admin_user_password() + non_existent_user = _random_string() - fake_password = _random_string().encode() + fake_password = _random_string() + + process_input = "{0}\n{1}".format(fake_password, admin_password).encode() with pytest.raises(subprocess.CalledProcessError): - _call_action(['set-user-password', non_existent_user], - input=fake_password) + _call_action([ + 'set-user-password', non_existent_user, '--auth-user', admin_user + ], input=process_input) def test_rename_user(): """Test whether renaming a user works.""" - # create an admin user to create other users with - _create_user(groups=['admin']) + _create_admin_if_does_not_exist() old_username, password = _create_user(groups=['admin', _random_string()]) old_groups = _get_user_groups(old_username) @@ -304,6 +340,8 @@ def test_rename_user(): def test_delete_user(): """Test to check whether LDAP users can be deleted""" + _create_admin_if_does_not_exist() + username, password = _create_user(groups=[_random_string()]) _delete_user(username) groups_after = _get_user_groups(username) @@ -352,12 +390,13 @@ def test_delete_admin_group_fails(): def test_user_group_interactions(): """Test adding/removing user from a groups.""" + _create_admin_if_does_not_exist() + admin_user, admin_password = _get_admin_user_password() + group1 = _random_string() user1, _ = _create_user(groups=[group1]) assert [group1] == _get_user_groups(user1) - admin_user, admin_password = _get_admin_user_password() - # add-user-to-group is not idempotent with pytest.raises(subprocess.CalledProcessError): _call_action(