2017-02-10 00:09:57 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-05-29 17:50:51 +00:00
|
|
|
from util.config.validator import ValidatorContext
|
2017-02-10 00:09:57 +00:00
|
|
|
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
|
|
|
|
|
2017-04-24 18:52:30 +00:00
|
|
|
from test.fixtures import *
|
2018-05-29 17:50:51 +00:00
|
|
|
from app import config_provider
|
2017-02-10 00:09:57 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('unvalidated_config', [
|
|
|
|
({}),
|
|
|
|
({'AUTHENTICATION_TYPE': 'Database'}),
|
|
|
|
])
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_validate_noop(unvalidated_config, app):
|
2018-06-01 15:31:19 +00:00
|
|
|
config = ValidatorContext(unvalidated_config, config_provider=config_provider)
|
2018-05-29 17:50:51 +00:00
|
|
|
LDAPValidator.validate(config)
|
2017-02-10 00:09:57 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('unvalidated_config', [
|
|
|
|
({'AUTHENTICATION_TYPE': 'LDAP'}),
|
|
|
|
({'AUTHENTICATION_TYPE': 'LDAP', 'LDAP_ADMIN_DN': 'foo'}),
|
|
|
|
])
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_invalid_config(unvalidated_config, app):
|
2017-02-10 00:09:57 +00:00
|
|
|
with pytest.raises(ConfigValidationException):
|
2018-06-01 15:31:19 +00:00
|
|
|
config = ValidatorContext(unvalidated_config, config_provider=config_provider)
|
2018-05-29 17:50:51 +00:00
|
|
|
LDAPValidator.validate(config)
|
2017-02-10 00:09:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('uri', [
|
|
|
|
'foo',
|
|
|
|
'http://foo',
|
|
|
|
'ldap:foo',
|
|
|
|
])
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_invalid_uri(uri, app):
|
2017-02-10 00:09:57 +00:00
|
|
|
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):
|
2018-06-01 15:31:19 +00:00
|
|
|
config = ValidatorContext(config, config_provider=config_provider)
|
2018-05-29 17:50:51 +00:00
|
|
|
LDAPValidator.validate(config)
|
2017-02-10 00:09:57 +00:00
|
|
|
|
|
|
|
|
2018-07-16 15:18:22 +00:00
|
|
|
@pytest.mark.parametrize('admin_dn, admin_passwd, user_rdn, expected_exception', [
|
|
|
|
('uid=testy,ou=employees,dc=quay,dc=io', 'password', ['ou=employees'], None),
|
|
|
|
('uid=invalidadmindn', 'password', ['ou=employees'], ConfigValidationException),
|
|
|
|
('uid=testy,ou=employees,dc=quay,dc=io', 'invalid_password', ['ou=employees'], ConfigValidationException),
|
|
|
|
('uid=testy,ou=employees,dc=quay,dc=io', 'password', ['ou=invalidgroup'], ConfigValidationException),
|
2017-02-10 00:09:57 +00:00
|
|
|
])
|
2018-07-16 15:18:22 +00:00
|
|
|
def test_validated_ldap(admin_dn, admin_passwd, user_rdn, expected_exception, app):
|
2017-02-10 00:09:57 +00:00
|
|
|
config = {}
|
|
|
|
config['AUTHENTICATION_TYPE'] = 'LDAP'
|
|
|
|
config['LDAP_BASE_DN'] = ['dc=quay', 'dc=io']
|
2018-07-16 15:18:22 +00:00
|
|
|
config['LDAP_ADMIN_DN'] = admin_dn
|
|
|
|
config['LDAP_ADMIN_PASSWD'] = admin_passwd
|
|
|
|
config['LDAP_USER_RDN'] = user_rdn
|
2017-02-10 00:09:57 +00:00
|
|
|
|
2018-07-16 15:18:22 +00:00
|
|
|
unvalidated_config = ValidatorContext(config, config_provider=config_provider)
|
2018-05-29 17:50:51 +00:00
|
|
|
|
2017-02-10 00:09:57 +00:00
|
|
|
if expected_exception is not None:
|
|
|
|
with pytest.raises(ConfigValidationException):
|
|
|
|
with mock_ldap():
|
2018-05-29 17:50:51 +00:00
|
|
|
LDAPValidator.validate(unvalidated_config)
|
2017-02-10 00:09:57 +00:00
|
|
|
else:
|
|
|
|
with mock_ldap():
|
2018-05-29 17:50:51 +00:00
|
|
|
LDAPValidator.validate(unvalidated_config)
|