1 | ## $Id: tests.py 8859 2012-06-30 07:58:20Z 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 |
---|
23 | from zope.testbrowser.testing import Browser |
---|
24 | from datetime import datetime, timedelta |
---|
25 | from zope.interface.verify import verifyClass, verifyObject |
---|
26 | from zope.component import createObject |
---|
27 | from zope.component.hooks import setSite, clearSite |
---|
28 | from waeup.kofa.app import University |
---|
29 | from waeup.kofa.interfaces import IUserAccount |
---|
30 | from waeup.kofa.mandates.interfaces import ( |
---|
31 | IMandatesContainer, IMandate) |
---|
32 | from waeup.kofa.mandates.container import MandatesContainer |
---|
33 | from waeup.kofa.mandates.mandate import PasswordMandate |
---|
34 | from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase) |
---|
35 | |
---|
36 | class MandatesContainerTestCase(FunctionalTestCase): |
---|
37 | |
---|
38 | layer = FunctionalLayer |
---|
39 | |
---|
40 | def test_interfaces(self): |
---|
41 | # Make sure the correct interfaces are implemented. |
---|
42 | self.assertTrue( |
---|
43 | verifyClass( |
---|
44 | IMandatesContainer, MandatesContainer) |
---|
45 | ) |
---|
46 | self.assertTrue( |
---|
47 | verifyObject( |
---|
48 | IMandatesContainer, MandatesContainer()) |
---|
49 | ) |
---|
50 | self.assertTrue( |
---|
51 | verifyClass( |
---|
52 | IMandate, PasswordMandate) |
---|
53 | ) |
---|
54 | self.assertTrue( |
---|
55 | verifyObject( |
---|
56 | IMandate, PasswordMandate()) |
---|
57 | ) |
---|
58 | return |
---|
59 | |
---|
60 | def setUp(self): |
---|
61 | super(MandatesContainerTestCase, self).setUp() |
---|
62 | |
---|
63 | # Setup a sample site for each test |
---|
64 | app = University() |
---|
65 | self.dc_root = tempfile.mkdtemp() |
---|
66 | app['datacenter'].setStoragePath(self.dc_root) |
---|
67 | |
---|
68 | # Prepopulate the ZODB... |
---|
69 | self.getRootFolder()['app'] = app |
---|
70 | # we add the site immediately after creation to the |
---|
71 | # ZODB. Catalogs and other local utilities are not setup |
---|
72 | # before that step. |
---|
73 | self.app = self.getRootFolder()['app'] |
---|
74 | # Set site here. Some of the following setup code might need |
---|
75 | # to access grok.getSite() and should get our new app then |
---|
76 | setSite(app) |
---|
77 | |
---|
78 | self.browser = Browser() |
---|
79 | self.browser.handleErrors = False |
---|
80 | |
---|
81 | def tearDown(self): |
---|
82 | super(MandatesContainerTestCase, self).tearDown() |
---|
83 | clearSite() |
---|
84 | shutil.rmtree(self.dc_root) |
---|
85 | |
---|
86 | def test_set_student_password(self): |
---|
87 | student = createObject('waeup.Student') |
---|
88 | # Add and execute a mandate with missing parameters. |
---|
89 | mandate = PasswordMandate() |
---|
90 | self.app['mandates'].addMandate(mandate) |
---|
91 | msg = mandate.execute() |
---|
92 | self.assertEqual(msg, u'Wrong mandate parameters.') |
---|
93 | # Add and execute an expired mandate. |
---|
94 | mandate = PasswordMandate(days=0) |
---|
95 | self.app['mandates'].addMandate(mandate) |
---|
96 | msg = mandate.execute() |
---|
97 | self.assertEqual(msg, u'Mandate expired.') |
---|
98 | # Add and execute a perfect mandate |
---|
99 | mandate = PasswordMandate() |
---|
100 | mandate.params['user'] = student |
---|
101 | mandate.params['password'] = 'mypwd1' |
---|
102 | self.app['mandates'].addMandate(mandate) |
---|
103 | msg = mandate.execute() |
---|
104 | # Password has been set. |
---|
105 | self.assertEqual(msg, 'Password has been successfully set. Proceed to ' |
---|
106 | 'the login page and enter your credentials.') |
---|
107 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
108 | # All mandates have been removed. |
---|
109 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|
110 | |
---|
111 | def test_set_officer_password(self): |
---|
112 | self.app['users'].addUser('bob', 'bobssecret') |
---|
113 | officer = self.app['users']['bob'] |
---|
114 | mandate = PasswordMandate() |
---|
115 | mandate.params['user'] = officer |
---|
116 | mandate.params['password'] = 'mypwd1' |
---|
117 | self.app['mandates'].addMandate(mandate) |
---|
118 | msg = mandate.execute() |
---|
119 | # Password has been set. |
---|
120 | self.assertEqual(msg, 'Password has been successfully set. Proceed to ' |
---|
121 | 'the login page and enter your credentials.') |
---|
122 | self.assertTrue(IUserAccount(officer).checkPassword('mypwd1')) |
---|
123 | |
---|
124 | def test_set_applicant_password(self): |
---|
125 | applicant = createObject('waeup.Applicant') |
---|
126 | mandate = PasswordMandate() |
---|
127 | mandate.params['user'] = applicant |
---|
128 | mandate.params['password'] = 'mypwd1' |
---|
129 | self.app['mandates'].addMandate(mandate) |
---|
130 | msg = mandate.execute() |
---|
131 | # Password has been set. |
---|
132 | self.assertEqual(msg, 'Password has been successfully set. Proceed to ' |
---|
133 | 'the login page and enter your credentials.') |
---|
134 | self.assertTrue(IUserAccount(applicant).checkPassword('mypwd1')) |
---|
135 | |
---|
136 | def test_remove_expired(self): |
---|
137 | # mandate1 is an old mandate which just expired. |
---|
138 | mandate1 = PasswordMandate(days=0) |
---|
139 | self.app['mandates'].addMandate(mandate1) |
---|
140 | # mandate2 is a new mandate with default time delta. |
---|
141 | mandate2 = PasswordMandate(mandate_id='23456') |
---|
142 | self.app['mandates'].addMandate(mandate2) |
---|
143 | self.assertEqual(len(self.app['mandates'].keys()), 2) |
---|
144 | self.app['mandates'].removeExpired() |
---|
145 | # Only the new mandate remains in the container. |
---|
146 | self.assertEqual(len(self.app['mandates'].keys()), 1) |
---|
147 | self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456']) |
---|
148 | |
---|
149 | def test_browser(self): |
---|
150 | student = createObject('waeup.Student') |
---|
151 | self.app['students'].addStudent(student) |
---|
152 | mandate = PasswordMandate() |
---|
153 | mandate.params['user'] = student |
---|
154 | mandate.params['password'] = 'mypwd1' |
---|
155 | self.app['mandates'].addMandate(mandate) |
---|
156 | self.browser.open('http://localhost/app/mandate?mandate_id=%s' |
---|
157 | % mandate.mandate_id) |
---|
158 | # Password has been set. |
---|
159 | self.assertTrue('Password has been successfully set. Proceed to ' |
---|
160 | 'the login page and enter your credentials.' |
---|
161 | in self.browser.contents) |
---|
162 | self.assertTrue(IUserAccount(student).checkPassword('mypwd1')) |
---|
163 | # All mandates have been removed. |
---|
164 | self.assertEqual(len(self.app['mandates'].keys()), 0) |
---|