Back to index

plone3  3.1.7
testRegistrationTool.py
Go to the documentation of this file.
00001 #
00002 # Tests the registration tool
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 
00007 from AccessControl import Unauthorized
00008 from Products.CMFCore.permissions import AddPortalMember
00009 
00010 member_id = 'new_member'
00011 
00012 
00013 class TestRegistrationTool(PloneTestCase.PloneTestCase):
00014 
00015     def afterSetUp(self):
00016         self.registration = self.portal.portal_registration
00017         self.portal.acl_users.userFolderAddUser("userid", "password",
00018                 (), (), ())
00019         self.portal.acl_users._doAddGroup("groupid", ())
00020 
00021     def testJoinCreatesUser(self):
00022         self.registration.addMember(member_id, 'secret',
00023                           properties={'username': member_id, 'email': 'foo@bar.com'})
00024         user = self.portal.acl_users.getUserById(member_id)
00025         self.failUnless(user, 'addMember failed to create user')
00026 
00027     def testJoinWithUppercaseEmailCreatesUser(self):
00028         self.registration.addMember(member_id, 'secret',
00029                           properties={'username': member_id, 'email': 'FOO@BAR.COM'})
00030         user = self.portal.acl_users.getUserById(member_id)
00031         self.failUnless(user, 'addMember failed to create user')
00032 
00033     def testJoinWithoutEmailRaisesValueError(self):
00034         self.assertRaises(ValueError,
00035                           self.registration.addMember,
00036                           member_id, 'secret',
00037                           properties={'username': member_id, 'email': ''})
00038 
00039     def testJoinWithBadEmailRaisesValueError(self):
00040         self.assertRaises(ValueError,
00041                           self.registration.addMember,
00042                           member_id, 'secret',
00043                           properties={'username': member_id, 'email': 'foo@bar.com, fred@bedrock.com'})
00044 
00045     def testJoinAsExistingMemberRaisesValueError(self):
00046         self.assertRaises(ValueError,
00047                           self.registration.addMember,
00048                           PloneTestCase.default_user, 'secret',
00049                           properties={'username': 'Dr FooBar', 'email': 'foo@bar.com'})
00050 
00051     def testJoinAsExistingNonMemberUserRaisesValueError(self):
00052         # http://dev.plone.org/plone/ticket/3221
00053         self.portal.acl_users._doAddUser(member_id, 'secret', [], [])
00054         self.assertRaises(ValueError,
00055                           self.registration.addMember,
00056                           member_id, 'secret',
00057                           properties={'username': member_id, 'email': 'foo@bar.com'})
00058 
00059     def testJoinWithPortalIdAsUsernameRaisesValueError(self):
00060         self.assertRaises(ValueError,
00061                           self.registration.addMember,
00062                           self.portal.getId(), 'secret',
00063                           properties={'username': 'Dr FooBar', 'email': 'foo@bar.com'})
00064 
00065     def testJoinWithoutPermissionRaisesUnauthorized(self):
00066         # http://dev.plone.org/plone/ticket/3000
00067         self.portal.manage_permission(AddPortalMember, ['Manager'], acquire=0)
00068         self.assertRaises(Unauthorized,
00069                           self.registration.restrictedTraverse, 'addMember')
00070 
00071     def testJoinWithoutPermissionRaisesUnauthorizedFormScript(self):
00072         # http://dev.plone.org/plone/ticket/3000
00073         self.portal.manage_permission(AddPortalMember, ['Manager'], acquire=0)
00074         self.app.REQUEST['username'] = member_id
00075         # TODO: register has a proxy role but we trip over
00076         # validate_registration... (2.0.5)
00077         self.assertRaises(Unauthorized, self.portal.register)
00078 
00079     def testNewIdAllowed(self):
00080         self.assertEqual(self.registration.isMemberIdAllowed('newuser'), 1)
00081 
00082 
00083     def testTakenUserId(self):
00084         self.assertEqual(self.registration.isMemberIdAllowed('userid'), 0)
00085 
00086 
00087     def testTakenGroupd(self):
00088         self.assertEqual(self.registration.isMemberIdAllowed('groupid'), 0)
00089 
00090     def testIsMemberIdAllowedIfSubstringOfExisting(self):
00091         # http://dev.plone.org/plone/ticket/6396
00092         self.failUnless(self.registration.isMemberIdAllowed('useri'))
00093         
00094 
00095 class TestPasswordGeneration(PloneTestCase.PloneTestCase):
00096 
00097     def afterSetUp(self):
00098         self.registration = self.portal.portal_registration
00099 
00100     def testMD5BaseAttribute(self):
00101         # Verify that if the _v_md5base attribute is missing, things
00102         # fall back to the class attribute and its default value.
00103         self.registration._md5base()
00104         self.failIfEqual(self.registration._v_md5base, None)
00105         delattr(self.registration, '_v_md5base')
00106         self.assertEqual(self.registration._v_md5base, None)
00107 
00108     def testGetRandomPassword(self):
00109         pw = self.registration.getPassword(6)
00110         self.assertEqual(len(pw), 6)
00111 
00112     def testGetDeterministicPassword(self):
00113         salt = 'foo'
00114         pw = self.registration.getPassword(6, salt)
00115         self.assertEqual(len(pw), 6)
00116         # Passing in the same length and salt should give the same
00117         # result, every time.
00118         self.assertEqual(pw, self.registration.getPassword(6, salt))
00119         self.assertEqual(pw, self.registration.getPassword(6, salt))
00120         # These should fail
00121         self.failIfEqual(pw, self.registration.getPassword(7, salt))
00122         self.failIfEqual(pw, self.registration.getPassword(6, salt+'x'))
00123 
00124     def testGeneratePassword(self):
00125         pw = self.registration.generatePassword()
00126         self.assertEqual(len(pw), 6)
00127 
00128     def testGenerateResetCode(self):
00129         salt = 'foo'
00130         rc = self.registration.generateResetCode(salt)
00131         self.assertEqual(rc, self.registration.generateResetCode(salt))
00132 
00133 
00134 def test_suite():
00135     from unittest import TestSuite, makeSuite
00136     suite = TestSuite()
00137     suite.addTest(makeSuite(TestRegistrationTool))
00138     suite.addTest(makeSuite(TestPasswordGeneration))
00139     return suite