1 | ## $Id: tests.py 8583 2012-05-31 20:34:23Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 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 | import os |
---|
19 | import shutil |
---|
20 | import tempfile |
---|
21 | import datetime |
---|
22 | from zope.intid.interfaces import IIntIds |
---|
23 | from zope.interface.verify import verifyClass, verifyObject |
---|
24 | from zope.component.hooks import setSite, clearSite |
---|
25 | from zope.component import createObject, getUtility |
---|
26 | from zope.catalog.interfaces import ICatalog |
---|
27 | from zope.testbrowser.testing import Browser |
---|
28 | from waeup.kofa.app import University |
---|
29 | from waeup.kofa.university.faculty import Faculty |
---|
30 | from waeup.kofa.university.department import Department |
---|
31 | from waeup.kofa.testing import FunctionalTestCase |
---|
32 | from waeup.kofa.configuration import SessionConfiguration |
---|
33 | from waeup.kofa.applicants.container import ApplicantsContainer |
---|
34 | from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup |
---|
35 | from waeup.kofa.interfaces import IBatchProcessor |
---|
36 | from waeup.aaue.testing import FunctionalLayer |
---|
37 | from waeup.aaue.applicants.export import CustomApplicantsExporter |
---|
38 | from waeup.aaue.applicants.batching import CustomApplicantProcessor |
---|
39 | |
---|
40 | |
---|
41 | class ApplicantUITest(FunctionalTestCase): |
---|
42 | """Perform some browser tests. |
---|
43 | """ |
---|
44 | layer = FunctionalLayer |
---|
45 | |
---|
46 | def setUp(self): |
---|
47 | super(ApplicantUITest, self).setUp() |
---|
48 | # Setup a sample site for each test |
---|
49 | app = University() |
---|
50 | self.dc_root = tempfile.mkdtemp() |
---|
51 | app['datacenter'].setStoragePath(self.dc_root) |
---|
52 | |
---|
53 | # Prepopulate the ZODB... |
---|
54 | self.getRootFolder()['app'] = app |
---|
55 | # we add the site immediately after creation to the |
---|
56 | # ZODB. Catalogs and other local utilities are not setup |
---|
57 | # before that step. |
---|
58 | self.app = self.getRootFolder()['app'] |
---|
59 | # Set site here. Some of the following setup code might need |
---|
60 | # to access grok.getSite() and should get our new app then |
---|
61 | setSite(app) |
---|
62 | |
---|
63 | # Add an two different applicants containers |
---|
64 | self.pgcontainer = ApplicantsContainer() |
---|
65 | self.pgcontainer.code = u'pgft2011' |
---|
66 | self.pgcontainer.prefix = u'pgft' |
---|
67 | self.pgcontainer.year = 2011 |
---|
68 | self.pgcontainer.application_fee = 300.0 |
---|
69 | self.pgcontainer.title = u'This is the pgft2011 container' |
---|
70 | self.app['applicants']['pgft2011'] = self.pgcontainer |
---|
71 | self.ugcontainer = ApplicantsContainer() |
---|
72 | self.ugcontainer.code = u'putme2011' |
---|
73 | self.ugcontainer.prefix = u'putme' |
---|
74 | self.ugcontainer.year = 2011 |
---|
75 | self.ugcontainer.application_fee = 200.0 |
---|
76 | self.ugcontainer.title = u'This is the app2011 container' |
---|
77 | self.app['applicants']['app2011'] = self.ugcontainer |
---|
78 | |
---|
79 | # Populate university |
---|
80 | self.certificate = createObject('waeup.Certificate') |
---|
81 | self.certificate.code = 'CERT1' |
---|
82 | self.certificate.application_category = 'basic' |
---|
83 | self.certificate.start_level = 100 |
---|
84 | self.certificate.end_level = 500 |
---|
85 | self.app['faculties']['fac1'] = Faculty() |
---|
86 | self.app['faculties']['fac1']['dep1'] = Department() |
---|
87 | self.app['faculties']['fac1']['dep1'].certificates.addCertificate( |
---|
88 | self.certificate) |
---|
89 | |
---|
90 | # Add (customized) applicants |
---|
91 | pgapplicant = createObject(u'waeup.Applicant') |
---|
92 | pgapplicant.firstname = u'Anna' |
---|
93 | pgapplicant.lastname = u'Post' |
---|
94 | self.app['applicants']['pgft2011'].addApplicant(pgapplicant) |
---|
95 | self.pgapplication_number = pgapplicant.application_number |
---|
96 | self.pgapplicant = self.app['applicants']['pgft2011'][ |
---|
97 | self.pgapplication_number] |
---|
98 | |
---|
99 | ugapplicant = createObject(u'waeup.Applicant') |
---|
100 | ugapplicant.firstname = u'Klaus' |
---|
101 | ugapplicant.lastname = u'Under' |
---|
102 | self.app['applicants']['app2011'].addApplicant(ugapplicant) |
---|
103 | self.ugapplication_number = ugapplicant.application_number |
---|
104 | self.ugapplicant = self.app['applicants']['app2011'][ |
---|
105 | self.ugapplication_number] |
---|
106 | self.pgapplicant_path = ('http://localhost/app/applicants/pgft2011/%s' |
---|
107 | % self.pgapplication_number) |
---|
108 | self.ugapplicant_path = ('http://localhost/app/applicants/app2011/%s' |
---|
109 | % self.ugapplication_number) |
---|
110 | |
---|
111 | self.browser = Browser() |
---|
112 | self.browser.handleErrors = False |
---|
113 | |
---|
114 | def tearDown(self): |
---|
115 | super(ApplicantUITest, self).tearDown() |
---|
116 | shutil.rmtree(self.dc_root) |
---|
117 | clearSite() |
---|
118 | return |
---|
119 | |
---|
120 | def fill_correct_values(self): |
---|
121 | self.browser.getControl(name="form.reg_number").value = '1234' |
---|
122 | self.browser.getControl(name="form.firstname").value = 'John' |
---|
123 | self.browser.getControl(name="form.lastname").value = 'Tester' |
---|
124 | self.browser.getControl(name="form.course1").value = ['CERT1'] |
---|
125 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
126 | self.browser.getControl(name="form.lga").value = ['foreigner'] |
---|
127 | self.browser.getControl(name="form.sex").value = ['m'] |
---|
128 | self.browser.getControl(name="form.email").value = 'xx@yy.zz' |
---|
129 | |
---|
130 | def test_manage_and_view_applicant(self): |
---|
131 | # Managers can manage pg applicants |
---|
132 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
133 | # The IPGApplicant interface is really used in all pages |
---|
134 | self.browser.open(self.pgapplicant_path) |
---|
135 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
136 | self.assertTrue('Employer' in self.browser.contents) |
---|
137 | self.browser.open(self.pgapplicant_path + '/manage') |
---|
138 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
139 | self.assertTrue('Employer' in self.browser.contents) |
---|
140 | self.browser.open(self.pgapplicant_path + '/edit') |
---|
141 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
142 | self.assertTrue('Employer' in self.browser.contents) |
---|
143 | self.browser.open(self.pgapplicant_path + '/application_slip.pdf') |
---|
144 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
145 | # If we view the applicant in the ug container, |
---|
146 | # the employer field doesn't appear |
---|
147 | self.browser.open(self.ugapplicant_path) |
---|
148 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
149 | self.assertFalse('Employer' in self.browser.contents) |
---|
150 | self.browser.open(self.ugapplicant_path + '/manage') |
---|
151 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
152 | # We can save the applicant |
---|
153 | self.fill_correct_values() |
---|
154 | self.browser.getControl("Save").click() |
---|
155 | self.assertMatches('...Form has been saved...', self.browser.contents) |
---|
156 | self.assertFalse('Employer' in self.browser.contents) |
---|
157 | self.browser.open(self.ugapplicant_path + '/edit') |
---|
158 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
159 | self.assertFalse('Employer' in self.browser.contents) |
---|
160 | self.browser.open(self.ugapplicant_path + '/application_slip.pdf') |
---|
161 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
162 | return |
---|
163 | |
---|
164 | def test_application_payment(self): |
---|
165 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
166 | |
---|
167 | # UG (UTME) applicant |
---|
168 | self.browser.open(self.ugapplicant_path) |
---|
169 | self.browser.open(self.ugapplicant_path + '/manage') |
---|
170 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
171 | self.fill_correct_values() |
---|
172 | self.browser.getControl("Save").click() |
---|
173 | self.assertMatches('...Form has been saved...', self.browser.contents) |
---|
174 | self.fill_correct_values() |
---|
175 | self.browser.getControl("Save").click() |
---|
176 | self.browser.getControl("Add online payment ticket").click() |
---|
177 | self.assertMatches('...Payment ticket created...', |
---|
178 | self.browser.contents) |
---|
179 | self.assertMatches('...Amount Authorized...', |
---|
180 | self.browser.contents) |
---|
181 | payment_id = self.ugapplicant.keys()[0] |
---|
182 | payment = self.ugapplicant[payment_id] |
---|
183 | self.assertEqual(payment.p_item,'This is the app2011 container') |
---|
184 | self.assertEqual(payment.p_session,2011) |
---|
185 | self.assertEqual(payment.p_category,'application') |
---|
186 | self.assertEqual(payment.amount_auth, 200.0) |
---|
187 | |
---|
188 | # PG applicants pay more |
---|
189 | self.browser.open(self.pgapplicant_path) |
---|
190 | self.browser.open(self.pgapplicant_path + '/manage') |
---|
191 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
192 | self.fill_correct_values() |
---|
193 | self.browser.getControl(name="form.reg_number").value = '2345' |
---|
194 | self.browser.getControl(name="form.firstname").value = 'Anna' |
---|
195 | self.browser.getControl("Save").click() |
---|
196 | self.assertMatches('...Form has been saved...', self.browser.contents) |
---|
197 | self.browser.getControl("Add online payment ticket").click() |
---|
198 | self.assertMatches('...Payment ticket created...', |
---|
199 | self.browser.contents) |
---|
200 | self.assertMatches('...Amount Authorized...', |
---|
201 | self.browser.contents) |
---|
202 | payment_id = self.pgapplicant.keys()[0] |
---|
203 | payment = self.pgapplicant[payment_id] |
---|
204 | self.assertEqual(payment.p_item,'This is the pgft2011 container') |
---|
205 | self.assertEqual(payment.p_session,2011) |
---|
206 | self.assertEqual(payment.p_category,'application') |
---|
207 | self.assertEqual(payment.amount_auth, 300.0) |
---|
208 | return |
---|
209 | |
---|
210 | class ApplicantsExporterTest(ApplicantImportExportSetup): |
---|
211 | |
---|
212 | layer = FunctionalLayer |
---|
213 | |
---|
214 | def setUp(self): |
---|
215 | super(ApplicantsExporterTest, self).setUp() |
---|
216 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
217 | self.cat = getUtility(ICatalog, name='applicants_catalog') |
---|
218 | self.intids = getUtility(IIntIds) |
---|
219 | return |
---|
220 | |
---|
221 | def setup_applicant(self, applicant): |
---|
222 | # set predictable values for `applicant` |
---|
223 | applicant.reg_number = u'123456' |
---|
224 | applicant.applicant_id = u'dp2011_654321' |
---|
225 | applicant.firstname = u'Anna' |
---|
226 | applicant.lastname = u'Tester' |
---|
227 | applicant.middlename = u'M.' |
---|
228 | applicant.date_of_birth = datetime.date(1981, 2, 4) |
---|
229 | applicant.sex = 'f' |
---|
230 | applicant.email = 'anna@sample.com' |
---|
231 | applicant.phone = u'+234-123-12345' |
---|
232 | applicant.course1 = self.certificate |
---|
233 | applicant.course2 = self.certificate |
---|
234 | applicant.course_admitted = self.certificate |
---|
235 | applicant.notice = u'Some notice\nin lines.' |
---|
236 | applicant.screening_score = 98 |
---|
237 | applicant.screening_venue = u'Exam Room' |
---|
238 | applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM' |
---|
239 | applicant.password = 'any password' |
---|
240 | return applicant |
---|
241 | |
---|
242 | def test_export_reimport_all(self): |
---|
243 | # we can export all applicants in a portal |
---|
244 | # set values we can expect in export file |
---|
245 | self.applicant = self.setup_applicant(self.applicant) |
---|
246 | exporter = CustomApplicantsExporter() |
---|
247 | exporter.export_all(self.app, self.outfile) |
---|
248 | result = open(self.outfile, 'rb').read() |
---|
249 | # The exported records do contain a real date in their |
---|
250 | # history dict. We skip the date and split the comparison |
---|
251 | # into two parts. |
---|
252 | self.assertTrue( |
---|
253 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
254 | 'course_admitted,date_of_birth,display_fullname,email,emp2_end,' |
---|
255 | 'emp2_position,emp2_reason,emp2_start,emp_end,emp_position,' |
---|
256 | 'emp_reason,emp_start,employer,employer2,firstname,history,' |
---|
257 | 'hq_degree,hq_disc,hq_matric_no,hq_school,hq_session,hq_type,' |
---|
258 | 'jamb_score,jamb_subjects,lastname,lga,locked,middlename,' |
---|
259 | 'nationality,notice,nysc_lga,' |
---|
260 | 'nysc_year,password,perm_address,phone,pp_school,presently_inst,' |
---|
261 | 'reg_number,screening_date,screening_score,screening_venue,sex,' |
---|
262 | 'state,student_id,' |
---|
263 | 'container_code' |
---|
264 | in result) |
---|
265 | self.assertTrue( |
---|
266 | 'Application initialized by system\'],,,,,,,,,Tester,,0,M.,,' |
---|
267 | '"Some notice\nin lines.",,,any password,,+234-123-12345,,,' |
---|
268 | '123456,"Saturday, 16th June 2012 2:00:00 PM",98,Exam Room,f,' |
---|
269 | 'initialized,,dp2011' in result) |
---|
270 | # We can import the same file with if we ignore some columns. |
---|
271 | # Since the applicants_catalog hasn't been notified, the same |
---|
272 | # record with same reg_number can be imported twice. |
---|
273 | processor = CustomApplicantProcessor() |
---|
274 | result = processor.doImport( |
---|
275 | self.outfile, |
---|
276 | ['ignore1','application_date','ignore2','course1','course2','' |
---|
277 | 'course_admitted','date_of_birth','ignore3','email','emp2_end','' |
---|
278 | 'emp2_position','emp2_reason','emp2_start','emp_end','emp_position','' |
---|
279 | 'emp_reason','emp_start','employer','employer2','firstname','ignore4','' |
---|
280 | 'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type','' |
---|
281 | 'jamb_score','jamb_subjects','lastname','lga','locked','middlename','' |
---|
282 | 'nationality','notice','nysc_lga','' |
---|
283 | 'nysc_year','password','perm_address','phone','pp_school','presently_inst','' |
---|
284 | 'reg_number','screening_date','screening_score','screening_venue','sex','' |
---|
285 | 'state','student_id','container_code'], |
---|
286 | mode='create') |
---|
287 | num_succ, num_fail, finished_path, failed_path = result |
---|
288 | self.assertEqual(num_succ,1) |
---|
289 | self.assertEqual(num_fail,0) |
---|
290 | return |
---|