1 | ## |
---|
2 | ## test_certificatecontainer.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Sun May 29 02:21:16 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 | """ |
---|
23 | Tests for certificate containers and related components. |
---|
24 | """ |
---|
25 | import shutil |
---|
26 | import tempfile |
---|
27 | import unittest |
---|
28 | from zope.component.hooks import setSite, clearSite |
---|
29 | from zope.interface.verify import verifyClass, verifyObject |
---|
30 | |
---|
31 | from waeup.sirp.app import University |
---|
32 | from waeup.sirp.interfaces import DuplicationError |
---|
33 | from waeup.sirp.testing import ( |
---|
34 | FunctionalLayer, doctestsuite_for_module, FunctionalTestCase) |
---|
35 | from waeup.sirp.university.certificate import Certificate |
---|
36 | from waeup.sirp.university.certificatecontainer import CertificateContainer |
---|
37 | from waeup.sirp.university.interfaces import ICertificateContainer |
---|
38 | |
---|
39 | |
---|
40 | class CertificateContainerTests(FunctionalTestCase): |
---|
41 | |
---|
42 | layer = FunctionalLayer |
---|
43 | |
---|
44 | def setUp(self): |
---|
45 | super(CertificateContainerTests, self).setUp() |
---|
46 | |
---|
47 | # Setup a sample site for each test |
---|
48 | app = University() |
---|
49 | self.dc_root = tempfile.mkdtemp() |
---|
50 | app['datacenter'].setStoragePath(self.dc_root) |
---|
51 | |
---|
52 | # Prepopulate the ZODB... |
---|
53 | self.getRootFolder()['app'] = app |
---|
54 | self.app = self.getRootFolder()['app'] |
---|
55 | setSite(self.app) |
---|
56 | |
---|
57 | def tearDown(self): |
---|
58 | super(CertificateContainerTests, self).tearDown() |
---|
59 | clearSite() |
---|
60 | shutil.rmtree(self.dc_root) |
---|
61 | |
---|
62 | def test_create(self): |
---|
63 | # we can create instances |
---|
64 | container = CertificateContainer() |
---|
65 | result = isinstance(container, CertificateContainer) |
---|
66 | assert result is True |
---|
67 | |
---|
68 | def test_provides_icertificatecontainer(self): |
---|
69 | # instances tell they provide their main interface |
---|
70 | container = CertificateContainer() |
---|
71 | result = ICertificateContainer.providedBy(container) |
---|
72 | assert result is True |
---|
73 | |
---|
74 | def test_interfaces(self): |
---|
75 | # class and instances comply with their main interface |
---|
76 | container = CertificateContainer() |
---|
77 | result1 = verifyClass(ICertificateContainer, CertificateContainer) |
---|
78 | result2 = verifyObject(ICertificateContainer, container) |
---|
79 | |
---|
80 | def test_add_certificate_non_icertificate(self): |
---|
81 | # we cannot add certificates that do not implement ICertificate. |
---|
82 | container = CertificateContainer() |
---|
83 | self.assertRaises( |
---|
84 | TypeError, container.addCertificate, 'some_string') |
---|
85 | |
---|
86 | def test_add_certificate_no_duplicate_entry(self): |
---|
87 | # we cannot add certificates that are registered already. |
---|
88 | container = CertificateContainer() |
---|
89 | # We must place this container in the ZODB to make catalogs work. |
---|
90 | self.app['certs'] = container |
---|
91 | cert1 = Certificate(code="CERT1") |
---|
92 | cert2 = Certificate(code="CERT1") |
---|
93 | container.addCertificate(cert1) |
---|
94 | self.assertRaises( |
---|
95 | DuplicationError, |
---|
96 | container.addCertificate, cert2) |
---|
97 | |
---|
98 | def test_setitem_duplicate_entry(self): |
---|
99 | # we cannot add certificates whose code exists already in catalog |
---|
100 | container1 = CertificateContainer() |
---|
101 | container2 = CertificateContainer() |
---|
102 | self.app['certs1'] = container1 # enable catalogs |
---|
103 | self.app['certs2'] = container2 |
---|
104 | cert1 = Certificate(code="CERT1") |
---|
105 | cert2 = Certificate(code="CERT1") |
---|
106 | self.app['certs1']['CERT1'] = cert1 |
---|
107 | self.assertRaises( |
---|
108 | DuplicationError, |
---|
109 | self.app['certs2'].__setitem__, 'CERT1', cert2) |
---|
110 | assert len(container2) == 0 |
---|
111 | |
---|
112 | def test_setitem_name_unequal_code(self): |
---|
113 | # we cannot add certificates whose code != key |
---|
114 | container = CertificateContainer() |
---|
115 | self.app['certs'] = container # enable catalogs |
---|
116 | cert1 = Certificate(code="CERT1") |
---|
117 | self.assertRaises( |
---|
118 | ValueError, |
---|
119 | container.__setitem__, 'OtherKey', cert1) |
---|
120 | assert len(container) == 0 |
---|
121 | |
---|
122 | def test_clear(self): |
---|
123 | # clear() really empties the container. |
---|
124 | container = CertificateContainer() |
---|
125 | self.app['certs'] = container # enable catalogs |
---|
126 | cert1 = Certificate(code="CERT1") |
---|
127 | cert2 = Certificate(code="CERT2") |
---|
128 | container['CERT1'] = cert1 |
---|
129 | container['CERT2'] = cert2 |
---|
130 | container.clear() |
---|
131 | assert len(container) == 0 |
---|
132 | |
---|
133 | |
---|
134 | def test_suite(): |
---|
135 | suite = unittest.TestSuite(( |
---|
136 | unittest.makeSuite(CertificateContainerTests), |
---|
137 | doctestsuite_for_module( |
---|
138 | 'waeup.sirp.university.certificatecontainer'), |
---|
139 | )) |
---|
140 | return suite |
---|
141 | |
---|
142 | if __name__=='__main__': |
---|
143 | unittest.main(defaultTest='test_suite') |
---|