Back to index

plone3  3.1.7
testMembershipTool.py
Go to the documentation of this file.
00001 #
00002 # MembershipTool tests
00003 #
00004 
00005 import os
00006 from cStringIO import StringIO
00007 
00008 from OFS.Image import Image
00009 
00010 from Products.CMFPlone.tests import PloneTestCase
00011 from Products.CMFPlone.tests import dummy
00012 from Products.CMFCore.tests.base.testcase import WarningInterceptor
00013 
00014 from AccessControl.User import nobody
00015 from AccessControl import Unauthorized
00016 from Acquisition import aq_base
00017 from DateTime import DateTime
00018 from zExceptions import BadRequest
00019 
00020 default_user = PloneTestCase.default_user
00021 default_password = PloneTestCase.default_password
00022 
00023 
00024 class TestMembershipTool(PloneTestCase.PloneTestCase, WarningInterceptor):
00025 
00026     def afterSetUp(self):
00027         self.membership = self.portal.portal_membership
00028         self.groups = self.portal.portal_groups
00029         self._trap_warning_output()
00030 
00031     def addMember(self, username, fullname, email, roles, last_login_time):
00032         self.membership.addMember(username, 'secret', roles, [])
00033         member = self.membership.getMemberById(username)
00034         member.setMemberProperties({'fullname': fullname, 'email': email,
00035                                     'last_login_time': DateTime(last_login_time),})
00036     def makeRealImage(self):
00037         import Products.CMFPlone as plone
00038         plone_path = os.path.dirname(plone.__file__)
00039         path = os.path.join(plone_path, 'tests', 'images', 'test.jpg')
00040         image = open(path, 'rb')
00041         image_upload = dummy.FileUpload(dummy.FieldStorage(image))
00042         return image_upload
00043 
00044     def testNoMorePersonalFolder(self):
00045         # .personal folders are history
00046         personal = getattr(self.folder, self.membership.personal_id, None)
00047         self.assertEqual(personal, None)
00048         self.assertEqual(self.membership.getPersonalFolder(default_user), None)
00049 
00050     def testGetPersonalFolderIfNoHome(self):
00051         # Should return None as the user has no home folder
00052         members = self.membership.getMembersFolder()
00053         members._delObject(default_user)
00054         self.assertEqual(self.membership.getPersonalFolder(default_user), None)
00055 
00056     def testGetPersonalPortrait(self):
00057         # Should return the default portrait
00058         self.assertEqual(self.membership.getPersonalPortrait(default_user).getId(), 'defaultUser.gif')
00059 
00060     def testChangeMemberPortrait(self):
00061         # Should change the portrait image
00062         # first we need a valid image
00063         image = self.makeRealImage()
00064         self.membership.changeMemberPortrait(image, default_user)
00065         self.assertEqual(self.membership.getPersonalPortrait(default_user).getId(), default_user)
00066         self.assertEqual(self.membership.getPersonalPortrait(default_user).meta_type, 'Image')
00067 
00068     def testDeletePersonalPortrait(self):
00069         # Should delete the portrait image
00070         image = self.makeRealImage()
00071         self.membership.changeMemberPortrait(image, default_user)
00072         self.assertEqual(self.membership.getPersonalPortrait(default_user).getId(), default_user)
00073         self.membership.deletePersonalPortrait(default_user)
00074         self.assertEqual(self.membership.getPersonalPortrait(default_user).getId(), 'defaultUser.gif')
00075 
00076     def testGetPersonalPortraitWithoutPassingId(self):
00077         # Should return the logged in users portrait if no id is given
00078         image = self.makeRealImage()
00079         self.membership.changeMemberPortrait(image, default_user)
00080         self.assertEqual(self.membership.getPersonalPortrait().getId(), default_user)
00081         self.assertEqual(self.membership.getPersonalPortrait().meta_type, 'Image')
00082 
00083     def testListMembers(self):
00084         # Should return the members list
00085         members = self.membership.listMembers()
00086         self.assertEqual(len(members), 1)
00087         self.assertEqual(members[0].getId(), default_user)
00088 
00089     def testListMembersSkipsGroups(self):
00090         # Should only return real members, not groups
00091         uf = self.portal.acl_users
00092         self.groups.addGroup('Foo')
00093         self.groups.addGroup('Bar')
00094         self.assertEqual(len(uf.getUserNames()), 1)
00095         members = self.membership.listMembers()
00096         self.assertEqual(len(members), 1)
00097         self.assertEqual(members[0].getId(), default_user)
00098 
00099     def testListMemberIds(self):
00100         # Should return the members ids list
00101         memberids = self.membership.listMemberIds()
00102         self.assertEqual(len(memberids), 1)
00103         self.assertEqual(memberids[0], default_user)
00104 
00105     def testListMemberIdsSkipsGroups(self):
00106         # Should only return real members, not groups
00107         uf = self.portal.acl_users
00108         self.groups.addGroup('Foo')
00109         self.groups.addGroup('Bar')
00110         self.assertEqual(len(uf.getUserNames()), 1)
00111         memberids = self.membership.listMemberIds()
00112         self.assertEqual(len(memberids), 1)
00113         self.assertEqual(memberids[0], default_user)
00114 
00115     def testCurrentPassword(self):
00116         # Password checking should work
00117         self.failUnless(self.membership.testCurrentPassword('secret'))
00118         self.failIf(self.membership.testCurrentPassword('geheim'))
00119 
00120     def testSetPassword(self):
00121         # Password should be changed
00122         self.membership.setPassword('geheim')
00123         self.failUnless(self.membership.testCurrentPassword('geheim'))
00124 
00125     def testSetPasswordIfAnonymous(self):
00126         # Anonymous should not be able to change password
00127         self.logout()
00128         try:
00129             self.membership.setPassword('geheim')
00130         except BadRequest:
00131             import sys; e, v, tb = sys.exc_info(); del tb
00132             if str(v) == 'Not logged in.':
00133                 pass
00134             else:
00135                 raise
00136 
00137     def testSetPasswordAndKeepGroups(self):
00138         # Password should be changed and user must not change group membership
00139         group2 = 'g2'
00140         groups = self.groups
00141         groups.addGroup(group2, None, [], [])
00142         group = groups.getGroupById(group2)
00143         self.loginAsPortalOwner() # GRUF 3.52
00144         group.addMember(default_user)
00145         self.login(default_user) # Back to normal
00146         ugroups = self.portal.acl_users.getUserById(default_user).getGroups()
00147         self.membership.setPassword('geheim')
00148         self.failUnless(self.portal.acl_users.getUserById(default_user).getGroups() == ugroups)
00149 
00150     def testGetMemberById(self):
00151         # This should work for portal users,
00152         self.failIfEqual(self.membership.getMemberById(default_user), None)
00153         # return None for unknown users,
00154         self.assertEqual(self.membership.getMemberById('foo'), None)
00155         # and return None for users defined outside of the portal.
00156         ##self.assertEqual(self.membership.getMemberById(PloneTestCase.portal_owner), None)
00157         # Since CMF 1.5.2 the membershiptool will search "up"
00158         self.failIfEqual(self.membership.getMemberById(PloneTestCase.portal_owner), None)
00159 
00160     def testGetMemberByIdIsWrapped(self):
00161         member = self.membership.getMemberById(default_user)
00162         self.failIfEqual(member, None)
00163         self.assertEqual(member.__class__.__name__, 'MemberData')
00164         self.assertEqual(member.aq_parent.__class__.__name__, 'PloneUser')
00165 
00166     def testGetAuthenticatedMember(self):
00167         member = self.membership.getAuthenticatedMember()
00168         self.assertEqual(member.getUserName(), default_user)
00169 
00170     def testGetAuthenticatedMemberIsWrapped(self):
00171         member = self.membership.getAuthenticatedMember()
00172         self.assertEqual(member.getUserName(), default_user)
00173         self.assertEqual(member.__class__.__name__, 'MemberData')
00174         self.assertEqual(member.aq_parent.__class__.__name__, 'PloneUser')
00175 
00176     def testGetAuthenticatedMemberIfAnonymous(self):
00177         self.logout()
00178         member = self.membership.getAuthenticatedMember()
00179         self.assertEqual(member.getUserName(), 'Anonymous User')
00180 
00181     def testAnonymousMemberIsNotWrapped(self):
00182         # Also see http://dev.plone.org/plone/ticket/1851
00183         self.logout()
00184         member = self.membership.getAuthenticatedMember()
00185         self.failIfEqual(member.__class__.__name__, 'MemberData')
00186         self.assertEqual(member.__class__.__name__, 'SpecialUser')
00187 
00188     def testIsAnonymousUser(self):
00189         self.failIf(self.membership.isAnonymousUser())
00190         self.logout()
00191         self.failUnless(self.membership.isAnonymousUser())
00192 
00193     def testWrapUserWrapsBareUser(self):
00194         user = self.portal.acl_users.getUserById(default_user)
00195         # TODO: GRUF users are wrapped
00196         self.failUnless(hasattr(user, 'aq_base'))
00197         user = aq_base(user)
00198         user = self.membership.wrapUser(user)
00199         self.assertEqual(user.__class__.__name__, 'MemberData')
00200         self.assertEqual(user.aq_parent.__class__.__name__, 'PloneUser')
00201         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, 'PluggableAuthService')
00202 
00203     def testWrapUserWrapsWrappedUser(self):
00204         user = self.portal.acl_users.getUserById(default_user)
00205         # TODO: GRUF users are wrapped
00206         self.failUnless(hasattr(user, 'aq_base'))
00207         user = self.membership.wrapUser(user)
00208         self.assertEqual(user.__class__.__name__, 'MemberData')
00209         self.assertEqual(user.aq_parent.__class__.__name__, 'PloneUser')
00210         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, 'PluggableAuthService')
00211 
00212     def testWrapUserDoesntWrapMemberData(self):
00213         user = self.portal.acl_users.getUserById(default_user)
00214         user.getMemberId = lambda x: 1
00215         user = self.membership.wrapUser(user)
00216         self.assertEqual(user.__class__.__name__, 'PloneUser')
00217 
00218     def testWrapUserDoesntWrapAnonymous(self):
00219         user = self.membership.wrapUser(nobody)
00220         self.assertEqual(user.__class__.__name__, 'SpecialUser')
00221 
00222     def testWrapUserWrapsAnonymous(self):
00223         self.failIf(hasattr(nobody, 'aq_base'))
00224         user = self.membership.wrapUser(nobody, wrap_anon=1)
00225         self.assertEqual(user.__class__.__name__, 'MemberData')
00226         self.assertEqual(user.aq_parent.__class__.__name__, 'SpecialUser')
00227         self.assertEqual(user.aq_parent.aq_parent.__class__.__name__, 'PluggableAuthService')
00228 
00229     def testGetCandidateLocalRoles(self):
00230         self.assertEqual(self.membership.getCandidateLocalRoles(self.folder), ('Owner',))
00231         self.setRoles(['Member', 'Reviewer'])
00232         self.assertEqual(self.membership.getCandidateLocalRoles(self.folder), ('Owner', 'Reviewer'))
00233 
00234     def testSetLocalRoles(self):
00235         self.failUnless('Owner' in self.folder.get_local_roles_for_userid(default_user))
00236         self.setRoles(['Member', 'Reviewer'])
00237         self.membership.setLocalRoles(self.folder, [default_user, 'user2'], 'Reviewer')
00238         self.assertEqual(self.folder.get_local_roles_for_userid(default_user), ('Owner', 'Reviewer'))
00239         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ('Reviewer',))
00240 
00241     def testDeleteLocalRoles(self):
00242         self.setRoles(['Member', 'Reviewer'])
00243         self.membership.setLocalRoles(self.folder, ['user2'], 'Reviewer')
00244         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ('Reviewer',))
00245         self.membership.deleteLocalRoles(self.folder, ['user2'])
00246         self.assertEqual(self.folder.get_local_roles_for_userid('user2'), ())
00247 
00248     def testGetHomeFolder(self):
00249         self.failIfEqual(self.membership.getHomeFolder(), None)
00250         self.assertEqual(self.membership.getHomeFolder('user2'), None)
00251 
00252     def testGetHomeUrl(self):
00253         self.failIfEqual(self.membership.getHomeUrl(), None)
00254         self.assertEqual(self.membership.getHomeUrl('user2'), None)
00255 
00256     def testGetAuthenticatedMemberInfo(self):
00257         member = self.membership.getAuthenticatedMember()
00258         member.setMemberProperties({'fullname': 'Test user'})
00259         info = self.membership.getMemberInfo()
00260         self.assertEqual(info['fullname'], 'Test user')
00261 
00262     def testGetMemberInfo(self):
00263         self.membership.addMember('user2', 'secret', ['Member'], [],
00264                                   properties={'fullname': 'Second user'})
00265         info = self.membership.getMemberInfo('user2')
00266         self.assertEqual(info['fullname'], 'Second user')
00267 
00268     def testGetCandidateLocalRolesIncludesLocalRolesOnObjectForManager(self):
00269         self.folder._addRole('my_test_role')
00270         self.folder.manage_setLocalRoles(default_user, ('Manager','Owner'))
00271         roles = self.membership.getCandidateLocalRoles(self.folder)
00272         self.failUnless('my_test_role' in roles,
00273                         'my_test_role not in: %s'%str(roles))
00274 
00275     def testGetCandidateLocalRolesIncludesLocalRolesOnObjectForAssignees(self):
00276         self.folder._addRole('my_test_role')
00277         self.folder.manage_setLocalRoles(default_user, ('my_test_role','Owner'))
00278         roles = self.membership.getCandidateLocalRoles(self.folder)
00279         self.failUnless('Owner' in roles)
00280         self.failUnless('my_test_role' in roles)
00281         self.assertEqual(len(roles), 2)
00282 
00283     def testGetCandidateLocalRolesForManager(self):
00284         self.folder._addRole('my_test_role')
00285         self.folder.manage_setLocalRoles(default_user, ('Manager','Owner'))
00286         roles = self.membership.getCandidateLocalRoles(self.folder)
00287         self.failUnless('Manager' in roles)
00288         self.failUnless('Owner' in roles)
00289         self.failUnless('Reviewer' in roles)
00290 
00291     def testGetCandidateLocalRolesForOwner(self):
00292         self.folder._addRole('my_test_role')
00293         roles = self.membership.getCandidateLocalRoles(self.folder)
00294         self.failUnless('Owner' in roles)
00295         self.assertEqual(len(roles), 1)
00296 
00297     def testGetCandidateLocalRolesForAssigned(self):
00298         self.folder._addRole('my_test_role')
00299         self.folder.manage_setLocalRoles(default_user, ('Reviewer','Owner'))
00300         roles = self.membership.getCandidateLocalRoles(self.folder)
00301         self.failUnless('Owner' in roles)
00302         self.failUnless('Reviewer' in roles)
00303         self.assertEqual(len(roles), 2)
00304 
00305     def test_bug4333_delete_user_remove_memberdata(self):
00306         # delete user should delete portal_memberdata
00307         memberdata = self.portal.portal_memberdata
00308         self.setRoles(['Manager'])
00309         self.addMember('barney', 'Barney Rubble', 'barney@bedrock.com', ['Member'], '2002-01-01')
00310         barney = self.membership.getMemberById('barney')
00311         self.failUnlessEqual(barney.getProperty('email'), 'barney@bedrock.com')
00312         del barney
00313 
00314         self.membership.deleteMembers(['barney'])
00315         md = memberdata._members
00316         self.failIf(md.has_key('barney'))
00317 
00318         # There is an _v_ variable that is killed at the end of each request
00319         # which stores a temporary version of the member object, this is
00320         # a problem in this test.  In fact, this test does not really
00321         # demonstrate the bug, which is actually caused by the script not
00322         # using the tool.
00323         memberdata._v_temps = None
00324 
00325         self.membership.addMember('barney', 'secret', ['Member'], [])
00326         barney = self.membership.getMemberById('barney')
00327         self.failIfEqual(barney.getProperty('fullname'), 'Barney Rubble')
00328         self.failIfEqual(barney.getProperty('email'), 'barney@bedrock.com')
00329 
00330     def testBogusMemberPortrait(self):
00331         # Should change the portrait image
00332         bad_file = dummy.File(data='<div>This is a lie!!!</div>',
00333                               headers={'content_type':'image/jpeg'})
00334         self.assertRaises(IOError, self.membership.changeMemberPortrait,
00335                           bad_file, default_user)
00336 
00337     def testGetBadMembers(self):
00338         # Should list members with bad images
00339         # We should not have any bad images out of the box
00340         self.assertEqual(self.membership.getBadMembers(), [])
00341         # Let's add one
00342         bad_file = Image(id=default_user, title='',
00343                                file=StringIO('<div>This is a lie!!!</div>'))
00344         # Manually set a bad image using private methods
00345         self.portal.portal_memberdata._setPortrait(bad_file, default_user)
00346         self.assertEqual(self.membership.getBadMembers(), [default_user])
00347         # Try an empty image
00348         empty_file =  Image(id=default_user, title='', file=StringIO(''))
00349         self.portal.portal_memberdata._setPortrait(empty_file, default_user)
00350         self.assertEqual(self.membership.getBadMembers(), [])
00351         # And a good image
00352         self.membership.changeMemberPortrait(self.makeRealImage(), default_user)
00353         self.assertEqual(self.membership.getBadMembers(), [])
00354 
00355     def beforeTearDown(self):
00356         self._free_warning_output()
00357 
00358 
00359 class TestCreateMemberarea(PloneTestCase.PloneTestCase):
00360 
00361     def afterSetUp(self):
00362         self.membership = self.portal.portal_membership
00363         self.membership.addMember('user2', 'secret', ['Member'], [])
00364 
00365     def testCreateMemberarea(self):
00366         # Should create a memberarea for user2
00367         if self.membership.memberareaCreationFlag == True:
00368             members = self.membership.getMembersFolder()
00369             self.membership.createMemberarea('user2')
00370             memberfolder = self.membership.getHomeFolder('user2')
00371             self.failUnless(memberfolder, 'createMemberarea failed to create memberarea')
00372             # member area creation should be on by default
00373             self.failUnless(self.membership.getMemberareaCreationFlag())
00374 
00375     def testCreatMemberareaUsesCurrentUser(self):
00376         if self.membership.memberareaCreationFlag == True:
00377             # Should create a memberarea for user2
00378             self.login('user2')
00379             self.membership.createMemberarea()
00380             memberfolder = self.membership.getHomeFolder('user2')
00381             self.failUnless(memberfolder, 'createMemberarea failed to create memberarea for current user')
00382         else:
00383             pass
00384 
00385     def testNoMemberareaIfNoMembersFolder(self):
00386         # Should not create a memberarea if the Members folder is missing
00387         self.portal._delObject('Members')
00388         self.membership.createMemberarea('user2')
00389         memberfolder = self.membership.getHomeFolder('user2')
00390         self.failIf(memberfolder, 'createMemberarea unexpectedly created a memberarea')
00391 
00392     def testNoMemberareaIfMemberareaExists(self):
00393         # Should not attempt to create a memberarea if a memberarea already exists
00394         self.membership.createMemberarea('user2')
00395         # The second call should do nothing (not cause an error)
00396         self.membership.createMemberarea('user2')
00397 
00398     def testNotifyScriptIsCalled(self):
00399         # The notify script should be called
00400         if self.membership.memberareaCreationFlag == True:
00401             self.portal.notifyMemberAreaCreated = dummy.Raiser(dummy.Error)
00402             self.assertRaises(dummy.Error, self.membership.createMemberarea, 'user2')       
00403 
00404     def testCreateMemberareaAlternateName(self):
00405         # Alternate method name 'createMemberaArea' should work
00406         if self.membership.memberareaCreationFlag == True:
00407             members = self.membership.getMembersFolder()
00408             self.membership.createMemberArea('user2')
00409             memberfolder = self.membership.getHomeFolder('user2')
00410             self.failUnless(memberfolder, 'createMemberArea failed to create memberarea')
00411 
00412     def testCreateMemberareaLPF(self):
00413         # Should create a Large Plone Folder instead of a normal Folder
00414         if self.membership.memberareaCreationFlag == True:
00415             self.membership.setMemberAreaType('Large Plone Folder')
00416             self.membership.createMemberarea('user2')
00417             memberfolder = self.membership.getHomeFolder('user2')
00418             self.assertEqual(memberfolder.getPortalTypeName(), 'Large Plone Folder')
00419 
00420     def testCreateMemberareaWhenDisabled(self):
00421         # Should not create a member area
00422         self.membership.setMemberareaCreationFlag = False
00423         self.failIf(self.membership.getMemberareaCreationFlag())
00424         self.membership.createMemberarea('user2')
00425         memberfolder = self.membership.getHomeFolder('user2')
00426         self.failIf(memberfolder, 'createMemberarea created memberarea despite flag')
00427 
00428 class TestMemberareaSetup(PloneTestCase.PloneTestCase):
00429 
00430     def afterSetUp(self):
00431         self.membership = self.portal.portal_membership
00432         self.membership.addMember('user2', 'secret', ['Member'], [])
00433         self.membership.createMemberarea('user2')
00434         self.home = self.membership.getHomeFolder('user2')
00435 
00436     def testMemberareaIsPloneFolder(self):
00437         if self.membership.memberareaCreationFlag == True:
00438             # Memberarea should be a Plone folder
00439             self.assertEqual(self.home.meta_type, 'ATFolder')
00440             self.assertEqual(self.home.portal_type, 'Folder')
00441 
00442     def testMemberareaIsOwnedByMember(self):
00443         if self.membership.memberareaCreationFlag == True:
00444             # Memberarea should be owned by member
00445             try: owner_info = self.home.getOwnerTuple()
00446             except AttributeError:
00447                 owner_info = self.home.getOwner(info=1)
00448             self.assertEqual(owner_info[0], [PloneTestCase.portal_name, 'acl_users'])
00449             self.assertEqual(owner_info[1], 'user2')
00450             self.assertEqual(len(self.home.get_local_roles()), 1)
00451             self.assertEqual(self.home.get_local_roles_for_userid('user2'), ('Owner',))
00452 
00453     def testMemberareaHasDescription(self):
00454         # Memberarea should have a description - not in 2.1 ~limi
00455         #self.failUnless(self.home.Description())
00456         pass
00457 
00458     def testMemberareaIsCataloged(self):
00459         if self.membership.memberareaCreationFlag == True:
00460             # Memberarea should be cataloged
00461             catalog = self.portal.portal_catalog
00462             self.failUnless(catalog(id='user2', Type='Folder', Title="user2"),
00463                             "Could not find user2's home folder in the catalog")
00464 
00465     def testHomePageNotExists(self):
00466         if self.membership.memberareaCreationFlag == True:
00467             # Should not have an index_html document anymore
00468             self.failIf('index_html' in self.home.objectIds())
00469 
00470 
00471 class TestSearchForMembers(PloneTestCase.PloneTestCase, WarningInterceptor):
00472 
00473     def afterSetUp(self):
00474         self.memberdata = self.portal.portal_memberdata
00475         self.membership = self.portal.portal_membership
00476         # Don't let default_user disturb results
00477         self.portal.acl_users._doDelUsers([default_user])
00478         # Add some members
00479         self.addMember('fred', 'Fred Flintstone',
00480                        'fred@bedrock.com', ['Member', 'Reviewer'],
00481                        '2002-01-01')
00482         self.addMember('barney', 'Barney Rubble',
00483                        'barney@bedrock.com', ['Member'],
00484                        '2002-01-01')
00485         self.addMember('brubble', 'Bambam Rubble',
00486                        'bambam@bambam.net', ['Member'],
00487                        '2003-12-31')
00488         # MUST reset this
00489         self.memberdata._v_temps = None
00490         self._trap_warning_output()
00491 
00492 
00493     def addMember(self, username, fullname, email, roles, last_login_time):
00494         self.membership.addMember(username, 'secret', roles, [])
00495         member = self.membership.getMemberById(username)
00496         member.setMemberProperties({'fullname': fullname, 'email': email,
00497                                     'last_login_time': DateTime(last_login_time),})
00498 
00499     def testSearchById(self):
00500         # Should search id and fullname
00501         search = self.membership.searchForMembers
00502         self.assertEqual(len(search(name='brubble')), 0)
00503         self.assertEqual(len(search(name='barney')), 1)
00504         self.assertEqual(len(search(name='rubble')), 2)
00505 
00506     def testSearchByName(self):
00507         # Should search id and fullname
00508         search = self.membership.searchForMembers
00509         self.assertEqual(len(search(name='rubble')), 2)
00510         self.assertEqual(len(search(name='stone')), 1)
00511 
00512     def testSearchByEmail(self):
00513         search = self.membership.searchForMembers
00514         self.assertEqual(len(search(email='bedrock')), 2)
00515         self.assertEqual(len(search(email='bambam')), 1)
00516 
00517     def testSearchByRoles(self):
00518         search = self.membership.searchForMembers
00519         self.assertEqual(len(search(roles=['Member'])), 3)
00520         self.assertEqual(len(search(roles=['Reviewer'])), 1)
00521 
00522 
00523     def testSearchByNameAndEmail(self):
00524         search = self.membership.searchForMembers
00525         self.assertEqual(len(search(name='rubble', email='bedrock')), 1)
00526         self.assertEqual(len(search(name='bambam', email='bedrock')), 0)
00527 
00528     def testSearchByNameAndRoles(self):
00529         search = self.membership.searchForMembers
00530         self.assertEqual(len(search(name='fred', roles=['Reviewer'])), 1)
00531         self.assertEqual(len(search(name='fred', roles=['Manager'])), 0)
00532 
00533 
00534     def testSearchByEmailAndRoles(self):
00535         search = self.membership.searchForMembers
00536         self.assertEqual(len(search(email='fred', roles=['Reviewer'])), 1)
00537         self.assertEqual(len(search(email='fred', roles=['Manager'])), 0)
00538 
00539     def beforeTearDown(self):
00540         self._free_warning_output()
00541 
00542 
00543 class TestDefaultUserAndPasswordNotChanged(PloneTestCase.PloneTestCase):
00544     # A test for a silly transaction/persistency bug in PlonePAS
00545 
00546     def afterSetUp(self):
00547         self.membership = self.portal.portal_membership
00548 
00549     def testDefaultUserAndPasswordUnchanged(self):
00550         member = self.membership.getAuthenticatedMember()
00551         self.assertEqual(member.getUserName(), default_user)
00552         self.failUnless(self.membership.testCurrentPassword(default_password))
00553         self.failIf(self.membership.testCurrentPassword('geheim'))
00554 
00555 
00556 class TestMethodProtection(PloneTestCase.PloneTestCase):
00557     # MembershipTool is missing security declarations
00558     # http://dev.plone.org/plone/ticket/5432
00559 
00560     _unprotected = (
00561         'changeMemberPortrait',
00562         'deletePersonalPortrait',
00563         'testCurrentPassword',
00564     )
00565 
00566     def afterSetUp(self):
00567         self.membership = self.portal.portal_membership
00568 
00569     def assertUnprotected(self, object, method):
00570         self.logout()
00571         object.restrictedTraverse(method)
00572 
00573     def assertProtected(self, object, method):
00574         self.logout()
00575         self.assertRaises(Unauthorized, object.restrictedTraverse, method)
00576 
00577     for method in _unprotected:
00578         exec "def testUnprotected_%s(self):" \
00579              "    self.assertProtected(self.membership, '%s')" % (method, method)
00580 
00581         exec "def testMemberAccessible_%s(self):" \
00582              "    self.membership.restrictedTraverse('%s')" % (method, method)
00583 
00584 
00585 def test_suite():
00586     from unittest import TestSuite, makeSuite
00587     suite = TestSuite()
00588     suite.addTest(makeSuite(TestMembershipTool))
00589     suite.addTest(makeSuite(TestCreateMemberarea))
00590     suite.addTest(makeSuite(TestMemberareaSetup))
00591     suite.addTest(makeSuite(TestSearchForMembers))
00592     suite.addTest(makeSuite(TestDefaultUserAndPasswordNotChanged))
00593     suite.addTest(makeSuite(TestMethodProtection))
00594     return suite