source: main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/tests/test_accesscodes.py @ 6418

Last change on this file since 6418 was 6417, checked in by uli, 14 years ago

Reorganize tests. For what is over, make most of the remaining accesscode stuff work again. Still lots to do.

File size: 9.7 KB
Line 
1##
2## test_accesscodes.py
3## Login : <uli@pu.smp.net>
4## Started on  Sun Jun 12 13:07:58 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 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##
22import doctest
23import os
24import re
25import shutil
26import tempfile
27import unittest
28
29from datetime import datetime
30from hurry.workflow.interfaces import InvalidTransitionError, IWorkflowState
31from zope.app.testing.functional import (
32    FunctionalTestCase, FunctionalTestSetup, getRootFolder)
33from zope.component.hooks import setSite, clearSite
34from zope.interface.verify import verifyObject, verifyClass
35from zope.testing import renormalizing
36from waeup.sirp.app import University
37from waeup.sirp.testing import FunctionalLayer
38from waeup.sirp.accesscodes.accesscodes import (
39    AccessCodeBatch, get_access_code, invalidate_accesscode, AccessCode,
40    disable_accesscode, reenable_accesscode, fire_transition,
41    AccessCodeBatchContainer,)
42from waeup.sirp.accesscodes.interfaces import (
43    IAccessCode, IAccessCodeBatch,  IAccessCodeBatchContainer,)
44from waeup.sirp.accesscodes.workflow import INITIALIZED, USED, DISABLED
45
46
47
48
49class AccessCodeHelpersTests(FunctionalTestCase):
50    # Tests for helpers like get_access_code, disable_accesscode, ...
51
52    layer = FunctionalLayer
53
54    def setUp(self):
55        super(AccessCodeHelpersTests, self).setUp()
56
57        # Prepopulate ZODB
58        app = University()
59        self.dc_root = tempfile.mkdtemp()
60        app['datacenter'].setStoragePath(self.dc_root)
61
62        # Prepopulate the ZODB...
63        self.getRootFolder()['app'] = app
64        self.app = self.getRootFolder()['app']
65
66        # Create batch
67        batch = AccessCodeBatch('now', 'manfred', 'APP', 6.6, 0)
68        self.app['accesscodes'].addBatch(batch)
69
70        # Fill batch with accesscodes
71        batch.addAccessCode(0, '11111111')
72        batch.addAccessCode(1, '22222222')
73        batch.addAccessCode(2, '33333333')
74        self.ac1 = batch.getAccessCode('APP-1-11111111')
75        self.ac2 = batch.getAccessCode('APP-1-22222222')
76        self.ac3 = batch.getAccessCode('APP-1-33333333')
77
78        setSite(self.app)
79        return
80
81    def tearDown(self):
82        shutil.rmtree(self.dc_root)
83        super(AccessCodeHelpersTests, self).tearDown()
84        return
85
86    def test_get_access_code(self):
87        ac = get_access_code('APP-1-11111111')
88        assert ac is self.ac1
89
90    def test_get_access_code_not_string(self):
91        ac = get_access_code(object())
92        assert ac is None
93
94    def test_get_access_code_no_proper_pin(self):
95        ac = get_access_code('APP-without_pin')
96        assert ac is None
97
98    def test_get_access_code_invalid_batch_num(self):
99        ac = get_access_code('APP-invalid-11111111')
100        assert ac is None
101
102    def test_get_access_code_invalid_pin(self):
103        ac = get_access_code('APP-1-notexistent')
104        assert ac is None
105
106    def test_invalidate_accesscode(self):
107        assert self.ac1.used is False
108        result = invalidate_accesscode('APP-1-11111111')
109        assert self.ac1.used is True
110        assert result is True
111
112    def test_disable_accesscode_unused(self):
113        # we can disable initialized acs
114        assert self.ac1.disabled is False
115        disable_accesscode('APP-1-11111111')
116        assert self.ac1.disabled is True
117
118    def test_disable_accesscode_used(self):
119        # we can disable already used acs
120        assert self.ac1.disabled is False
121        invalidate_accesscode('APP-1-11111111')
122        disable_accesscode('APP-1-11111111')
123        assert self.ac1.disabled is True
124
125    def test_reenable_accesscode(self):
126        # we can reenable disabled acs
127        disable_accesscode('APP-1-11111111')
128        result = reenable_accesscode('APP-1-11111111')
129        assert result is True
130        assert self.ac1.disabled is False
131
132    def test_fire_transition(self):
133        # we can fire transitions generally
134        fire_transition('APP-1-11111111', 'use')
135        assert IWorkflowState(self.ac1).getState() is USED
136
137    def test_fire_transition_toward(self):
138        # the `toward` keyword is respected
139        fire_transition('APP-1-11111111', DISABLED, toward=True)
140        assert IWorkflowState(self.ac1).getState() is DISABLED
141
142    def test_fire_transition_no_site(self):
143        # when no site is available, we will get a TypeError
144        clearSite()
145        self.assertRaises(
146            KeyError,
147            fire_transition, 'APP-1-11111111', 'use')
148
149    def test_fire_transition_broken_ac_id(self):
150        # if we get an invalid access code id (of wrong format) we get
151        # ValueErrors
152        self.assertRaises(
153            ValueError,
154            fire_transition, '11111111', 'use')
155
156    def test_fire_transition_invalid_batch_id(self):
157        # if we request a non-existent batch_id, we'll get a KeyError
158        self.assertRaises(
159            KeyError,
160            fire_transition, 'FOO-1-11111111', 'use')
161
162    def test_fire_transition_invalid_ac(self):
163        # if we request a non-exitent access-code, we'll get a KeyError
164        self.assertRaises(
165            KeyError,
166            fire_transition, 'APP-1-NONSENSE', 'use')
167
168    def test_fire_transition_undef_trans_id(self):
169        # asking for undefined transition id means a KeyError
170        self.assertRaises(
171            KeyError,
172            fire_transition, 'APP-1-11111111', 'nonsense')
173
174    def test_fire_transition_invalid_transition(self):
175        # asking for a forbidden transition will result in
176        # InvalidTransitionError
177        self.assertRaises(
178            InvalidTransitionError,
179            fire_transition, 'APP-1-11111111', 'init') # already initialized
180
181
182class AccessCodeTests(FunctionalTestCase):
183    # Tests for AccessCode class
184
185    layer = FunctionalLayer
186
187    def setUp(self):
188        super(AccessCodeTests, self).setUp()
189
190        # Prepopulate ZODB
191        app = University()
192        self.dc_root = tempfile.mkdtemp()
193        app['datacenter'].setStoragePath(self.dc_root)
194
195        # Prepopulate the ZODB...
196        self.getRootFolder()['app'] = app
197        self.app = self.getRootFolder()['app']
198
199        setSite(self.app)
200        return
201
202    def tearDown(self):
203        shutil.rmtree(self.dc_root)
204        super(AccessCodeTests, self).tearDown()
205        return
206
207    def test_iface(self):
208        ac = AccessCode('1', '12345678')
209        assert verifyObject(IAccessCode, ac)
210        assert verifyClass(IAccessCode, AccessCode)
211
212class AccessCodeBatchTests(FunctionalTestCase):
213    # Tests for AccessCodeBatch class
214
215    layer = FunctionalLayer
216
217    def setUp(self):
218        super(AccessCodeBatchTests, self).setUp()
219
220        # Prepopulate ZODB
221        app = University()
222        self.dc_root = tempfile.mkdtemp()
223        app['datacenter'].setStoragePath(self.dc_root)
224
225        # Prepopulate the ZODB...
226        self.getRootFolder()['app'] = app
227        self.app = self.getRootFolder()['app']
228
229        setSite(self.app)
230        return
231
232    def tearDown(self):
233        shutil.rmtree(self.dc_root)
234        super(AccessCodeBatchTests, self).tearDown()
235        return
236
237    def test_iface(self):
238        batch = AccessCodeBatch(
239            datetime(2009, 12, 23), 'Fred','APP', 12.12, 3, num=10)
240        assert verifyObject(IAccessCodeBatch, batch)
241        assert verifyClass(IAccessCodeBatch, AccessCodeBatch)
242
243class AccessCodeBatchContainerTests(FunctionalTestCase):
244    # Tests for AccessCodeContainer class
245
246    layer = FunctionalLayer
247
248    def setUp(self):
249        super(AccessCodeBatchContainerTests, self).setUp()
250
251        # Prepopulate ZODB
252        app = University()
253        self.dc_root = tempfile.mkdtemp()
254        app['datacenter'].setStoragePath(self.dc_root)
255
256        # Prepopulate the ZODB...
257        self.getRootFolder()['app'] = app
258        self.app = self.getRootFolder()['app']
259
260        setSite(self.app)
261        return
262
263    def tearDown(self):
264        shutil.rmtree(self.dc_root)
265        super(AccessCodeBatchContainerTests, self).tearDown()
266        return
267
268    def test_iface(self):
269        accesscodes = AccessCodeBatchContainer()
270        assert verifyObject(IAccessCodeBatchContainer, accesscodes)
271        assert verifyClass(IAccessCodeBatchContainer, AccessCodeBatchContainer)
272
273
274checker = renormalizing.RENormalizing([
275        (re.compile('[\d]{10}'), '<10-DIGITS>'),
276        ])
277
278def setUp(test):
279    FunctionalTestSetup().setUp()
280
281def tearDown(self, test=None):
282    FunctionalTestSetup().tearDown()
283
284
285
286def test_suite():
287    suite = unittest.TestSuite()
288    for testcase in [
289        AccessCodeHelpersTests,
290        AccessCodeTests,
291        AccessCodeBatchTests,
292        AccessCodeBatchContainerTests,
293        ]:
294        suite.addTests(unittest.TestLoader().loadTestsFromTestCase(testcase))
295    for filename in [
296        #'accesscodes.txt',
297        'browser.txt'
298        ]:
299        path = os.path.join(
300            os.path.dirname(os.path.dirname(__file__)), filename)
301        test = doctest.DocFileSuite(
302            path,
303            module_relative=False,
304            setUp=setUp, tearDown=tearDown,
305            globs = dict(getRootFolder = getRootFolder),
306            optionflags = doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE,
307            checker = checker,
308            )
309        test.layer = FunctionalLayer
310        suite.addTest(test)
311    return suite
Note: See TracBrowser for help on using the repository browser.