source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_applicant.py @ 14466

Last change on this file since 14466 was 14109, checked in by Henrik Bettermann, 8 years ago

Fix useless test.

  • Property svn:keywords set to Id
File size: 9.1 KB
Line 
1## $Id: test_applicant.py 14109 2016-08-22 13:40: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"""Tests for applicants and related.
19"""
20import grok
21import shutil
22import tempfile
23from StringIO import StringIO
24from zope.component import adapts, queryUtility
25from zope.component.interfaces import IFactory
26from zope.interface import verify, implements
27from zope.location.interfaces import ILocation
28from waeup.kofa.image.interfaces import IKofaImageFile
29from waeup.kofa.imagestorage import DefaultStorage
30from waeup.kofa.interfaces import IFileStoreHandler, IFileStoreNameChooser
31from waeup.kofa.applicants import (
32    Applicant, ApplicantFactory,
33    ApplicantImageStoreHandler, ApplicantImageNameChooser,
34    )
35from waeup.kofa.applicants.interfaces import IApplicant
36from waeup.kofa.applicants.payment import ApplicantOnlinePayment
37from waeup.kofa.applicants.refereereport import ApplicantRefereeReport
38from waeup.kofa.testing import FunctionalTestCase, FunctionalLayer
39
40class FakeImageLocation(object):
41    implements(ILocation)
42    adapts(IKofaImageFile)
43    def __init__(self, context):
44        pass
45
46class 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
106class 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
177class 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.__parent__ = fake_container
201        self.assertTrue(appl.password is None)
202        self.assertTrue(appl.firstname is None)
203        self.assertTrue(appl.lastname is None)
204        self.assertTrue(appl.email is None)
205        self.assertEqual(appl.container_code, 'folder-')
206        appl.firstname = u'Anna'
207        self.assertEqual(appl.container_code, 'folder+')
208        return
209
210    def test_payments(self):
211        payment = ApplicantOnlinePayment()
212        no_payment = ApplicantRefereeReport()
213        self.applicant['pid'] = payment
214        self.applicant['nopid'] = no_payment
215        self.assertEqual(len(self.applicant.values()),2)
216        self.assertEqual(len(self.applicant.payments),1)
217        self.assertEqual(self.applicant.payments[0], payment)
218        return
219
220    def test_refereereports(self):
221        report = ApplicantRefereeReport()
222        no_report = ApplicantOnlinePayment()
223        self.applicant['rid'] = report
224        self.applicant['norid'] = no_report
225        self.assertEqual(len(self.applicant.values()),2)
226        self.assertEqual(len(self.applicant.refereereports),1)
227        self.assertEqual(self.applicant.refereereports[0], report)
228        return
229
230class ApplicantFactoryTest(FunctionalTestCase):
231
232    layer = FunctionalLayer
233
234    def setUp(self):
235        super(ApplicantFactoryTest, self).setUp()
236        self.factory = ApplicantFactory()
237        return
238
239    def tearDown(self):
240        super(ApplicantFactoryTest, self).tearDown()
241        return
242
243    def test_interfaces(self):
244        verify.verifyClass(IFactory, ApplicantFactory)
245        verify.verifyObject(IFactory, self.factory)
246
247    def test_factory(self):
248        obj = self.factory()
249        assert isinstance(obj, Applicant)
250
251    def test_getInterfaces(self):
252        implemented_by = self.factory.getInterfaces()
253        assert implemented_by.isOrExtends(IApplicant)
Note: See TracBrowser for help on using the repository browser.