1 | ## $Id: test_applicant.py 7241 2011-12-01 07:55:32Z 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 os |
---|
22 | import shutil |
---|
23 | import tempfile |
---|
24 | import unittest |
---|
25 | from StringIO import StringIO |
---|
26 | from zope.component import adapts, queryUtility |
---|
27 | from zope.component.interfaces import IFactory |
---|
28 | from zope.interface import verify, implements |
---|
29 | from zope.location.interfaces import ILocation |
---|
30 | from waeup.sirp.image.interfaces import IWAeUPImageFile |
---|
31 | from waeup.sirp.imagestorage import DefaultStorage |
---|
32 | from waeup.sirp.interfaces import IFileStoreHandler, IFileStoreNameChooser |
---|
33 | from waeup.sirp.applicants import ( |
---|
34 | Applicant, ApplicantFactory, ApplicantsContainer, |
---|
35 | ApplicantImageStoreHandler, ApplicantImageNameChooser, |
---|
36 | ) |
---|
37 | from waeup.sirp.applicants.interfaces import IApplicant |
---|
38 | from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer |
---|
39 | |
---|
40 | class FakeImageLocation(object): |
---|
41 | implements(ILocation) |
---|
42 | adapts(IWAeUPImageFile) |
---|
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() |
---|
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 | 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/xxx_1234.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(container=fake_container) |
---|
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_check_name(self): |
---|
147 | # we can check file ids for applicants |
---|
148 | fake_container = grok.Container() |
---|
149 | fake_container.__name__ = 'folder' |
---|
150 | fake_container.code = 'folder' |
---|
151 | appl = Applicant(container=fake_container) |
---|
152 | appl.__parent__ = fake_container |
---|
153 | chooser = IFileStoreNameChooser(appl) |
---|
154 | result1 = chooser.checkName('foo') |
---|
155 | result2 = chooser.checkName( |
---|
156 | '__img-applicant__folder/%s.jpg' % appl.applicant_id) |
---|
157 | self.assertEqual(result1, False) |
---|
158 | self.assertEqual(result2, True) |
---|
159 | return |
---|
160 | |
---|
161 | class ApplicantTest(FunctionalTestCase): |
---|
162 | |
---|
163 | layer = FunctionalLayer |
---|
164 | |
---|
165 | def setUp(self): |
---|
166 | super(ApplicantTest, self).setUp() |
---|
167 | self.applicant = Applicant() |
---|
168 | return |
---|
169 | |
---|
170 | def tearDown(self): |
---|
171 | super(ApplicantTest, self).tearDown() |
---|
172 | return |
---|
173 | |
---|
174 | def test_interfaces(self): |
---|
175 | verify.verifyClass(IApplicant, Applicant) |
---|
176 | verify.verifyObject(IApplicant, self.applicant) |
---|
177 | return |
---|
178 | |
---|
179 | class ApplicantFactoryTest(FunctionalTestCase): |
---|
180 | |
---|
181 | layer = FunctionalLayer |
---|
182 | |
---|
183 | def setUp(self): |
---|
184 | super(ApplicantFactoryTest, self).setUp() |
---|
185 | self.factory = ApplicantFactory() |
---|
186 | return |
---|
187 | |
---|
188 | def tearDown(self): |
---|
189 | super(ApplicantFactoryTest, self).tearDown() |
---|
190 | return |
---|
191 | |
---|
192 | def test_interfaces(self): |
---|
193 | verify.verifyClass(IFactory, ApplicantFactory) |
---|
194 | verify.verifyObject(IFactory, self.factory) |
---|
195 | |
---|
196 | def test_factory(self): |
---|
197 | obj = self.factory(container=None) |
---|
198 | assert isinstance(obj, Applicant) |
---|
199 | |
---|
200 | def test_getInterfaces(self): |
---|
201 | implemented_by = self.factory.getInterfaces() |
---|
202 | assert implemented_by.isOrExtends(IApplicant) |
---|