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

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

Improve accesscode triggers.

File size: 4.4 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 zope.app.testing.functional import FunctionalTestCase
26from zope.component.hooks import setSite
27from waeup.sirp.app import University
28from waeup.sirp.testing import FunctionalLayer
29from waeup.sirp.accesscodes.accesscodes import (
30    AccessCodeBatch, get_access_code, invalidate_accesscode,
31    disable_accesscode, reenable_accesscode)
32
33class AccessCodeHelpersTests(FunctionalTestCase):
34
35    layer = FunctionalLayer
36
37    def setUp(self):
38        super(AccessCodeHelpersTests, self).setUp()
39
40        # Prepopulate ZODB
41        app = University()
42        self.dc_root = tempfile.mkdtemp()
43        app['datacenter'].setStoragePath(self.dc_root)
44
45        # Prepopulate the ZODB...
46        self.getRootFolder()['app'] = app
47        self.app = self.getRootFolder()['app']
48
49        # Create batch
50        batch = AccessCodeBatch('now', 'manfred', 'APP', 6.6, 0)
51        self.app['accesscodes'].addBatch(batch)
52
53        # Fill batch with accesscodes
54        batch.addAccessCode(0, '11111111')
55        batch.addAccessCode(1, '22222222')
56        batch.addAccessCode(2, '33333333')
57        self.ac1 = batch.getAccessCode('APP-1-11111111')
58        self.ac2 = batch.getAccessCode('APP-1-22222222')
59        self.ac3 = batch.getAccessCode('APP-1-33333333')
60
61        setSite(self.app)
62        return
63
64    def tearDown(self):
65        shutil.rmtree(self.dc_root)
66        super(AccessCodeHelpersTests, self).tearDown()
67        return
68
69    def test_get_access_code(self):
70        ac = get_access_code('APP-1-11111111')
71        assert ac is self.ac1
72
73    def test_get_access_code_not_string(self):
74        ac = get_access_code(object())
75        assert ac is None
76
77    def test_get_access_code_no_proper_pin(self):
78        ac = get_access_code('APP-without_pin')
79        assert ac is None
80
81    def test_get_access_code_invalid_batch_num(self):
82        ac = get_access_code('APP-invalid-11111111')
83        assert ac is None
84
85    def test_get_access_code_invalid_pin(self):
86        ac = get_access_code('APP-1-notexistent')
87        assert ac is None
88
89    def test_invalidate_accesscode(self):
90        assert self.ac1._invalidation_date is None
91        result = invalidate_accesscode('APP-1-11111111')
92        assert self.ac1._invalidation_date is not None
93        assert result is True
94
95    def test_invalidate_used_accesscode(self):
96        invalidate_accesscode('APP-1-11111111')
97        inv_date = self.ac1._invalidation_date
98        result = invalidate_accesscode('APP-1-11111111')
99        assert result is False
100        assert self.ac1._invalidation_date == inv_date
101
102    def test_invalidate_disabled_accesscode(self):
103        # disabled acs cannot be invalidated.
104        disable_accesscode('APP-1-11111111')
105        result = invalidate_accesscode('APP-1-11111111')
106        assert result is False
107        assert self.ac1._disabled is True
108
109    def test_disable_accesscode_unused(self):
110        assert self.ac1._disabled is False
111        disable_accesscode('APP-1-11111111')
112        assert self.ac1._disabled is True
113
114    def test_disable_accesscode_used(self):
115        assert self.ac1._disabled is False
116        invalidate_accesscode('APP-1-11111111')
117        disable_accesscode('APP-1-11111111')
118        assert self.ac1._disabled is True
119
120    def test_reenable_accesscode(self):
121        disable_accesscode('APP-1-11111111')
122        result = reenable_accesscode('APP-1-11111111')
123        assert result is True
124        assert self.ac1._disabled is False
125
126    def test_reenable_accesscode_enabled(self):
127        result = reenable_accesscode('APP-1-11111111')
128        assert result is False
129        assert self.ac1._disabled is False
Note: See TracBrowser for help on using the repository browser.