Back to index

plone3  3.1.7
testControlPanelScripts.py
Go to the documentation of this file.
00001 #
00002 # Tests the control panel scripts
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from zExceptions import Forbidden
00007 from cStringIO import StringIO
00008 
00009 from DateTime import DateTime
00010 
00011 default_user = PloneTestCase.default_user
00012 default_password = PloneTestCase.default_password
00013 
00014 
00015 class TestNoGETControlPannel(PloneTestCase.FunctionalTestCase):
00016 
00017     def afterSetUp(self):
00018         self.folder_path = '/'+self.folder.absolute_url(1)
00019         self.setRoles(['Manager'])
00020         self.portal.portal_membership.addMember('bribri', 'secret', ['Manager'], [])
00021         self.login('bribri')
00022 
00023     def _onlyPOST(self, path, qstring='', success=200, rpath=None):
00024         qstring += '&%s=%s' % self.getAuthenticator()
00025         basic_auth = '%s:%s' % ('bribri', 'secret')
00026         env = dict()
00027         if rpath:
00028             env['HTTP_REFERER'] = self.app.absolute_url() + rpath
00029         response = self.publish('%s?%s' % (path, qstring), basic_auth, env,
00030                                 handle_errors=True)
00031         self.assertEqual(response.getStatus(), 403)
00032 
00033         data = StringIO(qstring)
00034         response = self.publish(path, basic_auth, env, request_method='POST',
00035                                 stdin=data)
00036         self.assertEqual(response.getStatus(), success)
00037 
00038     def test_prefsGroupMemberAdd(self):
00039         self.groups = self.portal.portal_groups
00040         self.groups.groupWorkspacesCreationFlag = 0
00041         self.groups.addGroup('foo')
00042         path = self.folder_path + '/prefs_group_members_add'
00043         qstring = 'groupname=%s&add:list=%s' % ('foo',default_user)
00044         self._onlyPOST(path, qstring)
00045 
00046 
00047     def test_prefsGroupMemberRemove(self):
00048         self.groups = self.portal.portal_groups
00049         self.groups.groupWorkspacesCreationFlag = 0
00050         self.groups.addGroup('foo')
00051         group = self.groups.getGroupById('foo')
00052         group.addMember(default_user)
00053         path = self.folder_path + '/prefs_group_members_delete'
00054         qstring = 'groupname=%s&delete:list=%s' % ('foo',default_user)
00055         self._onlyPOST(path, qstring)
00056 
00057     def test_prefsGroupEdit(self):
00058         path = self.folder_path + '/prefs_group_edit'
00059         qstring = 'addname=%s' % 'foo'
00060         self._onlyPOST(path, qstring, success=302)
00061 
00062     def test_changeOwnership(self):
00063         path = self.folder_path + '/change_ownership'
00064         qstring = 'userid=%s' % default_user
00065         self._onlyPOST(path, qstring, success=302)
00066 
00067     def test_loginChangePassword(self):
00068         path = self.folder_path + '/login_change_password'
00069         qstring = 'password=foo'
00070         self._onlyPOST(path, qstring)
00071 
00072     def test_prefsUserManager(self):
00073         path = self.folder_path + '/prefs_user_manage'
00074         qstring = 'users.id:records=%s&users.roles:list:records=Manager' % default_user
00075         self._onlyPOST(path, qstring,success=302)
00076         path = self.folder_path + '/prefs_user_manage'
00077         qstring = 'delete:list=%s' % default_user
00078         self._onlyPOST(path, qstring,success=302)
00079 
00080     def test_prefsGroupModify(self):
00081         self.groups = self.portal.portal_groups
00082         self.groups.groupWorkspacesCreationFlag = 0
00083         self.groups.addGroup('foo')
00084         path = self.folder_path + '/prefs_group_modify'
00085         qstring = 'group_foo:list=Manager'
00086         self._onlyPOST(path,qstring)
00087         path = self.folder_path + '/prefs_group_modify'
00088         qstring = 'delete:list=foo'
00089         self._onlyPOST(path,qstring)
00090         
00091         
00092 
00093 class TestPrefsUserManage(PloneTestCase.PloneTestCase):
00094 
00095     def afterSetUp(self):
00096         self.membership = self.portal.portal_membership
00097         self.membership.memberareaCreationFlag = 0
00098         self.setupAuthenticator()
00099 
00100     def addMember(self, username, fullname, email, roles, last_login_time):
00101         self.membership.addMember(username, 'secret', roles, [])
00102         member = self.membership.getMemberById(username)
00103         member.setMemberProperties({'fullname': fullname, 'email': email,
00104                                     'last_login_time': DateTime(last_login_time),})
00105 
00106     def testPrefsAddGroupPostOnly(self):
00107         self.setRoles(['Manager']) 
00108         self.setRequestMethod('GET')
00109         self.assertRaises(Forbidden, self.portal.prefs_group_edit, addname='foo')
00110 
00111     def testPrefsUserMembershipEditPostOnly(self):
00112        self.setRoles(['Manager'])
00113         self.groups = self.portal.portal_groups
00114         self.groups.groupWorkspacesCreationFlag = 0
00115         self.groups.addGroup('foo')
00116         self.setRequestMethod('GET')
00117         self.app.REQUEST.set('delete',['foo'])
00118         self.assertRaises(Forbidden, self.portal.prefs_user_membership_edit, userid='barney')
00119         del self.app.REQUEST.other['delete']
00120         self.app.REQUEST.set('add',['foo'])
00121         self.assertRaises(Forbidden, self.portal.prefs_user_membership_edit, userid='barney')
00122 
00123     def test_ploneChangePasswordPostOnly(self):
00124         self.login(default_user) 
00125         self.setRequestMethod('GET')
00126         self.assertRaises(Forbidden, self.portal.plone_change_password, current=default_password, password=default_password, password_confirm=default_password)
00127 
00128     def test_bug4333_delete_user_remove_memberdata(self):
00129         # delete user should delete portal_memberdata
00130         memberdata = self.portal.portal_memberdata
00131         self.setRoles(['Manager'])
00132         self.addMember('barney', 'Barney Rubble', 'barney@bedrock.com', ['Member'], '2002-01-01')
00133         barney = self.membership.getMemberById('barney')
00134         self.failUnlessEqual(barney.getProperty('email'), 'barney@bedrock.com')
00135         del barney
00136 
00137         self.setRequestMethod('POST')
00138         self.portal.prefs_user_manage(delete=['barney'])
00139         self.setRequestMethod('GET')
00140         md = memberdata._members
00141         self.failIf(md.has_key('barney'))
00142 
00143         # There is an _v_ variable that is killed at the end of each request
00144         # which stores a temporary version of the member object, this is
00145         # a problem in this test.
00146         memberdata._v_temps = None
00147 
00148         self.membership.addMember('barney', 'secret', ['Member'], [])
00149         barney = self.membership.getMemberById('barney')
00150         self.failIfEqual(barney.getProperty('fullname'), 'Barney Rubble')
00151         self.failIfEqual(barney.getProperty('email'), 'barney@bedrock.com')
00152 
00153 
00154 class TestAccessControlPanelScripts(PloneTestCase.FunctionalTestCase):
00155     '''Yipee, functional tests'''
00156 
00157     def afterSetUp(self):
00158         self.portal_path = self.portal.absolute_url(1)
00159         self.basic_auth = '%s:%s' % (default_user, default_password)
00160 
00161     def testPrefsUserDetails(self):
00162         '''Test access to user details.'''
00163         self.setRoles(['Manager'])
00164         
00165         response = self.publish('%s/portal_memberdata/prefs_user_details?userid=%s' %
00166                                 (self.portal_path, default_user),
00167                                 self.basic_auth)
00168 
00169         # this was failing in early Plone 2.5 due to missing five:traversable
00170         # declaration for tools
00171         self.assertEquals(response.getStatus(), 200)
00172 
00173 
00174 def test_suite():
00175     from unittest import TestSuite, makeSuite
00176     suite = TestSuite()
00177     suite.addTest(makeSuite(TestPrefsUserManage))
00178     suite.addTest(makeSuite(TestAccessControlPanelScripts))
00179     suite.addTest(makeSuite(TestNoGETControlPannel))
00180     return suite