source: main/waeup.kofa/backup-trunk/src/waeup/kofa/applicants/tests/test_applicantcopier.py @ 9819

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

Save student_id in application record.

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