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

Last change on this file since 13492 was 13216, checked in by Henrik Bettermann, 9 years ago

Extend and use container_code attribute to distinguish used and unused records.

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