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

Last change on this file since 16075 was 16075, checked in by Henrik Bettermann, 4 years ago

Customize saveCourses.

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