source: main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/applicants/tests/test_applicantcopier.py @ 9079

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

We need to customize the attributes to be copied. Therefore it's easier to define these attributes in a list.

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