source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_root.py @ 7244

Last change on this file since 7244 was 7193, checked in by Henrik Bettermann, 13 years ago

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1## $Id: test_root.py 7193 2011-11-25 07:21:29Z 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"""
19Test applicants root.
20"""
21import logging
22import shutil
23import tempfile
24import unittest
25from StringIO import StringIO
26from zope.component.hooks import setSite, clearSite
27from zope.interface.verify import verifyClass, verifyObject
28from waeup.sirp.app import University
29from waeup.sirp.applicants import (
30    interfaces, application_exists, get_applicant_data, Applicant,
31    ApplicantsContainer,
32    )
33from waeup.sirp.applicants.root import (
34    ApplicantsRoot, ApplicantsPlugin,
35    )
36from waeup.sirp.testing import (
37    FunctionalLayer, FunctionalTestCase, remove_logger)
38
39
40class FakeSite(dict):
41    pass
42
43class ApplicantsRootTestCase(FunctionalTestCase):
44
45    layer = FunctionalLayer
46
47    def setUp(self):
48        remove_logger('waeup.sirp.app.applicants')
49        super(ApplicantsRootTestCase, self).setUp()
50        # Setup a sample site for each test
51        # Prepopulate the ZODB...
52        app = University()
53        self.getRootFolder()['app'] = app
54        self.app = self.getRootFolder()['app']
55        setSite(self.app)
56        self.dc_root = tempfile.mkdtemp()
57        app['datacenter'].setStoragePath(self.dc_root)
58        return
59
60    def tearDown(self):
61        super(ApplicantsRootTestCase, self).tearDown()
62        shutil.rmtree(self.dc_root)
63        clearSite()
64        return
65
66    def test_interfaces(self):
67        # Make sure the correct interfaces are implemented.
68        self.assertTrue(
69            verifyClass(
70                interfaces.IApplicantsRoot, ApplicantsRoot)
71            )
72        self.assertTrue(
73            verifyObject(
74                interfaces.IApplicantsRoot, ApplicantsRoot())
75            )
76        return
77
78    def test_logger(self):
79        # We can get a logger from root
80        logger = self.app['applicants'].logger
81        assert logger is not None
82        assert logger.name == 'waeup.sirp.app.applicants'
83        handlers = logger.handlers
84        assert len(handlers) == 1
85        filename = logger.handlers[0].baseFilename
86        assert filename.endswith('applicants.log')
87        assert filename.startswith(self.dc_root)
88
89    def test_logger_multiple(self):
90        # Make sure the logger is still working after 2nd call
91        # First time we call it, it might be registered
92        logger = self.app['applicants'].logger
93        # At second call the already registered logger should be returned
94        logger = self.app['applicants'].logger
95        assert logger is not None
96        assert logger.name == 'waeup.sirp.app.applicants'
97        handlers = logger.handlers
98        assert len(handlers) == 1
99        filename = logger.handlers[0].baseFilename
100        assert filename.endswith('applicants.log')
101        assert filename.startswith(self.dc_root)
102
103class ApplicantsRootPluginTestCase(unittest.TestCase):
104    def create_logger(self):
105        # create a logger suitable for local tests.
106        test_logger = logging.getLogger('waeup.sirp.applicants.testlogger')
107        log = StringIO()
108        handler = logging.StreamHandler(log)
109        handler.setLevel(logging.DEBUG)
110        test_logger.addHandler(handler)
111        test_logger.setLevel(logging.DEBUG)
112        self.logger = test_logger
113        self.log = log
114        self.handler = handler
115        return self.logger
116
117    def remove_logger(self):
118        del self.handler
119        del self.logger
120        del self.log
121        pass
122
123    def get_log(self):
124        self.log.seek(0)
125        return self.log.read()
126
127    def setUp(self):
128        self.create_logger()
129        return
130
131    def tearDown(self):
132        self.remove_logger()
133        return
134
135    # Real tests start here...
136    def test_pluginsetup(self):
137        # Make sure we can add ApplicantsRoot to sites.
138        site = FakeSite()
139        plugin = ApplicantsPlugin()
140        plugin.setup(site, 'blah', self.logger)
141        self.assertTrue('applicants' in site.keys())
142        log = self.get_log()
143        self.assertTrue('Installed applicants root.' in log)
144        return
145
146    def test_update_new(self):
147        # Run update on a site without applicants root.
148        site = FakeSite()
149        plugin = ApplicantsPlugin()
150        plugin.update(site, 'blah', self.logger)
151        self.assertTrue('applicants' in site.keys())
152        log = self.get_log()
153        self.assertTrue('Updating site at <Unnamed Site>' in log)
154        self.assertTrue('Installed applicants root.' in log)
155        return
156
157    def test_update_outdated(self):
158        # Run update on a site with outdated applicants root.
159        site = FakeSite()
160        root = object() # # This is not a proper applicants root
161        site['applicants'] = root
162        plugin = ApplicantsPlugin()
163        plugin.update(site, 'blah', self.logger)
164        self.assertTrue(site['applicants'] is not root)
165        self.assertTrue(isinstance(site['applicants'], ApplicantsRoot))
166        log = self.get_log()
167        self.assertTrue('Outdated applicants folder detected' in log)
168        self.assertTrue('Updating site at <Unnamed Site>' in log)
169        self.assertTrue('Installed applicants root.' in log)
170        return
171
172    def test_update_uptodate(self):
173        # Run update on a site with proper applicants root.
174        site = FakeSite()
175        root = ApplicantsRoot()
176        site['applicants'] = root
177        plugin = ApplicantsPlugin()
178        plugin.update(site, 'blah', self.logger)
179        self.assertTrue(site['applicants'] is root)
180        log = self.get_log()
181        self.assertTrue('Updating site at <Unnamed Site>' in log)
182        self.assertTrue('Nothing to do' in log)
183        return
184
185    def test_update_log(self):
186        # Check that sitename is used in log messages on updates.
187        site = FakeSite()
188        site.__name__ = 'my_site'
189        plugin = ApplicantsPlugin()
190        plugin.update(site, 'blah', self.logger)
191        log = self.get_log()
192        self.assertTrue('Updating site at my_site.' in log)
193        return
194
195
196class HelperToolsTest(FunctionalTestCase):
197
198    layer = FunctionalLayer
199
200    def setUp(self):
201        super(HelperToolsTest, self).setUp()
202
203        # Setup a sample site for each test
204        app = University()
205        self.dc_root = tempfile.mkdtemp()
206        app['datacenter'].setStoragePath(self.dc_root)
207
208        # Prepopulate the ZODB...
209        self.getRootFolder()['app'] = app
210        self.app = self.getRootFolder()['app']
211        setSite(self.app)
212
213        # Insert some applicants containers and applicants...
214        self.container1 = ApplicantsContainer()
215        self.app['applicants']['foo'] = self.container1
216        self.container2 = ApplicantsContainer()
217        self.app['applicants']['bar'] = self.container2
218        self.ac = u'APP-99999'
219        self.applicant = Applicant()
220        self.applicant.access_code = self.ac
221        self.app['applicants']['foo'][self.ac] = self.applicant
222        return
223
224    def tearDown(self):
225        super(HelperToolsTest, self).tearDown()
226        shutil.rmtree(self.dc_root)
227        return
228
229    def test_application_exists(self):
230        result = application_exists('APP-99999')
231        assert result is True
232
233        result = application_exists('APP-44444')
234        assert result is False
235        return
236
237    def test_get_applicant_data(self):
238        result = get_applicant_data('APP-99999')
239        self.assertEqual(result, self.applicant)
240        return
241
242    def test_get_applicant_data_none(self):
243        result = get_applicant_data('NOT-EXISTIING')
244        assert result is None
245        return
246
247def suite():
248    suite = unittest.TestSuite()
249    for testcase in [
250            ApplicantsRootTestCase,
251            ApplicantsRootPluginTestCase,
252            HelperToolsTest,
253            ]:
254        suite.addTests(unittest.makeSuite(testcase))
255    return suite
256
257test_suite = suite
Note: See TracBrowser for help on using the repository browser.