[Zope3-checkins] SVN: ldapauth/trunk/ Added more tests in the check view

Roger Ineichen roger at projekt01.ch
Wed Jul 21 12:01:38 EDT 2004


Log message for revision 26656:
  Added more tests in the check view 
  There are two test a t this time which can be run over the ZMI.
  1. Test the python ldap conection wrapper and if the wrapper can bind the to the LDAP server.
  2. getPrincipals, this test calls a search on the LDAP server with the given username


Changed:
  U   ldapauth/trunk/browser/check.pt
  U   ldapauth/trunk/browser/check.py
  U   ldapauth/trunk/check.py


-=-
Modified: ldapauth/trunk/browser/check.pt
===================================================================
--- ldapauth/trunk/browser/check.pt	2004-07-21 15:38:10 UTC (rev 26655)
+++ ldapauth/trunk/browser/check.pt	2004-07-21 16:01:38 UTC (rev 26656)
@@ -77,7 +77,7 @@
 				</thead>
 				<tbody>
 					<tr tal:repeat="line testreport" >
-						<td><span tal:content="structure line">report entry</span></td>
+						<td><span tal:content="structure line">report entry</span>&nbsp;</td>
 					</tr>
 				</tbody>
 			</table>

Modified: ldapauth/trunk/browser/check.py
===================================================================
--- ldapauth/trunk/browser/check.py	2004-07-21 15:38:10 UTC (rev 26655)
+++ ldapauth/trunk/browser/check.py	2004-07-21 16:01:38 UTC (rev 26656)
@@ -57,13 +57,19 @@
         if runtest == "Run":
             un = self.request.get('username')
             pw = self.request.get('password')
+            
+            # get the ldapauth source
+            testadapter = ICheckLDAPAdapter(self.context)
 
             # test the connection to the LDAP server
-            self._addInfo("<strong>Test LDAP server connection</strong>")
-            testadapter = ICheckLDAPAdapter(self.context)
+            self._addInfo("<strong>Test python connection and LDAP server binding</strong>")
             self.report = self.report + testadapter.testConnection()
+            self._addInfo(" ")
 
             # test quering the LDAP server
+            self._addInfo("<strong>Test get principals</strong>")
+            self.report = self.report + testadapter.testGetPrincipals(un)
+            self._addInfo(" ")
 
             # test query the given username
 

Modified: ldapauth/trunk/check.py
===================================================================
--- ldapauth/trunk/check.py	2004-07-21 15:38:10 UTC (rev 26655)
+++ ldapauth/trunk/check.py	2004-07-21 16:01:38 UTC (rev 26656)
@@ -17,8 +17,9 @@
 """
 import ldap
 from zope.security.proxy import trustedRemoveSecurityProxy
+from zope.interface import implements
+from zope.app.pluggableauth import SimplePrincipal
 
-from zope.interface import implements
 from interfaces import ICheckLDAPAdapter
 
 
@@ -38,25 +39,87 @@
         self.report.append("... check existing connection")
 
         try:
-            conn = getattr(source, '_v_conn', None)
-            if conn:
-                self.report.append('... connection "%s" found' % conn)
+            connection = getattr(source, "_v_conn", None)
+
+            if connection != None:
+                self.report.append("... connection found")
+                self.report.append("... bind connection to LDAP server")
+                connection.simple_bind_s(source.manager_dn, source.manager_passwd)
+                self.report.append("... <strong>OK!</strong>")
+                return self.report
             else:
                 self.report.append("... no existing connection found")
-                self.report.append("... try to connect")
-             
-            if not conn:
-                connectstring = 'ldap://%s:%s' % (source.host, source.port)
-                self.report.append("... ... connecting to:")
-                self.report.append("... ... %s" % connectstring)
+                connectstring = "ldap://%s:%s" % (source.host, source.port)
+                self.report.append("... setup connection to: %s" % connectstring)
                 connection = ldap.initialize(connectstring)
-                self.report.append("... <strong>Connection OK!</strong>")
+                
+                try:
+                    self.report.append("... bind connection to LDAP server")
+                    connection.simple_bind_s(source.manager_dn, source.manager_passwd)
+                    self.report.append("... <strong>OK!</strong>")
+                    return self.report
+                except:
+                    self.report.append("... No LDAP server found")
+                    self.report.append("... <strong>Test faild!</strong>")
+                    return self.report
+
+        except:
+            self.report.append("... <strong>Test faild!</strong>")
+            return self.report
+            
+    def testGetPrincipals(self, name):
+        self.report = []
+        source = trustedRemoveSecurityProxy(self.context)
+
+        try:
+            connectstring = "ldap://%s:%s" % (source.host, source.port)
+            self.report.append("... setup connection to: %s" % connectstring)
+            l = self._connect(source)
+            self.report.append("... bind connection to LDAP server")
+            l.simple_bind_s(source.manager_dn, source.manager_passwd)
+            
+            if name == "" :
+                self.report.append("... test without a  name")
+                search = "(%s=*)" % source.login_attribute
+                self.report.append("... search string '%s'" % search)
+            else:
+                self.report.append("... test with name '%s'" % name)
+                search = "(%s=*%s*)" % (source.login_attribute, name)
+                self.report.append("... search string '%s'" % search)
+            
+            self.report.append("... search on LDAP server")
+            lsearch = l.search_s(source.basedn, ldap.SCOPE_ONELEVEL, search)
+            if lsearch:
+                self.report.append("... ... found %s items" % len(lsearch))
+            
+            self.report.append("... convert LDAP entries to principals")
+            principals = []
+            for node in lsearch:
+                node_dn, node_dict = node
+                principal = SimplePrincipal(
+                        login = node_dict[source.login_attribute][0],
+                        password = node_dict['userPassword'][0])
+                principals.append(principal)
+
+            if len(principals):
+                self.report.append("... ... converted %s LDAP item(s) to principals" % len(principals))
+                self.report.append("... <strong>OK!</strong>")
                 return self.report
             else:
-                self.report.append("... <strong>Connection OK!</strong>")
+                self.report.append("... ... there no entries found on the LDAP server")
+                self.report.append("... ... perhpas you tried a wrong search query")
+                self.report.append("... ... or you don't have data on the LDAP server")
+                self.report.append("... <strong>Maybe OK!</strong>")
                 return self.report
+                
+
         except:
-            self.report.append("... <strong>Connection test faild!</strong>")
+            self.report.append("... <strong>Test faild!</strong>")
             return self.report
-            
- 
+
+
+    # helper methods
+    def _connect(self, source):
+        connectstring = "ldap://%s:%s" % (source.host, source.port)
+        connection = ldap.initialize(connectstring)
+        return connection
\ No newline at end of file



More information about the Zope3-Checkins mailing list