[8846] | 1 | ## $Id: tests.py 13990 2016-06-25 05:00:54Z 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 | """ |
---|
| 19 | Tests for mandates. |
---|
| 20 | """ |
---|
| 21 | import tempfile |
---|
| 22 | import shutil |
---|
[8860] | 23 | import os |
---|
[8848] | 24 | from zope.testbrowser.testing import Browser |
---|
[8846] | 25 | from datetime import datetime, timedelta |
---|
| 26 | from zope.interface.verify import verifyClass, verifyObject |
---|
| 27 | from zope.component import createObject |
---|
| 28 | from zope.component.hooks import setSite, clearSite |
---|
| 29 | from waeup.kofa.app import University |
---|
| 30 | from waeup.kofa.interfaces import IUserAccount |
---|
| 31 | from waeup.kofa.mandates.interfaces import ( |
---|
| 32 | IMandatesContainer, IMandate) |
---|
| 33 | from waeup.kofa.mandates.container import MandatesContainer |
---|
[13988] | 34 | from waeup.kofa.mandates.mandate import PasswordMandate, RefereeReportMandate |
---|
[8846] | 35 | from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase) |
---|
| 36 | |
---|
| 37 | class 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( |
---|
[8857] | 53 | IMandate, PasswordMandate) |
---|
[8846] | 54 | ) |
---|
| 55 | self.assertTrue( |
---|
| 56 | verifyObject( |
---|
[8857] | 57 | IMandate, PasswordMandate()) |
---|
[8846] | 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 | |
---|
[8848] | 79 | self.browser = Browser() |
---|
| 80 | self.browser.handleErrors = False |
---|
| 81 | |
---|
[8846] | 82 | def tearDown(self): |
---|
| 83 | super(MandatesContainerTestCase, self).tearDown() |
---|
| 84 | clearSite() |
---|
| 85 | shutil.rmtree(self.dc_root) |
---|
| 86 | |
---|
[8857] | 87 | def test_set_student_password(self): |
---|
[8846] | 88 | student = createObject('waeup.Student') |
---|
| 89 | # Add and execute a mandate with missing parameters. |
---|
[8857] | 90 | mandate = PasswordMandate() |
---|
[13987] | 91 | IUserAccount(student).setPassword('old_pw') |
---|
[8846] | 92 | self.app['mandates'].addMandate(mandate) |
---|
[13986] | 93 | (msg, redirect_path) = mandate.execute() |
---|
| 94 | self.assertEqual(redirect_path, '') |
---|
[8846] | 95 | self.assertEqual(msg, u'Wrong mandate parameters.') |
---|
| 96 | # Add and execute an expired mandate. |
---|
[8857] | 97 | mandate = PasswordMandate(days=0) |
---|
[13987] | 98 | mandate.params['user'] = student |
---|
| 99 | mandate.params['password'] = 'mypwd1' |
---|
[8846] | 100 | self.app['mandates'].addMandate(mandate) |
---|
[13986] | 101 | (msg, redirect_path) = mandate.execute() |
---|
[8846] | 102 | self.assertEqual(msg, u'Mandate expired.') |
---|
[13986] | 103 | self.assertEqual(redirect_path, '') |
---|
[13987] | 104 | # Password has not been set |
---|
| 105 | self.assertTrue(IUserAccount(student).checkPassword('old_pw')) |
---|
[8848] | 106 | # Add and execute a perfect mandate |
---|
[8857] | 107 | mandate = PasswordMandate() |
---|
[8858] | 108 | mandate.params['user'] = student |
---|
[8846] | 109 | mandate.params['password'] = 'mypwd1' |
---|
| 110 | self.app['mandates'].addMandate(mandate) |
---|
[13986] | 111 | (msg, redirect_path) = mandate.execute() |
---|
[8846] | 112 | # Password has been set. |
---|
[13986] | 113 | self.assertEqual(msg, |
---|
| 114 | 'Password has been successfully set. Login with your new password.') |
---|
[13990] | 115 | self.assertEqual(redirect_path, '/login') |
---|
[8846] | 116 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
| 117 | # All mandates have been removed. |
---|
| 118 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|
[8860] | 119 | logfile = os.path.join( |
---|
| 120 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
| 121 | logcontent = open(logfile).read() |
---|
| 122 | self.assertTrue('system - PasswordMandate used: K1000000' in logcontent) |
---|
[8846] | 123 | |
---|
[8857] | 124 | def test_set_officer_password(self): |
---|
| 125 | self.app['users'].addUser('bob', 'bobssecret') |
---|
| 126 | officer = self.app['users']['bob'] |
---|
| 127 | mandate = PasswordMandate() |
---|
[8858] | 128 | mandate.params['user'] = officer |
---|
[8857] | 129 | mandate.params['password'] = 'mypwd1' |
---|
| 130 | self.app['mandates'].addMandate(mandate) |
---|
[13986] | 131 | (msg, redirect_path) = mandate.execute() |
---|
[8857] | 132 | # Password has been set. |
---|
[13986] | 133 | self.assertEqual(msg, |
---|
| 134 | 'Password has been successfully set. Login with your new password.') |
---|
[13990] | 135 | self.assertEqual(redirect_path, '/login') |
---|
[8857] | 136 | self.assertTrue(IUserAccount(officer).checkPassword('mypwd1')) |
---|
[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: bob' in logcontent) |
---|
[8857] | 141 | |
---|
[8859] | 142 | def test_set_applicant_password(self): |
---|
| 143 | applicant = createObject('waeup.Applicant') |
---|
[8860] | 144 | applicant.applicant_id = u'abc' |
---|
[8859] | 145 | mandate = PasswordMandate() |
---|
| 146 | mandate.params['user'] = applicant |
---|
| 147 | mandate.params['password'] = 'mypwd1' |
---|
| 148 | self.app['mandates'].addMandate(mandate) |
---|
[13986] | 149 | (msg, redirect_path) = mandate.execute() |
---|
[8859] | 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') |
---|
[8859] | 154 | self.assertTrue(IUserAccount(applicant).checkPassword('mypwd1')) |
---|
[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: abc' in logcontent) |
---|
[8859] | 159 | |
---|
[8846] | 160 | def test_remove_expired(self): |
---|
| 161 | # mandate1 is an old mandate which just expired. |
---|
[8857] | 162 | mandate1 = PasswordMandate(days=0) |
---|
[8846] | 163 | self.app['mandates'].addMandate(mandate1) |
---|
| 164 | # mandate2 is a new mandate with default time delta. |
---|
[8857] | 165 | mandate2 = PasswordMandate(mandate_id='23456') |
---|
[8846] | 166 | self.app['mandates'].addMandate(mandate2) |
---|
| 167 | self.assertEqual(len(self.app['mandates'].keys()), 2) |
---|
[13959] | 168 | self.assertEqual(self.app['mandates'].count, (1, 1, 2)) |
---|
[8910] | 169 | num_deleted = self.app['mandates'].removeExpired() |
---|
| 170 | self.assertEqual(num_deleted, 1) |
---|
[8846] | 171 | # Only the new mandate remains in the container. |
---|
| 172 | self.assertEqual(len(self.app['mandates'].keys()), 1) |
---|
| 173 | self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456']) |
---|
[13962] | 174 | logfile = os.path.join( |
---|
| 175 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
| 176 | logcontent = open(logfile).read() |
---|
| 177 | self.assertTrue('system - 1 mandates purged' in logcontent) |
---|
[8848] | 178 | |
---|
[13959] | 179 | def test_purge_mandates(self): |
---|
| 180 | # mandate1 is an old mandate which just expired. |
---|
| 181 | mandate1 = PasswordMandate(days=0) |
---|
| 182 | self.app['mandates'].addMandate(mandate1) |
---|
| 183 | # mandate2 is a new mandate with default time delta. |
---|
| 184 | mandate2 = PasswordMandate(mandate_id='23456') |
---|
| 185 | self.app['mandates'].addMandate(mandate2) |
---|
| 186 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 187 | self.browser.open('http://localhost/app/configuration') |
---|
| 188 | self.assertEqual(self.app['mandates'].count, (1, 1, 2)) |
---|
| 189 | self.assertTrue('<span>expired</span>' in self.browser.contents) |
---|
| 190 | self.browser.getControl("Purge mandates").click() |
---|
| 191 | self.assertTrue('1 mandate(s) were purged' in self.browser.contents) |
---|
| 192 | self.assertEqual(self.app['mandates'].count, (1, 0, 1)) |
---|
| 193 | |
---|
[13988] | 194 | def test_browser_set_password(self): |
---|
[8848] | 195 | student = createObject('waeup.Student') |
---|
| 196 | self.app['students'].addStudent(student) |
---|
[8857] | 197 | mandate = PasswordMandate() |
---|
[8858] | 198 | mandate.params['user'] = student |
---|
[8848] | 199 | mandate.params['password'] = 'mypwd1' |
---|
| 200 | self.app['mandates'].addMandate(mandate) |
---|
| 201 | self.browser.open('http://localhost/app/mandate?mandate_id=%s' |
---|
| 202 | % mandate.mandate_id) |
---|
| 203 | # Password has been set. |
---|
[11681] | 204 | self.assertTrue('Password has been successfully set. Login with your new password.' |
---|
[8848] | 205 | in self.browser.contents) |
---|
| 206 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
| 207 | # All mandates have been removed. |
---|
[8853] | 208 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|
[11680] | 209 | # We redirect to login page not to the frontpage. |
---|
| 210 | self.assertEqual(self.browser.url, 'http://localhost/app/login') |
---|
[13986] | 211 | # Mandate has been deleted |
---|
| 212 | self.browser.open('http://localhost/app/mandate?mandate_id=%s' |
---|
| 213 | % mandate.mandate_id) |
---|
| 214 | self.assertTrue('No mandate' in self.browser.contents) |
---|
[13990] | 215 | self.assertEqual(self.browser.url, 'http://localhost/app') |
---|
[13986] | 216 | # Mandate id is needed |
---|
| 217 | self.browser.open('http://localhost/app/mandate') |
---|
| 218 | self.assertTrue('Misuse' in self.browser.contents) |
---|
[13990] | 219 | self.assertEqual(self.browser.url, 'http://localhost/app') |
---|
[13988] | 220 | |
---|
| 221 | def test_refereereport_mandate(self): |
---|
| 222 | mandate = RefereeReportMandate() |
---|
| 223 | mandate.params['name'] = u'John Referee' |
---|
| 224 | mandate.params['email'] = 'aa@aa.aa' |
---|
| 225 | mandate.params['redirect_path'] = 'applicants/87689' |
---|
| 226 | self.app['mandates'].addMandate(mandate) |
---|
| 227 | (msg, redirect_path) = mandate.execute() |
---|
| 228 | self.assertEqual(msg, None) |
---|
| 229 | self.assertEqual(redirect_path, 'applicants/87689') |
---|
| 230 | # Mandate has not been deleted |
---|
| 231 | self.assertEqual(len(self.app['mandates'].keys()), 1) |
---|
| 232 | mandate.params['redirect_path'] = None |
---|
| 233 | (msg, redirect_path) = mandate.execute() |
---|
| 234 | self.assertEqual(msg, 'Wrong mandate parameters.') |
---|
| 235 | self.assertEqual(redirect_path, '') |
---|
| 236 | # Add and execute an expired mandate |
---|
| 237 | mandate2 = RefereeReportMandate(days=0) |
---|
| 238 | mandate2.params['name'] = u'John Referee' |
---|
| 239 | mandate2.params['email'] = 'aa@aa.aa' |
---|
| 240 | mandate2.params['redirect_path'] = 'applicants/87689' |
---|
| 241 | self.app['mandates'].addMandate(mandate2) |
---|
| 242 | (msg, redirect_path) = mandate2.execute() |
---|
| 243 | self.assertEqual(msg, 'Mandate expired.') |
---|
| 244 | self.assertEqual(redirect_path, '') |
---|
| 245 | # Both mandates still exist |
---|
| 246 | self.assertEqual(len(self.app['mandates'].keys()), 2) |
---|