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

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

Fix tests. Do not use fixed year numbers which will be of range in a few years.

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