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

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

Make provision against storing other objects than applicant payments in applicant containers.

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