Back to index

plone3  3.1.7
testUserAPI.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 ## GroupUserFolder
00003 ## Copyright (C)2006 Ingeniweb
00004 
00005 ## This program is free software; you can redistribute it and/or modify
00006 ## it under the terms of the GNU General Public License as published by
00007 ## the Free Software Foundation; either version 2 of the License, or
00008 ## (at your option) any later version.
00009 
00010 ## This program is distributed in the hope that it will be useful,
00011 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ## GNU General Public License for more details.
00014 
00015 ## You should have received a copy of the GNU General Public License
00016 ## along with this program; see the file COPYING. If not, write to the
00017 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 """
00019 
00020 """
00021 __version__ = "$Revision:  $"
00022 # $Source:  $
00023 # $Id: testUserAPI.py 35844 2007-01-17 10:45:00Z encolpe $
00024 __docformat__ = 'restructuredtext'
00025 
00026 
00027 
00028 import os, sys
00029 if __name__ == '__main__':
00030     execfile(os.path.join(sys.path[0], 'framework.py'))
00031 
00032 # Load fixture
00033 from Testing import ZopeTestCase
00034 
00035 # Permissions / security
00036 from AccessControl.Permissions import access_contents_information, view, add_documents_images_and_files, change_images_and_files, view_management_screens
00037 from AccessControl.SecurityManagement import newSecurityManager, noSecurityManager, getSecurityManager
00038 from AccessControl import Unauthorized
00039 from AccessControl.User import UnrestrictedUser
00040 
00041 import urllib
00042 import string
00043 
00044 # Create the error_log object
00045 app = ZopeTestCase.app()
00046 ZopeTestCase.utils.setupSiteErrorLog(app)
00047 ZopeTestCase.close(app)
00048 
00049     
00050 # Get global vars
00051 #from Products.GroupUserFolder.global_symbols import *
00052 from Products.GroupUserFolder import GRUFUser
00053 from Interface import Verify
00054 
00055 # Install our product
00056 ZopeTestCase.installProduct('GroupUserFolder')
00057 
00058 import GRUFTestCase
00059 import testInterface
00060 from Log import *
00061 
00062 
00063 class TestUserFolderAPI(GRUFTestCase.GRUFTestCase, testInterface.TestInterface):
00064 
00065     klasses = (        # tell which classes to check
00066         GRUFUser.GRUFUser,
00067         GRUFUser.GRUFGroup,
00068         )
00069 
00070     def test03ClassSecurityInfo(self,):
00071         """We inhibit this test: class security info is not managed this way with user objects
00072         """
00073         pass
00074     
00075     def test_getId(self,):
00076         u = self.gruf.getUser("u1")
00077         self.failUnless(u.getId() == "u1")
00078         u = self.gruf.getUser("group_g1")
00079         self.failUnless(u.getId() == "group_g1")
00080 
00081     def test_getUserName(self,):
00082         u = self.gruf.getUser("u1")
00083         self.failUnless(u.getUserName() == "u1")
00084         u = self.gruf.getUser("group_g1")
00085         self.failUnless(u.getUserName() == "g1")
00086 
00087     def test_getName(self,):
00088         u = self.gruf.getUser("u1")
00089         self.failUnless(u.getUserName() == "u1")
00090         u = self.gruf.getUser("group_g1")
00091         self.failUnless(u.getUserName() == "g1")
00092 
00093     def test_getRoles(self):
00094         u = self.gruf.getUser("u5")
00095         r = list(u.getRoles())
00096         r.sort()
00097         Log(LOG_DEBUG, r)
00098         self.failUnless(r == ["Authenticated", "r1", "r2", ])
00099 
00100     def test_setRoles(self,):
00101         # Initial situation
00102         u = self.gruf.getUser("u5")
00103         r = list(u.getRoles())
00104         r.sort()
00105         self.failUnless(r == ["Authenticated", "r1", "r2", ])
00106 
00107         # Regular role setting
00108         u.setRoles(["r3", "r2", "r1"])
00109         r = list(u.getRoles())
00110         r.sort()
00111         self.failUnless(r == ["Authenticated", "r1", "r2", "r3"])
00112 
00113         # Try to add an invalid role => should be prohibited
00114         try:
00115             u.setRoles(["r3", "r2", "r1", "bloub"])
00116         except ValueError:
00117             pass
00118         else:
00119             raise AssertionError, "Should raise a ValueError here"
00120 
00121         # Reset roles : we must keep group-defined roles
00122         u.setRoles([])
00123         r = list(u.getRoles())
00124         r.sort()
00125         self.failUnless(r == ["Authenticated", "r1", "r2", ])
00126 
00127         # Now reset roles on another user
00128         u = self.gruf.getUser("u6")
00129         r = list(u.getRoles())
00130         r.sort()
00131         self.failUnless(r == ["Authenticated", "r1", "r2", ])
00132         u.setRoles([])
00133         r = list(u.getRoles())
00134         r.sort()
00135         self.failUnless(r == ["Authenticated", "r2", ])
00136 
00137     def test_addRole(self,):
00138         # Initial situation
00139         u = self.gruf.getUser("u5")
00140         r = list(u.getRoles())
00141         r.sort()
00142         self.failUnless(r == ["Authenticated", "r1", "r2", ])
00143 
00144         # Regular role adding
00145         u.addRole("r3", )
00146         r = list(u.getRoles())
00147         r.sort()
00148         self.failUnless(r == ["Authenticated", "r1", "r2", "r3"])
00149 
00150         # Invalid role adding -> should raise
00151         try:
00152             u.addRole("bloub", )
00153         except ValueError:
00154             pass
00155         else:
00156             raise AssertionError, "Should raise a ValueError here"
00157 
00158     def test_removeRole(self,):
00159         # Initial situation
00160         u = self.gruf.getUser("u6")
00161         r = list(u.getRoles())
00162         r.sort()
00163         self.failUnless(r == ["Authenticated", "r1", "r2", ])
00164 
00165         # Regular role removing
00166         u.removeRole("r1", )
00167         r = list(u.getRoles())
00168         r.sort()
00169         self.failUnless(r == ["Authenticated", "r2", ])
00170 
00171         # Try to remove a non-used role => should be transparent
00172         u.removeRole("r1", )
00173         r = list(u.getRoles())
00174         r.sort()
00175         self.failUnless(r == ["Authenticated", "r2", ])
00176 
00177         # Remove group role : we must keep group-defined roles
00178         u.removeRole("r2")
00179         r = list(u.getRoles())
00180         r.sort()
00181         Log(LOG_DEBUG, r)
00182         self.failUnless(r == ["Authenticated", "r2", ])
00183 
00184     def test_getRolesInContext(self, ):
00185         r = self.gruf.getUser("u2").getRolesInContext(self.gruf_folder.lr)
00186         self.failUnless("r3" in r)
00187         r = self.gruf.getUser("u3").getRolesInContext(self.gruf_folder.lr)
00188         self.failUnless("r1" in r)
00189         self.failUnless("r3" in r)
00190 
00191     def test_has_permission(self, ):
00192         pass            # Don't know how to test this :(
00193 
00194     def test_allowed(self, ):
00195         pass            # XXX Will have to test that intensively !
00196 
00197     def test_has_role(self, ):
00198         u = self.gruf.getUser("u2")
00199         self.failUnless(u.has_role("r3", self.gruf_folder.lr, ))
00200 
00201     def test_isGroup(self, ):
00202         u = self.gruf.getUser("u1")
00203         self.failUnless(not u.isGroup())
00204         u = self.gruf.getUser("u2")
00205         self.failUnless(not u.isGroup())
00206         u = self.gruf.getUser("g1")
00207         self.failUnless(u.isGroup())
00208         u = self.gruf.getUser("ng2")
00209         self.failUnless(u.isGroup())
00210         u = self.gruf.getUser("g3")
00211         self.failUnless(u.isGroup())
00212         
00213 
00214     def test_getGroupNames(self,):
00215         # Regular test
00216         u = self.gruf.getUser("u2")
00217         g = u.getGroupNames()
00218         g.sort()
00219         self.failUnless(g == ["g1", ])
00220 
00221         # Empty list test
00222         u = self.gruf.getUser("u1")
00223         g = u.getGroupNames()
00224         g.sort()
00225         self.failUnless(g == [])
00226 
00227         # GroupOfGroup test
00228         u = self.gruf.getUser("ng1")
00229         g = u.getGroupNames()
00230         g.sort()
00231         self.failUnless(g == ["g1", ])
00232 
00233         # Non-transitivity test
00234         u = self.gruf.getUser("u10")
00235         g = u.getGroupNames()
00236         g.sort()
00237         self.failUnless(g == ["ng2", "ng3", ])
00238 
00239     def test_getGroupIds(self, ):
00240         # Regular test
00241         u = self.gruf.getUser("u2")
00242         g = u.getGroupIds()
00243         g.sort()
00244         self.failUnless(g == ["group_g1", ])
00245 
00246         # Empty list test
00247         u = self.gruf.getUser("u1")
00248         g = u.getGroupIds()
00249         g.sort()
00250         self.failUnless(g == [])
00251 
00252         # GroupOfGroup test
00253         u = self.gruf.getUser("ng1")
00254         g = u.getGroupIds()
00255         g.sort()
00256         self.failUnless(g == ["group_g1", ])
00257 
00258         # Non-transitivity test
00259         u = self.gruf.getUser("u10")
00260         g = u.getGroupIds()
00261         g.sort()
00262         self.failUnless(g == ["group_ng2", "group_ng3", ])
00263 
00264     def test_getGroups(self, ):
00265         # Regular test
00266         u = self.gruf.getUser("u2")
00267         g = u.getGroups()
00268         g.sort()
00269         self.failUnless(g == ["group_g1", ])
00270 
00271         # Empty list test
00272         u = self.gruf.getUser("u1")
00273         g = u.getGroups()
00274         g.sort()
00275         self.failUnless(g == [])
00276 
00277         # GroupOfGroup test
00278         u = self.gruf.getUser("ng1")
00279         g = u.getGroups()
00280         g.sort()
00281         self.failUnless(g == ["group_g1", ])
00282 
00283         # Transitivity test
00284         u = self.gruf.getUser("u10")
00285         g = u.getGroups()
00286         g.sort()
00287         self.failUnless(g == ["group_g2", "group_g3", "group_ng2", "group_ng3", ])
00288 
00289 
00290     def test_getImmediateGroups(self,):
00291         # Regular test
00292         u = self.gruf.getUser("u2")
00293         g = u.getImmediateGroups()
00294         g.sort()
00295         self.failUnless(g == ["group_g1", ])
00296 
00297         # Empty list test
00298         u = self.gruf.getUser("u1")
00299         g = u.getImmediateGroups()
00300         g.sort()
00301         self.failUnless(g == [])
00302 
00303         # GroupOfGroup test
00304         u = self.gruf.getUser("ng1")
00305         g = u.getImmediateGroups()
00306         g.sort()
00307         self.failUnless(g == ["group_g1", ])
00308 
00309         # Transitivity test
00310         u = self.gruf.getUser("u10")
00311         g = u.getImmediateGroups()
00312         g.sort()
00313         self.failUnless(g == ["group_ng2", "group_ng3", ], u.getImmediateGroups())
00314 
00315 
00316     def test_getAllGroupIds(self):
00317         # Regular test
00318         u = self.gruf.getUser("u2")
00319         g = u.getAllGroupIds()
00320         g.sort()
00321         self.failUnless(g == ["group_g1", ])
00322 
00323         # Empty list test
00324         u = self.gruf.getUser("u1")
00325         g = u.getAllGroupIds()
00326         g.sort()
00327         self.failUnless(g == [])
00328 
00329         # GroupOfGroup test
00330         u = self.gruf.getUser("ng1")
00331         g = u.getAllGroupIds()
00332         g.sort()
00333         self.failUnless(g == ["group_g1", ])
00334 
00335         # Transitivity test
00336         u = self.gruf.getUser("u10")
00337         g = u.getAllGroupIds()
00338         g.sort()
00339         self.failUnless(g == ["group_g2", "group_g3", "group_ng2", "group_ng3", ])
00340 
00341 
00342     def test_getAllGroupNames(self):
00343         # Regular test
00344         u = self.gruf.getUser("u2")
00345         g = u.getAllGroupNames()
00346         g.sort()
00347         self.failUnless(g == ["g1", ])
00348 
00349         # Empty list test
00350         u = self.gruf.getUser("u1")
00351         g = u.getAllGroupNames()
00352         g.sort()
00353         self.failUnless(g == [])
00354 
00355         # GroupOfGroup test
00356         u = self.gruf.getUser("ng1")
00357         g = u.getAllGroupNames()
00358         g.sort()
00359         self.failUnless(g == ["g1", ])
00360 
00361         # Transitivity test
00362         u = self.gruf.getUser("u10")
00363         g = u.getAllGroupNames()
00364         g.sort()
00365         self.failUnless(g == ["g2", "g3", "ng2", "ng3", ])
00366 
00367 
00368     def test_isInGroup(self, ):
00369         u = self.gruf.getUser("u2")
00370         self.failUnless(u.isInGroup("group_g1", ))
00371         self.failUnless(not u.isInGroup("g1", ))        # We don not allow group names
00372 
00373         # Empty list test
00374         u = self.gruf.getUser("u1")
00375         self.failUnless(not u.isInGroup("group_g1", ))
00376 
00377         # GroupOfGroup test
00378         u = self.gruf.getUser("ng1")
00379         self.failUnless(u.isInGroup("group_g1", ))
00380 
00381         # Transitivity test
00382         u = self.gruf.getUser("u10")
00383         self.failUnless(u.isInGroup("group_g2", ))
00384         self.failUnless(u.isInGroup("group_g3", ))
00385         self.failUnless(u.isInGroup("group_ng2", ))
00386         self.failUnless(u.isInGroup("group_ng3", ))
00387         
00388 
00389     def test_setGroups(self, ):
00390         self.gruf.userFolderAddUser(
00391             name = "created_user",
00392             password = "secret",
00393             groups = [],
00394             roles = (),
00395             domains = (),
00396             )
00397         u = self.gruf.getUser("created_user")
00398         u.setGroups(["g1", "g2", ], )
00399         self.compareGroups("created_user", ["g1", "g2", ], )
00400         u.setGroups([], )
00401         self.compareGroups("created_user", [], )
00402         u.setGroups(["group_g1", "group_g2", ], )
00403         self.compareGroups("created_user", ["g1", "g2", ], )
00404         try:
00405             u.setGroups(["group_g1", "group_g2", "bloub", ], )
00406         except ValueError:
00407             pass
00408         else:
00409             raise AssertionError, "Should raise ValueError"
00410 
00411     def test_addGroup(self, ):
00412         u = self.gruf.getUser("u1")
00413         self.failUnless(u.getGroups() == [])
00414         u.addGroup("g3")
00415         self.failUnless(u.getGroups() == ["group_g3"])
00416         u.addGroup("group_g2")
00417         r = u.getGroups()
00418         r.sort()
00419         self.failUnless(r == ["group_g2", "group_g3"])
00420         
00421 
00422     def test_removeGroup(self, ):
00423         u = self.gruf.getUser("u1")
00424         u.addGroup("group_g3")
00425         u.addGroup("group_g2")
00426         r = u.getGroups()
00427         r.sort()
00428         self.failUnless(r == ["group_g2", "group_g3"])
00429         u.removeGroup("group_g3")
00430         Log(LOG_DEBUG, u.getGroups())
00431         self.failUnless(u.getGroups() == ["group_g2", ])
00432         u.removeGroup("group_g2")
00433         self.failUnless(u.getGroups() == [])
00434         
00435         
00436     def test_getRealId(self, ):
00437         u = self.gruf.getUser("u1")
00438         self.failUnless(u.getRealId() == "u1")
00439         u = self.gruf.getUser("g1")
00440         self.failUnless(u.getRealId() == "g1")
00441         u = self.gruf.getUser("group_g1")
00442         self.failUnless(u.getRealId() == "g1")
00443         
00444 
00445     def test_getDomains(self,):
00446         """Return the list of domain restrictions for a user"""
00447         self.gruf.userFolderAddUser(
00448             "test_crea",
00449             "secret",
00450             [],
00451             ["a", 'b', 'c'],
00452             [],
00453             )
00454         u = self.gruf.getUser("test_crea")
00455         d = list(u.getDomains())
00456         d.sort()
00457         self.failUnless(d == ["a", "b", "c", ])
00458 
00459     
00460     def test_setPassword(self):
00461         # Regular user password
00462         user = self.gruf.getUser('u1')
00463         self.failUnless(user.authenticate('secret', self.app.REQUEST))
00464         user.setPassword("marih")
00465         self.failUnless(not user.authenticate('secret', self.app.REQUEST))
00466         self.failUnless(user.authenticate('marih', self.app.REQUEST))
00467 
00468         # Prohibit group password changing (the method shouldn't even be available)
00469         u = self.gruf.getUser("g1")
00470         try:
00471             u.setPassword("bloub")
00472         except AttributeError:
00473             pass                # Ok
00474         else:
00475             raise AssertionError, "Password change must be prohibited for groups"
00476 
00477     def test_setDomains(self, ):
00478         u = self.gruf.getUser("u1")
00479         self.failUnless(not u.getDomains())
00480         u.setDomains(["d1", "d2", "d3", ])
00481         d = list(u.getDomains())
00482         d.sort()
00483         self.failUnless(d == ["d1", "d2", "d3", ])
00484         u.setDomains([])
00485         self.failUnless(tuple(u.getDomains()) == ())
00486         u.setDomains(["xxx"])
00487         self.failUnless(tuple(u.getDomains()) == ("xxx", ))
00488         
00489 
00490     def test_addDomain(self, ):
00491         "..."
00492 
00493     def test_removeDomain(self, ):
00494         "..."
00495 
00496     def test_getMemberIds(self, ):
00497         u = self.gruf.getGroup("ng2")
00498         ulist = u.getMemberIds()
00499         ulist.sort()
00500         self.failUnless(ulist == ['group_ng3', 'group_ng4', 'group_ng5', 'u10', 'u11', 'u9', ])
00501 
00502     def test_getUserMemberIds(self, ):
00503         u = self.gruf.getGroup("ng2")
00504         ulist = u.getUserMemberIds()
00505         ulist.sort()
00506         self.failUnless(ulist == ['u10', 'u11', 'u9', ])
00507 
00508         u = self.gruf.getGroup("g2")
00509         ulist = u.getUserMemberIds()
00510         ulist.sort()
00511         self.failUnless(ulist == ['u10', 'u11', 'u3', 'u4', 'u5', 'u9', ])
00512 
00513     def test_getGroupMemberIds(self, ):
00514         u = self.gruf.getGroup("ng2")
00515         ulist = u.getGroupMemberIds()
00516         ulist.sort()
00517         self.failUnless(ulist == ['group_ng3', 'group_ng4', 'group_ng5'])
00518 
00519     def test_hasMember(self, ):
00520         self.failUnless(self.gruf.getGroup("g2").hasMember("u4"))
00521         self.failUnless(self.gruf.getGroup("g2").hasMember("group_ng2"))
00522 
00523     def test_addMember(self, ):
00524         g = self.gruf.getGroup("ng3")
00525         self.failUnless("u1" not in g.getMemberIds())
00526         g.addMember("u1")
00527         self.failUnless("u1" in g.getMemberIds())
00528 
00529     def test_removeMember(self, ):
00530         g = self.gruf.getGroup("ng3")
00531         self.failUnless("u1" not in g.getMemberIds())
00532         g.addMember("u1")
00533         self.failUnless("u1" in g.getMemberIds())
00534         g.removeMember("u1")
00535         self.failUnless("u1" not in g.getMemberIds())
00536 
00537         ## remove a group in a group
00538         # ng5 is in g2 and we add g3 in ng5
00539         g2 = self.gruf.getGroup("g2")
00540         ng5 = self.gruf.getGroup("ng5")
00541 
00542         self.failUnless("group_ng1" not in ng5.getMemberIds())
00543         self.failUnless("group_ng1" not in g2.getMemberIds())
00544         ng5.addMember("ng1")
00545 
00546         self.failUnless("group_ng1" in ng5.getMemberIds())
00547         self.failUnless("group_ng1" in g2.getMemberIds())
00548         ng5.removeMember("ng1")
00549 
00550         self.failUnless("group_ng1" not in ng5.getMemberIds())
00551         self.failUnless("group_ng1" not in g2.getMemberIds())
00552 
00553 
00554     def test_getProperty(self,):
00555         """Will raise for regular user folders
00556         """
00557         try:
00558             self.gruf.getUser("u1").getProperty("email")
00559         except:
00560             pass
00561         else:
00562             raise AssertionError, "Should raise"
00563 
00564     def test_hasProperty(self,):
00565         self.failUnless(not self.gruf.getUser("u1").hasProperty("email"))
00566 
00567     def test_setProperty(self,):
00568         try:
00569             self.gruf.getUser("u1").setProperty("email", "test@test.com")
00570         except NotImplementedError:
00571             pass
00572         else:
00573             raise AssertionError, "Should raise here."
00574 
00575 
00576 
00577 if __name__ == '__main__':
00578     framework(descriptions=1, verbosity=1)
00579 else:
00580     import unittest
00581     def test_suite():
00582         suite = unittest.TestSuite()
00583         suite.addTest(unittest.makeSuite(TestUserFolderAPI))
00584         return suite
00585