source: main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_authentication.py @ 6670

Last change on this file since 6670 was 6668, checked in by uli, 13 years ago

Add first tests for authentication, only the basic components
yet. Main part (authenticatorPlugin) yet to come.

File size: 3.9 KB
Line 
1##
2## test_authentication.py
3## Login : <uli@pu.smp.net>
4## Started on  Fri Sep  2 15:25:56 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22import unittest
23from zope.component import provideUtility, queryUtility, getGlobalSiteManager
24from zope.interface.verify import verifyClass, verifyObject
25from zope.password.password import SSHAPasswordManager
26from zope.password.interfaces import IPasswordManager
27from zope.pluggableauth import PluggableAuthentication
28from waeup.sirp.interfaces import IAuthPluginUtility, IUserAccount
29from waeup.sirp.students.authentication import (
30    StudentsAuthenticatorSetup, StudentAccount)
31
32
33class StudentsAuthenticatorSetupTests(unittest.TestCase):
34
35    def test_iface(self):
36        obj = StudentsAuthenticatorSetup()
37        verifyClass(IAuthPluginUtility, StudentsAuthenticatorSetup)
38        verifyObject(IAuthPluginUtility, obj)
39        return
40
41    def test_register(self):
42        # Make sure registration works.
43        setup = StudentsAuthenticatorSetup()
44        pau = PluggableAuthentication()
45        setup.register(pau)
46        self.assertTrue('students' in pau.authenticatorPlugins)
47        return
48
49    def test_unregister(self):
50        # Make sure deregistration works.
51        setup = StudentsAuthenticatorSetup()
52        pau = PluggableAuthentication()
53        pau.authenticatorPlugins = ('students')
54        setup.unregister(pau)
55        self.assertTrue('students' not in pau.authenticatorPlugins)
56        return
57
58
59class FakeStudent(object):
60    student_id = 'test_stud'
61    name = 'Test User'
62    password = None
63
64class StudentAccountTests(unittest.TestCase):
65
66    def setUp(self):
67        self.fake_stud = FakeStudent()
68        self.account = StudentAccount(self.fake_stud)
69        # We have to setup a password manager utility manually as we
70        # have no functional test. In functional tests this would
71        # happen automatically, but it would take a lot more time to
72        # run the tests.
73        provideUtility(
74            SSHAPasswordManager(), IPasswordManager, 'SSHA')
75        return
76
77    def tearDown(self):
78        # Clear up the SSHA utility
79        ssha_manager = queryUtility(
80            IPasswordManager, name='SSHA', default=None)
81        if ssha_manager is not None:
82            gsm = getGlobalSiteManager()
83            gsm.unregisterUtility(ssha_manager)
84        return
85
86    def test_iface(self):
87        verifyClass(IUserAccount, StudentAccount)
88        verifyObject(IUserAccount, self.account)
89        return
90
91    def test_set_password(self):
92        # make sure we can set a password.
93        self.account.setPassword('secret')
94        self.assertTrue(self.fake_stud.password is not None)
95        # we do not store plaintext passwords
96        self.assertTrue(self.fake_stud.password != 'secret')
97        return
98
99    def test_check_password(self):
100        # make sure we can check a password.
101        self.account.setPassword('secret')
102        result1 = self.account.checkPassword(None)
103        result2 = self.account.checkPassword('nonsense')
104        result3 = self.account.checkPassword('secret')
105        self.assertEqual(result1, False)
106        self.assertEqual(result2, False)
107        self.assertEqual(result3, True)
108        return
Note: See TracBrowser for help on using the repository browser.