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

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

Let's create all students from applicants root.

  • Property svn:keywords set to Id
File size: 9.8 KB
Line 
1## $Id: test_applicantcopier.py 9900 2013-01-17 13:03:55Z 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.middlename, 'Anthony')
97        self.assertEqual(student.lastname, 'Tester')
98        # student_id set in application record?
99        self.assertEqual(self.applicant.student_id, student.student_id)
100        # Check if passport image has been copied
101        storage = getUtility(IExtFileStore)
102        file_id = IFileStoreNameChooser(
103            student).chooseName(attr='passport.jpg')
104        fd = storage.getFile(file_id)
105        file_len = len(fd.read())
106        self.assertEqual(file_len_orig, file_len)
107        # Check if application slip exists and is a PDF file
108        file_id = IFileStoreNameChooser(
109            student).chooseName(attr='application_slip.pdf')
110        pdf = storage.getFile(file_id).read()
111        self.assertTrue(len(pdf) > 0)
112        self.assertEqual(pdf[:8], '%PDF-1.4')
113        # Check if there is an application slip link in UI
114        self.assertTrue('Application Slip' in self.browser.contents)
115        self.browser.getLink("Application Slip").click()
116        self.assertEqual(self.browser.headers['Status'], '200 Ok')
117        self.assertEqual(self.browser.headers['Content-Type'],
118                         'application/pdf')
119        # Has the student been properly indexed?
120        # Yes, we can find the student in department
121        self.browser.open('http://localhost/app/students')
122        self.browser.getControl(name="searchtype").value = ['depcode']
123        self.browser.getControl(name="searchterm").value = 'dep1'
124        self.browser.getControl("Search").click()
125        self.assertMatches('...John Anthony Tester...', self.browser.contents)
126        # Has the student studycourse the correct attributes?
127        self.assertEqual(student['studycourse'].certificate.code, 'CERT1')
128        self.assertEqual(student['studycourse'].entry_session, session)
129        self.assertEqual(student['studycourse'].entry_mode, 'ug_ft')
130        self.assertEqual(student['studycourse'].current_session, session)
131        self.assertEqual(student['studycourse'].current_level, 100)
132
133    def test_batch_copying(self):
134        self.prepare_applicant()
135        IWorkflowState(self.applicant).setState('admitted')
136        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
137        self.browser.getControl("Save").click()
138        self.browser.open(self.manage_container_path)
139        self.browser.getControl("Create students from selected", index=0).click()
140        self.assertTrue('No applicant selected' in self.browser.contents)
141        ctrl = self.browser.getControl(name='val_id')
142        ctrl.getControl(value=self.applicant.application_number).selected = True
143        self.browser.getControl("Create students from selected", index=0).click()
144        self.assertTrue('1 students successfully created' in self.browser.contents)
145
146    def test_hidden_batch_copying_container(self):
147        logfile = os.path.join(
148            self.app['datacenter'].storage, 'logs', 'applicants.log')
149        self.prepare_applicant()
150        self.browser.open(self.container_path + '/createallstudents')
151        self.assertTrue('No student could be created' in self.browser.contents)
152        IWorkflowState(self.applicant).setState('admitted')
153        notify(grok.ObjectModifiedEvent(self.applicant))
154        self.browser.open(self.container_path + '/createallstudents')
155        self.assertTrue('No student could be created' in self.browser.contents)
156        logcontent = open(logfile).read()
157        self.assertTrue('No course admitted provided' in logcontent)
158        self.browser.open(self.manage_path)
159        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
160        self.browser.getControl("Save").click()
161        # date_of_birth is not required for applicants but for students
162        self.applicant.date_of_birth = None
163        self.browser.open(self.container_path + '/createallstudents')
164        self.assertTrue('No student could be created' in self.browser.contents)
165        logcontent = open(logfile).read()
166        self.assertTrue('RequiredMissing: date_of_birth' in logcontent)
167        self.browser.open(self.manage_path)
168        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
169        self.browser.getControl("Save").click()
170        self.browser.open(self.container_path + '/createallstudents')
171        self.assertTrue('1 students successfully created' in self.browser.contents)
172
173    def test_hidden_batch_copying_all(self):
174        logfile = os.path.join(
175            self.app['datacenter'].storage, 'logs', 'applicants.log')
176        self.prepare_applicant()
177        self.browser.open(self.manage_path)
178        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
179        #self.browser.getControl(name="form.course_admitted").value = ['CERT1']
180        self.browser.getControl("Save").click()
181        IWorkflowState(self.applicant).setState('admitted')
182        notify(grok.ObjectModifiedEvent(self.applicant))
183        self.browser.open(self.root_path + '/createallstudents')
184        self.assertTrue('No student could be created' in self.browser.contents)
185        logcontent = open(logfile).read()
186        self.assertTrue('No course admitted provided' in logcontent)
187        self.applicant.course_admitted = self.certificate
188        self.browser.open(self.root_path + '/createallstudents')
189        self.assertTrue('1 students successfully created' in self.browser.contents)
190
191    def test_copier_wo_passport(self):
192        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
193        self.browser.open(self.manage_path)
194        self.fill_correct_values()
195        # Let's try to create the student
196        IWorkflowState(self.applicant).setState('admitted')
197        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
198        self.browser.getControl("Save").click()
199        (success, msg) = self.applicant.createStudent()
200        self.assertTrue('created' in msg)
Note: See TracBrowser for help on using the repository browser.