1 | ## $Id: test_certificatecontainer.py 7195 2011-11-25 07:34:07Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | Tests for certificate containers and related components. |
---|
20 | """ |
---|
21 | import shutil |
---|
22 | import tempfile |
---|
23 | import unittest |
---|
24 | from zope.component.hooks import setSite, clearSite |
---|
25 | from zope.interface.verify import verifyClass, verifyObject |
---|
26 | |
---|
27 | from waeup.sirp.app import University |
---|
28 | from waeup.sirp.interfaces import DuplicationError |
---|
29 | from waeup.sirp.testing import ( |
---|
30 | FunctionalLayer, doctestsuite_for_module, FunctionalTestCase) |
---|
31 | from waeup.sirp.university.certificate import Certificate |
---|
32 | from waeup.sirp.university.certificatecontainer import CertificateContainer |
---|
33 | from waeup.sirp.university.interfaces import ICertificateContainer |
---|
34 | |
---|
35 | |
---|
36 | class CertificateContainerTests(FunctionalTestCase): |
---|
37 | |
---|
38 | layer = FunctionalLayer |
---|
39 | |
---|
40 | def setUp(self): |
---|
41 | super(CertificateContainerTests, self).setUp() |
---|
42 | |
---|
43 | # Setup a sample site for each test |
---|
44 | app = University() |
---|
45 | self.dc_root = tempfile.mkdtemp() |
---|
46 | app['datacenter'].setStoragePath(self.dc_root) |
---|
47 | |
---|
48 | # Prepopulate the ZODB... |
---|
49 | self.getRootFolder()['app'] = app |
---|
50 | self.app = self.getRootFolder()['app'] |
---|
51 | setSite(self.app) |
---|
52 | |
---|
53 | def tearDown(self): |
---|
54 | super(CertificateContainerTests, self).tearDown() |
---|
55 | clearSite() |
---|
56 | shutil.rmtree(self.dc_root) |
---|
57 | |
---|
58 | def test_create(self): |
---|
59 | # we can create instances |
---|
60 | container = CertificateContainer() |
---|
61 | result = isinstance(container, CertificateContainer) |
---|
62 | assert result is True |
---|
63 | |
---|
64 | def test_provides_icertificatecontainer(self): |
---|
65 | # instances tell they provide their main interface |
---|
66 | container = CertificateContainer() |
---|
67 | result = ICertificateContainer.providedBy(container) |
---|
68 | assert result is True |
---|
69 | |
---|
70 | def test_interfaces(self): |
---|
71 | # class and instances comply with their main interface |
---|
72 | container = CertificateContainer() |
---|
73 | result1 = verifyClass(ICertificateContainer, CertificateContainer) |
---|
74 | result2 = verifyObject(ICertificateContainer, container) |
---|
75 | |
---|
76 | def test_add_certificate_non_icertificate(self): |
---|
77 | # we cannot add certificates that do not implement ICertificate. |
---|
78 | container = CertificateContainer() |
---|
79 | self.assertRaises( |
---|
80 | TypeError, container.addCertificate, 'some_string') |
---|
81 | |
---|
82 | def test_add_certificate_no_duplicate_entry(self): |
---|
83 | # we cannot add certificates that are registered already. |
---|
84 | container = CertificateContainer() |
---|
85 | # We must place this container in the ZODB to make catalogs work. |
---|
86 | self.app['certs'] = container |
---|
87 | cert1 = Certificate(code="CERT1") |
---|
88 | cert2 = Certificate(code="CERT1") |
---|
89 | container.addCertificate(cert1) |
---|
90 | self.assertRaises( |
---|
91 | DuplicationError, |
---|
92 | container.addCertificate, cert2) |
---|
93 | |
---|
94 | def test_setitem_duplicate_entry(self): |
---|
95 | # we cannot add certificates whose code exists already in catalog |
---|
96 | container1 = CertificateContainer() |
---|
97 | container2 = CertificateContainer() |
---|
98 | self.app['certs1'] = container1 # enable catalogs |
---|
99 | self.app['certs2'] = container2 |
---|
100 | cert1 = Certificate(code="CERT1") |
---|
101 | cert2 = Certificate(code="CERT1") |
---|
102 | self.app['certs1']['CERT1'] = cert1 |
---|
103 | self.assertRaises( |
---|
104 | DuplicationError, |
---|
105 | self.app['certs2'].__setitem__, 'CERT1', cert2) |
---|
106 | assert len(container2) == 0 |
---|
107 | |
---|
108 | def test_setitem_name_unequal_code(self): |
---|
109 | # we cannot add certificates whose code != key |
---|
110 | container = CertificateContainer() |
---|
111 | self.app['certs'] = container # enable catalogs |
---|
112 | cert1 = Certificate(code="CERT1") |
---|
113 | self.assertRaises( |
---|
114 | ValueError, |
---|
115 | container.__setitem__, 'OtherKey', cert1) |
---|
116 | assert len(container) == 0 |
---|
117 | |
---|
118 | def test_clear(self): |
---|
119 | # clear() really empties the container. |
---|
120 | container = CertificateContainer() |
---|
121 | self.app['certs'] = container # enable catalogs |
---|
122 | cert1 = Certificate(code="CERT1") |
---|
123 | cert2 = Certificate(code="CERT2") |
---|
124 | container['CERT1'] = cert1 |
---|
125 | container['CERT2'] = cert2 |
---|
126 | container.clear() |
---|
127 | assert len(container) == 0 |
---|
128 | |
---|
129 | |
---|
130 | def test_suite(): |
---|
131 | suite = unittest.TestSuite(( |
---|
132 | unittest.makeSuite(CertificateContainerTests), |
---|
133 | doctestsuite_for_module( |
---|
134 | 'waeup.sirp.university.certificatecontainer'), |
---|
135 | )) |
---|
136 | return suite |
---|
137 | |
---|
138 | if __name__=='__main__': |
---|
139 | unittest.main(defaultTest='test_suite') |
---|