Files
yunohost/tests/test_ldapauth.py
machine-a-tsoins edb4c397df
Some checks failed
Check for new n releases / updater (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
🍽 Fork YunoHost — snapshot mangé par la machine à tsoins
Upstream: https://github.com/YunoHost/yunohost @ 3a5f8bac8301c450897b96cbd43a4c7d3ba750fb
But (José) : transformer tout le code en bions + ploxions du xerboxion.
La carte de digestion vit au labo : /yunohost-digest.json
2026-07-03 17:20:06 +00:00

100 lines
3.3 KiB
Python

#!/usr/bin/env python3
#
# Copyright (c) 2024 YunoHost Contributors
#
# This file is part of YunoHost (see https://yunohost.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import pytest
from moulinette import m18n
from moulinette.core import MoulinetteError
from yunohost.authenticators.ldap_admin import Authenticator as LDAPAuth
from yunohost.domain import _get_maindomain
from yunohost.user import user_create, user_delete, user_list, user_update
def setup_function(function):
for u in user_list()["users"]:
user_delete(u, purge=True, force=True)
maindomain = _get_maindomain()
if os.system("systemctl is-active slapd >/dev/null") != 0:
os.system("systemctl start slapd && sleep 3")
user_create("alice", maindomain, "Yunohost", admin=True, fullname="Alice White")
user_create("bob", maindomain, "test123Ynh", fullname="Bob Snow")
def teardown_function():
os.system("systemctl is-active slapd >/dev/null || systemctl start slapd; sleep 5")
for u in user_list()["users"]:
user_delete(u, purge=True, force=True)
def test_authenticate():
LDAPAuth().authenticate_credentials(credentials="alice:Yunohost")
def test_authenticate_with_no_user():
with pytest.raises(MoulinetteError):
LDAPAuth().authenticate_credentials(credentials="Yunohost")
with pytest.raises(MoulinetteError):
LDAPAuth().authenticate_credentials(credentials=":Yunohost")
def test_authenticate_with_user_who_is_not_admin():
with pytest.raises(MoulinetteError) as exception:
LDAPAuth().authenticate_credentials(credentials="bob:test123Ynh")
translation = m18n.n("invalid_credentials")
expected_msg = translation.format()
assert expected_msg in str(exception)
def test_authenticate_with_wrong_password():
with pytest.raises(MoulinetteError) as exception:
LDAPAuth().authenticate_credentials(credentials="alice:bad_password_lul")
translation = m18n.n("invalid_credentials")
expected_msg = translation.format()
assert expected_msg in str(exception)
def test_authenticate_server_down():
os.system("systemctl stop slapd && sleep 5")
LDAPAuth().authenticate_credentials(credentials="alice:Yunohost")
def test_authenticate_change_password():
LDAPAuth().authenticate_credentials(credentials="alice:Yunohost")
user_update("alice", change_password="plopette")
with pytest.raises(MoulinetteError) as exception:
LDAPAuth().authenticate_credentials(credentials="alice:Yunohost")
translation = m18n.n("invalid_credentials")
expected_msg = translation.format()
assert expected_msg in str(exception)
LDAPAuth().authenticate_credentials(credentials="alice:plopette")