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.CMFTestCase import CMFTestCase
00010 from Acquisition import aq_base
00011 from AccessControl.User import nobody
00012 from AccessControl.AuthEncoding import pw_validate
00013 
00014 CMFTestCase.setupCMFSite()
00015 default_user = CMFTestCase.default_user
00016 
00017 try: from zExceptions import BadRequest
00018 except ImportError: BadRequest = 'BadRequest'
00019 
00020 
00021 class TestMembershipTool(CMFTestCase.CMFTestCase):
00022 
00023     def afterSetUp(self):
00024         self.membership = self.portal.portal_membership
00025         self.membership.addMember('user2', 'secret', ['Member'], [])
00026 
00027     def testAddMember(self):
00028         self.failUnless(self.portal.acl_users.getUserById('user2'))
00029 
00030     def testAddMemberIfMemberExists(self):
00031         # Nothing should happen
00032         self.membership.addMember('user2', 'secret', ['Member'], [])
00033         self.failUnless(self.portal.acl_users.getUserById('user2'))
00034 
00035     def testGetMemberById(self):
00036         user = self.membership.getMemberById(default_user)
00037         self.failIfEqual(user, None)
00038         self.assertEqual(user.__class__.__name__, 'MemberData')
00039         self.assertEqual(user.aq_parent.__class__.__name__, 'User')
00040 
00041     def testListMemberIds(self):
00042         ids = self.membership.listMemberIds()
00043         self.assertEqual(len(ids), 2)
00044         self.failUnless(default_user in ids)
00045         self.failUnless('user2' in ids)
00046 
00047     def testListMembers(self):
00048         members = self.membership.listMembers()
00049         self.assertEqual(len(members), 2)
00050         self.assertEqual(members[0].__class__.__name__, 'MemberData')
00051         self.assertEqual(members[0].aq_parent.__class__.__name__, 'User')
00052         self.assertEqual(members[1].__class__.__name__, 'MemberData')
00053         self.assertEqual(members[1].aq_parent.__class__.__name__, 'User')
00054 
00055     def testGetAuthenticatedMember(self):
00056         member = self.membership.getAuthenticatedMember()
00057         self.assertEqual(member.getUserName(), default_user)
00058 
00059     def testGetAuthenticatedMemberIfAnonymous(self):
00060         self.logout()
00061         member = self.membership.getAuthenticatedMember()
00062         self.assertEqual(member.getUserName(), 'Anonymous User')
00063 
00064     def testIsAnonymousUser(self):
00065         self.failIf(self.membership.isAnonymousUser())
00066         self.logout()
00067         self.failUnless(self.membership.isAnonymousUser())
00068 
00069     def testSetPassword(self):
00070         self.membership.setPassword('geheim')
00071         member = self.membership.getMemberById(default_user)
00072         auth = self.membership.getAuthenticatedMember()
00073         self.failUnless(pw_validate(member.getPassword(), 'geheim'))
00074         self.failUnless(pw_validate(auth.getPassword(), 'geheim'))
00075 
00076     def testSetPasswordIfAnonymous(self):
00077         self.logout()
00078         try:
00079             self.membership.setPassword('geheim')
00080         except BadRequest:
00081             pass
00082         except:
00083             # String exceptions suck (but CMF < 1.5 has them)
00084             e,v,tb = sys.exc_info(); del tb
00085             if str(e) != 'Bad Request':
00086                 raise
00087         else:
00088             self.fail('Anonymous can change password')
00089 
00090     def testWrapUserWrapsBareUser(self):
00091         user = self.portal.acl_users.getUserById(default_user)
00092         self.failIf(hasattr(user, 'aq_base'))
00093         user = self.membership.wrapUser(user)
00094         self.assertEqual(user.__class__.__name__, 'MemberData')
00095         self.assertEqual(user.aq_parent.__class__.__name__, 'User')
00096         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, 'UserFolder')
00097 
00098     def testWrapUserWrapsWrappedUser(self):
00099         user = self.portal.acl_users.getUserById(default_user)
00100         user = user.__of__(self.portal.acl_users)
00101         user = self.membership.wrapUser(user)
00102         self.assertEqual(user.__class__.__name__, 'MemberData')
00103         self.assertEqual(user.aq_parent.__class__.__name__, 'User')
00104         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, 'UserFolder')
00105 
00106     def testWrapUserDoesntWrapMemberData(self):
00107         user = self.portal.acl_users.getUserById(default_user)
00108         user.getMemberId = lambda x: 1
00109         user = self.membership.wrapUser(user)
00110         self.assertEqual(user.__class__.__name__, 'User')
00111 
00112     def testWrapUserWrapsAnonymous(self):
00113         self.failIf(hasattr(nobody, 'aq_base'))
00114         user = self.membership.wrapUser(nobody, wrap_anon=1)
00115         self.assertEqual(user.__class__.__name__, 'MemberData')
00116         self.assertEqual(user.aq_parent.__class__.__name__, 'SpecialUser')
00117         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, 'UserFolder')
00118 
00119     def testWrapUserDoesntWrapAnonymous(self):
00120         user = self.membership.wrapUser(nobody)
00121         self.assertEqual(user.__class__.__name__, 'SpecialUser')
00122 
00123     def testGetPortalRoles(self):
00124         roles = self.membership.getPortalRoles()
00125         self.assertEqual(len(roles), 4)
00126         self.failUnless('Manager' in roles)
00127         self.failUnless('Member' in roles)
00128         self.failUnless('Owner' in roles)
00129         self.failUnless('Reviewer' in roles)
00130 
00131     def testSetRoleMapping(self):
00132         self.membership.setRoleMapping('Reviewer', 'FooRole')
00133         self.assertEqual(self.membership.role_map['Reviewer'], 'FooRole')
00134 
00135     def testGetMappedRole(self):
00136         self.membership.setRoleMapping('Reviewer', 'FooRole')
00137         self.assertEqual(self.membership.getMappedRole('Reviewer'), 'FooRole')
00138 
00139     def testWrapUserMapsRoles(self):
00140         self.membership.setRoleMapping('Reviewer', 'FooRole')
00141         self.setRoles(['FooRole'])
00142         user = self.portal.acl_users.getUserById(default_user)
00143         user = self.membership.wrapUser(user)
00144         self.assertEqual(user.getRoles(), ('FooRole', 'Reviewer', 'Authenticated'))
00145 
00146     def testMemberareaCreationFlag(self):
00147         self.failUnless(self.membership.getMemberareaCreationFlag())
00148 
00149     if CMFTestCase.CMF15:
00150 
00151         def testCreateMemberarea(self):
00152             # CMF 1.5 requires user2 to be logged in!
00153             self.login('user2')
00154             members = self.membership.getMembersFolder()
00155             self.failIf(hasattr(aq_base(members), 'user2'))
00156             self.membership.createMemberArea('user2')
00157             self.failUnless(hasattr(aq_base(members), 'user2'))
00158 
00159         def testCreateMemberareaIfDisabled(self):
00160             # No longer works in CMF 1.5
00161             self.membership.setMemberareaCreationFlag() # toggle
00162             members = self.membership.getMembersFolder()
00163             self.failIf(hasattr(aq_base(members), 'user2'))
00164             self.membership.createMemberArea('user2')
00165             #self.failUnless(hasattr(aq_base(members), 'user2'))
00166             self.failIf(hasattr(aq_base(members), 'user2'))
00167 
00168         def testWrapUserCreatesMemberarea(self):
00169             # No longer the case in CMF 1.5
00170             members = self.membership.getMembersFolder()
00171             user = self.portal.acl_users.getUserById('user2')
00172             user = self.membership.wrapUser(user)
00173             #self.failUnless(hasattr(aq_base(members), 'user2'))
00174             self.failIf(hasattr(aq_base(members), 'user2'))
00175 
00176     else:
00177 
00178         def testCreateMemberarea(self):
00179             members = self.membership.getMembersFolder()
00180             self.failIf(hasattr(aq_base(members), 'user2'))
00181             self.membership.createMemberarea('user2')
00182             self.failUnless(hasattr(aq_base(members), 'user2'))
00183 
00184         def testCreateMemberareaIfDisabled(self):
00185             # This should work even if the flag is off
00186             self.membership.setMemberareaCreationFlag() # toggle
00187             members = self.membership.getMembersFolder()
00188             self.failIf(hasattr(aq_base(members), 'user2'))
00189             self.membership.createMemberarea('user2')
00190             self.failUnless(hasattr(aq_base(members), 'user2'))
00191 
00192         def testWrapUserCreatesMemberarea(self):
00193             members = self.membership.getMembersFolder()
00194             user = self.portal.acl_users.getUserById('user2')
00195             user = self.membership.wrapUser(user)
00196             self.failUnless(hasattr(aq_base(members), 'user2'))
00197 
00198     def testWrapUserDoesntCreateMemberarea(self):
00199         # No member area is created if the flag is off
00200         self.membership.setMemberareaCreationFlag()
00201         members = self.membership.getMembersFolder()
00202         user = self.portal.acl_users.getUserById('user2')
00203         user = self.membership.wrapUser(user)
00204         self.failIf(hasattr(aq_base(members), 'user2'))
00205 
00206     def testGetCandidateLocalRoles(self):
00207         self.assertEqual(self.membership.getCandidateLocalRoles(self.folder), ('Owner',))
00208         self.setRoles(['Member', 'Reviewer'])
00209         self.assertEqual(self.membership.getCandidateLocalRoles(self.folder), ('Owner', 'Reviewer'))
00210 
00211     def testSetLocalRoles(self):
00212         self.failUnless('Owner' in self.folder.get_local_roles_for_userid(default_user))
00213         self.setRoles(['Member', 'Reviewer'])
00214         self.membership.setLocalRoles(self.folder, [default_user, 'user2'], 'Reviewer')
00215         self.assertEqual(self.folder.get_local_roles_for_userid(default_user), ('Owner', 'Reviewer'))
00216         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ('Reviewer',))
00217 
00218     def testDeleteLocalRoles(self):
00219         self.setRoles(['Member', 'Reviewer'])
00220         self.membership.setLocalRoles(self.folder, ['user2'], 'Reviewer')
00221         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ('Reviewer',))
00222         self.membership.deleteLocalRoles(self.folder, ['user2'])
00223         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ())
00224 
00225     def testGetHomeFolder(self):
00226         self.failIfEqual(self.membership.getHomeFolder(), None)
00227         self.assertEqual(self.membership.getHomeFolder('user2'), None)
00228 
00229     def testGetHomeUrl(self):
00230         self.failIfEqual(self.membership.getHomeUrl(), None)
00231         self.assertEqual(self.membership.getHomeUrl('user2'), None)
00232 
00233 
00234 def test_suite():
00235     from unittest import TestSuite, makeSuite
00236     suite = TestSuite()
00237     suite.addTest(makeSuite(TestMembershipTool))
00238     return suite
00239 
00240 if __name__ == '__main__':
00241     framework()
00242