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

Last change on this file since 16158 was 15609, checked in by Henrik Bettermann, 5 years ago

Finalize parents access.

  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1## $Id: tests.py 15609 2019-09-26 11:38:36Z 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
23import os
24from zope.testbrowser.testing import Browser
25from datetime import datetime, timedelta
26from zope.interface.verify import verifyClass, verifyObject
27from zope.component import createObject
28from zope.component.hooks import setSite, clearSite
29from waeup.kofa.app import University
30from waeup.kofa.interfaces import IUserAccount
31from waeup.kofa.mandates.interfaces import (
32    IMandatesContainer, IMandate)
33from waeup.kofa.mandates.container import MandatesContainer
34from waeup.kofa.mandates.mandate import (
35    PasswordMandate, RefereeReportMandate, ParentsPasswordMandate)
36from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
37from waeup.kofa.tests.test_authentication import SECRET
38
39class MandatesContainerTestCase(FunctionalTestCase):
40
41    layer = FunctionalLayer
42
43    def test_interfaces(self):
44        # Make sure the correct interfaces are implemented.
45        self.assertTrue(
46            verifyClass(
47                IMandatesContainer, MandatesContainer)
48            )
49        self.assertTrue(
50            verifyObject(
51                IMandatesContainer, MandatesContainer())
52            )
53        self.assertTrue(
54            verifyClass(
55                IMandate, PasswordMandate)
56            )
57        self.assertTrue(
58            verifyObject(
59                IMandate, PasswordMandate())
60            )
61        self.assertTrue(
62            verifyClass(
63                IMandate, RefereeReportMandate)
64            )
65        self.assertTrue(
66            verifyObject(
67                IMandate, RefereeReportMandate())
68            )
69        self.assertTrue(
70            verifyClass(
71                IMandate, ParentsPasswordMandate)
72            )
73        self.assertTrue(
74            verifyObject(
75                IMandate, ParentsPasswordMandate())
76            )
77        return
78
79    def setUp(self):
80        super(MandatesContainerTestCase, self).setUp()
81
82        # Setup a sample site for each test
83        app = University()
84        self.dc_root = tempfile.mkdtemp()
85        app['datacenter'].setStoragePath(self.dc_root)
86
87        # Prepopulate the ZODB...
88        self.getRootFolder()['app'] = app
89        # we add the site immediately after creation to the
90        # ZODB. Catalogs and other local utilities are not setup
91        # before that step.
92        self.app = self.getRootFolder()['app']
93        # Set site here. Some of the following setup code might need
94        # to access grok.getSite() and should get our new app then
95        setSite(app)
96
97        self.browser = Browser()
98        self.browser.handleErrors = False
99
100    def tearDown(self):
101        super(MandatesContainerTestCase, self).tearDown()
102        clearSite()
103        shutil.rmtree(self.dc_root)
104
105    def test_set_student_password(self):
106        student = createObject('waeup.Student')
107        # Add and execute a mandate with missing parameters.
108        mandate = PasswordMandate()
109        IUserAccount(student).setPassword('old_pw')
110        self.app['mandates'].addMandate(mandate)
111        (msg, redirect_path) = mandate.execute()
112        self.assertEqual(redirect_path, '')
113        self.assertEqual(msg, u'Wrong mandate parameters.')
114        # Add and execute an expired mandate.
115        mandate = PasswordMandate(days=0)
116        mandate.params['user'] = student
117        mandate.params['password'] = 'mypwd1'
118        self.app['mandates'].addMandate(mandate)
119        (msg, redirect_path) = mandate.execute()
120        self.assertEqual(msg, u'Mandate expired.')
121        self.assertEqual(redirect_path, '')
122        # Password has not been set
123        self.assertTrue(IUserAccount(student).checkPassword('old_pw'))
124        # Add and execute a perfect mandate
125        mandate = PasswordMandate()
126        mandate.params['user'] = student
127        mandate.params['password'] = 'mypwd1'
128        self.app['mandates'].addMandate(mandate)
129        (msg, redirect_path) = mandate.execute()
130        # Password has been set.
131        self.assertEqual(msg,
132            'Password has been successfully set. Login with your new password.')
133        self.assertEqual(redirect_path, '/login')
134        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
135        # All mandates have been removed.
136        self.assertEqual(len(self.app['mandates'].keys()), 0)
137        logfile = os.path.join(
138            self.app['datacenter'].storage, 'logs', 'main.log')
139        logcontent = open(logfile).read()
140        self.assertTrue('system - PasswordMandate used: K1000000' in logcontent)
141
142    def test_set_officer_password(self):
143        self.app['users'].addUser('bob', SECRET)
144        officer = self.app['users']['bob']
145        mandate = PasswordMandate()
146        mandate.params['user'] = officer
147        mandate.params['password'] = SECRET
148        self.app['mandates'].addMandate(mandate)
149        (msg, redirect_path) = mandate.execute()
150        # Password has been set.
151        self.assertEqual(msg,
152            'Password has been successfully set. Login with your new password.')
153        self.assertEqual(redirect_path, '/login')
154        self.assertTrue(IUserAccount(officer).checkPassword(SECRET))
155        logfile = os.path.join(
156            self.app['datacenter'].storage, 'logs', 'main.log')
157        logcontent = open(logfile).read()
158        self.assertTrue('system - PasswordMandate used: bob' in logcontent)
159
160    def test_set_applicant_password(self):
161        applicant = createObject('waeup.Applicant')
162        applicant.applicant_id = u'abc'
163        mandate = PasswordMandate()
164        mandate.params['user'] = applicant
165        mandate.params['password'] = 'mypwd1'
166        self.app['mandates'].addMandate(mandate)
167        (msg, redirect_path) = mandate.execute()
168        # Password has been set.
169        self.assertEqual(msg,
170            'Password has been successfully set. Login with your new password.')
171        self.assertEqual(redirect_path, '/login')
172        self.assertTrue(IUserAccount(applicant).checkPassword('mypwd1'))
173        logfile = os.path.join(
174            self.app['datacenter'].storage, 'logs', 'main.log')
175        logcontent = open(logfile).read()
176        self.assertTrue('system - PasswordMandate used: abc' in logcontent)
177
178    def test_remove_expired(self):
179        # mandate1 is an old mandate which just expired.
180        mandate1 = PasswordMandate(days=0)
181        self.app['mandates'].addMandate(mandate1)
182        # mandate2 is a new mandate with default time delta.
183        mandate2 = PasswordMandate(mandate_id='23456')
184        self.app['mandates'].addMandate(mandate2)
185        self.assertEqual(len(self.app['mandates'].keys()), 2)
186        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
187        num_deleted = self.app['mandates'].removeExpired()
188        self.assertEqual(num_deleted, 1)
189        # Only the new mandate remains in the container.
190        self.assertEqual(len(self.app['mandates'].keys()), 1)
191        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
192        logfile = os.path.join(
193            self.app['datacenter'].storage, 'logs', 'main.log')
194        logcontent = open(logfile).read()
195        self.assertTrue('system - 1 mandates purged' in logcontent)
196
197    def test_purge_mandates(self):
198        # mandate1 is an old mandate which just expired.
199        mandate1 = PasswordMandate(days=0)
200        self.app['mandates'].addMandate(mandate1)
201        # mandate2 is a new mandate with default time delta.
202        mandate2 = PasswordMandate(mandate_id='23456')
203        self.app['mandates'].addMandate(mandate2)
204        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
205        self.browser.open('http://localhost/app/configuration')
206        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
207        self.assertTrue('<span>expired</span>' in self.browser.contents)
208        self.browser.getControl("Purge mandates").click()
209        self.assertTrue('1 mandate(s) were purged' in self.browser.contents)
210        self.assertEqual(self.app['mandates'].count, (1, 0, 1))
211
212    def test_browser_set_password(self):
213        student = createObject('waeup.Student')
214        self.app['students'].addStudent(student)
215        mandate = PasswordMandate()
216        mandate.params['user'] = student
217        mandate.params['password'] = 'mypwd1'
218        self.app['mandates'].addMandate(mandate)
219        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
220            % mandate.mandate_id)
221        # Password has been set.
222        self.assertTrue('Password has been successfully set. Login with your new password.'
223            in self.browser.contents)
224        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
225        # All mandates have been removed.
226        self.assertEqual(len(self.app['mandates'].keys()), 0)
227        # We redirect to login page not to the frontpage.
228        self.assertEqual(self.browser.url, 'http://localhost/app/login')
229        # Mandate has been deleted
230        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
231            % mandate.mandate_id)
232        self.assertTrue('No mandate' in self.browser.contents)
233        self.assertEqual(self.browser.url, 'http://localhost/app')
234        # Mandate id is needed
235        self.browser.open('http://localhost/app/mandate')
236        self.assertTrue('Misuse' in self.browser.contents)
237        self.assertEqual(self.browser.url, 'http://localhost/app')
238
239    def test_refereereport_mandate(self):
240        mandate = RefereeReportMandate()
241        mandate.params['name'] = u'John Referee'
242        mandate.params['email'] = 'aa@aa.aa'
243        mandate.params['redirect_path'] = 'applicants/87689'
244        self.app['mandates'].addMandate(mandate)
245        (msg, redirect_path) = mandate.execute()
246        self.assertEqual(msg, None)
247        self.assertEqual(redirect_path, 'applicants/87689')
248        # Mandate has not been deleted
249        self.assertEqual(len(self.app['mandates'].keys()), 1)
250        mandate.params['redirect_path'] = None
251        (msg, redirect_path) = mandate.execute()
252        self.assertEqual(msg, 'Wrong mandate parameters.')
253        self.assertEqual(redirect_path, '')
254        # Add and execute an expired mandate
255        mandate2 = RefereeReportMandate(days=0)
256        mandate2.params['name'] = u'John Referee'
257        mandate2.params['email'] = 'aa@aa.aa'
258        mandate2.params['redirect_path'] = 'applicants/87689'
259        self.app['mandates'].addMandate(mandate2)
260        (msg, redirect_path) = mandate2.execute()
261        self.assertEqual(msg, 'Mandate expired.')
262        self.assertEqual(redirect_path, '')
263        # Both mandates still exist
264        self.assertEqual(len(self.app['mandates'].keys()), 2)
265
266    def test_set_parents_password(self):
267        student = createObject('waeup.Student')
268        # Add and execute a mandate with missing parameters.
269        mandate = ParentsPasswordMandate()
270        self.app['mandates'].addMandate(mandate)
271        (msg, redirect_path) = mandate.execute()
272        self.assertEqual(redirect_path, '')
273        self.assertEqual(msg, u'Wrong mandate parameters.')
274        # Add and execute an expired mandate.
275        mandate = PasswordMandate(days=0)
276        mandate.params['student'] = student
277        mandate.params['password'] = 'mypwd1'
278        self.app['mandates'].addMandate(mandate)
279        (msg, redirect_path) = mandate.execute()
280        self.assertEqual(msg, u'Mandate expired.')
281        self.assertEqual(redirect_path, '')
282        # Password has not been set
283        self.assertEqual(student.getParentsPassword(), None)
284        # Add and execute a perfect mandate
285        mandate = ParentsPasswordMandate()
286        mandate.params['student'] = student
287        mandate.params['password'] = 'mypwd1'
288        self.app['mandates'].addMandate(mandate)
289        (msg, redirect_path) = mandate.execute()
290        # Parents password has been set and must be used for login.
291        self.assertEqual(msg,
292            'Parents password has been successfully set. '
293            'Login with your new parents password.')
294        self.assertEqual(redirect_path, '/login')
295        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
296        # All mandates have been removed.
297        self.assertEqual(len(self.app['mandates'].keys()), 0)
298        logfile = os.path.join(
299            self.app['datacenter'].storage, 'logs', 'main.log')
300        logcontent = open(logfile).read()
301        self.assertTrue('system - ParentsPasswordMandate used: K1000000'
302                        in logcontent)
Note: See TracBrowser for help on using the repository browser.