Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import pytest
from dataclasses import fields
from unittest.mock import MagicMock

from linuxmusterTools.ldapconnector.models.common import LMNModel
from linuxmusterTools.ldapconnector.models.lmnusermixin import LMNUserMixin
from linuxmusterTools.ldapconnector.models.lmngroup import LMNGroupModel
from linuxmusterTools.ldapconnector.models.lmnproject import LMNProjectModel
from linuxmusterTools.ldapconnector.models.lmnuser import LMNUserModel
from linuxmusterTools.ldapconnector.urls.ldaprouter import router

DN = 'CN=johndoe,OU=7a,OU=Students,OU=default-school,OU=SCHOOLS,DC=linuxmuster,DC=lan'
Expand Down Expand Up @@ -48,6 +50,35 @@ def test_common_name_on_ou_dn(self):
assert obj.common_name('OU=Students,DC=test,DC=lan') == 'Students'


class TestStudentParents:

@pytest.mark.parametrize('school, username', [
('default-school', 'anna.test'),
('agy', 'agy-anna.test'),
])
@pytest.mark.parametrize('group, expected_parents', [
({}, []),
({'member': ['CN=parent.test,OU=Parents,DC=test,DC=lan']}, ['parent.test']),
])
def test_initializes_dotted_student_with_parent_lookup(
self, monkeypatch, school, username, group, expected_parents
):
read_group = MagicMock(return_value=group)
monkeypatch.setattr(router.lr, 'get_single', read_group)
data = {field.name: field.type() for field in fields(LMNUserModel) if field.init}
data.update(cn=username, sophomorixRole='student', sophomorixSchoolname=school)

user = LMNUserModel(**data, custom_fields_config={})

assert user.parents == expected_parents
read_group.assert_called_once()
args, kwargs = read_group.call_args
assert args[0] is LMNGroupModel
assert f'(cn={username}-parents)' in args[1]
assert kwargs['school'] == school
assert kwargs['subdn'] == f'OU={school},OU=SCHOOLS,'


class TestCheckSchoolclassNumber:

def test_numeric_prefix_returns_int(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ def test_groups_collection_route(self):
func, data = router._find_method('/groups')
assert func.type == 'collection'

@pytest.mark.parametrize('name', [
'anna.test-parents',
'agy-anna.test-parents',
'annatest-parents',
'anna-test-parents',
'anna_test-parents',
'anna test-parents',
])
def test_single_unit_route(self, name):
func, data = router._find_method(f'/units/{name}')
assert func.type == 'single'
assert data == {'name': name}
assert f'(cn={name})' in func(**data)

@pytest.mark.parametrize('name', [
'anna/test-parents',
'anna*test-parents',
'anna(test)-parents',
'anna\\test-parents',
])
def test_single_unit_route_rejects_invalid_names(self, name):
with pytest.raises(Exception, match='unknown'):
router._find_method(f'/units/{name}')

def test_devices_collection_route(self):
func, data = router._find_method('/devices')
assert func.type == 'collection'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_all_units():

return ldap_filter

@router.single(r'/units/(?P<name>[\w\-_ ]*)', models.LMNGroupModel, subdn=f'OU={SCHOOL_MARKER},OU=SCHOOLS,')
@router.single(r'/units/(?P<name>[\w\-_. ]*)', models.LMNGroupModel, subdn=f'OU={SCHOOL_MARKER},OU=SCHOOLS,')
def get_group(name=''):
"""
Get a unit specified by its name. The terminology "unit" was chosen in order to differenciate with a "group" from
Expand Down