示例#1
0
 def testSearch(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     expected = {
         u'ssmith': {
             'password': u'passwordsmith',
             'cn': u'Sheri Smith',
             'name': u'smith',
             'uid': u'ssmith',
             'email': [u'*****@*****.**',
                       u'*****@*****.**',
                       u'*****@*****.**'
                  ],
             },
         u'jsmith': {
             'password': u'passwordsmith',
             'cn': u'John Smith',
             'name': u'Smith',
             'uid': u'jsmith',
             'email': [
                 '*****@*****.**',
                 '*****@*****.**',
                 '*****@*****.**'
                 ],
             }
         }
     ret = app._search('smith')
     assert expected == ret
示例#2
0
 def testLog(self):
     app = LdapCherry()
     cfg = { 'global' : {}}
     for t in ['none', 'file', 'syslog', 'stdout']:
         cfg['global']['log.access_handler']=t
         cfg['global']['log.error_handler']=t
         app._set_access_log(cfg, logging.DEBUG)
         app._set_error_log(cfg, logging.DEBUG)
示例#3
0
 def testAddUserOneBackend(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_adldap.cfg', app)
     inv = ldapcherry.backend.backendAD.Backend(adcfg, cherrypy.log, u'test☭', adattr, 'sAMAccountName')
     inv.add_user(addefault_user.copy())
     form = {'groups': {}, 'attrs': {'password1': u'password☭P455', 'password2': u'password☭P455', 'cn': u'Test ☭ Test', 'name': u'Test ☭', 'uidNumber': u'1000', 'gidNumber': u'1000', 'home': u'/home/test', 'first-name': u'Test ☭', 'email': u'*****@*****.**', 'uid': u'☭default_user'}, 'roles': {'admin-lv3': u'on', 'admin-lv2': u'on', 'users': u'on'}}
     app._adduser(form)
     app._deleteuser(u'☭default_user')
示例#4
0
 def testMissingParameters(self):
     app = LdapCherry()
     try:
         app.reload({})
     except SystemExit:
         return
     else:
         raise AssertionError("expected an exception")
示例#5
0
 def testAuth(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     app.auth_mode = 'and'
     ret1 = app._auth('jsmith', 'passwordsmith')
     app.auth_mode = 'or'
     ret2 = app._auth('jsmith', 'passwordsmith')
     assert ret2 == {'connected': True, 'isadmin': False} and \
         ret1 == {'connected': True, 'isadmin': False}
示例#6
0
 def testInitgBackendModuleFail(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     cfg = {'backends': {'ldap.module': 'ldapcherry.backend'}}
     try:
         app._init_backends(cfg)
     except BackendModuleInitFail:
         return
     else:
         raise AssertionError("expected an exception")
示例#7
0
 def testMissingBackendModule(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     cfg = {'backends': {'ldap.module': 'dontexists'}}
     try:
         app._init_backends(cfg)
     except BackendModuleLoadingFail:
         return
     else:
         raise AssertionError("expected an exception")
示例#8
0
 def testMissingBackend(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     del app.backends_params['ad']
     try:
         app._check_backends()
     except MissingBackend:
         return
     else:
         raise AssertionError("expected an exception")
示例#9
0
 def testLoginFailure(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     app.auth_mode = 'or'
     try:
         app.login(u'jwatsoné', u'wrongPasswordé')
     except cherrypy.HTTPRedirect as e:
         expected = 'http://127.0.0.1:8080/signin'
         assert e.urls[0] == expected
     else:
         raise AssertionError("expected an exception")
示例#10
0
 def testLogin(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     app.auth_mode = 'or'
     try:
         app.login('jwatson', 'passwordwatson')
     except cherrypy.HTTPRedirect as e:
         expected = 'http://127.0.0.1:8080/'
         assert e[0][0] == expected
     else:
         raise AssertionError("expected an exception")
示例#11
0
 def testGetUser(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     expected = {
         'password': u'passwordsmith',
         'cn': u'Sheri Smith',
         'uid': u'ssmith',
         'name': u'smith',
         'email': [u'*****@*****.**',
                  u'*****@*****.**',
                  u'*****@*****.**'
             ],
         }
     ret = app._get_user('ssmith')
     assert expected == ret
示例#12
0
 def testModifUser(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     form = {'groups': {}, 'attrs': {'password1': u'password☭', 'password2': u'password☭', 'cn': u'Test ☭ Test', 'name': u'Test ☭', 'uidNumber': u'1000', 'gidNumber': u'1000', 'home': u'/home/test', 'first-name': u'Test ☭', 'email': u'*****@*****.**', 'uid': u'test'}, 'roles': {'admin-lv3': u'on', 'admin-lv2': u'on', 'users': u'on'}}
     app._adduser(form)
     modify_form = { 'attrs': {'first-name': u'Test42 ☭', 'uid': u'test'}, 'roles': { 'admin-lv3': u'on'}}
     app._modify(modify_form)
     app._deleteuser('test')
示例#13
0
 def testHtml(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     pages = {
             'signin': app.signin(),
             'index': app.index(),
             'searchuser': app.searchuser('smit'),
             'searchadmin':app.searchadmin('smit'),
             'adduser': app.adduser(),
             'modify':app.modify('jsmith'),
             'selfmodify':app.selfmodify(),
             }
     for page in pages:
         print(page)
         htmlvalidator(pages[page])
示例#14
0
 def testModifyUserOneBackend(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_adldap.cfg', app)
     inv = ldapcherry.backend.backendAD.Backend(adcfg, cherrypy.log, u'test☭', adattr, 'sAMAccountName')
     try:
         app._deleteuser(u'☭default_user')
     except:
         pass
     inv.add_user(addefault_user.copy())
     modify_form = { 'attrs': {'first-name': u'Test42 ☭', 'uid': u'☭default_user'}, 'roles': { 'admin-lv3': u'on'}}
     app._modify(modify_form)
     app._deleteuser(u'☭default_user')
示例#15
0
 def testNaughtyStrings(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     with open('./tests/cfg/blns.json') as data_file:
         data = json.load(data_file)
     for attr in data:
         print('testing: ' + attr)
         # delete whatever is happening...
         try:
             app._deleteuser('test')
         except:
             pass
         form = {'groups': {}, 'attrs': {'password1': u'password☭', 'password2': u'password☭', 'cn': 'Test', 'name': attr, 'uidNumber': u'1000', 'gidNumber': u'1000', 'home': u'/home/test', 'first-name': u'Test ☭', 'email': u'*****@*****.**', 'uid': 'test'}, 'roles': {'admin-lv3': u'on', 'admin-lv2': u'on', 'users': u'on'}}
         app._adduser(form)
         page = app.searchuser('test'),
         app._deleteuser('test')
         htmlvalidator(page[0])
示例#16
0
def start(configfile=None, daemonize=False, environment=None,
          fastcgi=False, scgi=False, pidfile=None,
          cgi=False, debug=False):
    """Subscribe all engine plugins and start the engine."""
    sys.path = [''] + sys.path

    # monkey patching cherrypy to disable config interpolation
    def new_as_dict(self, raw=True, vars=None):
        """Convert an INI file to a dictionary"""
        # Load INI file into a dict
        result = {}
        for section in self.sections():
            if section not in result:
                result[section] = {}
            for option in self.options(section):
                value = self.get(section, option, raw=raw, vars=vars)
                try:
                    value = cherrypy.lib.reprconf.unrepr(value)
                except Exception:
                    x = sys.exc_info()[1]
                    msg = ("Config error in section: %r, option: %r, "
                           "value: %r. Config values must be valid Python." %
                           (section, option, value))
                    raise ValueError(msg, x.__class__.__name__, x.args)
                result[section][option] = value
        return result
    cherrypy.lib.reprconf.Parser.as_dict = new_as_dict

    instance = LdapCherry()
    app = cherrypy.tree.mount(instance, '/', configfile)
    cherrypy.config.update(configfile)
    instance.reload(app.config, debug)

    engine = cherrypy.engine

    # Turn off autoreload
    cherrypy.config.update({'engine.autoreload.on': False})

    if environment is not None:
        cherrypy.config.update({'environment': environment})

    # Only daemonize if asked to.
    if daemonize:
        # Don't print anything to stdout/sterr.
        cherrypy.config.update({'log.screen': False})
        plugins.Daemonizer(engine).subscribe()

    if pidfile:
        plugins.PIDFile(engine, pidfile).subscribe()

    if hasattr(engine, "signal_handler"):
        engine.signal_handler.subscribe()
    if hasattr(engine, "console_control_handler"):
        engine.console_control_handler.subscribe()

    if (fastcgi and (scgi or cgi)) or (scgi and cgi):
        cherrypy.log.error("You may only specify one of the cgi, fastcgi, and "
                           "scgi options.", 'ENGINE')
        sys.exit(1)
    elif fastcgi or scgi or cgi:
        # Turn off the default HTTP server (which is subscribed by default).
        cherrypy.server.unsubscribe()

        addr = cherrypy.server.bind_addr
        if fastcgi:
            f = servers.FlupFCGIServer(application=cherrypy.tree,
                                       bindAddress=addr)
        elif scgi:
            f = servers.FlupSCGIServer(application=cherrypy.tree,
                                       bindAddress=addr)
        else:
            f = servers.FlupCGIServer(application=cherrypy.tree,
                                      bindAddress=addr)
        s = servers.ServerAdapter(engine, httpserver=f, bind_addr=addr)
        s.subscribe()

    # Always start the engine; this will start all other services
    try:
        engine.start()
    except Exception as e:
        # Assume the error has been logged already via bus.log.
        sys.exit(1)
    else:
        engine.block()
示例#17
0
 def testPPolicy(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     wrong = app._checkppolicy('password')['match']
     good = app._checkppolicy('Passw0rd.')['match']
     assert wrong == False and good == True
示例#18
0
 def testDeleteUserOneBackend(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_adldap.cfg', app)
     inv = ldapcherry.backend.backendAD.Backend(adcfg, cherrypy.log, u'test☭', adattr, 'sAMAccountName')
     inv.add_user(addefault_user.copy())
     app._deleteuser(u'☭default_user')
示例#19
0
 def testNoneModify(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     app.modify(user=None)
示例#20
0
 def testRandomException(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     e = Exception()
     app._handle_exception(e)
示例#21
0
 def testNoneType(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     app.modify('ssmith')
示例#22
0
 def testParse(self):
     app = LdapCherry()
     form = {'attr.val': 'val', 'role.id': 'id', 'group.ldap.id': 'id'}
     ret = app._parse_params(form)
     expected = {'attrs': {'val': 'val'}, 'roles': {'id': 'id'}, 'groups': {'ldap': ['id']}}
     assert expected == ret
示例#23
0
 def testNoneType(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     app.modify('ssmith')
示例#24
0
 def testNoneModify(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry_test.ini', app)
     app.modify(user=None)
示例#25
0
        if section not in result:
            result[section] = {}
        for option in self.options(section):
            value = self.get(section, option, raw=raw, vars=vars)
            try:
                value = cherrypy.lib.reprconf.unrepr(value)
            except Exception:
                x = sys.exc_info()[1]
                msg = ("Config error in section: %r, option: %r, "
                       "value: %r. Config values must be valid Python." %
                       (section, option, value))
                raise ValueError(msg, x.__class__.__name__, x.args)
            result[section][option] = value
    return result
cherrypy.lib.reprconf.Parser.as_dict = new_as_dict


def loadconf(configfile, instance):
    app = cherrypy.tree.mount(instance, '/', configfile)
    cherrypy.config.update(configfile)
    instance.reload(app.config)

app = LdapCherry()
loadconf('./tests/cfg/ldapcherry.ini', app)
ret = app._get_user('ssmith')
print ret

}

form = {'groups': {}, 'attrs': {'password1': u'password☭', 'password2': u'password☭', 'shell': u'/bin/zsh', 'cn': u'Test ☭ Test', 'name': u'Test ☭', 'uidNumber': u'1000', 'gidNumber': u'1000', 'home': u'/home/test', 'first-name': u'Test ☭', 'email': u'*****@*****.**', 'uid': u'test'}, 'roles': {'admin-lv3': u'on', 'admin-lv2': u'on', 'users': u'on'}}
示例#26
0
 def testPPolicy(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     wrong = app._checkppolicy('password')['match']
     good = app._checkppolicy('Passw0rd.')['match']
     assert wrong == False and good == True
示例#27
0
 def testRandomException(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     e = Exception()
     app._handle_exception(e)
示例#28
0
 def testNominal(self):
     app = LdapCherry()
     loadconf('./tests/cfg/ldapcherry.ini', app)
     return True