[7193] | 1 | ## $Id: test_authentication.py 8983 2012-07-12 11:43:12Z henrik $ |
---|
[5461] | 2 | ## |
---|
[7193] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5461] | 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
[7193] | 8 | ## |
---|
[5461] | 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
[7193] | 13 | ## |
---|
[5461] | 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | import unittest |
---|
[5897] | 19 | from zope.authentication.interfaces import IAuthentication |
---|
[7240] | 20 | from zope.component import provideUtility, queryUtility, getGlobalSiteManager |
---|
| 21 | from zope.interface.verify import verifyClass, verifyObject |
---|
| 22 | from zope.password.password import SSHAPasswordManager |
---|
| 23 | from zope.password.interfaces import IPasswordManager |
---|
| 24 | from zope.pluggableauth import PluggableAuthentication |
---|
| 25 | from zope.securitypolicy.role import Role |
---|
| 26 | from zope.securitypolicy.interfaces import IRole, Allow |
---|
[7811] | 27 | from waeup.kofa.authentication import get_principal_role_manager |
---|
| 28 | from waeup.kofa.interfaces import IAuthPluginUtility, IUserAccount |
---|
| 29 | from waeup.kofa.applicants.authentication import ( |
---|
[7240] | 30 | ApplicantsAuthenticatorSetup, ApplicantAccount) |
---|
[7811] | 31 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
| 32 | from waeup.kofa.testing import FunctionalLayer |
---|
[5461] | 33 | |
---|
[7240] | 34 | class ApplicantsAuthenticatorSetupTests(unittest.TestCase): |
---|
[5897] | 35 | |
---|
[7240] | 36 | def test_iface(self): |
---|
| 37 | obj = ApplicantsAuthenticatorSetup() |
---|
| 38 | verifyClass(IAuthPluginUtility, ApplicantsAuthenticatorSetup) |
---|
| 39 | verifyObject(IAuthPluginUtility, obj) |
---|
[6123] | 40 | return |
---|
| 41 | |
---|
[7240] | 42 | def test_register(self): |
---|
| 43 | # Make sure registration works. |
---|
| 44 | setup = ApplicantsAuthenticatorSetup() |
---|
| 45 | pau = PluggableAuthentication() |
---|
| 46 | setup.register(pau) |
---|
| 47 | self.assertTrue('applicants' in pau.authenticatorPlugins) |
---|
[5461] | 48 | return |
---|
| 49 | |
---|
[7240] | 50 | def test_unregister(self): |
---|
| 51 | # Make sure deregistration works. |
---|
| 52 | setup = ApplicantsAuthenticatorSetup() |
---|
| 53 | pau = PluggableAuthentication() |
---|
| 54 | pau.authenticatorPlugins = ('applicants') |
---|
| 55 | setup.unregister(pau) |
---|
| 56 | self.assertTrue('applicants' not in pau.authenticatorPlugins) |
---|
[5461] | 57 | return |
---|
| 58 | |
---|
| 59 | |
---|
[7240] | 60 | class FakeApplicant(object): |
---|
| 61 | applicant_id = 'test_appl' |
---|
[7364] | 62 | display_fullname = 'Tilman Gause' |
---|
[7240] | 63 | password = None |
---|
| 64 | email = None |
---|
| 65 | phone = None |
---|
[8983] | 66 | suspended = False |
---|
[5461] | 67 | |
---|
| 68 | |
---|
[7240] | 69 | class MinimalPAU(PluggableAuthentication): |
---|
| 70 | def getPrincipal(self, id): |
---|
| 71 | return 'faked principal' |
---|
[5461] | 72 | |
---|
[7240] | 73 | class ApplicantAccountTests(unittest.TestCase): |
---|
[5908] | 74 | |
---|
[7240] | 75 | def setUp(self): |
---|
| 76 | self.fake_stud = FakeApplicant() |
---|
| 77 | self.account = ApplicantAccount(self.fake_stud) |
---|
[5908] | 78 | |
---|
[7240] | 79 | # We provide a minimal PAU |
---|
| 80 | pau = MinimalPAU() |
---|
| 81 | provideUtility(pau, IAuthentication) |
---|
[6452] | 82 | |
---|
[7240] | 83 | # We register a role |
---|
| 84 | test_role = Role('waeup.test.Role', 'Testing Role') |
---|
| 85 | provideUtility(test_role, IRole, name='waeup.test.Role') |
---|
[5461] | 86 | |
---|
[7240] | 87 | # We have to setup a password manager utility manually as we |
---|
| 88 | # have no functional test. In functional tests this would |
---|
| 89 | # happen automatically, but it would take a lot more time to |
---|
| 90 | # run the tests. |
---|
| 91 | provideUtility( |
---|
| 92 | SSHAPasswordManager(), IPasswordManager, 'SSHA') |
---|
[5461] | 93 | return |
---|
| 94 | |
---|
[5463] | 95 | def tearDown(self): |
---|
[7240] | 96 | self.account.roles = [] # make sure roles are reset |
---|
| 97 | gsm = getGlobalSiteManager() |
---|
| 98 | to_clean = [] |
---|
| 99 | # Clear up utilities registered in setUp |
---|
| 100 | to_clean.append( |
---|
| 101 | (IPasswordManager, queryUtility( |
---|
| 102 | IPasswordManager, name='SSHA', default=None))) |
---|
| 103 | to_clean.append( |
---|
| 104 | (IAuthentication, queryUtility(IAuthentication, default=None))) |
---|
| 105 | to_clean.append( |
---|
| 106 | (IRole, queryUtility(IRole, name='test.Role', default=None))) |
---|
| 107 | for iface, elem in to_clean: |
---|
| 108 | if elem is not None: |
---|
| 109 | gsm.unregisterUtility(elem, iface) |
---|
[5463] | 110 | return |
---|
| 111 | |
---|
[7240] | 112 | def test_iface(self): |
---|
| 113 | verifyClass(IUserAccount, ApplicantAccount) |
---|
| 114 | verifyObject(IUserAccount, self.account) |
---|
[5463] | 115 | return |
---|
| 116 | |
---|
[7240] | 117 | def test_set_password(self): |
---|
| 118 | # make sure we can set a password. |
---|
| 119 | self.account.setPassword('secret') |
---|
| 120 | self.assertTrue(self.fake_stud.password is not None) |
---|
| 121 | # we do not store plaintext passwords |
---|
| 122 | self.assertTrue(self.fake_stud.password != 'secret') |
---|
[8351] | 123 | # passwords are stored as bytestream |
---|
| 124 | self.assertTrue(isinstance(self.fake_stud.password, basestring)) |
---|
| 125 | self.assertFalse(isinstance(self.fake_stud.password, unicode)) |
---|
[5463] | 126 | return |
---|
| 127 | |
---|
[7240] | 128 | def test_check_password(self): |
---|
| 129 | # make sure we can check a password. |
---|
| 130 | self.account.setPassword('secret') |
---|
| 131 | result1 = self.account.checkPassword(None) |
---|
| 132 | result2 = self.account.checkPassword('nonsense') |
---|
| 133 | result3 = self.account.checkPassword('secret') |
---|
| 134 | self.assertEqual(result1, False) |
---|
| 135 | self.assertEqual(result2, False) |
---|
| 136 | self.assertEqual(result3, True) |
---|
[5463] | 137 | return |
---|
| 138 | |
---|
[7240] | 139 | def test_check_unset_password(self): |
---|
| 140 | # empty and unset passwords do not match anything |
---|
| 141 | self.fake_stud.password = None |
---|
| 142 | result1 = self.account.checkPassword('') |
---|
| 143 | self.fake_stud.password = '' |
---|
| 144 | result2 = self.account.checkPassword('') |
---|
| 145 | self.assertEqual(result1, False) |
---|
| 146 | self.assertEqual(result2, False) |
---|
[5463] | 147 | return |
---|
| 148 | |
---|
[7240] | 149 | def test_check_password_no_string(self): |
---|
| 150 | # if passed in password is not a string, we gain no access |
---|
| 151 | self.fake_stud.password = 'secret' |
---|
| 152 | result1 = self.account.checkPassword(None) |
---|
| 153 | result2 = self.account.checkPassword(object()) |
---|
| 154 | self.assertEqual(result1, False) |
---|
| 155 | self.assertEqual(result2, False) |
---|
[5463] | 156 | return |
---|
| 157 | |
---|
[7240] | 158 | def test_role_set(self): |
---|
| 159 | # make sure we can set roles for principals denoted by account |
---|
| 160 | prm = get_principal_role_manager() |
---|
| 161 | self.assertEqual(prm.getPrincipalsAndRoles(), []) |
---|
| 162 | self.account.roles = ['waeup.test.Role'] |
---|
| 163 | self.assertEqual( |
---|
| 164 | prm.getPrincipalsAndRoles(), |
---|
| 165 | [('waeup.test.Role', 'test_appl', Allow)]) |
---|
[5463] | 166 | return |
---|
| 167 | |
---|
[7240] | 168 | def test_role_get(self): |
---|
| 169 | # make sure we can get roles set for an account |
---|
| 170 | self.assertEqual(self.account.roles, []) |
---|
| 171 | self.account.roles = ['waeup.test.Role',] # set a role |
---|
| 172 | self.assertEqual(self.account.roles, ['waeup.test.Role']) |
---|
[5463] | 173 | return |
---|
| 174 | |
---|
[7240] | 175 | class FunctionalApplicantAuthTests(ApplicantsFullSetup): |
---|
[5463] | 176 | |
---|
[7240] | 177 | layer = FunctionalLayer |
---|
[5463] | 178 | |
---|
| 179 | def setUp(self): |
---|
[7240] | 180 | super(FunctionalApplicantAuthTests, self).setUp() |
---|
[5463] | 181 | return |
---|
| 182 | |
---|
| 183 | def tearDown(self): |
---|
[7240] | 184 | super(FunctionalApplicantAuthTests, self).tearDown() |
---|
[5463] | 185 | return |
---|