source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_applicantcopier.py @ 11482

Last change on this file since 11482 was 11482, checked in by Henrik Bettermann, 11 years ago

'Create student' option removed from 'Application Transition'

select field.

  • Property svn:keywords set to Id
File size: 10.0 KB
Line 
1## $Id: test_applicantcopier.py 11482 2014-03-11 09:10:47Z 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"""
19Tests for the creation of student containers with data from admitted applicants.
20"""
21import os
22import grok
23from datetime import datetime
24from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
25from zope.event import notify
26from zope.component import getUtility
27from zope.i18n import translate
28from waeup.kofa.testing import FunctionalLayer
29from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser
30from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
31
32session = datetime.now().year - 2
33
34class ApplicantCopierFunctionalTests(ApplicantsFullSetup):
35
36    layer = FunctionalLayer
37
38    def setUp(self):
39        super(ApplicantCopierFunctionalTests, self).setUp()
40        return
41
42    def prepare_applicant(self):
43        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
44        self.browser.open(self.manage_path)
45        self.fill_correct_values()
46        self.browser.getControl("Save").click()
47        # Upload a passport picture
48        ctrl = self.browser.getControl(name='form.passport')
49        file_obj = open(
50            os.path.join(os.path.dirname(__file__), 'test_image.jpg'),'rb')
51        file_ctrl = ctrl.mech_control
52        file_ctrl.add_file(file_obj, filename='my_photo.jpg')
53        self.browser.getControl("Save").click() # submit form
54        return
55
56    def test_copier(self):
57        self.prepare_applicant()
58        storage = getUtility(IExtFileStore)
59        file_id = IFileStoreNameChooser(self.applicant).chooseName()
60        # The stored image can be fetched
61        fd = storage.getFile(file_id)
62        file_len_orig = len(fd.read())
63        # Let's try to create the student
64        (success, msg) = self.applicant.createStudent()
65        self.assertTrue(msg == 'Applicant has not yet been admitted.')
66        IWorkflowState(self.applicant).setState('admitted')
67        (success, msg) = self.applicant.createStudent()
68        self.assertTrue(msg == 'No course admitted provided.')
69        self.browser.open(self.manage_path)
70        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
71        self.browser.getControl("Save").click()
72        # Maybe the certificate has meanwhile been removed
73        del self.app['faculties']['fac1']['dep1'].certificates['CERT1']
74        (success, msg) = self.applicant.createStudent()
75        self.assertFalse(success)
76        self.assertTrue('ConstraintNotSatisfied: CERT1' in msg)
77        # Ok, lets add the certificate and try again
78        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
79            self.certificate)
80        # Managers are not allowed to trigger the create transition manually
81        self.assertFalse('<option value="create">' in self.browser.contents)
82        # Student can be created
83        (success, msg) = self.applicant.createStudent()
84        self.assertTrue('created' in msg)
85        # The applicant is locked
86        self.assertTrue(self.applicant.locked)
87        student_id = translate(msg, 'waeup.kofa').split()[1]
88        # View student container just created
89        student = self.app['students'][student_id]
90        student_path = 'http://localhost/app/students/%s' % student_id
91        self.browser.open(student_path)
92        self.assertEqual(self.browser.headers['Status'], '200 Ok')
93        self.assertEqual(self.browser.url, student_path)
94        # Student is in state admitted
95        self.assertEqual(student.state, 'admitted')
96        # Attributes properly set?
97        self.assertEqual(student.email, 'xx@yy.zz')
98        self.assertEqual(student.firstname, 'John')
99        self.assertEqual(student.middlename, 'Anthony')
100        self.assertEqual(student.lastname, 'Tester')
101        # student_id set in application record?
102        self.assertEqual(self.applicant.student_id, student.student_id)
103        # Check if passport image has been copied
104        storage = getUtility(IExtFileStore)
105        file_id = IFileStoreNameChooser(
106            student).chooseName(attr='passport.jpg')
107        fd = storage.getFile(file_id)
108        file_len = len(fd.read())
109        self.assertEqual(file_len_orig, file_len)
110        # Check if application slip exists and is a PDF file
111        file_id = IFileStoreNameChooser(
112            student).chooseName(attr='application_slip.pdf')
113        pdf = storage.getFile(file_id).read()
114        self.assertTrue(len(pdf) > 0)
115        self.assertEqual(pdf[:8], '%PDF-1.4')
116        # Check if there is an application slip link in UI
117        self.assertTrue('Application Slip' in self.browser.contents)
118        self.browser.getLink("Application Slip").click()
119        self.assertEqual(self.browser.headers['Status'], '200 Ok')
120        self.assertEqual(self.browser.headers['Content-Type'],
121                         'application/pdf')
122        # Has the student been properly indexed?
123        # Yes, we can find the student in department
124        self.browser.open('http://localhost/app/students')
125        self.browser.getControl(name="searchtype").value = ['depcode']
126        self.browser.getControl(name="searchterm").value = 'dep1'
127        self.browser.getControl("Find student(s)").click()
128        self.assertMatches('...John Anthony Tester...', self.browser.contents)
129        # Has the student studycourse the correct attributes?
130        self.assertEqual(student['studycourse'].certificate.code, 'CERT1')
131        self.assertEqual(student['studycourse'].entry_session, session)
132        self.assertEqual(student['studycourse'].entry_mode, 'ug_ft')
133        self.assertEqual(student['studycourse'].current_session, session)
134        self.assertEqual(student['studycourse'].current_level, 100)
135
136    def test_batch_copying(self):
137        self.prepare_applicant()
138        IWorkflowState(self.applicant).setState('admitted')
139        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
140        self.browser.getControl("Save").click()
141        self.browser.open(self.manage_container_path)
142        self.browser.getControl("Create students from selected", index=0).click()
143        self.assertTrue('No applicant selected' in self.browser.contents)
144        ctrl = self.browser.getControl(name='val_id')
145        ctrl.getControl(value=self.applicant.application_number).selected = True
146        self.browser.getControl("Create students from selected", index=0).click()
147        self.assertTrue('1 students successfully created' in self.browser.contents)
148
149    def test_hidden_batch_copying_container(self):
150        logfile = os.path.join(
151            self.app['datacenter'].storage, 'logs', 'applicants.log')
152        self.prepare_applicant()
153        self.browser.open(self.container_path + '/createallstudents')
154        self.assertTrue('No student could be created' in self.browser.contents)
155        IWorkflowState(self.applicant).setState('admitted')
156        notify(grok.ObjectModifiedEvent(self.applicant))
157        self.browser.open(self.container_path + '/createallstudents')
158        self.assertTrue('No student could be created' in self.browser.contents)
159        logcontent = open(logfile).read()
160        self.assertTrue('No course admitted provided' in logcontent)
161        self.browser.open(self.manage_path)
162        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
163        self.browser.getControl("Save").click()
164        # date_of_birth is not required for applicants but for students
165        self.applicant.date_of_birth = None
166        self.browser.open(self.container_path + '/createallstudents')
167        self.assertTrue('No student could be created' in self.browser.contents)
168        logcontent = open(logfile).read()
169        self.assertTrue('RequiredMissing: date_of_birth' in logcontent)
170        self.browser.open(self.manage_path)
171        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
172        self.browser.getControl("Save").click()
173        self.browser.open(self.container_path + '/createallstudents')
174        self.assertTrue('1 students successfully created' in self.browser.contents)
175
176    def test_hidden_batch_copying_all(self):
177        logfile = os.path.join(
178            self.app['datacenter'].storage, 'logs', 'applicants.log')
179        self.prepare_applicant()
180        self.browser.open(self.manage_path)
181        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
182        #self.browser.getControl(name="form.course_admitted").value = ['CERT1']
183        self.browser.getControl("Save").click()
184        IWorkflowState(self.applicant).setState('admitted')
185        notify(grok.ObjectModifiedEvent(self.applicant))
186        self.browser.open(self.root_path + '/createallstudents')
187        self.assertTrue('No student could be created' in self.browser.contents)
188        logcontent = open(logfile).read()
189        self.assertTrue('No course admitted provided' in logcontent)
190        self.applicant.course_admitted = self.certificate
191        self.browser.open(self.root_path + '/createallstudents')
192        self.assertTrue('1 students successfully created' in self.browser.contents)
193
194    def test_copier_wo_passport(self):
195        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
196        self.browser.open(self.manage_path)
197        self.fill_correct_values()
198        # Let's try to create the student
199        IWorkflowState(self.applicant).setState('admitted')
200        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
201        self.browser.getControl("Save").click()
202        (success, msg) = self.applicant.createStudent()
203        self.assertTrue('created' in msg)
Note: See TracBrowser for help on using the repository browser.