mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
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 <veiko17@disroot.org>
This commit is contained in:
parent
9d6cbceb15
commit
87b2799e3b
@ -21,7 +21,19 @@ from plinth.tests import config as test_config
|
|||||||
_cleanup_users = None
|
_cleanup_users = None
|
||||||
_cleanup_groups = 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):
|
def _random_string(length=8):
|
||||||
@ -150,6 +162,14 @@ def _delete_user(username):
|
|||||||
_call_action(['remove-user', username], input=process_input)
|
_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():
|
def _get_admin_user_password():
|
||||||
"""Return an admin username and password."""
|
"""Return an admin username and password."""
|
||||||
|
|
||||||
@ -161,6 +181,10 @@ def _get_admin_user_password():
|
|||||||
if test_config.admin_username in admin_users:
|
if test_config.admin_username in admin_users:
|
||||||
return (test_config.admin_username, test_config.admin_password)
|
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')
|
return (admin_users[0], admin_users[0] + '_passwd')
|
||||||
|
|
||||||
|
|
||||||
@ -200,8 +224,10 @@ def _delete_group(groupname):
|
|||||||
|
|
||||||
def test_create_user():
|
def test_create_user():
|
||||||
"""Test whether creating a new user works."""
|
"""Test whether creating a new user works."""
|
||||||
username, password = _create_user(groups=['admin', _random_string()])
|
_create_admin_if_does_not_exist()
|
||||||
# assert_can_login_to_console(username, password)
|
|
||||||
|
username, password = _create_user(groups=[_random_string()])
|
||||||
|
|
||||||
assert _try_login_to_ssh(username, password)
|
assert _try_login_to_ssh(username, password)
|
||||||
assert username in _get_samba_users()
|
assert username in _get_samba_users()
|
||||||
with pytest.raises(subprocess.CalledProcessError):
|
with pytest.raises(subprocess.CalledProcessError):
|
||||||
@ -210,12 +236,13 @@ def test_create_user():
|
|||||||
|
|
||||||
def test_change_user_password():
|
def test_change_user_password():
|
||||||
"""Test changing 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)
|
old_password_hash = _get_password_hash(username)
|
||||||
new_password = 'pass $123'
|
new_password = 'pass $123'
|
||||||
|
|
||||||
admin_user, admin_password = _get_admin_user_password()
|
|
||||||
|
|
||||||
process_input = "{0}\n{1}".format(new_password, admin_password).encode()
|
process_input = "{0}\n{1}".format(new_password, admin_password).encode()
|
||||||
_call_action(['set-user-password', username, '--auth-user', admin_user],
|
_call_action(['set-user-password', username, '--auth-user', admin_user],
|
||||||
input=process_input)
|
input=process_input)
|
||||||
@ -231,6 +258,8 @@ def test_change_user_password():
|
|||||||
|
|
||||||
def test_change_password_as_non_admin_user():
|
def test_change_password_as_non_admin_user():
|
||||||
"""Test changing user password as a non-admin user."""
|
"""Test changing user password as a non-admin user."""
|
||||||
|
_create_admin_if_does_not_exist()
|
||||||
|
|
||||||
username, old_password = _create_user()
|
username, old_password = _create_user()
|
||||||
old_password_hash = _get_password_hash(username)
|
old_password_hash = _get_password_hash(username)
|
||||||
new_password = 'pass $123'
|
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():
|
def test_change_other_users_password_as_non_admin():
|
||||||
"""Test that changing other user's password as a non-admin user fails."""
|
"""Test that changing other user's password as a non-admin user fails."""
|
||||||
|
_create_admin_if_does_not_exist()
|
||||||
|
|
||||||
username1, password1 = _create_user()
|
username1, password1 = _create_user()
|
||||||
username2, _ = _create_user()
|
username2, _ = _create_user()
|
||||||
new_password = 'pass $123'
|
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():
|
def test_set_password_for_non_existent_user():
|
||||||
"""Test setting password for a 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()
|
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):
|
with pytest.raises(subprocess.CalledProcessError):
|
||||||
_call_action(['set-user-password', non_existent_user],
|
_call_action([
|
||||||
input=fake_password)
|
'set-user-password', non_existent_user, '--auth-user', admin_user
|
||||||
|
], input=process_input)
|
||||||
|
|
||||||
|
|
||||||
def test_rename_user():
|
def test_rename_user():
|
||||||
"""Test whether renaming a user works."""
|
"""Test whether renaming a user works."""
|
||||||
# create an admin user to create other users with
|
_create_admin_if_does_not_exist()
|
||||||
_create_user(groups=['admin'])
|
|
||||||
|
|
||||||
old_username, password = _create_user(groups=['admin', _random_string()])
|
old_username, password = _create_user(groups=['admin', _random_string()])
|
||||||
old_groups = _get_user_groups(old_username)
|
old_groups = _get_user_groups(old_username)
|
||||||
@ -304,6 +340,8 @@ def test_rename_user():
|
|||||||
|
|
||||||
def test_delete_user():
|
def test_delete_user():
|
||||||
"""Test to check whether LDAP users can be deleted"""
|
"""Test to check whether LDAP users can be deleted"""
|
||||||
|
_create_admin_if_does_not_exist()
|
||||||
|
|
||||||
username, password = _create_user(groups=[_random_string()])
|
username, password = _create_user(groups=[_random_string()])
|
||||||
_delete_user(username)
|
_delete_user(username)
|
||||||
groups_after = _get_user_groups(username)
|
groups_after = _get_user_groups(username)
|
||||||
@ -352,12 +390,13 @@ def test_delete_admin_group_fails():
|
|||||||
|
|
||||||
def test_user_group_interactions():
|
def test_user_group_interactions():
|
||||||
"""Test adding/removing user from a groups."""
|
"""Test adding/removing user from a groups."""
|
||||||
|
_create_admin_if_does_not_exist()
|
||||||
|
admin_user, admin_password = _get_admin_user_password()
|
||||||
|
|
||||||
group1 = _random_string()
|
group1 = _random_string()
|
||||||
user1, _ = _create_user(groups=[group1])
|
user1, _ = _create_user(groups=[group1])
|
||||||
assert [group1] == _get_user_groups(user1)
|
assert [group1] == _get_user_groups(user1)
|
||||||
|
|
||||||
admin_user, admin_password = _get_admin_user_password()
|
|
||||||
|
|
||||||
# add-user-to-group is not idempotent
|
# add-user-to-group is not idempotent
|
||||||
with pytest.raises(subprocess.CalledProcessError):
|
with pytest.raises(subprocess.CalledProcessError):
|
||||||
_call_action(
|
_call_action(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user