Back to index

plone3  3.1.7
testMembershipTool.py
Go to the documentation of this file.
00001 #
00002 # Tests the membership tool
00003 #
00004 
00005 import os, sys
00006 if __name__ == '__main__':
00007     execfile(os.path.join(sys.path[0], 'framework.py'))
00008 
00009 from Products.PloneTestCase import PloneTestCase
00010 from Acquisition import aq_base
00011 from AccessControl.User import nobody
00012 
00013 PloneTestCase.setupPloneSite()
00014 default_user = PloneTestCase.default_user
00015 
00016 if PloneTestCase.PLONE25:
00017     USERFOLDER = 'PluggableAuthService'
00018     USERTYPE = 'PloneUser'
00019 else:
00020     USERFOLDER = 'GroupUserFolder'
00021     USERTYPE = 'GRUFUser'
00022 
00023 try: from zExceptions import BadRequest
00024 except ImportError: BadRequest = 'BadRequest'
00025 
00026 
00027 class TestMembershipTool(PloneTestCase.PloneTestCase):
00028 
00029     def afterSetUp(self):
00030         self.membership = self.portal.portal_membership
00031         self.membership.memberareaCreationFlag = 1
00032         self.membership.addMember('user2', 'secret', ['Member'], [])
00033 
00034     def testAddMember(self):
00035         self.failUnless(self.portal.acl_users.getUserById('user2'))
00036 
00037     def testAddMemberIfMemberExists(self):
00038         # Nothing should happen
00039         self.membership.addMember('user2', 'secret', ['Member'], [])
00040         self.failUnless(self.portal.acl_users.getUserById('user2'))
00041 
00042     def testGetMemberById(self):
00043         user = self.membership.getMemberById(default_user)
00044         self.failIfEqual(user, None)
00045         self.assertEqual(user.__class__.__name__, 'MemberData')
00046         self.assertEqual(user.aq_parent.__class__.__name__, USERTYPE)
00047 
00048     def testListMemberIds(self):
00049         ids = self.membership.listMemberIds()
00050         self.assertEqual(len(ids), 2)
00051         self.failUnless(default_user in ids)
00052         self.failUnless('user2' in ids)
00053 
00054     def testListMembers(self):
00055         members = self.membership.listMembers()
00056         self.assertEqual(len(members), 2)
00057         self.assertEqual(members[0].__class__.__name__, 'MemberData')
00058         self.assertEqual(members[0].aq_parent.__class__.__name__, USERTYPE)
00059         self.assertEqual(members[1].__class__.__name__, 'MemberData')
00060         self.assertEqual(members[1].aq_parent.__class__.__name__, USERTYPE)
00061 
00062     def testGetAuthenticatedMember(self):
00063         member = self.membership.getAuthenticatedMember()
00064         self.assertEqual(member.getUserName(), default_user)
00065 
00066     def testGetAuthenticatedMemberIfAnonymous(self):
00067         self.logout()
00068         member = self.membership.getAuthenticatedMember()
00069         self.assertEqual(member.getUserName(), 'Anonymous User')
00070 
00071     def testIsAnonymousUser(self):
00072         self.failIf(self.membership.isAnonymousUser())
00073         self.logout()
00074         self.failUnless(self.membership.isAnonymousUser())
00075 
00076     if PloneTestCase.PLONE25:
00077 
00078         def testSetPassword(self):
00079             # PAS does not provide the password
00080             self.membership.setPassword('geheim')
00081             member = self.membership.getMemberById(default_user)
00082             #self.assertEqual(member.getPassword(), 'geheim')
00083             self.assertEqual(member.getPassword(), None)
00084 
00085     else:
00086 
00087         def testSetPassword(self):
00088             self.membership.setPassword('geheim')
00089             member = self.membership.getMemberById(default_user)
00090             self.assertEqual(member.getPassword(), 'geheim')
00091 
00092     def testSetPasswordIfAnonymous(self):
00093         self.logout()
00094         try:
00095             self.membership.setPassword('geheim')
00096         except BadRequest:
00097             pass
00098         except:
00099             # String exceptions suck
00100             e,v,tb = sys.exc_info(); del tb
00101             if str(v) == 'Bad Request':
00102                 pass
00103         else:
00104             self.fail('Anonymous can change password')
00105 
00106     def testWrapUserWrapsBareUser(self):
00107         user = self.portal.acl_users.getUserById(default_user)
00108         # XXX: GRUF users are wrapped
00109         self.failUnless(hasattr(user, 'aq_base'))
00110         user = aq_base(user)
00111         user = self.membership.wrapUser(user)
00112         self.assertEqual(user.__class__.__name__, 'MemberData')
00113         self.assertEqual(user.aq_parent.__class__.__name__, USERTYPE)
00114         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, USERFOLDER)
00115 
00116     def testWrapUserWrapsWrappedUser(self):
00117         user = self.portal.acl_users.getUserById(default_user)
00118         # XXX: GRUF users are wrapped
00119         self.failUnless(hasattr(user, 'aq_base'))
00120         user = self.membership.wrapUser(user)
00121         self.assertEqual(user.__class__.__name__, 'MemberData')
00122         self.assertEqual(user.aq_parent.__class__.__name__, USERTYPE)
00123         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, USERFOLDER)
00124 
00125     def testWrapUserDoesntWrapMemberData(self):
00126         user = self.portal.acl_users.getUserById(default_user)
00127         user.getMemberId = lambda x: 1
00128         user = self.membership.wrapUser(user)
00129         self.assertEqual(user.__class__.__name__, USERTYPE)
00130 
00131     def testWrapUserWrapsAnonymous(self):
00132         self.failIf(hasattr(nobody, 'aq_base'))
00133         user = self.membership.wrapUser(nobody, wrap_anon=1)
00134         self.assertEqual(user.__class__.__name__, 'MemberData')
00135         self.assertEqual(user.aq_parent.__class__.__name__, 'SpecialUser')
00136         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, USERFOLDER)
00137 
00138     def testWrapUserDoesntWrapAnonymous(self):
00139         user = self.membership.wrapUser(nobody)
00140         self.assertEqual(user.__class__.__name__, 'SpecialUser')
00141 
00142     if PloneTestCase.PLONE30:
00143 
00144         def testGetPortalRoles(self):
00145             roles = self.membership.getPortalRoles()
00146             self.assertEqual(len(roles), 7)
00147             self.failUnless('Manager' in roles)
00148             self.failUnless('Member' in roles)
00149             self.failUnless('Owner' in roles)
00150             self.failUnless('Reviewer' in roles)
00151             self.failUnless('Reader' in roles)
00152             self.failUnless('Editor' in roles)
00153             self.failUnless('Contributor' in roles)
00154 
00155     else:
00156 
00157         def testGetPortalRoles(self):
00158             roles = self.membership.getPortalRoles()
00159             self.assertEqual(len(roles), 4)
00160             self.failUnless('Manager' in roles)
00161             self.failUnless('Member' in roles)
00162             self.failUnless('Owner' in roles)
00163             self.failUnless('Reviewer' in roles)
00164 
00165     def testSetRoleMapping(self):
00166         self.membership.setRoleMapping('Reviewer', 'FooRole')
00167         self.assertEqual(self.membership.role_map['Reviewer'], 'FooRole')
00168 
00169     def testGetMappedRole(self):
00170         self.membership.setRoleMapping('Reviewer', 'FooRole')
00171         self.assertEqual(self.membership.getMappedRole('Reviewer'), 'FooRole')
00172 
00173     # XXX: Plone does not map roles
00174     #def testWrapUserMapsRoles(self):
00175     #    self.membership.setRoleMapping('Reviewer', 'FooRole')
00176     #    self.setRoles(['FooRole'])
00177     #    user = self.portal.acl_users.getUserById(default_user)
00178     #    user = self.membership.wrapUser(user)
00179     #    self.assertEqual(user.getRoles(), ('FooRole', 'Reviewer', 'Authenticated'))
00180 
00181     def testMemberareaCreationFlag(self):
00182         self.failUnless(self.membership.getMemberareaCreationFlag())
00183 
00184     if PloneTestCase.PLONE21:
00185 
00186         def testCreateMemberarea(self):
00187             # CMF 1.5 requires user2 to be logged in!
00188             self.login('user2')
00189             members = self.membership.getMembersFolder()
00190             self.failIf(hasattr(aq_base(members), 'user2'))
00191             self.membership.createMemberArea('user2')
00192             self.failUnless(hasattr(aq_base(members), 'user2'))
00193 
00194         def testCreateMemberareaIfDisabled(self):
00195             # No longer works in CMF 1.5
00196             self.membership.setMemberareaCreationFlag() # toggle
00197             members = self.membership.getMembersFolder()
00198             self.failIf(hasattr(aq_base(members), 'user2'))
00199             self.membership.createMemberArea('user2')
00200             #self.failUnless(hasattr(aq_base(members), 'user2'))
00201             self.failIf(hasattr(aq_base(members), 'user2'))
00202 
00203         def testWrapUserCreatesMemberarea(self):
00204             # No longer the case in CMF 1.5
00205             members = self.membership.getMembersFolder()
00206             user = self.portal.acl_users.getUserById('user2')
00207             user = self.membership.wrapUser(user)
00208             #self.failUnless(hasattr(aq_base(members), 'user2'))
00209             self.failIf(hasattr(aq_base(members), 'user2'))
00210 
00211     else:
00212 
00213         def testCreateMemberarea(self):
00214             members = self.membership.getMembersFolder()
00215             self.failIf(hasattr(aq_base(members), 'user2'))
00216             self.membership.createMemberarea('user2')
00217             self.failUnless(hasattr(aq_base(members), 'user2'))
00218 
00219         def testCreateMemberareaIfDisabled(self):
00220             # This should work even if the flag is off
00221             self.membership.setMemberareaCreationFlag() # toggle
00222             members = self.membership.getMembersFolder()
00223             self.failIf(hasattr(aq_base(members), 'user2'))
00224             self.membership.createMemberarea('user2')
00225             self.failUnless(hasattr(aq_base(members), 'user2'))
00226 
00227         def testWrapUserCreatesMemberarea(self):
00228             members = self.membership.getMembersFolder()
00229             user = self.portal.acl_users.getUserById('user2')
00230             user = self.membership.wrapUser(user)
00231             self.failUnless(hasattr(aq_base(members), 'user2'))
00232 
00233     def testWrapUserDoesntCreateMemberarea(self):
00234         # No member area is created if the flag is off
00235         self.membership.setMemberareaCreationFlag()
00236         members = self.membership.getMembersFolder()
00237         user = self.portal.acl_users.getUserById('user2')
00238         user = self.membership.wrapUser(user)
00239         self.failIf(hasattr(aq_base(members), 'user2'))
00240 
00241     def testGetCandidateLocalRoles(self):
00242         self.assertEqual(self.membership.getCandidateLocalRoles(self.folder), ('Owner',))
00243         self.setRoles(['Member', 'Reviewer'])
00244         self.assertEqual(self.membership.getCandidateLocalRoles(self.folder), ('Owner', 'Reviewer'))
00245 
00246     def testSetLocalRoles(self):
00247         self.failUnless('Owner' in self.folder.get_local_roles_for_userid(default_user))
00248         self.setRoles(['Member', 'Reviewer'])
00249         self.membership.setLocalRoles(self.folder, [default_user, 'user2'], 'Reviewer')
00250         self.assertEqual(self.folder.get_local_roles_for_userid(default_user), ('Owner', 'Reviewer'))
00251         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ('Reviewer',))
00252 
00253     def testDeleteLocalRoles(self):
00254         self.setRoles(['Member', 'Reviewer'])
00255         self.membership.setLocalRoles(self.folder, ['user2'], 'Reviewer')
00256         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ('Reviewer',))
00257         self.membership.deleteLocalRoles(self.folder, ['user2'])
00258         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ())
00259 
00260     def testGetHomeFolder(self):
00261         self.failIfEqual(self.membership.getHomeFolder(), None)
00262         self.assertEqual(self.membership.getHomeFolder('user2'), None)
00263 
00264     def testGetHomeUrl(self):
00265         self.failIfEqual(self.membership.getHomeUrl(), None)
00266         self.assertEqual(self.membership.getHomeUrl('user2'), None)
00267 
00268 
00269 def test_suite():
00270     from unittest import TestSuite, makeSuite
00271     suite = TestSuite()
00272     suite.addTest(makeSuite(TestMembershipTool))
00273     return suite
00274 
00275 if __name__ == '__main__':
00276     framework()
00277