Changeset - ff2ea58debb5
[Not reviewed]
beta
0 5 0
Marcin Kuzminski - 13 years ago 2013-04-04 18:31:15
marcin@python-works.com
fixed ldap tests when ldap lib is installed
5 files changed with 74 insertions and 57 deletions:
0 comments (0 inline, 0 general)
rhodecode/model/forms.py
Show inline comments
 
@@ -338,16 +338,13 @@ def LdapSettingsForm(tls_reqcert_choices
 
        ldap_tls_reqcert = v.OneOf(tls_reqcert_choices)
 
        ldap_dn_user = v.UnicodeString(strip=True,)
 
        ldap_dn_pass = v.UnicodeString(strip=True,)
 
        ldap_base_dn = v.UnicodeString(strip=True,)
 
        ldap_filter = v.UnicodeString(strip=True,)
 
        ldap_search_scope = v.OneOf(search_scope_choices)
 
        ldap_attr_login = All(
 
            v.AttrLoginValidator(),
 
            v.UnicodeString(strip=True,)
 
        )
 
        ldap_attr_login = v.AttrLoginValidator()(not_empty=True)
 
        ldap_attr_firstname = v.UnicodeString(strip=True,)
 
        ldap_attr_lastname = v.UnicodeString(strip=True,)
 
        ldap_attr_email = v.UnicodeString(strip=True,)
 

	
 
    return _LdapSettingsForm
 

	
rhodecode/model/validators.py
Show inline comments
 
@@ -720,19 +720,13 @@ def AttrLoginValidator():
 
        messages = {
 
            'invalid_cn':
 
                  _(u'The LDAP Login attribute of the CN must be specified - '
 
                    'this is the name of the attribute that is equivalent '
 
                    'to "username"')
 
        }
 

	
 
        def validate_python(self, value, state):
 
            if not value or not isinstance(value, (str, unicode)):
 
                msg = M(self, 'invalid_cn', state)
 
                raise formencode.Invalid(msg, value, state,
 
                    error_dict=dict(ldap_attr_login=msg)
 
                )
 
        messages['empty'] = messages['invalid_cn']
 

	
 
    return _validator
 

	
 

	
 
def NotReviewedRevisions(repo_id):
 
    class _validator(formencode.validators.FancyValidator):
rhodecode/tests/__init__.py
Show inline comments
 
@@ -49,13 +49,13 @@ if not is_windows:
 
    time.tzset()
 

	
 
log = logging.getLogger(__name__)
 

	
 
__all__ = [
 
    'parameterized', 'environ', 'url', 'get_new_dir', 'TestController',
 
    'SkipTest',
 
    'SkipTest', 'ldap_lib_installed',
 
    'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO',
 
    'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS',
 
    'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS',
 
    'TEST_USER_REGULAR_EMAIL', 'TEST_USER_REGULAR2_LOGIN',
 
    'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO',
 
    'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO',
 
@@ -114,12 +114,21 @@ TEST_REPO_PREFIX = 'vcs-test'
 

	
 
# cached repos if any !
 
# comment out to get some other repos from bb or github
 
GIT_REMOTE_REPO = jn(TESTS_TMP_PATH, GIT_REPO)
 
HG_REMOTE_REPO = jn(TESTS_TMP_PATH, HG_REPO)
 

	
 
#skip ldap tests if LDAP lib is not installed
 
ldap_lib_installed = False
 
try:
 
    import ldap
 
    ldap_lib_installed = True
 
except ImportError:
 
    # means that python-ldap is not installed
 
    pass
 

	
 

	
 
def get_new_dir(title):
 
    """
 
    Returns always new directory path.
 
    """
 
    from rhodecode.tests.vcs.utils import get_normalized_path
rhodecode/tests/functional/test_admin_ldap_settings.py
Show inline comments
 
from rhodecode.tests import *
 
from rhodecode.model.db import RhodeCodeSetting
 

	
 
skip_ldap_test = False
 
try:
 
    import ldap
 
except ImportError:
 
    # means that python-ldap is not installed
 
    skip_ldap_test = True
 
    pass
 

	
 

	
 
class TestLdapSettingsController(TestController):
 

	
 
    def test_index(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/ldap_settings',
 
                                    action='index'))
 
        response.mustcontain('LDAP administration')
 

	
 
    def test_ldap_save_settings(self):
 
        self.log_user()
 
        if skip_ldap_test:
 
        if ldap_lib_installed:
 
            raise SkipTest('skipping due to missing ldap lib')
 

	
 
        test_url = url(controller='admin/ldap_settings',
 
                       action='ldap_settings')
 

	
 
        response = self.app.post(url=test_url,
 
            params={'ldap_host' : u'dc.example.com',
 
                    'ldap_port' : '999',
 
                    'ldap_tls_kind' : 'PLAIN',
 
                    'ldap_tls_reqcert' : 'NEVER',
 
                    'ldap_dn_user':'test_user',
 
                    'ldap_dn_pass':'test_pass',
 
                    'ldap_base_dn':'test_base_dn',
 
                    'ldap_filter':'test_filter',
 
                    'ldap_search_scope':'BASE',
 
                    'ldap_attr_login':'test_attr_login',
 
                    'ldap_attr_firstname':'ima',
 
                    'ldap_attr_lastname':'tester',
 
                    'ldap_attr_email':'test@example.com' })
 
            params={'ldap_host': u'dc.example.com',
 
                    'ldap_port': '999',
 
                    'ldap_tls_kind': 'PLAIN',
 
                    'ldap_tls_reqcert': 'NEVER',
 
                    'ldap_dn_user': 'test_user',
 
                    'ldap_dn_pass': 'test_pass',
 
                    'ldap_base_dn': 'test_base_dn',
 
                    'ldap_filter': 'test_filter',
 
                    'ldap_search_scope': 'BASE',
 
                    'ldap_attr_login': 'test_attr_login',
 
                    'ldap_attr_firstname': 'ima',
 
                    'ldap_attr_lastname': 'tester',
 
                    'ldap_attr_email': 'test@example.com' })
 

	
 
        new_settings = RhodeCodeSetting.get_ldap_settings()
 
        self.assertEqual(new_settings['ldap_host'], u'dc.example.com',
 
                         'fail db write compare')
 

	
 
        self.checkSessionFlash(response,
 
                               'LDAP settings updated successfully')
 

	
 
    def test_ldap_error_form(self):
 
    def test_ldap_error_form_wrong_port_number(self):
 
        self.log_user()
 
        if skip_ldap_test:
 
        if ldap_lib_installed:
 
            raise SkipTest('skipping due to missing ldap lib')
 

	
 
        test_url = url(controller='admin/ldap_settings',
 
                       action='ldap_settings')
 

	
 
        response = self.app.post(url=test_url,
 
            params={'ldap_host' : '',
 
                    'ldap_port' : 'i-should-be-number',
 
                    'ldap_tls_kind' : 'PLAIN',
 
                    'ldap_tls_reqcert' : 'NEVER',
 
                    'ldap_dn_user':'',
 
                    'ldap_dn_pass':'',
 
                    'ldap_base_dn':'',
 
                    'ldap_filter':'',
 
                    'ldap_search_scope':'BASE',
 
                    'ldap_attr_login':'', #  <----- missing required input
 
                    'ldap_attr_firstname':'',
 
                    'ldap_attr_lastname':'',
 
                    'ldap_attr_email':'' })
 
            params={'ldap_host': '',
 
                    'ldap_port': 'i-should-be-number',  # bad port num
 
                    'ldap_tls_kind': 'PLAIN',
 
                    'ldap_tls_reqcert': 'NEVER',
 
                    'ldap_dn_user': '',
 
                    'ldap_dn_pass': '',
 
                    'ldap_base_dn': '',
 
                    'ldap_filter': '',
 
                    'ldap_search_scope': 'BASE',
 
                    'ldap_attr_login': '',
 
                    'ldap_attr_firstname': '',
 
                    'ldap_attr_lastname': '',
 
                    'ldap_attr_email': ''})
 

	
 
        response.mustcontain("""<span class="error-message">"""
 
                             """Please enter a number</span><br />""")
 

	
 
    def test_ldap_error_form(self):
 
        self.log_user()
 
        if ldap_lib_installed:
 
            raise SkipTest('skipping due to missing ldap lib')
 

	
 
        test_url = url(controller='admin/ldap_settings',
 
                       action='ldap_settings')
 

	
 
        response = self.app.post(url=test_url,
 
            params={'ldap_host': 'Host',
 
                    'ldap_port': '123',
 
                    'ldap_tls_kind': 'PLAIN',
 
                    'ldap_tls_reqcert': 'NEVER',
 
                    'ldap_dn_user': '',
 
                    'ldap_dn_pass': '',
 
                    'ldap_base_dn': '',
 
                    'ldap_filter': '',
 
                    'ldap_search_scope': 'BASE',
 
                    'ldap_attr_login': '',  # <----- missing required input
 
                    'ldap_attr_firstname': '',
 
                    'ldap_attr_lastname': '',
 
                    'ldap_attr_email': ''})
 

	
 
        response.mustcontain("""<span class="error-message">The LDAP Login"""
 
                             """ attribute of the CN must be specified""")
 

	
 

	
 
        response.mustcontain("""<span class="error-message">Please """
 
                             """enter a number</span>""")
 

	
 
    def test_ldap_login(self):
 
        pass
 

	
 
    def test_ldap_login_incorrect(self):
 
        pass
