source: main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py @ 8857

Last change on this file since 8857 was 8857, checked in by Henrik Bettermann, 12 years ago

Let's use the PasswordMandate? also for change password requests (part1).

  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1## $Id: tests.py 8857 2012-06-30 07:03:20Z henrik $
2##
3## Copyright (C) 2012 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##
18"""
19Tests for mandates.
20"""
21import tempfile
22import shutil
23from zope.testbrowser.testing import Browser
24from datetime import datetime, timedelta
25from zope.interface.verify import verifyClass, verifyObject
26from zope.component import createObject
27from zope.component.hooks import setSite, clearSite
28from waeup.kofa.app import University
29from waeup.kofa.interfaces import IUserAccount
30from waeup.kofa.mandates.interfaces import (
31    IMandatesContainer, IMandate)
32from waeup.kofa.mandates.container import MandatesContainer
33from waeup.kofa.mandates.mandate import PasswordMandate
34from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
35
36class MandatesContainerTestCase(FunctionalTestCase):
37
38    layer = FunctionalLayer
39
40    def test_interfaces(self):
41        # Make sure the correct interfaces are implemented.
42        self.assertTrue(
43            verifyClass(
44                IMandatesContainer, MandatesContainer)
45            )
46        self.assertTrue(
47            verifyObject(
48                IMandatesContainer, MandatesContainer())
49            )
50        self.assertTrue(
51            verifyClass(
52                IMandate, PasswordMandate)
53            )
54        self.assertTrue(
55            verifyObject(
56                IMandate, PasswordMandate())
57            )
58        return
59
60    def setUp(self):
61        super(MandatesContainerTestCase, self).setUp()
62
63        # Setup a sample site for each test
64        app = University()
65        self.dc_root = tempfile.mkdtemp()
66        app['datacenter'].setStoragePath(self.dc_root)
67
68        # Prepopulate the ZODB...
69        self.getRootFolder()['app'] = app
70        # we add the site immediately after creation to the
71        # ZODB. Catalogs and other local utilities are not setup
72        # before that step.
73        self.app = self.getRootFolder()['app']
74        # Set site here. Some of the following setup code might need
75        # to access grok.getSite() and should get our new app then
76        setSite(app)
77
78        self.browser = Browser()
79        self.browser.handleErrors = False
80
81    def tearDown(self):
82        super(MandatesContainerTestCase, self).tearDown()
83        clearSite()
84        shutil.rmtree(self.dc_root)
85
86    def test_set_student_password(self):
87        student = createObject('waeup.Student')
88        self.app['students'].addStudent(student)
89        # Add and execute a mandate with missing parameters.
90        mandate = PasswordMandate()
91        self.app['mandates'].addMandate(mandate)
92        msg = mandate.execute()
93        self.assertEqual(msg, u'Wrong mandate parameters.')
94        # Add and execute an expired mandate.
95        mandate = PasswordMandate(days=0)
96        self.app['mandates'].addMandate(mandate)
97        msg = mandate.execute()
98        self.assertEqual(msg, u'Mandate expired.')
99        # Add and execute a perfect mandate
100        mandate = PasswordMandate()
101        mandate.params['user_id'] = student.student_id
102        mandate.params['password'] = 'mypwd1'
103        mandate.params['user_type'] = 'student'
104        self.app['mandates'].addMandate(mandate)
105        msg = mandate.execute()
106        # Password has been set.
107        self.assertEqual(msg, 'Password has been successfully set. Proceed to '
108            'the login page and enter your credentials.')
109        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
110        # All mandates have been removed.
111        self.assertEqual(len(self.app['mandates'].keys()), 0)
112
113    def test_set_officer_password(self):
114        self.app['users'].addUser('bob', 'bobssecret')
115        officer = self.app['users']['bob']
116        mandate = PasswordMandate()
117        mandate.params['user_id'] = 'bob'
118        mandate.params['password'] = 'mypwd1'
119        mandate.params['user_type'] = 'officer'
120        self.app['mandates'].addMandate(mandate)
121        msg = mandate.execute()
122        # Password has been set.
123        self.assertEqual(msg, 'Password has been successfully set. Proceed to '
124            'the login page and enter your credentials.')
125        self.assertTrue(IUserAccount(officer).checkPassword('mypwd1'))
126
127    def test_remove_expired(self):
128        # mandate1 is an old mandate which just expired.
129        mandate1 = PasswordMandate(days=0)
130        self.app['mandates'].addMandate(mandate1)
131        # mandate2 is a new mandate with default time delta.
132        mandate2 = PasswordMandate(mandate_id='23456')
133        self.app['mandates'].addMandate(mandate2)
134        self.assertEqual(len(self.app['mandates'].keys()), 2)
135        self.app['mandates'].removeExpired()
136        # Only the new mandate remains in the container.
137        self.assertEqual(len(self.app['mandates'].keys()), 1)
138        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
139
140    def test_browser(self):
141        student = createObject('waeup.Student')
142        self.app['students'].addStudent(student)
143        mandate = PasswordMandate()
144        mandate.params['user_id'] = student.student_id
145        mandate.params['user_type'] = 'student'
146        mandate.params['password'] = 'mypwd1'
147        self.app['mandates'].addMandate(mandate)
148        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
149            % mandate.mandate_id)
150        # Password has been set.
151        self.assertTrue('Password has been successfully set. Proceed to '
152                        'the login page and enter your credentials.'
153            in self.browser.contents)
154        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
155        # All mandates have been removed.
156        self.assertEqual(len(self.app['mandates'].keys()), 0)
Note: See TracBrowser for help on using the repository browser.