vendor/CMF/1.6.3/CMFCore

view tests/test_MemberDataTool.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Unit tests for MemberDataTool module.
15 $Id$
16 """
18 from unittest import TestCase, TestSuite, makeSuite, main
19 import Testing
20 try:
21 import Zope2
22 except ImportError: # BBB: for Zope 2.7
23 import Zope as Zope2
24 Zope2.startup()
26 import Acquisition
28 class DummyUserFolder(Acquisition.Implicit):
30 def __init__(self):
31 self._users = {}
33 def _addUser(self, user):
34 self._users[user.getUserName()] = user
36 def userFolderEditUser(self, name, password, roles, domains):
37 user = self._users[name]
38 if password is not None:
39 user.__ = password
40 # Emulate AccessControl.User's stupid behavior (should test None)
41 user.roles = tuple(roles)
42 user.domains = tuple(domains)
44 def getUsers(self):
45 return self._users.values()
48 class DummyUser(Acquisition.Implicit):
50 def __init__(self, name, password, roles, domains):
51 self.name = name
52 self.__ = password
53 self.roles = tuple(roles)
54 self.domains = tuple(domains)
56 def getUserName(self):
57 return self.name
59 def getRoles(self):
60 return self.roles + ('Authenticated',)
62 def getDomains(self):
63 return self.domains
66 class DummyMemberDataTool(Acquisition.Implicit):
67 pass
70 class MemberDataToolTests(TestCase):
72 def _makeOne(self, *args, **kw):
73 from Products.CMFCore.MemberDataTool import MemberDataTool
75 return MemberDataTool(*args, **kw)
77 def test_z2interfaces(self):
78 from Interface.Verify import verifyClass
79 from Products.CMFCore.interfaces.portal_actions \
80 import ActionProvider as IActionProvider
81 from Products.CMFCore.interfaces.portal_memberdata \
82 import portal_memberdata as IMemberDataTool
83 from Products.CMFCore.MemberDataTool import MemberDataTool
85 verifyClass(IActionProvider, MemberDataTool)
86 verifyClass(IMemberDataTool, MemberDataTool)
88 def test_z3interfaces(self):
89 try:
90 from zope.interface.verify import verifyClass
91 from Products.CMFCore.interfaces import IActionProvider
92 from Products.CMFCore.interfaces import IMemberDataTool
93 except ImportError:
94 # BBB: for Zope 2.7
95 return
96 from Products.CMFCore.MemberDataTool import MemberDataTool
98 verifyClass(IActionProvider, MemberDataTool)
99 verifyClass(IMemberDataTool, MemberDataTool)
101 def test_deleteMemberData(self):
102 tool = self._makeOne()
103 tool.registerMemberData('Dummy', 'user_foo')
104 self.failUnless( tool._members.has_key('user_foo') )
105 self.failUnless( tool.deleteMemberData('user_foo') )
106 self.failIf( tool._members.has_key('user_foo') )
107 self.failIf( tool.deleteMemberData('user_foo') )
109 def test_pruneMemberData(self):
110 # This needs a tad more setup
111 from OFS.Folder import Folder
112 from Products.CMFCore.MembershipTool import MembershipTool
113 folder = Folder('test')
114 folder._setObject('portal_memberdata', self._makeOne())
115 folder._setObject('portal_membership', MembershipTool())
116 folder._setObject('acl_users', DummyUserFolder())
117 tool = folder.portal_memberdata
119 # Create some members
120 for i in range(20):
121 tool.registerMemberData( 'Dummy_%i' % i
122 , 'user_foo_%i' % i
123 )
125 # None of these fake members are in the user folder, which means
126 # there are 20 members and 20 "orphans"
127 contents = tool.getMemberDataContents()
128 info_dict = contents[0]
129 self.assertEqual(info_dict['member_count'], 20)
130 self.assertEqual(info_dict['orphan_count'], 20)
132 # Calling the prune method should delete all orphans, so we end
133 # up with no members in the tool.
134 tool.pruneMemberDataContents()
135 contents = tool.getMemberDataContents()
136 info_dict = contents[0]
137 self.assertEqual(info_dict['member_count'], 0)
138 self.assertEqual(info_dict['orphan_count'], 0)
141 class MemberDataTests(TestCase):
143 def _makeOne(self, *args, **kw):
144 from Products.CMFCore.MemberDataTool import MemberData
146 return MemberData(*args, **kw)
148 def test_z2interfaces(self):
149 from Interface.Verify import verifyClass
150 from Products.CMFCore.interfaces.portal_memberdata \
151 import MemberData as IMemberData
152 from Products.CMFCore.MemberDataTool import MemberData
154 verifyClass(IMemberData, MemberData)
156 def test_z3interfaces(self):
157 try:
158 from zope.interface.verify import verifyClass
159 from Products.CMFCore.interfaces import IMemberData
160 except ImportError:
161 # BBB: for Zope 2.7
162 return
163 from Products.CMFCore.MemberDataTool import MemberData
165 verifyClass(IMemberData, MemberData)
167 def test_setSecurityProfile(self):
168 mdtool = DummyMemberDataTool()
169 aclu = DummyUserFolder()
170 user = DummyUser('bob', 'pw', ['Role'], ['domain'])
171 aclu._addUser(user)
172 user = user.__of__(aclu)
173 member = self._makeOne(None, 'bob').__of__(mdtool).__of__(user)
174 member.setSecurityProfile(password='newpw')
175 self.assertEqual(user.__, 'newpw')
176 self.assertEqual(list(user.roles), ['Role'])
177 self.assertEqual(list(user.domains), ['domain'])
178 member.setSecurityProfile(roles=['NewRole'])
179 self.assertEqual(user.__, 'newpw')
180 self.assertEqual(list(user.roles), ['NewRole'])
181 self.assertEqual(list(user.domains), ['domain'])
182 member.setSecurityProfile(domains=['newdomain'])
183 self.assertEqual(user.__, 'newpw')
184 self.assertEqual(list(user.roles), ['NewRole'])
185 self.assertEqual(list(user.domains), ['newdomain'])
188 def test_suite():
189 return TestSuite((
190 makeSuite( MemberDataToolTests ),
191 makeSuite( MemberDataTests ),
192 ))
194 if __name__ == '__main__':
195 main(defaultTest='test_suite')