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

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

Finalize parents access.

  • Property svn:keywords set to Id
File size: 12.9 KB
RevLine 
[8846]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
[8860]23import os
[8848]24from zope.testbrowser.testing import Browser
[8846]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
[15607]34from waeup.kofa.mandates.mandate import (
35    PasswordMandate, RefereeReportMandate, ParentsPasswordMandate)
[8846]36from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
[15287]37from waeup.kofa.tests.test_authentication import SECRET
[8846]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(
[8857]55                IMandate, PasswordMandate)
[8846]56            )
57        self.assertTrue(
58            verifyObject(
[8857]59                IMandate, PasswordMandate())
[8846]60            )
[15607]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            )
[8846]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
[8848]97        self.browser = Browser()
98        self.browser.handleErrors = False
99
[8846]100    def tearDown(self):
101        super(MandatesContainerTestCase, self).tearDown()
102        clearSite()
103        shutil.rmtree(self.dc_root)
104
[8857]105    def test_set_student_password(self):
[8846]106        student = createObject('waeup.Student')
107        # Add and execute a mandate with missing parameters.
[8857]108        mandate = PasswordMandate()
[13987]109        IUserAccount(student).setPassword('old_pw')
[8846]110        self.app['mandates'].addMandate(mandate)
[13986]111        (msg, redirect_path) = mandate.execute()
112        self.assertEqual(redirect_path, '')
[8846]113        self.assertEqual(msg, u'Wrong mandate parameters.')
114        # Add and execute an expired mandate.
[8857]115        mandate = PasswordMandate(days=0)
[13987]116        mandate.params['user'] = student
117        mandate.params['password'] = 'mypwd1'
[8846]118        self.app['mandates'].addMandate(mandate)
[13986]119        (msg, redirect_path) = mandate.execute()
[8846]120        self.assertEqual(msg, u'Mandate expired.')
[13986]121        self.assertEqual(redirect_path, '')
[13987]122        # Password has not been set
123        self.assertTrue(IUserAccount(student).checkPassword('old_pw'))
[8848]124        # Add and execute a perfect mandate
[8857]125        mandate = PasswordMandate()
[8858]126        mandate.params['user'] = student
[8846]127        mandate.params['password'] = 'mypwd1'
128        self.app['mandates'].addMandate(mandate)
[13986]129        (msg, redirect_path) = mandate.execute()
[8846]130        # Password has been set.
[13986]131        self.assertEqual(msg,
132            'Password has been successfully set. Login with your new password.')
[13990]133        self.assertEqual(redirect_path, '/login')
[8846]134        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
135        # All mandates have been removed.
136        self.assertEqual(len(self.app['mandates'].keys()), 0)
[8860]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)
[8846]141
[8857]142    def test_set_officer_password(self):
[15287]143        self.app['users'].addUser('bob', SECRET)
[8857]144        officer = self.app['users']['bob']
145        mandate = PasswordMandate()
[8858]146        mandate.params['user'] = officer
[15287]147        mandate.params['password'] = SECRET
[8857]148        self.app['mandates'].addMandate(mandate)
[13986]149        (msg, redirect_path) = mandate.execute()
[8857]150        # Password has been set.
[13986]151        self.assertEqual(msg,
152            'Password has been successfully set. Login with your new password.')
[13990]153        self.assertEqual(redirect_path, '/login')
[15287]154        self.assertTrue(IUserAccount(officer).checkPassword(SECRET))
[8860]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)
[8857]159
[8859]160    def test_set_applicant_password(self):
161        applicant = createObject('waeup.Applicant')
[8860]162        applicant.applicant_id = u'abc'
[8859]163        mandate = PasswordMandate()
164        mandate.params['user'] = applicant
165        mandate.params['password'] = 'mypwd1'
166        self.app['mandates'].addMandate(mandate)
[13986]167        (msg, redirect_path) = mandate.execute()
[8859]168        # Password has been set.
[13986]169        self.assertEqual(msg,
170            'Password has been successfully set. Login with your new password.')
[13990]171        self.assertEqual(redirect_path, '/login')
[8859]172        self.assertTrue(IUserAccount(applicant).checkPassword('mypwd1'))
[8860]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)
[8859]177
[8846]178    def test_remove_expired(self):
179        # mandate1 is an old mandate which just expired.
[8857]180        mandate1 = PasswordMandate(days=0)
[8846]181        self.app['mandates'].addMandate(mandate1)
182        # mandate2 is a new mandate with default time delta.
[8857]183        mandate2 = PasswordMandate(mandate_id='23456')
[8846]184        self.app['mandates'].addMandate(mandate2)
185        self.assertEqual(len(self.app['mandates'].keys()), 2)
[13959]186        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
[8910]187        num_deleted = self.app['mandates'].removeExpired()
188        self.assertEqual(num_deleted, 1)
[8846]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'])
[13962]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)
[8848]196
[13959]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
[13988]212    def test_browser_set_password(self):
[8848]213        student = createObject('waeup.Student')
214        self.app['students'].addStudent(student)
[8857]215        mandate = PasswordMandate()
[8858]216        mandate.params['user'] = student
[8848]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.
[11681]222        self.assertTrue('Password has been successfully set. Login with your new password.'
[8848]223            in self.browser.contents)
224        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
225        # All mandates have been removed.
[8853]226        self.assertEqual(len(self.app['mandates'].keys()), 0)
[11680]227        # We redirect to login page not to the frontpage.
228        self.assertEqual(self.browser.url, 'http://localhost/app/login')
[13986]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)
[13990]233        self.assertEqual(self.browser.url, 'http://localhost/app')
[13986]234        # Mandate id is needed
235        self.browser.open('http://localhost/app/mandate')
236        self.assertTrue('Misuse' in self.browser.contents)
[13990]237        self.assertEqual(self.browser.url, 'http://localhost/app')
[13988]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)
[15607]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. '
[15609]293            'Login with your new parents password.')
[15607]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.