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

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

Catch error if certificate has been removed between application submission and student creation.

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