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

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

Update accesscode tests: check for exceptions raised when using the
convenience functions invalidate_accesscode() and friends.

Also start to convert tests not to expect access code attributes like
_disabled any more, because we will remove these soon (states will be
handled/set via workflows only).

File size: 5.8 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 shutil
23import tempfile
24import unittest
25from hurry.workflow.interfaces import InvalidTransitionError, IWorkflowState
26from zope.app.testing.functional import FunctionalTestCase
27from zope.component.hooks import setSite, clearSite
28from waeup.sirp.app import University
29from waeup.sirp.testing import FunctionalLayer
30from waeup.sirp.accesscodes.accesscodes import (
31    AccessCodeBatch, get_access_code, invalidate_accesscode,
32    disable_accesscode, reenable_accesscode, fire_transition)
33from waeup.sirp.accesscodes.workflow import INITIALIZED, USED, DISABLED
34
35class AccessCodeHelpersTests(FunctionalTestCase):
36
37    layer = FunctionalLayer
38
39    def setUp(self):
40        super(AccessCodeHelpersTests, self).setUp()
41
42        # Prepopulate ZODB
43        app = University()
44        self.dc_root = tempfile.mkdtemp()
45        app['datacenter'].setStoragePath(self.dc_root)
46
47        # Prepopulate the ZODB...
48        self.getRootFolder()['app'] = app
49        self.app = self.getRootFolder()['app']
50
51        # Create batch
52        batch = AccessCodeBatch('now', 'manfred', 'APP', 6.6, 0)
53        self.app['accesscodes'].addBatch(batch)
54
55        # Fill batch with accesscodes
56        batch.addAccessCode(0, '11111111')
57        batch.addAccessCode(1, '22222222')
58        batch.addAccessCode(2, '33333333')
59        self.ac1 = batch.getAccessCode('APP-1-11111111')
60        self.ac2 = batch.getAccessCode('APP-1-22222222')
61        self.ac3 = batch.getAccessCode('APP-1-33333333')
62
63        setSite(self.app)
64        return
65
66    def tearDown(self):
67        shutil.rmtree(self.dc_root)
68        super(AccessCodeHelpersTests, self).tearDown()
69        return
70
71    def test_get_access_code(self):
72        ac = get_access_code('APP-1-11111111')
73        assert ac is self.ac1
74
75    def test_get_access_code_not_string(self):
76        ac = get_access_code(object())
77        assert ac is None
78
79    def test_get_access_code_no_proper_pin(self):
80        ac = get_access_code('APP-without_pin')
81        assert ac is None
82
83    def test_get_access_code_invalid_batch_num(self):
84        ac = get_access_code('APP-invalid-11111111')
85        assert ac is None
86
87    def test_get_access_code_invalid_pin(self):
88        ac = get_access_code('APP-1-notexistent')
89        assert ac is None
90
91    def test_invalidate_accesscode(self):
92        assert self.ac1._invalidation_date is None
93        result = invalidate_accesscode('APP-1-11111111')
94        assert self.ac1._invalidation_date is not None
95        assert result is True
96
97    def test_disable_accesscode_unused(self):
98        # we can disable initialized acs
99        assert self.ac1._disabled is False
100        disable_accesscode('APP-1-11111111')
101        assert self.ac1._disabled is True
102
103    def test_disable_accesscode_used(self):
104        # we can disable already used acs
105        assert self.ac1._disabled is False
106        invalidate_accesscode('APP-1-11111111')
107        disable_accesscode('APP-1-11111111')
108        assert self.ac1._disabled is True
109
110    def test_reenable_accesscode(self):
111        # we can reenable disabled acs
112        disable_accesscode('APP-1-11111111')
113        result = reenable_accesscode('APP-1-11111111')
114        assert result is True
115        assert self.ac1._disabled is False
116
117    def test_fire_transition(self):
118        # we can fire transitions generally
119        fire_transition('APP-1-11111111', 'use')
120        assert IWorkflowState(self.ac1).getState() is USED
121
122    def test_fire_transition_toward(self):
123        # the `toward` keyword is respected
124        fire_transition('APP-1-11111111', DISABLED, toward=True)
125        assert IWorkflowState(self.ac1).getState() is DISABLED
126
127    def test_fire_transition_no_site(self):
128        # when no site is available, we will get a TypeError
129        clearSite()
130        self.assertRaises(
131            KeyError,
132            fire_transition, 'APP-1-11111111', 'use')
133
134    def test_fire_transition_broken_ac_id(self):
135        # if we get an invalid access code id (of wrong format) we get
136        # ValueErrors
137        self.assertRaises(
138            ValueError,
139            fire_transition, '11111111', 'use')
140
141    def test_fire_transition_invalid_batch_id(self):
142        # if we request a non-existent batch_id, we'll get a KeyError
143        self.assertRaises(
144            KeyError,
145            fire_transition, 'FOO-1-11111111', 'use')
146
147    def test_fire_transition_invalid_ac(self):
148        # if we request a non-exitent access-code, we'll get a KeyError
149        self.assertRaises(
150            KeyError,
151            fire_transition, 'APP-1-NONSENSE', 'use')
152
153    def test_fire_transition_undef_trans_id(self):
154        # asking for undefined transition id means a KeyError
155        self.assertRaises(
156            KeyError,
157            fire_transition, 'APP-1-11111111', 'nonsense')
158
159    def test_fire_transition_invalid_transition(self):
160        # asking for a forbidden transition will result in
161        # InvalidTransitionError
162        self.assertRaises(
163            InvalidTransitionError,
164            fire_transition, 'APP-1-11111111', 'init') # already initialized
Note: See TracBrowser for help on using the repository browser.