rhodecode/tests/test_validators.py
Show inline comments
 
@@ -217,18 +217,22 @@ class TestReposGroups(unittest.TestCase)
 
        email = TEST_USER_REGULAR2_EMAIL
 

	
 
        self.assertEqual(email, validator.to_python(email))
 
        self.assertRaises(formencode.Invalid, validator.to_python, 'err')
 

	
 
    def test_LdapLibValidator(self):
 
        validator = v.LdapLibValidator()
 
        self.assertRaises(v.LdapImportError, validator.to_python, 'err')
 
        if ldap_lib_installed:
 
            validator = v.LdapLibValidator()
 
            self.assertEqual("DN", validator.to_python('DN'))
 
        else:
 
            validator = v.LdapLibValidator()
 
            self.assertRaises(v.LdapImportError, validator.to_python, 'err')
 

	
 
    def test_AttrLoginValidator(self):
 
        validator = v.AttrLoginValidator()
 
        self.assertRaises(formencode.Invalid, validator.to_python, 123)
 
        self.assertEqual('DN_attr', validator.to_python('DN_attr'))
 

	
 
    def test_NotReviewedRevisions(self):
 
        repo_id = Repository.get_by_repo_name(HG_REPO).repo_id
 
        validator = v.NotReviewedRevisions(repo_id)
 
        rev = '0' * 40
 
        # add status for a rev, that should throw an error because it is already
0 comments (0 inline, 0 general)