Back to index

plone3  3.1.7
test_membership_tool.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 if __name__ == '__main__':
00021     execfile(os.path.join(os.path.dirname(sys.argv[0]), 'framework.py'))
00022 
00023 from PlonePASTestCase import PlonePASTestCase
00024 
00025 from cStringIO import StringIO
00026 from zExceptions import BadRequest
00027 from Acquisition import aq_base, aq_inner, aq_parent
00028 from Products.CMFCore.utils import getToolByName
00029 from Products.PlonePAS.tools.memberdata import MemberData
00030 from Products.PlonePAS.plugins.ufactory import PloneUser
00031 
00032 class MembershipToolTest(PlonePASTestCase):
00033 
00034     def afterSetUp(self):
00035         self.mt = mt = getToolByName(self.portal, 'portal_membership')
00036         self.md = md = getToolByName(self.portal, 'portal_memberdata')
00037 
00038         self.member_id = 'member1'
00039         # Create a new Member
00040         mt.addMember(self.member_id, 'pw', ['Member'], [],
00041                      {'email': 'member1@host.com',
00042                       'title': 'Member #1'})
00043 
00044     def test_get_member(self):
00045         member = self.portal.acl_users.getUserById(self.member_id)
00046         self.failIf(member is None)
00047 
00048         # Should be wrapped into the PAS.
00049         got = aq_base(aq_parent(member))
00050         expected = aq_base(self.portal.acl_users)
00051         self.assertEquals(got, expected)
00052 
00053         self.failUnless(isinstance(member, PloneUser))
00054 
00055     def test_get_member_by_id(self):
00056         # Use tool way of getting member by id. This returns a
00057         # MemberData object wrapped by the member
00058         member = self.mt.getMemberById(self.member_id)
00059         self.failIf(member is None)
00060         self.failUnless(isinstance(member, MemberData))
00061         self.failUnless(isinstance(aq_parent(member), PloneUser))
00062 
00063     def test_id_clean(self):
00064         from Products.PlonePAS.utils import cleanId, decleanId
00065         a = [
00066              "asdfasdf",
00067              "asdf-asdf",
00068              "asdf--asdf",
00069              "asdf---asdf",
00070              "asdf----asdf",
00071              "asdf-----asdf",
00072              "asdf%asdf",
00073              "asdf%%asdf",
00074              "asdf%%%asdf",
00075              "asdf%%%%asdf",
00076              "asdf%%%%%asdf",
00077              "asdf-%asdf",
00078              "asdf%-asdf",
00079              "asdf-%-asdf",
00080              "asdf%-%asdf",
00081              "asdf--%asdf",
00082              "asdf%--asdf",
00083              "asdf--%-asdf",
00084              "asdf-%--asdf",
00085              "asdf--%--asdf",
00086              "asdf%-%asdf",
00087              "asdf%--%asdf",
00088              "asdf%---%asdf",
00089              "-asdf",
00090              "--asdf",
00091              "---asdf",
00092              "----asdf",
00093              "-----asdf",
00094              "asdf-",
00095              "asdf--",
00096              "asdf---",
00097              "asdf----",
00098              "asdf-----",
00099              "%asdf",
00100              "%%asdf",
00101              "%%%asdf",
00102              "%%%%asdf",
00103              "%%%%%asdf",
00104              "asdf%",
00105              "asdf%%",
00106              "asdf%%%",
00107              "asdf%%%%",
00108              "asdf%%%%%",
00109              "asdf\x00asdf",
00110         ]
00111         b = [cleanId(id) for id in a]
00112         c = [decleanId(id) for id in b]
00113         ac = zip(a,c)
00114         for aa, cc in ac:
00115             self.failUnless(aa==cc)
00116 
00117 class MemberAreaTest(PlonePASTestCase):
00118 
00119     def afterSetUp(self):
00120         self.mt = mt = getToolByName(self.portal, 'portal_membership')
00121         self.md = md = getToolByName(self.portal, 'portal_memberdata')
00122         # Enable member-area creation
00123         self.mt.memberareaCreationFlag = 1
00124         # Those are all valid chars in Zope.
00125         self.mid = "Member #1 - Houston, TX. ($100)"
00126         self.loginAsPortalOwner()
00127 
00128     def test_funky_member_ids_1(self):
00129         mid = self.mid
00130         minfo = (mid, 'pw', ['Member'], [])
00131 
00132         # Create a new User
00133         self.portal.acl_users._doAddUser(*minfo)
00134        self.mt.createMemberArea,(mid)
00135 
00136     def test_funky_member_ids_2(self):
00137         # Forward-slash is not allowed
00138         mid = self.mid + '/'
00139         minfo = (mid, 'pw', ['Member'], [])
00140 
00141         # Create a new User
00142         self.portal.acl_users._doAddUser(*minfo)
00143         self.mt.createMemberArea(mid)
00144 
00145     def test_memberareaCreationFlag_respected(self):
00146         self.portal.acl_users._doAddUser('foo', 'pw', ['Member'], [])
00147         self.portal.acl_users._doAddUser('bar', 'pw', ['Member'], [])
00148 
00149         self.failIf('foo' in self.portal.Members.objectIds())
00150         self.failIf('bar' in self.portal.Members.objectIds())
00151 
00152         self.mt.createMemberarea('foo')
00153         self.failUnless('foo' in self.portal.Members.objectIds())
00154 
00155         self.mt.memberareaCreationFlag = 0
00156         self.mt.createMemberArea('bar')
00157         self.failIf('bar' in self.portal.Members.objectIds())
00158 
00159 def test_suite():
00160     suite = unittest.TestSuite()
00161     suite.addTest(unittest.makeSuite(MembershipToolTest))
00162     suite.addTest(unittest.makeSuite(MemberAreaTest))
00163     return suite
00164