diff --git a/app/models.py b/app/models.py index 9a8aa707c147cf0703586ad23c7a2d6be5245e3c..ab17530547f38d9239b0b061339c47eff71f14d0 100644 --- a/app/models.py +++ b/app/models.py @@ -101,7 +101,7 @@ def save_user(dn, username, data, memberships): if user is None: user = User( username=username, - display_name=utils.attribute_to_string(data["cn"]), + display_name=utils.attribute_to_string(data["cn"]) or username, email=utils.attribute_to_string(data["mail"]), ) # Always update the user groups to keep them up-to-date diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index edfb19113679b2e82d186fe656c9026f8ccdec15..ee178a0053f082395fde917833f446c3e016e32a 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -175,8 +175,19 @@ def patch_ldap_authenticate(monkeypatch): response.user_groups = [{"cn": "CSEntry Admin"}] elif username == "audit" and password == "auditpasswd": response.status = AuthenticationResponseStatus.success - response.user_info = {"cn": "Auditor User", "mail": "audit@example.com"} - response.user_groups = [{"cn": "CSEntry Auditor"}] + response.user_dn = "uid=audit,ou=Service accounts,dc=esss,dc=lu,dc=se" + response.user_info = { + "uid": ["audit"], + "cn": [], + "mail": [], + "dn": "uid=audit,ou=Service accounts,dc=esss,dc=lu,dc=se", + } + response.user_groups = [ + { + "cn": ["CSEntry Auditor"], + "dn": "cn=CSEntry Auditor,ou=ICS,ou=Groups,dc=esss,dc=lu,dc=se", + } + ] elif username == "user_rw" and password == "userrw": response.status = AuthenticationResponseStatus.success response.user_info = {"cn": "User RW", "mail": "user_rw@example.com"} diff --git a/tests/functional/test_models.py b/tests/functional/test_models.py index aee0cbf818bdfc16ecdc2003ac8ca35ef3166572..3fb410f0e996b53830e2cc6a167ce22bf9d3cc68 100644 --- a/tests/functional/test_models.py +++ b/tests/functional/test_models.py @@ -1264,3 +1264,61 @@ def test_host_sensitive_field_update_on_network_change( instances, nb = models.Host.search("sensitive:true") assert nb == 1 assert instances[0].name == name + + +@pytest.mark.parametrize( + "dn,username,user_info,user_groups,expected_display_name,expected_email,expected_groups", + [ + ( + "uid=johndoe,ou=Users,dc=esss,dc=lu,dc=se", + "johndoe", + {"mail": "john.doe@example.org", "cn": "John Doe"}, + [{"cn": "group2"}, {"cn": "group1"}], + "John Doe", + "john.doe@example.org", + ["group1", "group2"], + ), + ( + "uid=johndoe,ou=Users,dc=esss,dc=lu,dc=se", + "johndoe", + {"mail": ["john.doe@example.org"], "cn": ["John Doe"]}, + [{"cn": ["group2"]}, {"cn": ["group1"]}], + "John Doe", + "john.doe@example.org", + ["group1", "group2"], + ), + ( + "uid=auditor,ou=Service accounts,dc=esss,dc=lu,dc=se", + "auditor", + { + "uid": ["auditor"], + "cn": [], + "mail": [], + "dn": "uid=csentry_svc,ou=Service accounts,dc=esss,dc=lu,dc=se", + }, + [ + { + "cn": ["csentry_auditors"], + "dn": "cn=csentry_auditors,ou=ICS,ou=Groups,dc=esss,dc=lu,dc=se", + } + ], + "auditor", + "", + ["csentry_auditors"], + ), + ], +) +def test_save_user( + dn, + username, + user_info, + user_groups, + expected_display_name, + expected_email, + expected_groups, +): + user = models.save_user(dn, username, user_info, user_groups) + assert user.username == username + assert user.display_name == expected_display_name + assert user.email == expected_email + assert user.groups == expected_groups diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 68542e37e826498e1187393316397e213fa08261..9257866c62644a25628ad756d46bbb5b02a1ec2d 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -9,6 +9,7 @@ This module defines utils tests. :license: BSD 2-Clause, see LICENSE for more details. """ +import pytest from pathlib import Path from app import utils @@ -45,3 +46,17 @@ class TestUniqueFilename: p = tmpdir.join("test") p.write("Hello") assert utils.unique_filename(p) == Path(tmpdir.join("test-1")) + + +@pytest.mark.parametrize( + "input,expected", + [ + ([], ""), + (["foo"], "foo"), + (["foo", "bar"], "foo"), + ("hello", "hello"), + ("", ""), + ], +) +def test_attribute_to_string(input, expected): + assert utils.attribute_to_string(input) == expected