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

Last change on this file since 15947 was 15947, checked in by Henrik Bettermann, 5 years ago

Remove redundant components and make adjustments to base package.

  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
1## $Id: test_browser.py 15947 2020-01-23 14:35:44Z 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 test_application_slip(self):
88        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
89        self.slip_path = self.view_path + '/application_slip.pdf'
90        self.browser.open(self.manage_path)
91        self.assertEqual(self.browser.headers['Status'], '200 Ok')
92        self.fill_correct_values()
93        self.browser.getControl("Save").click()
94        IWorkflowState(self.applicant).setState('submitted')
95        self.browser.open(self.manage_path)
96        self.browser.getLink("Download application slip").click()
97        self.assertEqual(self.browser.headers['Status'], '200 Ok')
98        self.assertEqual(self.browser.headers['Content-Type'],
99                         'application/pdf')
100        path = os.path.join(samples_dir(), 'application_slip.pdf')
101        open(path, 'wb').write(self.browser.contents)
102        print "Sample application_slip.pdf written to %s" % path
103
104    def test_upload_res_stat_by_manager(self):
105        # Add trans applicants container
106        self.transcontainer = ApplicantsContainer()
107        self.transcontainer.mode = 'create'
108        self.transcontainer.code = u'ug%s' % session_1
109        self.transcontainer.prefix = u'ug'
110        self.transcontainer.application_category = u'no'
111        self.transcontainer.year = session_1
112        self.transcontainer.application_fee = 300.0
113        self.transcontainer.title = u'This is the ug%s container' % session_1
114        self.app['applicants'][self.transcontainer.code] = self.transcontainer
115        delta = datetime.timedelta(days=10)
116        self.transcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
117        self.transcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
118        # Add applicant
119        transapplicant = createObject(u'waeup.Applicant')
120        transapplicant.firstname = u'Anna'
121        transapplicant.lastname = u'Post'
122        transapplicant.subtype = 'transfer'
123        self.app['applicants'][self.transcontainer.code].addApplicant(transapplicant)
124        self.transapplicant = self.app['applicants'][self.transcontainer.code][
125            transapplicant.application_number]
126        self.transapplicant_view_path = ('http://localhost/app/applicants/ug%s/%s'
127            % (session_1, transapplicant.application_number))
128        self.transapplicant_manage_path = ('http://localhost/app/applicants/ug%s/%s/manage'
129            % (session_1, transapplicant.application_number))
130        # Login
131        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
132        self.browser.open(self.transapplicant_manage_path)
133        # Create a pseudo file with acceptable size
134        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
135        pseudo_pdf = StringIO(pdf_content)
136        ctrl = self.browser.getControl(name='res_stat.pdf')
137        file_ctrl = ctrl.mech_control
138        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
139        self.browser.getControl("Save").click() # submit form
140        # Even though the form could not be saved ...
141        self.assertTrue('Required input is missing' in self.browser.contents)
142        # ... the file has been successfully uploaded
143        pdf_url = self.transapplicant_manage_path.replace('manage', 'res_stat.pdf')
144        self.browser.open(pdf_url)
145        self.assertEqual(
146            self.browser.headers['content-type'], 'application/pdf')
147        self.assertEqual(len(self.browser.contents), 307200)
148        # There is really a file stored for the applicant
149        storage = getUtility(IExtFileStore)
150        file_id = IFileStoreNameChooser(self.transapplicant).chooseName(
151            attr='res_stat.pdf')
152        # The stored file can be fetched
153        fd = storage.getFile(file_id)
154        file_len = len(fd.read())
155        self.assertEqual(file_len, 307200)
156        # A file link is displayed on the edit view ...
157        self.browser.open(self.transapplicant_manage_path)
158        self.assertTrue('<a href="res_stat.pdf">' in self.browser.contents)
159        # ... and on the dislay view
160        self.browser.open(self.transapplicant_view_path)
161        self.assertTrue('res_stat.pdf">Statement of Result</a>'
162            in self.browser.contents)
163        # Adding file is properly logged
164        logfile = os.path.join(
165            self.app['datacenter'].storage, 'logs', 'applicants.log')
166        logcontent = open(logfile).read()
167        self.assertTrue(
168            'zope.mgr - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage'
169            ' - %s - saved: res_stat.pdf'
170            % (self.transapplicant.applicant_id)
171            in logcontent)
172        # When an applicant is removed, also the pdf files are gone.
173        del self.app['applicants'][self.transcontainer.code][self.transapplicant.application_number]
174        fd = storage.getFile(file_id)
175        self.assertTrue(fd is None)
Note: See TracBrowser for help on using the repository browser.