def test_ldap_exception(LDAP, mock, monkeypatch, user_template_config): from devpi_ldap.main import AuthException monkeypatch.setattr(LDAP.ldap3.Connection, 'open', mock.Mock(side_effect=LDAP.LDAPException())) ldap = LDAP(user_template_config.strpath) with pytest.raises(AuthException) as e: ldap.validate('user', 'foo') assert e.value.args[0] == "Couldn't open LDAP connection to ldap://localhost"
def test_socket_timeout(LDAP, mock, monkeypatch, user_template_config): from devpi_ldap.main import AuthException import socket monkeypatch.setattr(LDAP.ldap3.Connection, 'open', mock.Mock(side_effect=socket.timeout())) ldap = LDAP(user_template_config.strpath) with pytest.raises(AuthException) as e: ldap.validate('user', 'foo') assert e.value.args[0] == "Timeout on LDAP connect to ldap://localhost"
def test_extra_result_data(LDAP, MockServer, group_user_template_config): class Connection(MockConnection): def search(self, base, search_filter, search_scope, attributes): result = MockConnection.search(self, base, search_filter, search_scope, attributes) if self.response: self.response.insert(0, {}) return result MockServer.users['user'] = dict(pw="password", groups=[dict(cn='users')]) LDAP.ldap3.Connection = Connection ldap = LDAP(group_user_template_config.strpath) assert ldap.validate('user', 'password') == dict(status="ok", groups=[u'users'])
def test_reject_as_unknown_empty(LDAP, reject_as_unknown_config): ldap = LDAP(reject_as_unknown_config.strpath) assert ldap.validate('user', '') == dict(status="unknown")
def test_empty_password_fails(LDAP, user_template_config): ldap = LDAP(user_template_config.strpath) assert ldap.validate('user', '') == dict(status="reject")