1 | ## $Id: test_applicant.py 13974 2016-06-22 13:19:20Z 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 | """Tests for applicants and related. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | import shutil |
---|
22 | import tempfile |
---|
23 | from StringIO import StringIO |
---|
24 | from zope.component import adapts, queryUtility |
---|
25 | from zope.component.interfaces import IFactory |
---|
26 | from zope.interface import verify, implements |
---|
27 | from zope.location.interfaces import ILocation |
---|
28 | from waeup.kofa.image.interfaces import IKofaImageFile |
---|
29 | from waeup.kofa.imagestorage import DefaultStorage |
---|
30 | from waeup.kofa.interfaces import IFileStoreHandler, IFileStoreNameChooser |
---|
31 | from waeup.kofa.applicants import ( |
---|
32 | Applicant, ApplicantFactory, |
---|
33 | ApplicantImageStoreHandler, ApplicantImageNameChooser, |
---|
34 | ) |
---|
35 | from waeup.kofa.applicants.interfaces import IApplicant |
---|
36 | from waeup.kofa.applicants.payment import ApplicantOnlinePayment |
---|
37 | from waeup.kofa.applicants.refereereport import ApplicantRefereeReport |
---|
38 | from waeup.kofa.testing import FunctionalTestCase, FunctionalLayer |
---|
39 | |
---|
40 | class FakeImageLocation(object): |
---|
41 | implements(ILocation) |
---|
42 | adapts(IKofaImageFile) |
---|
43 | def __init__(self, context): |
---|
44 | pass |
---|
45 | |
---|
46 | class HelperTests(FunctionalTestCase): |
---|
47 | |
---|
48 | layer = FunctionalLayer |
---|
49 | |
---|
50 | def setUp(self): |
---|
51 | super(HelperTests, self).setUp() |
---|
52 | self.workdir = tempfile.mkdtemp() |
---|
53 | return |
---|
54 | |
---|
55 | def tearDown(self): |
---|
56 | super(HelperTests, self).tearDown() |
---|
57 | shutil.rmtree(self.workdir) |
---|
58 | return |
---|
59 | |
---|
60 | def test_image_store_handler_util_accessible(self): |
---|
61 | # we can get an IFileStoreHandler utility for applicants |
---|
62 | handler = queryUtility(IFileStoreHandler, name='img-applicant') |
---|
63 | self.assertTrue( |
---|
64 | isinstance(handler, ApplicantImageStoreHandler)) |
---|
65 | return |
---|
66 | |
---|
67 | def test_image_store_handler(self): |
---|
68 | store = DefaultStorage() |
---|
69 | handler = queryUtility(IFileStoreHandler, name='img-applicant') |
---|
70 | result1 = handler.pathFromFileID( |
---|
71 | store, '/fake-root', '__img-applicant__sample.jpg') |
---|
72 | result2 = handler.pathFromFileID( |
---|
73 | store, '/fake-root', '__img-applicant__dir1/sample.jpg') |
---|
74 | result3 = handler.pathFromFileID( |
---|
75 | store, '/fake-root', '__img-applicant__dir1/dir2/sample.jpg') |
---|
76 | self.assertEqual( |
---|
77 | result1, '/fake-root/applicants/sample') |
---|
78 | self.assertEqual( |
---|
79 | result2, '/fake-root/applicants/dir1/sample') |
---|
80 | self.assertEqual( |
---|
81 | result3, '/fake-root/applicants/dir1/dir2/sample') |
---|
82 | return |
---|
83 | |
---|
84 | def test_image_store_handler_create(self): |
---|
85 | # we can create files in image store with the applicant image |
---|
86 | # store handler |
---|
87 | store = DefaultStorage(self.workdir) |
---|
88 | handler = queryUtility(IFileStoreHandler, name='img-applicant') |
---|
89 | file_obj, path, waeup_file = handler.createFile( |
---|
90 | store, store.root, 'sample.jpg', '__img_applicant__sample', |
---|
91 | StringIO('I am a JPG file')) |
---|
92 | self.assertEqual(path[-21:], 'applicants/sample.jpg') |
---|
93 | return |
---|
94 | |
---|
95 | #def test_image_store_handler_invalid_filename_ext(self): |
---|
96 | # # we only accept '.jpg' and '.png' as filename extensions. |
---|
97 | # store = DefaultStorage(self.workdir) |
---|
98 | # handler = queryUtility(IFileStoreHandler, name='img-applicant') |
---|
99 | # self.assertRaises( |
---|
100 | # ValueError, |
---|
101 | # handler.createFile, |
---|
102 | # store, store.root, 'sample.txt', '__img_applicant__sample', |
---|
103 | # StringIO('I am a txt file')) |
---|
104 | # return |
---|
105 | |
---|
106 | class ApplicantImageNameChooserTests(FunctionalTestCase): |
---|
107 | |
---|
108 | layer = FunctionalLayer |
---|
109 | |
---|
110 | def test_iface(self): |
---|
111 | # make sure we implement promised interfaces |
---|
112 | obj = ApplicantImageNameChooser(None) # needs a context normally |
---|
113 | verify.verifyClass(IFileStoreNameChooser, ApplicantImageNameChooser) |
---|
114 | verify.verifyObject(IFileStoreNameChooser, obj) |
---|
115 | return |
---|
116 | |
---|
117 | def test_name_chooser_available(self): |
---|
118 | # we can get a name chooser for applicant objects as adapter |
---|
119 | appl = Applicant() |
---|
120 | chooser = IFileStoreNameChooser(appl) |
---|
121 | self.assertTrue(chooser is not None) |
---|
122 | return |
---|
123 | |
---|
124 | def test_name_chooser_applicant_wo_container(self): |
---|
125 | # we can get an image filename for applicants not in a container |
---|
126 | appl = Applicant() |
---|
127 | appl.applicant_id = u'dummy_123456' |
---|
128 | chooser = IFileStoreNameChooser(appl) |
---|
129 | result = chooser.chooseName() |
---|
130 | # the file would be stored in a ``_default`` directory. |
---|
131 | self.assertEqual( |
---|
132 | result, '__img-applicant___default/dummy_123456.jpg') |
---|
133 | return |
---|
134 | |
---|
135 | def test_name_chooser_applicant_w_container(self): |
---|
136 | fake_container = grok.Container() |
---|
137 | fake_container.__name__ = 'folder' |
---|
138 | fake_container.code = 'folder' |
---|
139 | appl = Applicant() |
---|
140 | appl.applicant_id = u'folder_123456' |
---|
141 | appl.__parent__ = fake_container |
---|
142 | chooser = IFileStoreNameChooser(appl) |
---|
143 | result = chooser.chooseName() |
---|
144 | self.assertEqual( |
---|
145 | result, '__img-applicant__folder/%s.jpg' % appl.applicant_id) |
---|
146 | return |
---|
147 | |
---|
148 | def test_name_chooser_applicant_nonpassport_w_container(self): |
---|
149 | fake_container = grok.Container() |
---|
150 | fake_container.__name__ = 'folder' |
---|
151 | fake_container.code = 'folder' |
---|
152 | appl = Applicant() |
---|
153 | appl.applicant_id = u'folder_123456' |
---|
154 | appl.__parent__ = fake_container |
---|
155 | chooser = IFileStoreNameChooser(appl) |
---|
156 | result = chooser.chooseName(attr='anything.pdf') |
---|
157 | self.assertEqual( |
---|
158 | result, '__img-applicant__folder/anything_%s.pdf' % appl.applicant_id) |
---|
159 | return |
---|
160 | |
---|
161 | def test_name_chooser_check_name(self): |
---|
162 | # we can check file ids for applicants |
---|
163 | fake_container = grok.Container() |
---|
164 | fake_container.__name__ = 'folder' |
---|
165 | fake_container.code = 'folder' |
---|
166 | appl = Applicant() |
---|
167 | appl.applicant_id = u'folder_123456' |
---|
168 | appl.__parent__ = fake_container |
---|
169 | chooser = IFileStoreNameChooser(appl) |
---|
170 | result1 = chooser.checkName('foo') |
---|
171 | result2 = chooser.checkName( |
---|
172 | '__img-applicant__folder/%s.jpg' % appl.applicant_id) |
---|
173 | self.assertEqual(result1, False) |
---|
174 | self.assertEqual(result2, True) |
---|
175 | return |
---|
176 | |
---|
177 | class ApplicantTest(FunctionalTestCase): |
---|
178 | |
---|
179 | layer = FunctionalLayer |
---|
180 | |
---|
181 | def setUp(self): |
---|
182 | super(ApplicantTest, self).setUp() |
---|
183 | self.applicant = Applicant() |
---|
184 | return |
---|
185 | |
---|
186 | def tearDown(self): |
---|
187 | super(ApplicantTest, self).tearDown() |
---|
188 | return |
---|
189 | |
---|
190 | def test_interfaces(self): |
---|
191 | verify.verifyClass(IApplicant, Applicant) |
---|
192 | verify.verifyObject(IApplicant, self.applicant) |
---|
193 | return |
---|
194 | |
---|
195 | def test_container_code(self): |
---|
196 | fake_container = grok.Container() |
---|
197 | fake_container.__name__ = 'folder' |
---|
198 | fake_container.code = 'folder' |
---|
199 | appl = Applicant() |
---|
200 | appl.password is None |
---|
201 | appl.firstname is None |
---|
202 | appl.lastname is None |
---|
203 | appl.email is None |
---|
204 | appl.container_code is 'folder-' |
---|
205 | appl.firstname is 'Anna' |
---|
206 | appl.container_code is 'folder+' |
---|
207 | return |
---|
208 | |
---|
209 | def test_payments(self): |
---|
210 | payment = ApplicantOnlinePayment() |
---|
211 | no_payment = ApplicantRefereeReport() |
---|
212 | self.applicant['pid'] = payment |
---|
213 | self.applicant['nopid'] = no_payment |
---|
214 | self.assertEqual(len(self.applicant.values()),2) |
---|
215 | self.assertEqual(len(self.applicant.payments),1) |
---|
216 | self.assertEqual(self.applicant.payments[0], payment) |
---|
217 | return |
---|
218 | |
---|
219 | def test_refereereports(self): |
---|
220 | report = ApplicantRefereeReport() |
---|
221 | no_report = ApplicantOnlinePayment() |
---|
222 | self.applicant['rid'] = report |
---|
223 | self.applicant['norid'] = no_report |
---|
224 | self.assertEqual(len(self.applicant.values()),2) |
---|
225 | self.assertEqual(len(self.applicant.refereereports),1) |
---|
226 | self.assertEqual(self.applicant.refereereports[0], report) |
---|
227 | return |
---|
228 | |
---|
229 | class ApplicantFactoryTest(FunctionalTestCase): |
---|
230 | |
---|
231 | layer = FunctionalLayer |
---|
232 | |
---|
233 | def setUp(self): |
---|
234 | super(ApplicantFactoryTest, self).setUp() |
---|
235 | self.factory = ApplicantFactory() |
---|
236 | return |
---|
237 | |
---|
238 | def tearDown(self): |
---|
239 | super(ApplicantFactoryTest, self).tearDown() |
---|
240 | return |
---|
241 | |
---|
242 | def test_interfaces(self): |
---|
243 | verify.verifyClass(IFactory, ApplicantFactory) |
---|
244 | verify.verifyObject(IFactory, self.factory) |
---|
245 | |
---|
246 | def test_factory(self): |
---|
247 | obj = self.factory() |
---|
248 | assert isinstance(obj, Applicant) |
---|
249 | |
---|
250 | def test_getInterfaces(self): |
---|
251 | implemented_by = self.factory.getInterfaces() |
---|
252 | assert implemented_by.isOrExtends(IApplicant) |
---|