source: main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/applicants/tests/test_authentication.py @ 9169

Last change on this file since 9169 was 8351, checked in by uli, 13 years ago

Update tests.

  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1## $Id: test_authentication.py 8351 2012-05-04 22:52:53Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
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.
8##
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.
13##
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##
18import unittest
19from zope.authentication.interfaces import IAuthentication
20from zope.component import provideUtility, queryUtility, getGlobalSiteManager
21from zope.interface.verify import verifyClass, verifyObject
22from zope.password.password import SSHAPasswordManager
23from zope.password.interfaces import IPasswordManager
24from zope.pluggableauth import PluggableAuthentication
25from zope.securitypolicy.role import Role
26from zope.securitypolicy.interfaces import IRole, Allow
27from waeup.kofa.authentication import get_principal_role_manager
28from waeup.kofa.interfaces import IAuthPluginUtility, IUserAccount
29from waeup.kofa.applicants.authentication import (
30    ApplicantsAuthenticatorSetup, ApplicantAccount)
31from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
32from waeup.kofa.testing import FunctionalLayer
33
34class ApplicantsAuthenticatorSetupTests(unittest.TestCase):
35
36    def test_iface(self):
37        obj = ApplicantsAuthenticatorSetup()
38        verifyClass(IAuthPluginUtility, ApplicantsAuthenticatorSetup)
39        verifyObject(IAuthPluginUtility, obj)
40        return
41
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)
48        return
49
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)
57        return
58
59
60class FakeApplicant(object):
61    applicant_id = 'test_appl'
62    display_fullname = 'Tilman Gause'
63    password = None
64    email = None
65    phone = None
66
67
68class MinimalPAU(PluggableAuthentication):
69    def getPrincipal(self, id):
70        return 'faked principal'
71
72class ApplicantAccountTests(unittest.TestCase):
73
74    def setUp(self):
75        self.fake_stud = FakeApplicant()
76        self.account = ApplicantAccount(self.fake_stud)
77
78        # We provide a minimal PAU
79        pau = MinimalPAU()
80        provideUtility(pau, IAuthentication)
81
82        # We register a role
83        test_role = Role('waeup.test.Role', 'Testing Role')
84        provideUtility(test_role, IRole, name='waeup.test.Role')
85
86        # We have to setup a password manager utility manually as we
87        # have no functional test. In functional tests this would
88        # happen automatically, but it would take a lot more time to
89        # run the tests.
90        provideUtility(
91            SSHAPasswordManager(), IPasswordManager, 'SSHA')
92        return
93
94    def tearDown(self):
95        self.account.roles = [] # make sure roles are reset
96        gsm = getGlobalSiteManager()
97        to_clean = []
98        # Clear up utilities registered in setUp
99        to_clean.append(
100            (IPasswordManager, queryUtility(
101                    IPasswordManager, name='SSHA', default=None)))
102        to_clean.append(
103            (IAuthentication, queryUtility(IAuthentication, default=None)))
104        to_clean.append(
105            (IRole, queryUtility(IRole, name='test.Role', default=None)))
106        for iface, elem in to_clean:
107            if elem is not None:
108                gsm.unregisterUtility(elem, iface)
109        return
110
111    def test_iface(self):
112        verifyClass(IUserAccount, ApplicantAccount)
113        verifyObject(IUserAccount, self.account)
114        return
115
116    def test_set_password(self):
117        # make sure we can set a password.
118        self.account.setPassword('secret')
119        self.assertTrue(self.fake_stud.password is not None)
120        # we do not store plaintext passwords
121        self.assertTrue(self.fake_stud.password != 'secret')
122        # passwords are stored as bytestream
123        self.assertTrue(isinstance(self.fake_stud.password, basestring))
124        self.assertFalse(isinstance(self.fake_stud.password, unicode))
125        return
126
127    def test_check_password(self):
128        # make sure we can check a password.
129        self.account.setPassword('secret')
130        result1 = self.account.checkPassword(None)
131        result2 = self.account.checkPassword('nonsense')
132        result3 = self.account.checkPassword('secret')
133        self.assertEqual(result1, False)
134        self.assertEqual(result2, False)
135        self.assertEqual(result3, True)
136        return
137
138    def test_check_unset_password(self):
139        # empty and unset passwords do not match anything
140        self.fake_stud.password = None
141        result1 = self.account.checkPassword('')
142        self.fake_stud.password = ''
143        result2 = self.account.checkPassword('')
144        self.assertEqual(result1, False)
145        self.assertEqual(result2, False)
146        return
147
148    def test_check_password_no_string(self):
149        # if passed in password is not a string, we gain no access
150        self.fake_stud.password = 'secret'
151        result1 = self.account.checkPassword(None)
152        result2 = self.account.checkPassword(object())
153        self.assertEqual(result1, False)
154        self.assertEqual(result2, False)
155        return
156
157    def test_role_set(self):
158        # make sure we can set roles for principals denoted by account
159        prm = get_principal_role_manager()
160        self.assertEqual(prm.getPrincipalsAndRoles(), [])
161        self.account.roles = ['waeup.test.Role']
162        self.assertEqual(
163            prm.getPrincipalsAndRoles(),
164            [('waeup.test.Role', 'test_appl', Allow)])
165        return
166
167    def test_role_get(self):
168        # make sure we can get roles set for an account
169        self.assertEqual(self.account.roles, [])
170        self.account.roles = ['waeup.test.Role',] # set a role
171        self.assertEqual(self.account.roles, ['waeup.test.Role'])
172        return
173
174class FunctionalApplicantAuthTests(ApplicantsFullSetup):
175
176    layer = FunctionalLayer
177
178    def setUp(self):
179        super(FunctionalApplicantAuthTests, self).setUp()
180        return
181
182    def tearDown(self):
183        super(FunctionalApplicantAuthTests, self).tearDown()
184        return
Note: See TracBrowser for help on using the repository browser.