1 | ## |
---|
2 | ## test_applicant.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Tue Aug 24 04:36:11 2010 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2010 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """Tests for applicants and related. |
---|
23 | """ |
---|
24 | import unittest |
---|
25 | from StringIO import StringIO |
---|
26 | from hurry.file.interfaces import IFileRetrieval |
---|
27 | from zope.component import ( |
---|
28 | provideAdapter, adapts, getGlobalSiteManager, provideUtility) |
---|
29 | from zope.component.hooks import setSite |
---|
30 | from zope.component.interfaces import IFactory |
---|
31 | from zope.interface import verify, implements |
---|
32 | from zope.location.interfaces import ILocation |
---|
33 | from zope.publisher.base import TestRequest |
---|
34 | from zope.publisher.interfaces import NotFound |
---|
35 | from waeup.sirp.app import University |
---|
36 | from waeup.sirp.image import WAeUPImageFile, createWAeUPImageFile |
---|
37 | from waeup.sirp.image.interfaces import IWAeUPImageFile |
---|
38 | from waeup.sirp.applicants import ( |
---|
39 | ResultEntry, Applicant, ApplicantFactory, ApplicantTraverser, |
---|
40 | ) |
---|
41 | from waeup.sirp.applicants.interfaces import ( |
---|
42 | IResultEntry, IApplicant, |
---|
43 | ) |
---|
44 | from waeup.sirp.imagestorage import ImageStorageFileRetrieval |
---|
45 | from waeup.sirp.testing import (FunctionalTestCase, FunctionalLayer) |
---|
46 | |
---|
47 | class FakeImageLocation(object): |
---|
48 | implements(ILocation) |
---|
49 | adapts(IWAeUPImageFile) |
---|
50 | def __init__(self, context): |
---|
51 | pass |
---|
52 | |
---|
53 | class ResultEntryTest(unittest.TestCase): |
---|
54 | |
---|
55 | def setUp(self): |
---|
56 | self.result_entry = ResultEntry() |
---|
57 | return |
---|
58 | |
---|
59 | def tearDown(self): |
---|
60 | pass |
---|
61 | |
---|
62 | def test_interfaces(self): |
---|
63 | verify.verifyClass(IResultEntry, ResultEntry) |
---|
64 | verify.verifyObject(IResultEntry, self.result_entry) |
---|
65 | |
---|
66 | def test_resultentry(self): |
---|
67 | entry = ResultEntry('Some subject', 3.7) |
---|
68 | assert entry.subject == 'Some subject' |
---|
69 | assert entry.score == 3.7 |
---|
70 | |
---|
71 | class ApplicantTest(FunctionalTestCase): |
---|
72 | |
---|
73 | layer = FunctionalLayer |
---|
74 | |
---|
75 | def setUp(self): |
---|
76 | super(ApplicantTest, self).setUp() |
---|
77 | self.applicant = Applicant() |
---|
78 | return |
---|
79 | |
---|
80 | def tearDown(self): |
---|
81 | super(ApplicantTest, self).tearDown() |
---|
82 | return |
---|
83 | |
---|
84 | def test_interfaces(self): |
---|
85 | verify.verifyClass(IApplicant, Applicant) |
---|
86 | verify.verifyObject(IApplicant, self.applicant) |
---|
87 | return |
---|
88 | |
---|
89 | def test_passport_no_site(self): |
---|
90 | # make sure we get a real image stored in passport attr |
---|
91 | image = self.applicant.passport.file.read() |
---|
92 | self.assertEqual(len(image), 2059) |
---|
93 | return |
---|
94 | |
---|
95 | def test_passport_insite(self): |
---|
96 | # make sure we get a real image in passport attr when inside a site. |
---|
97 | # When an applicant is created 'inside a site', its passport |
---|
98 | # photograph should be put inside the images folder of the |
---|
99 | # site, even if it is only a placeholder. |
---|
100 | self.getRootFolder()['app'] = University() |
---|
101 | app = self.getRootFolder()['app'] |
---|
102 | setSite(app) |
---|
103 | applicant = Applicant() |
---|
104 | image = self.applicant.passport.file.read() |
---|
105 | self.assertEqual(len(image), 2059) |
---|
106 | # The image contains a file_id instead of real image-data |
---|
107 | self.assertEqual(self.applicant.passport.data, |
---|
108 | u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1') |
---|
109 | assert u'b48a1d39bbcb32e955d9ff2dea4ed0e6' in app['images'].keys() |
---|
110 | return |
---|
111 | |
---|
112 | class ApplicantFactoryTest(FunctionalTestCase): |
---|
113 | |
---|
114 | layer = FunctionalLayer |
---|
115 | |
---|
116 | def setUp(self): |
---|
117 | super(ApplicantFactoryTest, self).setUp() |
---|
118 | # Install a IFileRetrieval utility that returns WAeUPImageFiles. |
---|
119 | storage = ImageStorageFileRetrieval() |
---|
120 | provideUtility(storage, IFileRetrieval) |
---|
121 | self.factory = ApplicantFactory() |
---|
122 | return |
---|
123 | |
---|
124 | def tearDown(self): |
---|
125 | super(ApplicantFactoryTest, self).tearDown() |
---|
126 | return |
---|
127 | |
---|
128 | def test_interfaces(self): |
---|
129 | verify.verifyClass(IFactory, ApplicantFactory) |
---|
130 | verify.verifyObject(IFactory, self.factory) |
---|
131 | |
---|
132 | def test_factory(self): |
---|
133 | obj = self.factory() |
---|
134 | assert isinstance(obj, Applicant) |
---|
135 | |
---|
136 | def test_getInterfaces(self): |
---|
137 | implemented_by = self.factory.getInterfaces() |
---|
138 | assert implemented_by.isOrExtends(IApplicant) |
---|
139 | |
---|
140 | |
---|
141 | class ApplicantTraverserTest(FunctionalTestCase): |
---|
142 | |
---|
143 | layer = FunctionalLayer |
---|
144 | |
---|
145 | def setUp(self): |
---|
146 | super(ApplicantTraverserTest, self).setUp() |
---|
147 | provideAdapter(FakeImageLocation) |
---|
148 | self.applicant = Applicant() |
---|
149 | |
---|
150 | self.request = TestRequest('') |
---|
151 | return |
---|
152 | |
---|
153 | def tearDown(self): |
---|
154 | gsm = getGlobalSiteManager() |
---|
155 | gsm.unregisterAdapter(FakeImageLocation) |
---|
156 | super(ApplicantTraverserTest, self).tearDown() |
---|
157 | return |
---|
158 | |
---|
159 | def test_traverse_wo_passport(self): |
---|
160 | # Ask for some attribute not provided |
---|
161 | traverser = ApplicantTraverser( |
---|
162 | self.applicant, self.request |
---|
163 | ) |
---|
164 | self.assertRaises( |
---|
165 | NotFound, |
---|
166 | traverser.publishTraverse, self.request, 'passport' |
---|
167 | ) |
---|
168 | return |
---|
169 | |
---|
170 | def test_traverse_wo_image_passport_jpg(self): |
---|
171 | # Ask for applicant pic if we didn't provided one |
---|
172 | # We get a placeholder. |
---|
173 | traverser = ApplicantTraverser( |
---|
174 | self.applicant, self.request |
---|
175 | ) |
---|
176 | result = traverser.publishTraverse(self.request, 'passport.jpg') |
---|
177 | self.assertTrue(isinstance(result, FakeImageLocation)) |
---|
178 | return |
---|
179 | |
---|
180 | def test_traverse_w_image_passport_jpg(self): |
---|
181 | # Ask for applicant pic that's named 'passport.jpg' |
---|
182 | traverser = ApplicantTraverser( |
---|
183 | self.applicant, self.request |
---|
184 | ) |
---|
185 | self.applicant.passport = createWAeUPImageFile( |
---|
186 | 'nofile.jpg', StringIO('no-content')) |
---|
187 | self.applicant.passport.filename = 'mypic.jpg' |
---|
188 | result = traverser.publishTraverse(self.request, 'passport.jpg') |
---|
189 | self.assertTrue(isinstance(result, FakeImageLocation)) |
---|
190 | return |
---|
191 | |
---|
192 | def test_traverse_w_image_some_jpg(self): |
---|
193 | # Ask for applicant pic by correct name |
---|
194 | traverser = ApplicantTraverser( |
---|
195 | self.applicant, self.request |
---|
196 | ) |
---|
197 | self.applicant.passport = WAeUPImageFile('nofile.jpg', '') |
---|
198 | self.applicant.passport.filename = 'mypic.jpg' |
---|
199 | result = traverser.publishTraverse(self.request, 'mypic.jpg') |
---|
200 | self.assertTrue(isinstance(result, FakeImageLocation)) |
---|
201 | return |
---|