|
4 | 4 |
|
5 | 5 | import ldap
|
6 | 6 | import pytest
|
| 7 | +from django_auth_ldap import config |
7 | 8 | from rest_framework.serializers import ValidationError
|
8 | 9 | from typeguard import suppress_type_checks
|
9 | 10 |
|
|
12 | 13 | AuthenticatorPlugin,
|
13 | 14 | LDAPSearchField,
|
14 | 15 | LDAPSettings,
|
| 16 | + PosixUIDGroupType, |
| 17 | + find_class_in_modules, |
15 | 18 | validate_ldap_filter,
|
16 | 19 | )
|
17 | 20 | from ansible_base.authentication.models import Authenticator
|
@@ -683,3 +686,88 @@ def test_ldap_user_search_validation(
|
683 | 686 | )
|
684 | 687 | def test_ldap_search_field_is_single_search(value, expected_result):
|
685 | 688 | assert LDAPSearchField.is_single_search(value) is expected_result
|
| 689 | + |
| 690 | + |
| 691 | +@pytest.mark.parametrize( |
| 692 | + "cls_name,cls", |
| 693 | + [("PosixGroupType", config.PosixGroupType), ("PosixUIDGroupType", PosixUIDGroupType), ("NonExistentClass", None)], |
| 694 | +) |
| 695 | +def test_find_class_in_modules(cls_name, cls): |
| 696 | + found_cls = find_class_in_modules(cls_name) |
| 697 | + if found_cls: |
| 698 | + assert found_cls.__name__ == cls.__name__ |
| 699 | + else: |
| 700 | + assert found_cls is cls |
| 701 | + |
| 702 | + |
| 703 | +@pytest.fixture |
| 704 | +def group_type(): |
| 705 | + return PosixUIDGroupType() |
| 706 | + |
| 707 | + |
| 708 | +@pytest.fixture |
| 709 | +def ldap_user(): |
| 710 | + user = MagicMock() |
| 711 | + user.connection = MagicMock() |
| 712 | + return user |
| 713 | + |
| 714 | + |
| 715 | +@pytest.fixture |
| 716 | +def group_search(): |
| 717 | + return MagicMock() |
| 718 | + |
| 719 | + |
| 720 | +def test_user_groups_with_gidNumber(group_type, ldap_user, group_search): |
| 721 | + ldap_user.attrs = {"uid": ["jdoe"], "gidNumber": ["1000"]} |
| 722 | + mock_search = MagicMock() |
| 723 | + mock_search.execute.return_value = ["group1", "group2"] |
| 724 | + group_search.search_with_additional_term_string.return_value = mock_search |
| 725 | + groups = group_type.user_groups(ldap_user, group_search) |
| 726 | + assert groups == ["group1", "group2"] |
| 727 | + group_search.search_with_additional_term_string.assert_called_once() |
| 728 | + mock_search.execute.assert_called_once() |
| 729 | + |
| 730 | + |
| 731 | +def test_user_groups_without_gidNumber(group_type, ldap_user, group_search): |
| 732 | + ldap_user.attrs = {"uid": ["jdoe"]} |
| 733 | + mock_search = MagicMock() |
| 734 | + mock_search.execute.return_value = ["group3"] |
| 735 | + group_search.search_with_additional_term_string.return_value = mock_search |
| 736 | + groups = group_type.user_groups(ldap_user, group_search) |
| 737 | + assert groups == ["group3"] |
| 738 | + |
| 739 | + |
| 740 | +def test_user_groups_missing_uid(group_type, ldap_user, group_search): |
| 741 | + ldap_user.attrs = {"gidNumber": ["1000"]} |
| 742 | + groups = group_type.user_groups(ldap_user, group_search) |
| 743 | + assert groups == [] |
| 744 | + |
| 745 | + |
| 746 | +def test_is_member_by_memberUid(group_type, ldap_user): |
| 747 | + ldap_user.attrs = {"uid": ["jdoe"], "gidNumber": ["1000"]} |
| 748 | + ldap_user.connection.compare_s.side_effect = [True, False] |
| 749 | + result = group_type.is_member(ldap_user, "cn=group1,dc=example,dc=com") |
| 750 | + assert result is True |
| 751 | + assert ldap_user.connection.compare_s.call_count == 1 |
| 752 | + |
| 753 | + |
| 754 | +def test_is_member_by_gidNumber(group_type, ldap_user): |
| 755 | + ldap_user.attrs = {"uid": ["jdoe"], "gidNumber": ["1000"]} |
| 756 | + # Simulate memberUid fails, gidNumber succeeds |
| 757 | + ldap_user.connection.compare_s.side_effect = [False, True] |
| 758 | + result = group_type.is_member(ldap_user, "cn=group2,dc=example,dc=com") |
| 759 | + assert result is True |
| 760 | + assert ldap_user.connection.compare_s.call_count == 2 |
| 761 | + |
| 762 | + |
| 763 | +def test_is_member_none_match(group_type, ldap_user): |
| 764 | + ldap_user.attrs = {"uid": ["jdoe"], "gidNumber": ["1000"]} |
| 765 | + ldap_user.connection.compare_s.side_effect = [False, False] |
| 766 | + result = group_type.is_member(ldap_user, "cn=group3,dc=example,dc=com") |
| 767 | + assert result is False |
| 768 | + |
| 769 | + |
| 770 | +def test_is_member_missing_uid(group_type, ldap_user): |
| 771 | + ldap_user.attrs = {"gidNumber": ["1000"]} |
| 772 | + result = group_type.is_member(ldap_user, "cn=group,dc=example,dc=com") |
| 773 | + assert result is False |
0 commit comments