Back to index

plone3  3.1.7
test_basic_ops.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # PlonePAS - Adapt PluggableAuthService for use in Plone
00004 # Copyright (C) 2005 Enfold Systems, Kapil Thangavelu, et al
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """
00016 """
00017 
00018 import unittest
00019 
00020 from PlonePASTestCase import PlonePASTestCase
00021 
00022 from Products.CMFCore.utils import getToolByName
00023 
00024 from Products.PluggableAuthService.PluggableAuthService \
00025     import _SWALLOWABLE_PLUGIN_EXCEPTIONS
00026 from Products.PluggableAuthService.interfaces.authservice \
00027      import IPluggableAuthService
00028 from Products.PluggableAuthService.interfaces.plugins \
00029      import IRolesPlugin
00030 
00031 
00032 
00033 class BasicOpsTestCase(PlonePASTestCase):
00034 
00035     def afterSetUp(self):
00036         self.loginAsPortalOwner()
00037         self.acl_users = self.portal.acl_users
00038 
00039     def compareRoles(self, target, user, roles):
00040         """
00041         compareRoles(self, target, user, roles) => do not raise if
00042         user has exactly the specified roles.
00043 
00044         If target is None, test user roles (no local roles)
00045         """
00046         u = self.acl_users.getUser(user)
00047         if not u:
00048             raise RuntimeError, "compareRoles: Invalid user: '%s'" % user
00049         non_roles = ('Authenticated', 'Anonymous', '')
00050         if target is None:
00051             user_roles = list(u.getRoles())
00052         else:
00053             user_roles = list(u.getRolesInContext(target))
00054         actual_roles = filter(lambda x: x not in non_roles, user_roles)
00055         actual_roles.sort()
00056         wished_roles = list(roles)
00057         wished_roles.sort()
00058         if actual_roles == wished_roles:
00059             return 1
00060         raise RuntimeError, ("User %s: Whished roles: %s BUT current "
00061                              "roles: %s" % (user, wished_roles, actual_roles))
00062 
00063     def createUser(self, login="created_user", password="secret",
00064                    roles=[], groups=[], domains=()):
00065         self.acl_users.userFolderAddUser(
00066             login,
00067             password,
00068             roles = roles,
00069             groups = groups,
00070             domains = domains,
00071             )
00072 
00073     def test_installed(self):
00074         self.failUnless(IPluggableAuthService.providedBy(self.acl_users))
00075 
00076     def test_add(self):
00077         self.createUser()
00078         self.failUnless(self.acl_users.getUser("created_user"))
00079 
00080     def test_edit(self):
00081         # this will fail unless the PAS role plugin is told it manages
00082         # the new role.
00083         self.createUser()
00084         self.compareRoles(None, "created_user", [])
00085         self.acl_users.userFolderEditUser(
00086             "created_user", # name
00087             "secret2", # password
00088             roles = ["Member"],
00089             groups = ["g1"],
00090             domains = (),
00091             )
00092         self.compareRoles(None, "created_user", ['Member'])
00093 
00094     def test_edit_userDefinedRole(self):
00095        roleplugins = self.acl_users.plugins.listPlugins(IRolesPlugin)
00096         for id, plugin in roleplugins:
00097             try:
00098                 plugin.addRole('r1')
00099             except _SWALLOWABLE_PLUGIN_EXCEPTIONS:
00100                 pass
00101             else:
00102                 break
00103 
00104         self.createUser()
00105         self.compareRoles(None, "created_user", [])
00106         self.acl_users.userFolderEditUser(
00107             "created_user", # name
00108             "secret2", # password
00109             roles = ["r1"],
00110             groups = ["g1"],
00111             domains = (),
00112             )
00113         self.compareRoles(None, "created_user", ['r1'])
00114 
00115     def test_del(self):
00116         self.createUser()
00117         self.failUnless(self.acl_users.getUser("created_user"))
00118         self.acl_users.userFolderDelUsers(['created_user'])
00119         self.failIf(self.acl_users.getUser("created_user"))
00120 
00121     def test_search(self):
00122         self.createUser("created_user1")
00123         self.createUser("created_user2")
00124         mt = self.portal.portal_membership
00125         retlist = mt.searchForMembers(REQUEST=None, login="created_user1")
00126         usernames = [user.getUserName() for user in retlist]
00127        self.assertEquals(len(usernames), 1) 
00128         self.failUnless("created_user1" in usernames,
00129                         "'created_user1' not in %s" % usernames)
00130 
00131     def test_setpw(self):
00132         # there is more than one place where one can set the password.
00133         # insane. anyway we have to check the patch in pas.py userSetPassword 
00134         # here its checked in the general setup using ZODBUserManager.
00135         self.createUser()
00136         uf = self.acl_users
00137         new_secret = 'new_secret'
00138         uf.userSetPassword('created_user', new_secret)
00139         
00140         # possible to authenticate with new password?
00141         from Products.PluggableAuthService.interfaces.plugins \
00142             import IAuthenticationPlugin
00143         authenticators = uf.plugins.listPlugins(IAuthenticationPlugin)
00144         credentials = {'login': 'created_user', 'password': new_secret}
00145         result = None
00146         for aid, authenticator in authenticators:
00147             result = authenticator.authenticateCredentials(credentials)
00148             if result is not None:
00149                 break
00150         self.failUnless(result)        
00151         
00152         
00153 def test_suite():
00154     suite = unittest.TestSuite()
00155     suite.addTest(unittest.makeSuite(BasicOpsTestCase))
00156     return suite
00157