Back to index

plone3  3.1.7
testUserFolderBasics.py
Go to the documentation of this file.
00001 #
00002 # Generic user folder tests. Every user folder implementation
00003 # must pass these.
00004 #
00005 
00006 from Testing import ZopeTestCase
00007 from Products.CMFPlone.tests import PloneTestCase
00008 from Products.CMFCore.tests.base.testcase import WarningInterceptor
00009 
00010 import base64
00011 from AccessControl import Unauthorized
00012 
00013 default_user     = PloneTestCase.default_user
00014 default_password = PloneTestCase.default_password
00015 user_perms       = ZopeTestCase.standard_permissions
00016 user_role        = 'Member'
00017 
00018 
00019 class TestUserFolder(PloneTestCase.PloneTestCase, WarningInterceptor):
00020 
00021     def afterSetUp(self):
00022         self.logout()
00023         self.uf = self.portal.acl_users
00024         self.basic = 'Basic %s' % base64.encodestring('%s:%s' % (default_user, default_password))
00025         # Set up a published object accessible to the default user
00026         self.folder.addDTMLMethod('doc', file='')
00027         self.folder.doc.manage_permission('View', [user_role], acquire=0)
00028         # Rig the REQUEST so it looks like we traversed to 'doc'
00029         self.app.REQUEST.set('PUBLISHED', self.folder['doc'])
00030         self.app.REQUEST.set('PARENTS', [self.folder, self.app])
00031         folder_path = list(self.folder.getPhysicalPath())
00032         self.app.REQUEST.steps = folder_path + ['doc']
00033         self._trap_warning_output()
00034 
00035         if 'auto_group' in self.uf.objectIds():
00036             self.uf.manage_delObjects(['auto_group'])
00037 
00038         # Nuke Administators and Reviewers groups added in 2.1a2 migrations
00039         # (and any other migrated-in groups) to avoid test confusion
00040         self.portal.portal_groups.removeGroups(self.portal.portal_groups.listGroupIds())
00041 
00042     def testGetUser(self):
00043         self.failIfEqual(self.uf.getUser(default_user), None)
00044 
00045     def testGetBadUser(self):
00046         self.assertEqual(self.uf.getUser('user2'), None)
00047 
00048     def testGetUserById(self):
00049         self.failIfEqual(self.uf.getUserById(default_user), None)
00050 
00051     def testGetBadUserById(self):
00052         self.assertEqual(self.uf.getUserById('user2'), None)
00053 
00054     def testGetUsers(self):
00055         users = self.uf.getUsers()
00056         self.failUnless(users)
00057         self.assertEqual(users[0].getUserName(), default_user)
00058 
00059     def testGetUserNames(self):
00060         names = self.uf.getUserNames()
00061         self.failUnless(names)
00062         self.assertEqual(names[0], default_user)
00063 
00064     def testGetRoles(self):
00065         user = self.uf.getUser(default_user)
00066         self.failUnless(user_role in user.getRoles())
00067 
00068     def testGetRolesInContext(self):
00069         user = self.uf.getUser(default_user)
00070         self.folder.manage_addLocalRoles(default_user, ['Owner'])
00071         roles = user.getRolesInContext(self.folder)
00072         self.failUnless(user_role in roles)
00073         self.failUnless('Owner' in roles)
00074 
00075     def testHasRole(self):
00076         user = self.uf.getUser(default_user)
00077         self.failUnless(user.has_role(user_role, self.folder))
00078 
00079     def testHasLocalRole(self):
00080         user = self.uf.getUser(default_user)
00081         self.folder.manage_addLocalRoles(default_user, ['Owner'])
00082         self.failUnless(user.has_role('Owner', self.folder))
00083 
00084     def testHasPermission(self):
00085         user = self.uf.getUser(default_user)
00086         self.failUnless(user.has_permission('View', self.folder))
00087         self.folder.manage_role(user_role, ['Add Folders'])
00088         self.failUnless(user.has_permission('Add Folders', self.folder))
00089 
00090     def testHasLocalRolePermission(self):
00091         user = self.uf.getUser(default_user)
00092         self.folder.manage_role('Owner', ['Add Folders'])
00093         self.folder.manage_addLocalRoles(default_user, ['Owner'])
00094         self.failUnless(user.has_permission('Add Folders', self.folder))
00095 
00096     def testValidate(self):
00097         self.app.REQUEST._auth = self.basic
00098         user = self.uf.validate(self.app.REQUEST, self.basic, [user_role])
00099         self.failIfEqual(user, None)
00100         self.assertEqual(user.getUserName(), default_user)
00101 
00102     def testNotValidateWithoutAuth(self):
00103         self.app.REQUEST._auth = ''
00104         user = self.uf.validate(self.app.REQUEST, '', ['role1'])
00105         self.assertEqual(user, None)
00106 
00107     def testValidateWithoutRoles(self):
00108         self.app.REQUEST._auth = self.basic
00109         # Roles will be determined by looking at 'doc' itself
00110         user = self.uf.validate(self.app.REQUEST, self.basic)
00111         self.assertEqual(user.getUserName(), default_user)
00112 
00113     def testNotValidateWithEmptyRoles(self):
00114         self.app.REQUEST._auth = self.basic
00115         user = self.uf.validate(self.app.REQUEST, self.basic, [])
00116         self.assertEqual(user, None)
00117 
00118     def testNotValidateWithWrongRoles(self):
00119         self.app.REQUEST._auth = self.basic
00120         user = self.uf.validate(self.app.REQUEST, self.basic, ['Manager'])
00121         self.assertEqual(user, None)
00122 
00123     def testAllowAccessToUser(self):
00124         self.login()
00125         try:
00126             self.folder.restrictedTraverse('doc')
00127         except Unauthorized:
00128             self.fail('Unauthorized')
00129 
00130     def testDenyAccessToAnonymous(self):
00131         self.assertRaises(Unauthorized, self.folder.restrictedTraverse, 'doc')
00132 
00133     def beforeTearDown(self):
00134         self._free_warning_output()
00135 
00136 
00137 def test_suite():
00138     from unittest import TestSuite, makeSuite
00139     suite = TestSuite()
00140     suite.addTest(makeSuite(TestUserFolder))
00141     return suite