server/test/unittest_ldapuser.py
branchstable
changeset 6434 d99b742a9c49
parent 6340 470d8e828fda
child 6435 71b2a3fe7ba1
equal deleted inserted replaced
6433:95c40961df21 6434:d99b742a9c49
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """cubicweb.server.sources.ldapusers unit and functional tests
    18 """cubicweb.server.sources.ldapusers unit and functional tests"""
    19 
       
    20 """
       
    21 
    19 
    22 import socket
    20 import socket
    23 
    21 
    24 from logilab.common.testlib import TestCase, unittest_main, mock_object
    22 from logilab.common.testlib import TestCase, unittest_main, mock_object
    25 from cubicweb.devtools import TestServerConfiguration
    23 from cubicweb.devtools import TestServerConfiguration
    28 
    26 
    29 from cubicweb.server.sources.ldapuser import *
    27 from cubicweb.server.sources.ldapuser import *
    30 
    28 
    31 if '17.1' in socket.gethostbyname('ldap1'):
    29 if '17.1' in socket.gethostbyname('ldap1'):
    32     SYT = 'syt'
    30     SYT = 'syt'
       
    31     SYT_EMAIL = 'Sylvain Thenault'
    33     ADIM = 'adim'
    32     ADIM = 'adim'
       
    33     SOURCESFILE = 'data/sources_ldap1'
    34 else:
    34 else:
    35     SYT = 'sthenault'
    35     SYT = 'sthenault'
       
    36     SYT_EMAIL = 'sylvain.thenault@logilab.fr'
    36     ADIM = 'adimascio'
    37     ADIM = 'adimascio'
       
    38     SOURCESFILE = 'data/sources_ldap2'
    37 
    39 
    38 
    40 
    39 def nopwd_authenticate(self, session, login, password):
    41 def nopwd_authenticate(self, session, login, password):
    40     """used to monkey patch the source to get successful authentication without
    42     """used to monkey patch the source to get successful authentication without
    41     upassword checking
    43     upassword checking
    57 
    59 
    58 
    60 
    59 
    61 
    60 class LDAPUserSourceTC(CubicWebTC):
    62 class LDAPUserSourceTC(CubicWebTC):
    61     config = TestServerConfiguration('data')
    63     config = TestServerConfiguration('data')
    62     config.sources_file = lambda : 'data/sourcesldap'
    64     config.sources_file = lambda: SOURCESFILE
    63 
    65 
    64     def patch_authenticate(self):
    66     def patch_authenticate(self):
    65         self._orig_authenticate = LDAPUserSource.authenticate
    67         self._orig_authenticate = LDAPUserSource.authenticate
    66         LDAPUserSource.authenticate = nopwd_authenticate
    68         LDAPUserSource.authenticate = nopwd_authenticate
    67 
    69 
    99         self.assertEqual(e.firstname, None)
   101         self.assertEqual(e.firstname, None)
   100         self.assertEqual(e.surname, None)
   102         self.assertEqual(e.surname, None)
   101         self.assertEqual(e.in_group[0].name, 'users')
   103         self.assertEqual(e.in_group[0].name, 'users')
   102         self.assertEqual(e.owned_by[0].login, SYT)
   104         self.assertEqual(e.owned_by[0].login, SYT)
   103         self.assertEqual(e.created_by, ())
   105         self.assertEqual(e.created_by, ())
   104         self.assertEqual(e.primary_email[0].address, 'Sylvain Thenault')
   106         self.assertEqual(e.primary_email[0].address, SYT_EMAIL)
   105         # email content should be indexed on the user
   107         # email content should be indexed on the user
   106         rset = self.sexecute('CWUser X WHERE X has_text "thenault"')
   108         rset = self.sexecute('CWUser X WHERE X has_text "thenault"')
   107         self.assertEqual(rset.rows, [[e.eid]])
   109         self.assertEqual(rset.rows, [[e.eid]])
   108 
   110 
   109     def test_not(self):
   111     def test_not(self):
   396         RQLGeneratorTC.setUp(self)
   398         RQLGeneratorTC.setUp(self)
   397         ldapsource = repo.sources[-1]
   399         ldapsource = repo.sources[-1]
   398         self.pool = repo._get_pool()
   400         self.pool = repo._get_pool()
   399         session = mock_object(pool=self.pool)
   401         session = mock_object(pool=self.pool)
   400         self.o = RQL2LDAPFilter(ldapsource, session)
   402         self.o = RQL2LDAPFilter(ldapsource, session)
       
   403         self.ldapclasses = ''.join('(objectClass=%s)' % ldapcls
       
   404                                    for ldapcls in ldapsource.user_classes)
   401 
   405 
   402     def tearDown(self):
   406     def tearDown(self):
   403         repo._free_pool(self.pool)
   407         repo._free_pool(self.pool)
   404         RQLGeneratorTC.tearDown(self)
   408         RQLGeneratorTC.tearDown(self)
   405 
   409 
   406     def test_base(self):
   410     def test_base(self):
   407         rqlst = self._prepare('CWUser X WHERE X login "toto"').children[0]
   411         rqlst = self._prepare('CWUser X WHERE X login "toto"').children[0]
   408         self.assertEqual(self.o.generate(rqlst, 'X')[1],
   412         self.assertEqual(self.o.generate(rqlst, 'X')[1],
   409                           '(&(objectClass=top)(objectClass=posixAccount)(uid=toto))')
   413                           '(&%s(uid=toto))' % self.ldapclasses)
   410 
   414 
   411     def test_kwargs(self):
   415     def test_kwargs(self):
   412         rqlst = self._prepare('CWUser X WHERE X login %(x)s').children[0]
   416         rqlst = self._prepare('CWUser X WHERE X login %(x)s').children[0]
   413         self.o._args = {'x': "toto"}
   417         self.o._args = {'x': "toto"}
   414         self.assertEqual(self.o.generate(rqlst, 'X')[1],
   418         self.assertEqual(self.o.generate(rqlst, 'X')[1],
   415                           '(&(objectClass=top)(objectClass=posixAccount)(uid=toto))')
   419                           '(&%s(uid=toto))' % self.ldapclasses)
   416 
   420 
   417     def test_get_attr(self):
   421     def test_get_attr(self):
   418         rqlst = self._prepare('Any X WHERE E firstname X, E eid 12').children[0]
   422         rqlst = self._prepare('Any X WHERE E firstname X, E eid 12').children[0]
   419         self.assertRaises(UnknownEid, self.o.generate, rqlst, 'E')
   423         self.assertRaises(UnknownEid, self.o.generate, rqlst, 'E')
   420 
   424