import pytest from util.config.validators import ConfigValidationException from util.config.validators.validate_ldap import LDAPValidator from util.morecollections import AttrDict from test.test_ldap import mock_ldap from test.fixtures import * @pytest.mark.parametrize('unvalidated_config', [ ({}), ({'AUTHENTICATION_TYPE': 'Database'}), ]) def test_validate_noop(unvalidated_config, app): LDAPValidator.validate(unvalidated_config, None, None) @pytest.mark.parametrize('unvalidated_config', [ ({'AUTHENTICATION_TYPE': 'LDAP'}), ({'AUTHENTICATION_TYPE': 'LDAP', 'LDAP_ADMIN_DN': 'foo'}), ]) def test_invalid_config(unvalidated_config, app): with pytest.raises(ConfigValidationException): LDAPValidator.validate(unvalidated_config, None, None) @pytest.mark.parametrize('uri', [ 'foo', 'http://foo', 'ldap:foo', ]) def test_invalid_uri(uri, app): config = {} config['AUTHENTICATION_TYPE'] = 'LDAP' config['LDAP_BASE_DN'] = ['dc=quay', 'dc=io'] config['LDAP_ADMIN_DN'] = 'uid=testy,ou=employees,dc=quay,dc=io' config['LDAP_ADMIN_PASSWD'] = 'password' config['LDAP_USER_RDN'] = ['ou=employees'] config['LDAP_URI'] = uri with pytest.raises(ConfigValidationException): LDAPValidator.validate(config, None, None) @pytest.mark.parametrize('username, password, expected_exception', [ ('invaliduser', 'invalidpass', ConfigValidationException), ('someuser', 'invalidpass', ConfigValidationException), ('invaliduser', 'somepass', ConfigValidationException), ('someuser', 'somepass', None), ]) def test_validated_ldap(username, password, expected_exception, app): config = {} config['AUTHENTICATION_TYPE'] = 'LDAP' config['LDAP_BASE_DN'] = ['dc=quay', 'dc=io'] config['LDAP_ADMIN_DN'] = 'uid=testy,ou=employees,dc=quay,dc=io' config['LDAP_ADMIN_PASSWD'] = 'password' config['LDAP_USER_RDN'] = ['ou=employees'] if expected_exception is not None: with pytest.raises(ConfigValidationException): with mock_ldap(): LDAPValidator.validate(config, AttrDict(dict(username=username)), password) else: with mock_ldap(): LDAPValidator.validate(config, AttrDict(dict(username=username)), password)