1 | ## |
---|
2 | ## test_browser.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Tue Mar 29 11:31:11 2011 Uli Fouquet |
---|
5 | ## $Id: test_browser.py 6714 2011-09-12 08:27:14Z henrik $ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """ |
---|
23 | Test the student-related UI components. |
---|
24 | """ |
---|
25 | import shutil |
---|
26 | import tempfile |
---|
27 | from StringIO import StringIO |
---|
28 | from datetime import datetime |
---|
29 | from mechanize import LinkNotFoundError |
---|
30 | from zope.component import createObject |
---|
31 | from zope.component.hooks import setSite, clearSite |
---|
32 | from zope.security.interfaces import Unauthorized |
---|
33 | from zope.testbrowser.testing import Browser |
---|
34 | from hurry.workflow.interfaces import IWorkflowInfo |
---|
35 | from waeup.sirp.testing import ( |
---|
36 | FunctionalLayer, FunctionalTestCase, remove_logger, |
---|
37 | remove_new_loggers, |
---|
38 | ) |
---|
39 | from waeup.sirp.app import University |
---|
40 | from waeup.sirp.students.container import StudentsContainer |
---|
41 | from waeup.sirp.students.student import Student |
---|
42 | from waeup.sirp.university.faculty import Faculty |
---|
43 | from waeup.sirp.university.department import Department |
---|
44 | from waeup.sirp.interfaces import IUserAccount |
---|
45 | |
---|
46 | PH_LEN = 2059 # Length of placeholder file |
---|
47 | |
---|
48 | class StudentsFullSetup(FunctionalTestCase): |
---|
49 | # A test case that only contains a setup and teardown |
---|
50 | # |
---|
51 | # Complete setup for students handlings is rather complex and |
---|
52 | # requires lots of things created before we can start. This is a |
---|
53 | # setup that does all this, creates a university, creates PINs, |
---|
54 | # etc. so that we do not have to bother with that in different |
---|
55 | # test cases. |
---|
56 | |
---|
57 | layer = FunctionalLayer |
---|
58 | |
---|
59 | def setUp(self): |
---|
60 | super(StudentsFullSetup, self).setUp() |
---|
61 | |
---|
62 | # Setup a sample site for each test |
---|
63 | app = University() |
---|
64 | self.dc_root = tempfile.mkdtemp() |
---|
65 | app['datacenter'].setStoragePath(self.dc_root) |
---|
66 | |
---|
67 | # Prepopulate the ZODB... |
---|
68 | self.getRootFolder()['app'] = app |
---|
69 | # we add the site immediately after creation to the |
---|
70 | # ZODB. Catalogs and other local utilities are not setup |
---|
71 | # before that step. |
---|
72 | self.app = self.getRootFolder()['app'] |
---|
73 | # Set site here. Some of the following setup code might need |
---|
74 | # to access grok.getSite() and should get our new app then |
---|
75 | setSite(app) |
---|
76 | |
---|
77 | # Add student with subobjects (done by addStudent) |
---|
78 | student = Student() |
---|
79 | student.name = u'Anna Tester' |
---|
80 | self.test_student_id = self.app['students'].addStudent(student) |
---|
81 | # Set password |
---|
82 | IUserAccount(self.app['students'][self.test_student_id]).setPassword('spwd') |
---|
83 | |
---|
84 | self.login_path = 'http://localhost/app/login' |
---|
85 | self.container_path = 'http://localhost/app/students' |
---|
86 | self.manage_container_path = self.container_path + '/@@manage' |
---|
87 | self.add_student_path = self.container_path + '/addstudent' |
---|
88 | self.student_path = self.container_path + '/' + self.test_student_id |
---|
89 | self.manage_student_path = self.student_path + '/edit_base' |
---|
90 | self.clearance_student_path = self.student_path + '/view_clearance' |
---|
91 | self.personal_student_path = self.student_path + '/view_personal' |
---|
92 | self.edit_clearance_student_path = self.student_path + '/edit_clearance' |
---|
93 | self.edit_personal_student_path = self.student_path + '/edit_personal' |
---|
94 | |
---|
95 | self.studycourse_student_path = self.student_path + '/studycourse' |
---|
96 | self.payments_student_path = self.student_path + '/payments' |
---|
97 | self.accommodation_student_path = self.student_path + '/accommodation' |
---|
98 | self.history_student_path = self.student_path + '/history' |
---|
99 | |
---|
100 | # Create 5 access codes with prefix'PWD' and cost 9.99 each |
---|
101 | pin_container = self.app['accesscodes'] |
---|
102 | pin_container.createBatch( |
---|
103 | datetime.now(), 'some_userid', 'PWD', 9.99, 5) |
---|
104 | pins = pin_container[pin_container.keys()[0]].values() |
---|
105 | self.pins = [x.representation for x in pins] |
---|
106 | self.existing_pin = self.pins[0] |
---|
107 | parts = self.existing_pin.split('-')[1:] |
---|
108 | self.existing_series, self.existing_number = parts |
---|
109 | |
---|
110 | # Populate university |
---|
111 | certificate = createObject('waeup.Certificate') |
---|
112 | certificate.code = 'CERT1' |
---|
113 | certificate.application_category = 'basic' |
---|
114 | self.app['faculties']['fac1'] = Faculty() |
---|
115 | self.app['faculties']['fac1']['dep1'] = Department() |
---|
116 | self.app['faculties']['fac1']['dep1'].certificates.addCertificate( |
---|
117 | certificate) |
---|
118 | |
---|
119 | # Put the prepopulated site into test ZODB and prepare test |
---|
120 | # browser |
---|
121 | self.browser = Browser() |
---|
122 | self.browser.handleErrors = False |
---|
123 | |
---|
124 | def tearDown(self): |
---|
125 | super(StudentsFullSetup, self).tearDown() |
---|
126 | clearSite() |
---|
127 | shutil.rmtree(self.dc_root) |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | class StudentsContainerUITests(StudentsFullSetup): |
---|
132 | # Tests for StudentsContainer class views and pages |
---|
133 | |
---|
134 | layer = FunctionalLayer |
---|
135 | |
---|
136 | def test_anonymous_access(self): |
---|
137 | # Anonymous users can't access students containers |
---|
138 | self.assertRaises( |
---|
139 | Unauthorized, self.browser.open, self.container_path) |
---|
140 | self.assertRaises( |
---|
141 | Unauthorized, self.browser.open, self.manage_container_path) |
---|
142 | return |
---|
143 | |
---|
144 | def test_manage_access(self): |
---|
145 | # Managers can access the view page of students |
---|
146 | # containers and can perform actions |
---|
147 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
148 | self.browser.open(self.container_path) |
---|
149 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
150 | self.assertEqual(self.browser.url, self.container_path) |
---|
151 | self.browser.getLink("Manage student section").click() |
---|
152 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
153 | self.assertEqual(self.browser.url, self.manage_container_path) |
---|
154 | return |
---|
155 | |
---|
156 | def test_add_search_delete_students(self): |
---|
157 | # Managers can add search and remove students |
---|
158 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
159 | self.browser.open(self.manage_container_path) |
---|
160 | self.browser.getLink("Add student").click() |
---|
161 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
162 | self.assertEqual(self.browser.url, self.add_student_path) |
---|
163 | self.browser.getControl(name="form.name").value = 'Bob Tester' |
---|
164 | self.browser.getControl("Create student record").click() |
---|
165 | self.assertTrue('Student record created' in self.browser.contents) |
---|
166 | |
---|
167 | self.browser.open(self.container_path) |
---|
168 | self.browser.getControl("Search").click() |
---|
169 | self.assertTrue('Empty search string' in self.browser.contents) |
---|
170 | self.browser.getControl(name="searchtype").value = ['student_id'] |
---|
171 | self.browser.getControl(name="searchterm").value = self.test_student_id |
---|
172 | self.browser.getControl("Search").click() |
---|
173 | self.assertTrue('Anna Tester' in self.browser.contents) |
---|
174 | |
---|
175 | self.browser.open(self.manage_container_path) |
---|
176 | self.browser.getControl("Search").click() |
---|
177 | self.assertTrue('Empty search string' in self.browser.contents) |
---|
178 | self.browser.getControl(name="searchtype").value = ['name'] |
---|
179 | self.browser.getControl(name="searchterm").value = 'Anna Tester' |
---|
180 | self.browser.getControl("Search").click() |
---|
181 | self.assertTrue('Anna Tester' in self.browser.contents) |
---|
182 | # The old searchterm will be used again |
---|
183 | self.browser.getControl("Search").click() |
---|
184 | self.assertTrue('Anna Tester' in self.browser.contents) |
---|
185 | |
---|
186 | ctrl = self.browser.getControl(name='entries') |
---|
187 | ctrl.getControl(value=self.test_student_id).selected = True |
---|
188 | self.browser.getControl("Remove selected", index=0).click() |
---|
189 | self.assertTrue('Successfully removed' in self.browser.contents) |
---|
190 | self.browser.getControl(name="searchtype").value = ['student_id'] |
---|
191 | self.browser.getControl(name="searchterm").value = self.test_student_id |
---|
192 | self.browser.getControl("Search").click() |
---|
193 | self.assertTrue('No student found' in self.browser.contents) |
---|
194 | |
---|
195 | self.browser.open(self.container_path) |
---|
196 | self.browser.getControl(name="searchtype").value = ['student_id'] |
---|
197 | self.browser.getControl(name="searchterm").value = self.test_student_id |
---|
198 | self.browser.getControl("Search").click() |
---|
199 | self.assertTrue('No student found' in self.browser.contents) |
---|
200 | |
---|
201 | class StudentUITests(StudentsFullSetup): |
---|
202 | # Tests for Student class views and pages |
---|
203 | |
---|
204 | layer = FunctionalLayer |
---|
205 | |
---|
206 | def test_manage_access(self): |
---|
207 | # Managers can access the pages of students |
---|
208 | # and can perform actions |
---|
209 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
210 | |
---|
211 | self.browser.open(self.student_path) |
---|
212 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
213 | self.assertEqual(self.browser.url, self.student_path) |
---|
214 | self.browser.getLink("Manage").click() |
---|
215 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
216 | self.assertEqual(self.browser.url, self.manage_student_path) |
---|
217 | # Managers can edit base data and fire transitions |
---|
218 | self.browser.getControl(name="transition").value = ['admit'] |
---|
219 | self.browser.getControl(name="form.name").value = 'John Tester' |
---|
220 | self.browser.getControl(name="form.reg_number").value = '123' |
---|
221 | self.browser.getControl(name="password").value = 'secret' |
---|
222 | self.browser.getControl(name="control_password").value = 'secret' |
---|
223 | self.browser.getControl("Save").click() |
---|
224 | self.assertMatches('...Form has been saved...', |
---|
225 | self.browser.contents) |
---|
226 | self.browser.open(self.student_path) |
---|
227 | self.browser.getLink("Clearance Data").click() |
---|
228 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
229 | self.assertEqual(self.browser.url, self.clearance_student_path) |
---|
230 | self.browser.getLink("Manage").click() |
---|
231 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
232 | self.assertEqual(self.browser.url, self.edit_clearance_student_path) |
---|
233 | |
---|
234 | self.browser.open(self.student_path) |
---|
235 | self.browser.getLink("Personal Data").click() |
---|
236 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
237 | self.assertEqual(self.browser.url, self.personal_student_path) |
---|
238 | self.browser.getLink("Manage").click() |
---|
239 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
240 | self.assertEqual(self.browser.url, self.edit_personal_student_path) |
---|
241 | |
---|
242 | self.browser.open(self.student_path) |
---|
243 | self.browser.getLink("Study Course").click() |
---|
244 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
245 | self.assertEqual(self.browser.url, self.studycourse_student_path) |
---|
246 | self.browser.getLink("Manage").click() |
---|
247 | self.assertTrue('Manage study course' in self.browser.contents) |
---|
248 | |
---|
249 | self.browser.open(self.student_path) |
---|
250 | self.browser.getLink("Payments").click() |
---|
251 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
252 | self.assertEqual(self.browser.url, self.payments_student_path) |
---|
253 | |
---|
254 | self.browser.open(self.student_path) |
---|
255 | self.browser.getLink("Accommodation").click() |
---|
256 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
257 | self.assertEqual(self.browser.url, self.accommodation_student_path) |
---|
258 | |
---|
259 | self.browser.open(self.student_path) |
---|
260 | self.browser.getLink("History").click() |
---|
261 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
262 | self.assertEqual(self.browser.url, self.history_student_path) |
---|
263 | self.assertMatches('...Student admitted by zope.mgr...', |
---|
264 | self.browser.contents) |
---|
265 | return |
---|
266 | |
---|
267 | def test_student_access(self): |
---|
268 | # Students can access their own objects |
---|
269 | # and can perform actions |
---|
270 | remove_new_loggers(self.functional_old_loggers) |
---|
271 | self.browser.open(self.login_path) |
---|
272 | self.browser.getControl(name="form.login").value = self.test_student_id |
---|
273 | self.browser.getControl(name="form.password").value = 'spwd' |
---|
274 | self.browser.getControl("Login").click() |
---|
275 | self.assertEqual(self.browser.url, self.student_path) |
---|
276 | self.assertTrue('You logged in' in self.browser.contents) |
---|
277 | # Change password |
---|
278 | self.browser.getLink("Change password").click() |
---|
279 | self.browser.getControl(name="password").value = 'new_password' |
---|
280 | self.browser.getControl(name="control_password").value = 'new_password' |
---|
281 | self.browser.getControl("Save").click() |
---|
282 | self.assertTrue('Form has been saved' in self.browser.contents) |
---|
283 | # We are still logged in. Changing the password hasn't thrown us out. |
---|
284 | self.browser.getLink("My Data").click() |
---|
285 | self.assertEqual(self.browser.url, self.student_path) |
---|
286 | # We can logout |
---|
287 | self.browser.getLink("Logout").click() |
---|
288 | self.assertTrue('You have been logged out' in self.browser.contents) |
---|
289 | self.assertEqual(self.browser.url, 'http://localhost/app') |
---|
290 | # We can login again with the new password |
---|
291 | self.browser.getLink("Login").click() |
---|
292 | self.browser.open(self.login_path) |
---|
293 | self.browser.getControl(name="form.login").value = self.test_student_id |
---|
294 | self.browser.getControl(name="password").value = 'new_password' |
---|
295 | self.browser.getControl("Login").click() |
---|
296 | self.assertEqual(self.browser.url, self.student_path) |
---|
297 | self.assertTrue('You logged in' in self.browser.contents) |
---|
298 | return |
---|
299 | |
---|
300 | def test_setpassword(self): |
---|
301 | # Set password for first-time access |
---|
302 | # Add student with subobjects (done by addStudent) |
---|
303 | student = Student() |
---|
304 | student.reg_number = u'123456' |
---|
305 | student.name = u'Klaus Tester' |
---|
306 | test_student_id = self.app['students'].addStudent(student) |
---|
307 | setpassword_path = 'http://localhost/app/setpassword' |
---|
308 | student_path = 'http://localhost/app/students/%s' % test_student_id |
---|
309 | self.browser.open(setpassword_path) |
---|
310 | self.browser.getControl(name="form.acseries").value = self.existing_series |
---|
311 | self.browser.getControl(name="form.acnumber").value = self.existing_number |
---|
312 | self.browser.getControl(name="form.reg_number").value = '123456' |
---|
313 | self.browser.getControl("Show").click() |
---|
314 | self.assertMatches('...Password has been set. Your Student Id is...', |
---|
315 | self.browser.contents) |
---|
316 | # The student can login with the new credentials |
---|
317 | self.browser.open(self.login_path) |
---|
318 | self.browser.getControl(name="form.login").value = test_student_id |
---|
319 | self.browser.getControl(name="form.password").value = self.existing_number |
---|
320 | self.browser.getControl("Login").click() |
---|
321 | self.assertEqual(self.browser.url, student_path) |
---|
322 | self.assertTrue('You logged in' in self.browser.contents) |
---|
323 | return |
---|