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

Last change on this file since 9398 was 9398, checked in by Henrik Bettermann, 12 years ago

Lock applicant in handle_applicant_transition_event.

  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1## $Id: test_applicantcopier.py 9398 2012-10-23 19:22:22Z 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        (success, msg) = self.applicant.createStudent()
81        self.assertTrue('created' in msg)
82        # The applicant is locked
83        self.assertTrue(self.applicant.locked)
84        student_id = translate(msg, 'waeup.kofa').split()[1]
85        # View student container just created
86        student = self.app['students'][student_id]
87        student_path = 'http://localhost/app/students/%s' % student_id
88        self.browser.open(student_path)
89        self.assertEqual(self.browser.headers['Status'], '200 Ok')
90        self.assertEqual(self.browser.url, student_path)
91        # Student is in state admitted
92        self.assertEqual(student.state, 'admitted')
93        # Attributes properly set?
94        self.assertEqual(student.email, 'xx@yy.zz')
95        self.assertEqual(student.firstname, 'John')
96        self.assertEqual(student.lastname, 'Tester')
97        # student_id set in application record?
98        self.assertEqual(self.applicant.student_id, student.student_id)
99        # Check if passport image has been copied
100        storage = getUtility(IExtFileStore)
101        file_id = IFileStoreNameChooser(
102            student).chooseName(attr='passport.jpg')
103        fd = storage.getFile(file_id)
104        file_len = len(fd.read())
105        self.assertEqual(file_len_orig, file_len)
106        # Check if application slip exists and is a PDF file
107        file_id = IFileStoreNameChooser(
108            student).chooseName(attr='application_slip.pdf')
109        pdf = storage.getFile(file_id).read()
110        self.assertTrue(len(pdf) > 0)
111        self.assertEqual(pdf[:8], '%PDF-1.4')
112        # Check if there is an application slip link in UI
113        self.assertTrue('Application Slip' in self.browser.contents)
114        self.browser.getLink("Application Slip").click()
115        self.assertEqual(self.browser.headers['Status'], '200 Ok')
116        self.assertEqual(self.browser.headers['Content-Type'],
117                         'application/pdf')
118        # Has the student been properly indexed?
119        # Yes, we can find the student in department
120        self.browser.open('http://localhost/app/students')
121        self.browser.getControl(name="searchtype").value = ['depcode']
122        self.browser.getControl(name="searchterm").value = 'dep1'
123        self.browser.getControl("Search").click()
124        self.assertMatches('...John Tester...', self.browser.contents)
125        # Has the student studycourse the correct attributes?
126        self.assertEqual(student['studycourse'].certificate.code, 'CERT1')
127        self.assertEqual(student['studycourse'].entry_session, session)
128        self.assertEqual(student['studycourse'].entry_mode, 'ug_ft')
129        self.assertEqual(student['studycourse'].current_session, session)
130        self.assertEqual(student['studycourse'].current_level, 100)
131
132    def test_batch_copying(self):
133        self.prepare_applicant()
134        IWorkflowState(self.applicant).setState('admitted')
135        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
136        self.browser.getControl("Save").click()
137        self.browser.open(self.manage_container_path)
138        self.browser.getControl("Create students from selected", index=0).click()
139        self.assertTrue('No applicant selected' in self.browser.contents)
140        ctrl = self.browser.getControl(name='val_id')
141        ctrl.getControl(value=self.applicant.application_number).selected = True
142        self.browser.getControl("Create students from selected", index=0).click()
143        self.assertTrue('1 students successfully created' in self.browser.contents)
144
145    def test_hidden_batch_copying(self):
146        logfile = os.path.join(
147            self.app['datacenter'].storage, 'logs', 'applicants.log')
148        self.prepare_applicant()
149        self.browser.open(self.container_path + '/createallstudents')
150        self.assertTrue('No student could be created' in self.browser.contents)
151        IWorkflowState(self.applicant).setState('admitted')
152        notify(grok.ObjectModifiedEvent(self.applicant))
153        self.browser.open(self.container_path + '/createallstudents')
154        self.assertTrue('No student could be created' in self.browser.contents)
155        logcontent = open(logfile).read()
156        self.assertTrue('No course admitted provided' in logcontent)
157        self.browser.open(self.manage_path)
158        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
159        self.browser.getControl("Save").click()
160        # date_of_birth is not required for applicants but for students
161        self.applicant.date_of_birth = None
162        self.browser.open(self.container_path + '/createallstudents')
163        self.assertTrue('No student could be created' in self.browser.contents)
164        logcontent = open(logfile).read()
165        self.assertTrue('RequiredMissing: date_of_birth' in logcontent)
166        self.browser.open(self.manage_path)
167        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
168        self.browser.getControl("Save").click()
169        self.browser.open(self.container_path + '/createallstudents')
170        self.assertTrue('1 students successfully created' in self.browser.contents)
171
172    def test_copier_wo_passport(self):
173        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
174        self.browser.open(self.manage_path)
175        self.fill_correct_values()
176        # Let's try to create the student
177        IWorkflowState(self.applicant).setState('admitted')
178        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
179        self.browser.getControl("Save").click()
180        (success, msg) = self.applicant.createStudent()
181        self.assertTrue('created' in msg)
Note: See TracBrowser for help on using the repository browser.