source: main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/applicants/tests/test_browser.py @ 16546

Last change on this file since 16546 was 16546, checked in by Henrik Bettermann, 3 years ago

Adjust to changes in base package.

  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
1## $Id: test_browser.py 16546 2021-07-13 16:05:29Z henrik $
2##
3## Copyright (C) 2013 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"""
19Test the applicant-related UI components.
20"""
21import grok
22import datetime
23import pytz
24import os
25from StringIO import StringIO
26from zope.event import notify
27from zope.component import createObject, getUtility
28from zope.catalog.interfaces import ICatalog
29from zope.intid.interfaces import IIntIds
30from hurry.workflow.interfaces import IWorkflowState
31from zope.component import createObject, getUtility
32from waeup.kofa.configuration import SessionConfiguration
33from waeup.kofa.interfaces import (
34    IUserAccount, IExtFileStore, IFileStoreNameChooser)
35from kofacustom.iuokada.testing import FunctionalLayer
36from waeup.kofa.applicants.container import ApplicantsContainer
37from waeup.kofa.browser.tests.test_pdf import samples_dir
38from waeup.kofa.applicants.tests.test_browser import (ApplicantsFullSetup,
39    ApplicantsFullSetup, container_name_1, session_1)
40from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup
41from kofacustom.iuokada.applicants.export import CustomApplicantExporter
42from kofacustom.iuokada.applicants.batching import CustomApplicantProcessor
43
44SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
45
46class CustomApplicantUITests(ApplicantsFullSetup):
47    # Tests for uploading/browsing the passport image of appplicants
48
49    layer = FunctionalLayer
50
51class ApplicantExporterTest(ApplicantImportExportSetup):
52
53    layer = FunctionalLayer
54
55    def setUp(self):
56        super(ApplicantExporterTest, self).setUp()
57        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
58        self.cat = getUtility(ICatalog, name='applicants_catalog')
59        self.intids = getUtility(IIntIds)
60        return
61
62    def setup_applicant(self, applicant):
63        # set predictable values for `applicant`
64        applicant.reg_number = u'123456'
65        applicant.applicant_id = u'dp2011_654321'
66        applicant.firstname = u'Anna'
67        applicant.lastname = u'Tester'
68        applicant.middlename = u'M.'
69        applicant.nationality = u'NG'
70        applicant.date_of_birth = datetime.date(1981, 2, 4)
71        applicant.sex = 'f'
72        applicant.email = 'anna@sample.com'
73        applicant.phone = u'+234-123-12345'
74        applicant.course1 = self.certificate
75        applicant.course2 = self.certificate
76        applicant.course_admitted = self.certificate
77        applicant.notice = u'Some notice\nin lines.'
78        applicant.screening_score = 98
79        applicant.screening_venue = u'Exam Room'
80        applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM'
81        applicant.password = 'any password'
82        return applicant
83
84class ApplicantsContainerUITests(ApplicantsFullSetup):
85    # Tests for ApplicantsContainer class views and pages
86
87    layer = FunctionalLayer
88
89    def fill_correct_values(self):
90        # Fill the edit form with suitable values
91        self.browser.getControl(name="form.firstname").value = 'John'
92        self.browser.getControl(name="form.middlename").value = 'Anthony'
93        self.browser.getControl(name="form.lastname").value = 'Tester'
94        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
95        self.browser.getControl(name="form.sex").value = ['m']
96        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
97        self.browser.getControl(name="form.perm_address").value = 'Adresse'
98
99    def test_manage_application(self):
100        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
101        self.slip_path = self.view_path + '/application_slip.pdf'
102        self.browser.open(self.manage_path)
103        self.assertEqual(self.browser.headers['Status'], '200 Ok')
104        self.fill_correct_values()
105        self.browser.getControl(name="custom.course1").value = ['']
106        self.browser.getControl("Save").click()
107        self.assertTrue('Please select your 1st Choice Course of Study.'
108            in self.browser.contents)
109        self.browser.getControl(name="custom.course1").value = ['CERT1']
110        self.browser.getControl("Save").click()
111        self.assertEqual(self.applicant.course1, self.certificate)
112        self.assertEqual(self.applicant.course2, None)
113        IWorkflowState(self.applicant).setState('submitted')
114        self.browser.open(self.view_path)
115        self.browser.getLink("Download application slip").click()
116        self.assertEqual(self.browser.headers['Status'], '200 Ok')
117        self.assertEqual(self.browser.headers['Content-Type'],
118                         'application/pdf')
119        path = os.path.join(samples_dir(), 'application_slip.pdf')
120        open(path, 'wb').write(self.browser.contents)
121        print "Sample application_slip.pdf written to %s" % path
122        # Log entries have been added (course1 is missing because it was already stored)
123        logfile = os.path.join(
124            self.app['datacenter'].storage, 'logs', 'applicants.log')
125        logcontent = open(logfile).read()
126        self.assertTrue(
127            'zope.mgr - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage - '
128            '%s - saved: firstname + middlename + lastname + date_of_birth '
129            '+ sex + email'
130            % (self.applicant.applicant_id)
131            in logcontent)
132
133    def test_upload_res_stat_by_manager(self):
134        # Add trans applicants container
135        self.transcontainer = ApplicantsContainer()
136        self.transcontainer.mode = 'create'
137        self.transcontainer.code = u'ug%s' % session_1
138        self.transcontainer.prefix = u'ug'
139        self.transcontainer.application_category = u'no'
140        self.transcontainer.year = session_1
141        self.transcontainer.application_fee = 300.0
142        self.transcontainer.title = u'This is the ug%s container' % session_1
143        self.app['applicants'][self.transcontainer.code] = self.transcontainer
144        delta = datetime.timedelta(days=10)
145        self.transcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
146        self.transcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
147        # Add applicant
148        transapplicant = createObject(u'waeup.Applicant')
149        transapplicant.firstname = u'Anna'
150        transapplicant.lastname = u'Post'
151        transapplicant.subtype = 'transfer'
152        self.app['applicants'][self.transcontainer.code].addApplicant(transapplicant)
153        self.transapplicant = self.app['applicants'][self.transcontainer.code][
154            transapplicant.application_number]
155        self.transapplicant_view_path = ('http://localhost/app/applicants/ug%s/%s'
156            % (session_1, transapplicant.application_number))
157        self.transapplicant_manage_path = ('http://localhost/app/applicants/ug%s/%s/manage'
158            % (session_1, transapplicant.application_number))
159        # Login
160        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
161        self.browser.open(self.transapplicant_manage_path)
162        # Create a pseudo file with acceptable size
163        #pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
164        #pseudo_pdf = StringIO(pdf_content)
165        image = open(SAMPLE_IMAGE, 'rb')
166        ctrl = self.browser.getControl(name='res_stat')
167        file_ctrl = ctrl.mech_control
168        file_ctrl.add_file(image, filename='my_scan.jpg')
169        self.browser.getControl("Save").click() # submit form
170        # Even though the form could not be saved ...
171        self.assertTrue('Required input is missing' in self.browser.contents)
172        # ... the file has been successfully uploaded
173        image_url = self.transapplicant_manage_path.replace('manage', 'res_stat')
174        self.browser.open(image_url)
175        self.assertEqual(
176            self.browser.headers['content-type'], 'image/jpeg')
177        self.assertEqual(len(self.browser.contents), 2787)
178        # There is really a file stored for the applicant
179        storage = getUtility(IExtFileStore)
180        file_id = IFileStoreNameChooser(self.transapplicant).chooseName(
181            attr='res_stat.pdf')
182        # The stored file can be fetched
183        fd = storage.getFile(file_id)
184        file_len = len(fd.read())
185        self.assertEqual(file_len, 2787)
186        # A file link is displayed on the edit view ...
187        self.browser.open(self.transapplicant_manage_path)
188        self.assertTrue('<a href="res_stat">' in self.browser.contents)
189        # ... and on the dislay view
190        self.browser.open(self.transapplicant_view_path)
191        self.assertTrue('res_stat">Statement of Result</a>'
192            in self.browser.contents)
193        # Adding file is properly logged
194        logfile = os.path.join(
195            self.app['datacenter'].storage, 'logs', 'applicants.log')
196        logcontent = open(logfile).read()
197        self.assertTrue(
198            'zope.mgr - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage'
199            ' - %s - saved: res_stat'
200            % (self.transapplicant.applicant_id)
201            in logcontent)
202        # When an applicant is removed, also the pdf files are gone.
203        del self.app['applicants'][self.transcontainer.code][self.transapplicant.application_number]
204        fd = storage.getFile(file_id)
205        self.assertTrue(fd is None)
Note: See TracBrowser for help on using the repository browser.