Back to index

plone3  3.1.7
test_MemberDataTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for MemberDataTool module.
00014 
00015 $Id: test_MemberDataTool.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 import Acquisition
00022 from zope.testing.cleanup import cleanUp
00023 
00024 
00025 class DummyUserFolder(Acquisition.Implicit):
00026 
00027     def __init__(self):
00028         self._users = {}
00029 
00030     def _addUser(self, user):
00031         self._users[user.getUserName()] = user
00032 
00033     def userFolderEditUser(self, name, password, roles, domains):
00034         user = self._users[name]
00035         if password is not None:
00036             user.__ = password
00037         # Emulate AccessControl.User's stupid behavior (should test None)
00038         user.roles = tuple(roles)
00039         user.domains = tuple(domains)
00040 
00041     def getUsers(self):
00042         return self._users.values()
00043 
00044 
00045 class DummyUser(Acquisition.Implicit):
00046 
00047     def __init__(self, name, password, roles, domains):
00048         self.name = name
00049         self.__ = password
00050         self.roles = tuple(roles)
00051         self.domains = tuple(domains)
00052 
00053     def getUserName(self):
00054         return self.name
00055 
00056     def getRoles(self):
00057         return self.roles + ('Authenticated',)
00058 
00059     def getDomains(self):
00060         return self.domains
00061 
00062 
00063 class DummyMemberDataTool(Acquisition.Implicit):
00064     pass
00065 
00066 
00067 class MemberDataToolTests(unittest.TestCase):
00068 
00069     def _makeOne(self, *args, **kw):
00070         from Products.CMFCore.MemberDataTool import MemberDataTool
00071 
00072         return MemberDataTool(*args, **kw)
00073 
00074     def tearDown(self):
00075         cleanUp()
00076 
00077     def test_z2interfaces(self):
00078         from Interface.Verify import verifyClass
00079         from Products.CMFCore.interfaces.portal_memberdata \
00080                 import portal_memberdata as IMemberDataTool
00081         from Products.CMFCore.MemberDataTool import MemberDataTool
00082 
00083         verifyClass(IMemberDataTool, MemberDataTool)
00084 
00085     def test_z3interfaces(self):
00086         from zope.interface.verify import verifyClass
00087         from Products.CMFCore.interfaces import IMemberDataTool
00088         from Products.CMFCore.MemberDataTool import MemberDataTool
00089 
00090         verifyClass(IMemberDataTool, MemberDataTool)
00091 
00092     def test_deleteMemberData(self):
00093         tool = self._makeOne()
00094         tool.registerMemberData('Dummy', 'user_foo')
00095         self.failUnless( tool._members.has_key('user_foo') )
00096         self.failUnless( tool.deleteMemberData('user_foo') )
00097         self.failIf( tool._members.has_key('user_foo') )
00098         self.failIf( tool.deleteMemberData('user_foo') )
00099 
00100     def test_pruneMemberData(self):
00101         # This needs a tad more setup
00102         from OFS.Folder import Folder
00103         from Products.CMFCore.MembershipTool import MembershipTool
00104         folder = Folder('test')
00105         folder._setObject('portal_memberdata', self._makeOne())
00106         folder._setObject('portal_membership', MembershipTool())
00107         folder._setObject('acl_users', DummyUserFolder())
00108         tool = folder.portal_memberdata
00109 
00110         # Create some members
00111         for i in range(20):
00112             tool.registerMemberData( 'Dummy_%i' % i
00113                                    , 'user_foo_%i' % i
00114                                    )
00115 
00116         # None of these fake members are in the user folder, which means
00117         # there are 20 members and 20 "orphans"
00118         contents = tool.getMemberDataContents()
00119         info_dict = contents[0]
00120         self.assertEqual(info_dict['member_count'], 20)
00121         self.assertEqual(info_dict['orphan_count'], 20)
00122 
00123         # Calling the prune method should delete all orphans, so we end
00124         # up with no members in the tool.
00125         tool.pruneMemberDataContents()
00126         contents = tool.getMemberDataContents()
00127         info_dict = contents[0]
00128         self.assertEqual(info_dict['member_count'], 0)
00129         self.assertEqual(info_dict['orphan_count'], 0)
00130 
00131 
00132 class MemberDataTests(unittest.TestCase):
00133 
00134     def _makeOne(self, *args, **kw):
00135         from Products.CMFCore.MemberDataTool import MemberData
00136 
00137         return MemberData(*args, **kw)
00138 
00139     def test_z2interfaces(self):
00140         from Interface.Verify import verifyClass
00141         from Products.CMFCore.interfaces.portal_memberdata \
00142                 import MemberData as IMemberData
00143         from Products.CMFCore.MemberDataTool import MemberData
00144 
00145         verifyClass(IMemberData, MemberData)
00146 
00147     def test_z3interfaces(self):
00148         from zope.interface.verify import verifyClass
00149         from Products.CMFCore.interfaces import IMemberData
00150         from Products.CMFCore.MemberDataTool import MemberData
00151 
00152         verifyClass(IMemberData, MemberData)
00153 
00154     def test_setSecurityProfile(self):
00155         mdtool = DummyMemberDataTool()
00156         aclu = DummyUserFolder()
00157         user = DummyUser('bob', 'pw', ['Role'], ['domain'])
00158         aclu._addUser(user)
00159         user = user.__of__(aclu)
00160         member = self._makeOne(None, 'bob').__of__(mdtool).__of__(user)
00161         member.setSecurityProfile(password='newpw')
00162         self.assertEqual(user.__, 'newpw')
00163         self.assertEqual(list(user.roles), ['Role'])
00164         self.assertEqual(list(user.domains), ['domain'])
00165         member.setSecurityProfile(roles=['NewRole'])
00166         self.assertEqual(user.__, 'newpw')
00167         self.assertEqual(list(user.roles), ['NewRole'])
00168         self.assertEqual(list(user.domains), ['domain'])
00169         member.setSecurityProfile(domains=['newdomain'])
00170         self.assertEqual(user.__, 'newpw')
00171         self.assertEqual(list(user.roles), ['NewRole'])
00172         self.assertEqual(list(user.domains), ['newdomain'])
00173 
00174 
00175 def test_suite():
00176     return unittest.TestSuite((
00177         unittest.makeSuite(MemberDataToolTests),
00178         unittest.makeSuite(MemberDataTests),
00179         ))
00180 
00181 if __name__ == '__main__':
00182     unittest.main(defaultTest='test_suite')