|
14 | 14 | LDAPSearchField,
|
15 | 15 | LDAPSettings,
|
16 | 16 | PosixUIDGroupType,
|
| 17 | + default_connection_options, |
17 | 18 | find_class_in_modules,
|
18 | 19 | validate_ldap_filter,
|
19 | 20 | )
|
@@ -771,3 +772,118 @@ def test_is_member_missing_uid(group_type, ldap_user):
|
771 | 772 | ldap_user.attrs = {"gidNumber": ["1000"]}
|
772 | 773 | result = group_type.is_member(ldap_user, "cn=group,dc=example,dc=com")
|
773 | 774 | assert result is False
|
| 775 | + |
| 776 | + |
| 777 | +def test_ldap_config_defaults(): |
| 778 | + from ansible_base.authentication.authenticator_plugins.ldap import LDAPConfiguration, LDAPSettings |
| 779 | + |
| 780 | + config = LDAPConfiguration() |
| 781 | + errors = [] |
| 782 | + |
| 783 | + # Verify basic field defaults |
| 784 | + if config['START_TLS'].default is not False: |
| 785 | + errors.append(f"START_TLS did not default to false, got {config['START_TLS'].default}") |
| 786 | + |
| 787 | + # Verify CONNECTION_OPTIONS field default is empty (for clean UI) |
| 788 | + if config['CONNECTION_OPTIONS'].default != {}: |
| 789 | + errors.append(f"CONNECTION_OPTIONS field did not default to empty dict, got {config['CONNECTION_OPTIONS'].default}") |
| 790 | + |
| 791 | + # Verify that LDAPSettings properly applies defaults when CONNECTION_OPTIONS is empty |
| 792 | + test_config = { |
| 793 | + 'SERVER_URI': ['ldap://example.com'], |
| 794 | + 'CONNECTION_OPTIONS': {}, # Empty, should get merged with defaults |
| 795 | + 'GROUP_TYPE': 'PosixGroupType', |
| 796 | + 'GROUP_TYPE_PARAMS': {"name_attr": "cn"}, |
| 797 | + } |
| 798 | + settings = LDAPSettings(defaults=test_config) |
| 799 | + |
| 800 | + # Check that the defaults were applied in the settings object |
| 801 | + import ldap |
| 802 | + |
| 803 | + expected_referrals = ldap.OPT_REFERRALS in settings.CONNECTION_OPTIONS and settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS] == 0 |
| 804 | + |
| 805 | + if not expected_referrals: |
| 806 | + errors.append("LDAPSettings did not apply OPT_REFERRALS default when CONNECTION_OPTIONS was empty") |
| 807 | + |
| 808 | + assert errors == [] |
| 809 | + |
| 810 | + |
| 811 | +def test_ldap_connection_options_user_override(): |
| 812 | + import ldap |
| 813 | + |
| 814 | + from ansible_base.authentication.authenticator_plugins.ldap import LDAPSettings |
| 815 | + |
| 816 | + errors = [] |
| 817 | + |
| 818 | + # Test scenario 1: User overrides default values |
| 819 | + test_config_override = { |
| 820 | + 'SERVER_URI': ['ldap://example.com'], |
| 821 | + 'CONNECTION_OPTIONS': { |
| 822 | + 'OPT_REFERRALS': 1, # Override default value of default_connection_options['OPT_REFERRALS'] |
| 823 | + }, |
| 824 | + 'GROUP_TYPE': 'PosixGroupType', |
| 825 | + 'GROUP_TYPE_PARAMS': {"name_attr": "cn"}, |
| 826 | + } |
| 827 | + settings = LDAPSettings(defaults=test_config_override) |
| 828 | + |
| 829 | + # Verify user values override defaults |
| 830 | + if settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS] != 1: |
| 831 | + errors.append(f"Expected OPT_REFERRALS to be overridden to 1, got {settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS]}") |
| 832 | + |
| 833 | + # Test scenario 2: User provides additional options not in defaults |
| 834 | + test_config_additional = { |
| 835 | + 'SERVER_URI': ['ldap://example.com'], |
| 836 | + 'CONNECTION_OPTIONS': { |
| 837 | + 'OPT_PROTOCOL_VERSION': 3, # New option not in defaults |
| 838 | + }, |
| 839 | + 'GROUP_TYPE': 'PosixGroupType', |
| 840 | + 'GROUP_TYPE_PARAMS': {"name_attr": "cn"}, |
| 841 | + } |
| 842 | + settings = LDAPSettings(defaults=test_config_additional) |
| 843 | + |
| 844 | + # Verify defaults are still applied |
| 845 | + if settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS] != default_connection_options['OPT_REFERRALS']: |
| 846 | + errors.append( |
| 847 | + f"Expected OPT_REFERRALS default ({default_connection_options['OPT_REFERRALS']}) " |
| 848 | + f"to be preserved, got {settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS]}" |
| 849 | + ) |
| 850 | + # Verify additional option is included |
| 851 | + if settings.CONNECTION_OPTIONS[ldap.OPT_PROTOCOL_VERSION] != 3: |
| 852 | + errors.append(f"Expected OPT_PROTOCOL_VERSION to be set to 3, got {settings.CONNECTION_OPTIONS.get(ldap.OPT_PROTOCOL_VERSION)}") |
| 853 | + |
| 854 | + # Test scenario 3: Mixed scenario - some overrides, some defaults, some new |
| 855 | + test_config_mixed = { |
| 856 | + 'SERVER_URI': ['ldap://example.com'], |
| 857 | + 'CONNECTION_OPTIONS': { |
| 858 | + 'OPT_REFERRALS': 1, # Override default |
| 859 | + 'OPT_PROTOCOL_VERSION': 3, # New option |
| 860 | + }, |
| 861 | + 'GROUP_TYPE': 'PosixGroupType', |
| 862 | + 'GROUP_TYPE_PARAMS': {"name_attr": "cn"}, |
| 863 | + } |
| 864 | + settings = LDAPSettings(defaults=test_config_mixed) |
| 865 | + |
| 866 | + # Verify override |
| 867 | + if settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS] != 1: |
| 868 | + errors.append(f"Expected OPT_REFERRALS to be overridden to 1, got {settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS]}") |
| 869 | + # Verify new option |
| 870 | + if settings.CONNECTION_OPTIONS[ldap.OPT_PROTOCOL_VERSION] != 3: |
| 871 | + errors.append(f"Expected OPT_PROTOCOL_VERSION to be set to 3, got {settings.CONNECTION_OPTIONS.get(ldap.OPT_PROTOCOL_VERSION)}") |
| 872 | + |
| 873 | + # Test scenario 4: CONNECTION_OPTIONS is not a dict (edge case) |
| 874 | + test_config_non_dict = { |
| 875 | + 'SERVER_URI': ['ldap://example.com'], |
| 876 | + 'CONNECTION_OPTIONS': "invaaalid", # Not a dict |
| 877 | + 'GROUP_TYPE': 'PosixGroupType', |
| 878 | + 'GROUP_TYPE_PARAMS': {"name_attr": "cn"}, |
| 879 | + } |
| 880 | + settings = LDAPSettings(defaults=test_config_non_dict) |
| 881 | + |
| 882 | + # Should fall back to defaults only |
| 883 | + if settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS] != default_connection_options['OPT_REFERRALS']: |
| 884 | + errors.append( |
| 885 | + f"Expected OPT_REFERRALS default ({default_connection_options['OPT_REFERRALS']}) " |
| 886 | + f"when CONNECTION_OPTIONS is invalid, got {settings.CONNECTION_OPTIONS[ldap.OPT_REFERRALS]}" |
| 887 | + ) |
| 888 | + |
| 889 | + assert errors == [] |
0 commit comments