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 | ## |
---|
22 | import shutil |
---|
23 | import tempfile |
---|
24 | import unittest |
---|
25 | from zope.app.testing.functional import FunctionalTestCase |
---|
26 | from zope.component.hooks import setSite |
---|
27 | from waeup.sirp.app import University |
---|
28 | from waeup.sirp.testing import FunctionalLayer |
---|
29 | from waeup.sirp.accesscodes.accesscodes import ( |
---|
30 | AccessCodeBatch, get_access_code, invalidate_accesscode, |
---|
31 | disable_accesscode) |
---|
32 | |
---|
33 | class 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 | invalidate_accesscode('APP-1-11111111') |
---|
92 | assert self.ac1._invalidation_date is not None |
---|
93 | |
---|
94 | def test_disable_accesscode_unused(self): |
---|
95 | assert self.ac1._disabled is False |
---|
96 | disable_accesscode('APP-1-11111111') |
---|
97 | assert self.ac1._disabled is True |
---|
98 | |
---|
99 | def test_disable_accesscode_used(self): |
---|
100 | assert self.ac1._disabled is False |
---|
101 | invalidate_accesscode('APP-1-11111111') |
---|
102 | disable_accesscode('APP-1-11111111') |
---|
103 | assert self.ac1._disabled is True |
---|