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

Last change on this file since 13986 was 13986, checked in by Henrik Bettermann, 9 years ago

Provision for the new RefereeReportMandate? which redirects to another url.

  • Property svn:keywords set to Id
File size: 9.1 KB
Line 
1## $Id: tests.py 13986 2016-06-24 06:27:41Z 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 PasswordMandate
35from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
36
37class MandatesContainerTestCase(FunctionalTestCase):
38
39    layer = FunctionalLayer
40
41    def test_interfaces(self):
42        # Make sure the correct interfaces are implemented.
43        self.assertTrue(
44            verifyClass(
45                IMandatesContainer, MandatesContainer)
46            )
47        self.assertTrue(
48            verifyObject(
49                IMandatesContainer, MandatesContainer())
50            )
51        self.assertTrue(
52            verifyClass(
53                IMandate, PasswordMandate)
54            )
55        self.assertTrue(
56            verifyObject(
57                IMandate, PasswordMandate())
58            )
59        return
60
61    def setUp(self):
62        super(MandatesContainerTestCase, self).setUp()
63
64        # Setup a sample site for each test
65        app = University()
66        self.dc_root = tempfile.mkdtemp()
67        app['datacenter'].setStoragePath(self.dc_root)
68
69        # Prepopulate the ZODB...
70        self.getRootFolder()['app'] = app
71        # we add the site immediately after creation to the
72        # ZODB. Catalogs and other local utilities are not setup
73        # before that step.
74        self.app = self.getRootFolder()['app']
75        # Set site here. Some of the following setup code might need
76        # to access grok.getSite() and should get our new app then
77        setSite(app)
78
79        self.browser = Browser()
80        self.browser.handleErrors = False
81
82    def tearDown(self):
83        super(MandatesContainerTestCase, self).tearDown()
84        clearSite()
85        shutil.rmtree(self.dc_root)
86
87    def test_set_student_password(self):
88        student = createObject('waeup.Student')
89        # Add and execute a mandate with missing parameters.
90        mandate = PasswordMandate()
91        self.app['mandates'].addMandate(mandate)
92        (msg, redirect_path) = mandate.execute()
93        self.assertEqual(redirect_path, '')
94        self.assertEqual(msg, u'Wrong mandate parameters.')
95        # Add and execute an expired mandate.
96        mandate = PasswordMandate(days=0)
97        self.app['mandates'].addMandate(mandate)
98        (msg, redirect_path) = mandate.execute()
99        self.assertEqual(msg, u'Mandate expired.')
100        self.assertEqual(redirect_path, '')
101        # Add and execute a perfect mandate
102        mandate = PasswordMandate()
103        mandate.params['user'] = student
104        mandate.params['password'] = 'mypwd1'
105        self.app['mandates'].addMandate(mandate)
106        (msg, redirect_path) = mandate.execute()
107        # Password has been set.
108        self.assertEqual(msg,
109            'Password has been successfully set. Login with your new password.')
110        self.assertEqual(redirect_path, 'login')
111        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
112        # All mandates have been removed.
113        self.assertEqual(len(self.app['mandates'].keys()), 0)
114        logfile = os.path.join(
115            self.app['datacenter'].storage, 'logs', 'main.log')
116        logcontent = open(logfile).read()
117        self.assertTrue('system - PasswordMandate used: K1000000' in logcontent)
118
119    def test_set_officer_password(self):
120        self.app['users'].addUser('bob', 'bobssecret')
121        officer = self.app['users']['bob']
122        mandate = PasswordMandate()
123        mandate.params['user'] = officer
124        mandate.params['password'] = 'mypwd1'
125        self.app['mandates'].addMandate(mandate)
126        (msg, redirect_path) = mandate.execute()
127        # Password has been set.
128        self.assertEqual(msg,
129            'Password has been successfully set. Login with your new password.')
130        self.assertEqual(redirect_path, 'login')
131        self.assertTrue(IUserAccount(officer).checkPassword('mypwd1'))
132        logfile = os.path.join(
133            self.app['datacenter'].storage, 'logs', 'main.log')
134        logcontent = open(logfile).read()
135        self.assertTrue('system - PasswordMandate used: bob' in logcontent)
136
137    def test_set_applicant_password(self):
138        applicant = createObject('waeup.Applicant')
139        applicant.applicant_id = u'abc'
140        mandate = PasswordMandate()
141        mandate.params['user'] = applicant
142        mandate.params['password'] = 'mypwd1'
143        self.app['mandates'].addMandate(mandate)
144        (msg, redirect_path) = mandate.execute()
145        # Password has been set.
146        self.assertEqual(msg,
147            'Password has been successfully set. Login with your new password.')
148        self.assertEqual(redirect_path, 'login')
149        self.assertTrue(IUserAccount(applicant).checkPassword('mypwd1'))
150        logfile = os.path.join(
151            self.app['datacenter'].storage, 'logs', 'main.log')
152        logcontent = open(logfile).read()
153        self.assertTrue('system - PasswordMandate used: abc' in logcontent)
154
155    def test_remove_expired(self):
156        # mandate1 is an old mandate which just expired.
157        mandate1 = PasswordMandate(days=0)
158        self.app['mandates'].addMandate(mandate1)
159        # mandate2 is a new mandate with default time delta.
160        mandate2 = PasswordMandate(mandate_id='23456')
161        self.app['mandates'].addMandate(mandate2)
162        self.assertEqual(len(self.app['mandates'].keys()), 2)
163        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
164        num_deleted = self.app['mandates'].removeExpired()
165        self.assertEqual(num_deleted, 1)
166        # Only the new mandate remains in the container.
167        self.assertEqual(len(self.app['mandates'].keys()), 1)
168        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
169        logfile = os.path.join(
170            self.app['datacenter'].storage, 'logs', 'main.log')
171        logcontent = open(logfile).read()
172        self.assertTrue('system - 1 mandates purged' in logcontent)
173
174    def test_purge_mandates(self):
175        # mandate1 is an old mandate which just expired.
176        mandate1 = PasswordMandate(days=0)
177        self.app['mandates'].addMandate(mandate1)
178        # mandate2 is a new mandate with default time delta.
179        mandate2 = PasswordMandate(mandate_id='23456')
180        self.app['mandates'].addMandate(mandate2)
181        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
182        self.browser.open('http://localhost/app/configuration')
183        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
184        self.assertTrue('<span>expired</span>' in self.browser.contents)
185        self.browser.getControl("Purge mandates").click()
186        self.assertTrue('1 mandate(s) were purged' in self.browser.contents)
187        self.assertEqual(self.app['mandates'].count, (1, 0, 1))
188        return
189
190    def test_browser(self):
191        student = createObject('waeup.Student')
192        self.app['students'].addStudent(student)
193        mandate = PasswordMandate()
194        mandate.params['user'] = student
195        mandate.params['password'] = 'mypwd1'
196        self.app['mandates'].addMandate(mandate)
197        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
198            % mandate.mandate_id)
199        # Password has been set.
200        self.assertTrue('Password has been successfully set. Login with your new password.'
201            in self.browser.contents)
202        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
203        # All mandates have been removed.
204        self.assertEqual(len(self.app['mandates'].keys()), 0)
205        # We redirect to login page not to the frontpage.
206        self.assertEqual(self.browser.url, 'http://localhost/app/login')
207        # Mandate has been deleted
208        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
209            % mandate.mandate_id)
210        self.assertTrue('No mandate' in self.browser.contents)
211        self.assertEqual(self.browser.url, 'http://localhost/app/')
212        # Mandate id is needed
213        self.browser.open('http://localhost/app/mandate')
214        self.assertTrue('Misuse' in self.browser.contents)
215        self.assertEqual(self.browser.url, 'http://localhost/app/')
216        return
Note: See TracBrowser for help on using the repository browser.