source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_applicants.py @ 6551

Last change on this file since 6551 was 6539, checked in by uli, 13 years ago

Extend tests and use new IFileRetrieval when checking passport images.

File size: 6.7 KB
Line 
1##
2## test_applicants.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"""
24import unittest
25from StringIO import StringIO
26from hurry.file.interfaces import IFileRetrieval
27from zope.app.testing.functional import FunctionalTestCase
28from zope.component import (
29    provideAdapter, adapts, getGlobalSiteManager, provideUtility)
30from zope.component.hooks import setSite
31from zope.component.interfaces import IFactory
32from zope.interface import verify, implements
33from zope.location.interfaces import ILocation
34from zope.publisher.base import TestRequest
35from zope.publisher.interfaces import NotFound
36from waeup.sirp.app import University
37from waeup.sirp.image import WAeUPImageFile, createWAeUPImageFile
38from waeup.sirp.image.interfaces import IWAeUPImageFile
39from waeup.sirp.applicants import (
40    ResultEntry, Applicant, ApplicantFactory, ApplicantTraverser,
41    )
42from waeup.sirp.applicants.interfaces import (
43    IResultEntry, IApplicant,
44    )
45from waeup.sirp.imagestorage import ImageStorageFileRetrieval
46from waeup.sirp.testing import FunctionalLayer
47
48class FakeImageLocation(object):
49    implements(ILocation)
50    adapts(IWAeUPImageFile)
51    def __init__(self, context):
52        pass
53
54class ResultEntryTest(unittest.TestCase):
55
56    def setUp(self):
57        self.result_entry = ResultEntry()
58        return
59
60    def tearDown(self):
61        pass
62
63    def test_interfaces(self):
64        verify.verifyClass(IResultEntry, ResultEntry)
65        verify.verifyObject(IResultEntry, self.result_entry)
66
67    def test_resultentry(self):
68        entry = ResultEntry('Some subject', 3.7)
69        assert entry.subject == 'Some subject'
70        assert entry.score == 3.7
71
72class ApplicantTest(FunctionalTestCase):
73
74    layer = FunctionalLayer
75
76    def setUp(self):
77        super(ApplicantTest, self).setUp()
78        self.applicant = Applicant()
79        return
80
81    def tearDown(self):
82        super(ApplicantTest, self).tearDown()
83        return
84
85    def test_interfaces(self):
86        verify.verifyClass(IApplicant, Applicant)
87        verify.verifyObject(IApplicant, self.applicant)
88        return
89
90    def test_passport_no_site(self):
91        # make sure we get a real image stored in passport attr
92        image = self.applicant.passport.file.read()
93        self.assertEqual(len(image), 2059)
94        return
95
96    def test_passport_insite(self):
97        # make sure we get a real image in passport attr when inside a site.
98        # When an applicant is created 'inside a site', its passport
99        # photograph should be put inside the images folder of the
100        # site, even if it is only a placeholder.
101        self.getRootFolder()['app'] = University()
102        app = self.getRootFolder()['app']
103        setSite(app)
104        applicant = Applicant()
105        image = self.applicant.passport.file.read()
106        self.assertEqual(len(image), 2059)
107        # The image contains a file_id instead of real image-data
108        self.assertEqual(self.applicant.passport.data,
109                         u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')
110        assert u'b48a1d39bbcb32e955d9ff2dea4ed0e6' in app['images'].keys()
111        return
112
113class ApplicantFactoryTest(FunctionalTestCase):
114
115    layer = FunctionalLayer
116
117    def setUp(self):
118        # Install a IFileRetrieval utility that returns WAeUPImageFiles.
119        storage = ImageStorageFileRetrieval()
120        provideUtility(storage, IFileRetrieval)
121        self.factory = ApplicantFactory()
122
123    def tearDown(self):
124        pass
125
126    def test_interfaces(self):
127        verify.verifyClass(IFactory, ApplicantFactory)
128        verify.verifyObject(IFactory, self.factory)
129
130    def test_factory(self):
131        obj = self.factory()
132        assert isinstance(obj, Applicant)
133
134    def test_getInterfaces(self):
135        implemented_by = self.factory.getInterfaces()
136        assert implemented_by.isOrExtends(IApplicant)
137
138
139class ApplicantTraverserTest(FunctionalTestCase):
140
141    layer = FunctionalLayer
142
143    def setUp(self):
144        super(ApplicantTraverserTest, self).setUp()
145        provideAdapter(FakeImageLocation)
146        self.applicant = Applicant()
147
148        self.request = TestRequest('')
149        return
150
151    def tearDown(self):
152        gsm = getGlobalSiteManager()
153        gsm.unregisterAdapter(FakeImageLocation)
154        super(ApplicantTraverserTest, self).tearDown()
155        return
156
157    def test_traverse_wo_passport(self):
158        # Ask for some attribute not provided
159        traverser = ApplicantTraverser(
160            self.applicant, self.request
161            )
162        self.assertRaises(
163            NotFound,
164            traverser.publishTraverse, self.request, 'passport'
165            )
166        return
167
168    def test_traverse_wo_image_passport_jpg(self):
169        # Ask for applicant pic if we didn't provided one
170        # We get a placeholder.
171        traverser = ApplicantTraverser(
172            self.applicant, self.request
173            )
174        result = traverser.publishTraverse(self.request, 'passport.jpg')
175        self.assertTrue(isinstance(result, FakeImageLocation))
176        return
177
178    def test_traverse_w_image_passport_jpg(self):
179        # Ask for applicant pic that's named 'passport.jpg'
180        traverser = ApplicantTraverser(
181            self.applicant, self.request
182            )
183        self.applicant.passport = createWAeUPImageFile(
184            'nofile.jpg', StringIO('no-content'))
185        self.applicant.passport.filename = 'mypic.jpg'
186        result = traverser.publishTraverse(self.request, 'passport.jpg')
187        self.assertTrue(isinstance(result, FakeImageLocation))
188        return
189
190    def test_traverse_w_image_some_jpg(self):
191        # Ask for applicant pic by correct name
192        traverser = ApplicantTraverser(
193            self.applicant, self.request
194            )
195        self.applicant.passport = WAeUPImageFile('nofile.jpg', '')
196        self.applicant.passport.filename = 'mypic.jpg'
197        result = traverser.publishTraverse(self.request, 'mypic.jpg')
198        self.assertTrue(isinstance(result, FakeImageLocation))
199        return
Note: See TracBrowser for help on using the repository browser.