[8846] | 1 | ## $Id: tests.py 11681 2014-06-06 12:01:03Z 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 |
---|
[8857] | 34 | from waeup.kofa.mandates.mandate import PasswordMandate |
---|
[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() |
---|
[8846] | 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. |
---|
[8857] | 95 | mandate = PasswordMandate(days=0) |
---|
[8846] | 96 | self.app['mandates'].addMandate(mandate) |
---|
| 97 | msg = mandate.execute() |
---|
| 98 | self.assertEqual(msg, u'Mandate expired.') |
---|
[8848] | 99 | # Add and execute a perfect mandate |
---|
[8857] | 100 | mandate = PasswordMandate() |
---|
[8858] | 101 | mandate.params['user'] = student |
---|
[8846] | 102 | mandate.params['password'] = 'mypwd1' |
---|
| 103 | self.app['mandates'].addMandate(mandate) |
---|
| 104 | msg = mandate.execute() |
---|
| 105 | # Password has been set. |
---|
[11681] | 106 | self.assertEqual(msg, 'Password has been successfully set. Login with your new password.') |
---|
[8846] | 107 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
| 108 | # All mandates have been removed. |
---|
| 109 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|
[8860] | 110 | logfile = os.path.join( |
---|
| 111 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
| 112 | logcontent = open(logfile).read() |
---|
| 113 | self.assertTrue('system - PasswordMandate used: K1000000' in logcontent) |
---|
[8846] | 114 | |
---|
[8857] | 115 | def test_set_officer_password(self): |
---|
| 116 | self.app['users'].addUser('bob', 'bobssecret') |
---|
| 117 | officer = self.app['users']['bob'] |
---|
| 118 | mandate = PasswordMandate() |
---|
[8858] | 119 | mandate.params['user'] = officer |
---|
[8857] | 120 | mandate.params['password'] = 'mypwd1' |
---|
| 121 | self.app['mandates'].addMandate(mandate) |
---|
| 122 | msg = mandate.execute() |
---|
| 123 | # Password has been set. |
---|
[11681] | 124 | self.assertEqual(msg, 'Password has been successfully set. Login with your new password.') |
---|
[8857] | 125 | self.assertTrue(IUserAccount(officer).checkPassword('mypwd1')) |
---|
[8860] | 126 | logfile = os.path.join( |
---|
| 127 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
| 128 | logcontent = open(logfile).read() |
---|
| 129 | self.assertTrue('system - PasswordMandate used: bob' in logcontent) |
---|
[8857] | 130 | |
---|
[8859] | 131 | def test_set_applicant_password(self): |
---|
| 132 | applicant = createObject('waeup.Applicant') |
---|
[8860] | 133 | applicant.applicant_id = u'abc' |
---|
[8859] | 134 | mandate = PasswordMandate() |
---|
| 135 | mandate.params['user'] = applicant |
---|
| 136 | mandate.params['password'] = 'mypwd1' |
---|
| 137 | self.app['mandates'].addMandate(mandate) |
---|
| 138 | msg = mandate.execute() |
---|
| 139 | # Password has been set. |
---|
[11681] | 140 | self.assertEqual(msg, 'Password has been successfully set. Login with your new password.') |
---|
[8859] | 141 | self.assertTrue(IUserAccount(applicant).checkPassword('mypwd1')) |
---|
[8860] | 142 | logfile = os.path.join( |
---|
| 143 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
| 144 | logcontent = open(logfile).read() |
---|
| 145 | self.assertTrue('system - PasswordMandate used: abc' in logcontent) |
---|
[8859] | 146 | |
---|
[8846] | 147 | def test_remove_expired(self): |
---|
| 148 | # mandate1 is an old mandate which just expired. |
---|
[8857] | 149 | mandate1 = PasswordMandate(days=0) |
---|
[8846] | 150 | self.app['mandates'].addMandate(mandate1) |
---|
| 151 | # mandate2 is a new mandate with default time delta. |
---|
[8857] | 152 | mandate2 = PasswordMandate(mandate_id='23456') |
---|
[8846] | 153 | self.app['mandates'].addMandate(mandate2) |
---|
| 154 | self.assertEqual(len(self.app['mandates'].keys()), 2) |
---|
[8910] | 155 | num_deleted = self.app['mandates'].removeExpired() |
---|
| 156 | self.assertEqual(num_deleted, 1) |
---|
[8846] | 157 | # Only the new mandate remains in the container. |
---|
| 158 | self.assertEqual(len(self.app['mandates'].keys()), 1) |
---|
| 159 | self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456']) |
---|
[8848] | 160 | |
---|
| 161 | def test_browser(self): |
---|
| 162 | student = createObject('waeup.Student') |
---|
| 163 | self.app['students'].addStudent(student) |
---|
[8857] | 164 | mandate = PasswordMandate() |
---|
[8858] | 165 | mandate.params['user'] = student |
---|
[8848] | 166 | mandate.params['password'] = 'mypwd1' |
---|
| 167 | self.app['mandates'].addMandate(mandate) |
---|
| 168 | self.browser.open('http://localhost/app/mandate?mandate_id=%s' |
---|
| 169 | % mandate.mandate_id) |
---|
| 170 | # Password has been set. |
---|
[11681] | 171 | self.assertTrue('Password has been successfully set. Login with your new password.' |
---|
[8848] | 172 | in self.browser.contents) |
---|
| 173 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
| 174 | # All mandates have been removed. |
---|
[8853] | 175 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|
[11680] | 176 | # We redirect to login page not to the frontpage. |
---|
| 177 | self.assertEqual(self.browser.url, 'http://localhost/app/login') |
---|