Back to index

plone3  3.1.7
test_properties.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # PlonePAS - Adapt PluggableAuthService for use in Plone
00004 # Copyright (C) 2005 Enfold Systems, Kapil Thangavelu, et al
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """
00016 """
00017 
00018 import unittest
00019 
00020 from PlonePASTestCase import PlonePASTestCase
00021 
00022 from cStringIO import StringIO
00023 from Acquisition import aq_base, aq_inner, aq_parent
00024 from Products.CMFCore.utils import getToolByName
00025 from Products.PluggableAuthService.interfaces.plugins import IUserEnumerationPlugin
00026 from Products.PlonePAS.plugins.property import ZODBMutablePropertyProvider
00027 
00028 
00029 class PropertiesTest(PlonePASTestCase):
00030 
00031     def test_user_properties(self):
00032         mt = getToolByName(self.portal, 'portal_membership')
00033         md = getToolByName(self.portal, 'portal_memberdata')
00034 
00035         # Create a new Member
00036         mt.addMember('user1', 'u1', ['Member'], [],
00037                      {'email': 'user1@host.com',
00038                       'fullname': 'User #1'})
00039         member = mt.getMemberById('user1')
00040         self.failIf(member is None)
00041 
00042         # Assert user doesn't have the property yet
00043         self.failIf(member.hasProperty('age'))
00044 
00045         # Add new property
00046         md.manage_addProperty('age', 20, 'int')
00047 
00048         # Assert user has the property now
00049         self.failUnless(member.hasProperty('age'))
00050 
00051         # Get the property, should have the default value
00052         got = member.getProperty('age', None)
00053         expected = 20
00054         self.assertEquals(got, expected)
00055 
00056         # get a handle on the member
00057         member = mt.getMemberById('user1')
00058 
00059         # Set some member properties. Needs to be logged in as the user.
00060         self.login('user1')
00061         member.setMemberProperties({'age':30, 'fullname':'User #1 Is Cool',
00062                                     'email':'user1@anotherhost.qa'})
00063 
00064         # Check the properties have been set
00065         got = member.getProperty('age', None)
00066         expected = 30
00067         self.assertEquals(got, expected)
00068 
00069         got = member.getProperty('fullname', None)
00070         expected = 'User #1 Is Cool'
00071         self.assertEquals(got, expected)
00072 
00073         got = member.getProperty('email', None)
00074         expected = 'user1@anotherhost.qa'
00075         self.assertEquals(got, expected)
00076 
00077         # Delete the property
00078         md.manage_delProperties(ids=('age',))
00079 
00080         # re-get the member to reflect the new memberdata schema
00081         member = mt.getMemberById('user1')
00082 
00083         # Assert property is gone
00084         self.failIf(member.hasProperty('age'))
00085 
00086         # Get the property, should return default (None)
00087         got = member.getProperty('age', None)
00088         expected = None
00089         self.assertEquals(got, expected)
00090 
00091         # Other properties should still be there.
00092         got = member.getProperty('fullname', None)
00093         expected = 'User #1 Is Cool'
00094         self.assertEquals(got, expected)
00095 
00096         got = member.getProperty('email', None)
00097         expected = 'user1@anotherhost.qa'
00098         self.assertEquals(got, expected)
00099 
00100     def test_group_properties(self):
00101         gt = getToolByName(self.portal, 'portal_groups')
00102         gd = getToolByName(self.portal, 'portal_groupdata')
00103 
00104         self.loginAsPortalOwner()
00105 
00106         # Create a new Group
00107         gt.addGroup('group1', ['Reviewer'], [],
00108                      {'email': 'group1@host.com',
00109                       'title': 'Group #1'})
00110         group = gt.getGroupById('group1')
00111         self.failIf(group is None)
00112 
00113         # Assert group doesn't have the property yet
00114         self.failIf(group.hasProperty('karma'))
00115 
00116         # Add new property
00117         gd.manage_addProperty('karma', 20, 'int')
00118 
00119         # get group again to re-create with new groupdata schema
00120         group = gt.getGroupById('group1')
00121 
00122         # Assert group has the property now
00123         self.failUnless(group.hasProperty('karma'))
00124 
00125         # Get the property, should have the default value
00126         got = group.getProperty('karma', None)
00127         expected = 20
00128 
00129         self.assertEquals(got, expected)
00130 
00131         # Set some group properties
00132         group.setGroupProperties({'karma':30, 'title':'Group #1 Is Cool',
00133                                   'email':'group1@anotherhost.qa'})
00134 
00135         # Check the properties have been set
00136         got = group.getProperty('karma', None)
00137         expected = 30
00138         self.assertEquals(got, expected)
00139 
00140         got = group.getProperty('title', None)
00141         expected = 'Group #1 Is Cool'
00142         self.assertEquals(got, expected)
00143 
00144         got = group.getProperty('email', None)
00145         expected = 'group1@anotherhost.qa'
00146         self.assertEquals(got, expected)
00147 
00148         # Delete the property
00149         gd.manage_delProperties(ids=('karma',))
00150 
00151         # get group again to re-create with new groupdata schema
00152         group = gt.getGroupById('group1')
00153 
00154         # Assert property is gone
00155         self.failIf(group.hasProperty('karma'))
00156 
00157         # Get the property, should return default (None)
00158         got = group.getProperty('karma', None)
00159         expected = None
00160         self.assertEquals(got, expected)
00161 
00162         # Other properties should still be there.
00163         got = group.getProperty('title', None)
00164         expected = 'Group #1 Is Cool'
00165         self.assertEquals(got, expected)
00166 
00167         got = group.getProperty('email', None)
00168         expected = 'group1@anotherhost.qa'
00169         self.assertEquals(got, expected)
00170 
00171 
00172     def test_schema_for_mutable_property_provider(self):
00173         """Add a schema to a ZODBMutablePropertyProvider.
00174         """
00175 
00176         # Schema is list of tuples with name, type (string), value.
00177         # From the types it seems only 'lines' is handled differently.
00178         address_schema = [
00179             ('addresses', 'lines', ['Here', 'There']),
00180             ('city', 'str', 'Somewhere'),
00181             ('telephone', 'int', 1234567),
00182             ]
00183 
00184         # This used to give a ValueError, so we just check that it
00185         # does not.
00186         provider = ZODBMutablePropertyProvider(
00187             'address_plugin', "Address Plugin", schema=address_schema)
00188 
00189         # When this test passes, we are happy already, but let's add a
00190         # few more basic tests.
00191 
00192         # Create a new Member
00193         mt = getToolByName(self.portal, 'portal_membership')
00194         mt.addMember('user1', 'u1', ['Member'], [],
00195                      {'email': 'user1@host.com',
00196                       'fullname': 'User #1'})
00197         member = mt.getMemberById('user1')
00198         sheet = provider.getPropertiesForUser(member)
00199         self.assertEqual(
00200             sheet.propertyIds(), ['addresses', 'city', 'telephone'])
00201         self.assertEqual(sheet.propertyInfo('city'), 
00202                          {'type': 'str', 'id': 'city', 'mode': ''})
00203         self.assertEqual(sheet.getProperty('addresses'), ('Here', 'There'))
00204  
00205 
00206 class PropertySearchTest(PlonePASTestCase):
00207     def afterSetUp(self):
00208         self.mt = getToolByName(self.portal, 'portal_membership')
00209         self.md = getToolByName(self.portal, 'portal_memberdata')
00210 
00211         # Create a new Member
00212         self.mt.addMember('member1', 'pw', ['Member'], [],
00213                      {'email': 'member1@host.com',
00214                       'title': 'Member #1'})
00215         member = self.mt.getMemberById('member1')
00216         self.failIf(member is None)
00217 
00218         self.mt.addMember('member2', 'pw', ['Member'], [],
00219                      {'email': 'user2@otherhost.com',
00220                       'fullname': 'User #2'})
00221         member = self.mt.getMemberById('member2')
00222         self.failIf(member is None)
00223 
00224         self.pas=getToolByName(self.portal, "acl_users")
00225         for plugin in self.pas.plugins.getAllPlugins('IUserEnumerationPlugin')['active']:
00226             if plugin!='mutable_properties':
00227                 self.pas.plugins.deactivatePlugin(IUserEnumerationPlugin, plugin)
00228 
00229 
00230     def testPluginActivated(self):
00231         plugins = self.pas.plugins.getAllPlugins('IUserEnumerationPlugin')['active']
00232         self.assertEqual(plugins, ('mutable_properties',))
00233 
00234 
00235     def testEmptySearch(self):
00236         results=self.pas.searchUsers()
00237         self.assertEqual(len(results), 2)
00238 
00239 
00240     def testInexactStringSearch(self):
00241         results=self.pas.searchUsers(email="something@somewhere.tld")
00242         self.assertEqual(results, ())
00243 
00244         results=self.pas.searchUsers(email="member1@host.com", exact_match=False)
00245         results=[info['userid'] for info in results]
00246         self.assertEqual(results, ['member1'])
00247 
00248         results=self.pas.searchUsers(email="@host.com", exact_match=False)
00249         results=[info['userid'] for info in results]
00250         self.assertEqual(results, ['member1'])
00251 
00252         results=self.pas.searchUsers(email="member1@host.com", exact_match=True)
00253         results=[info['userid'] for info in results]
00254         self.assertEqual(results, ['member1'])
00255 
00256         results=self.pas.searchUsers(email="@host.com", exact_match=True)
00257         results=[info['userid'] for info in results]
00258         self.assertEqual(results, [])
00259 
00260 
00261     def testBooleanSearch(self):
00262         results=self.pas.searchUsers(visible_ids=True)
00263         results=[info['userid'] for info in results]
00264         self.assertEqual(results, [])
00265 
00266         results=self.pas.searchUsers(visible_ids=False)
00267         results=[info['userid'] for info in results]
00268         self.assertEqual(results, ['member1', 'member2'])
00269 
00270 
00271 def test_suite():
00272     suite = unittest.TestSuite()
00273     suite.addTest(unittest.makeSuite(PropertiesTest))
00274     suite.addTest(unittest.makeSuite(PropertySearchTest))
00275     return suite
00276