[7195] | 1 | ## $Id: test_certificatescontainer.py 7811 2012-03-08 19:00:51Z uli $ |
---|
[6222] | 2 | ## |
---|
[7195] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[6222] | 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. |
---|
[7195] | 8 | ## |
---|
[6222] | 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. |
---|
[7195] | 13 | ## |
---|
[6222] | 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 |
---|
[6230] | 24 | from zope.component.hooks import setSite, clearSite |
---|
[6222] | 25 | from zope.interface.verify import verifyClass, verifyObject |
---|
[6239] | 26 | |
---|
[7811] | 27 | from waeup.kofa.app import University |
---|
| 28 | from waeup.kofa.interfaces import DuplicationError |
---|
| 29 | from waeup.kofa.testing import ( |
---|
[6740] | 30 | FunctionalLayer, doctestsuite_for_module, FunctionalTestCase) |
---|
[7811] | 31 | from waeup.kofa.university.certificate import Certificate |
---|
| 32 | from waeup.kofa.university.certificatescontainer import CertificatesContainer |
---|
| 33 | from waeup.kofa.university.interfaces import ICertificatesContainer |
---|
[6222] | 34 | |
---|
[6230] | 35 | |
---|
[7333] | 36 | class CertificatesContainerTests(FunctionalTestCase): |
---|
[6222] | 37 | |
---|
| 38 | layer = FunctionalLayer |
---|
| 39 | |
---|
| 40 | def setUp(self): |
---|
[7333] | 41 | super(CertificatesContainerTests, self).setUp() |
---|
[6222] | 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): |
---|
[7333] | 54 | super(CertificatesContainerTests, self).tearDown() |
---|
[6222] | 55 | clearSite() |
---|
| 56 | shutil.rmtree(self.dc_root) |
---|
| 57 | |
---|
| 58 | def test_create(self): |
---|
| 59 | # we can create instances |
---|
[7333] | 60 | container = CertificatesContainer() |
---|
| 61 | result = isinstance(container, CertificatesContainer) |
---|
[6222] | 62 | assert result is True |
---|
| 63 | |
---|
[7333] | 64 | def test_provides_icertificatescontainer(self): |
---|
[6222] | 65 | # instances tell they provide their main interface |
---|
[7333] | 66 | container = CertificatesContainer() |
---|
| 67 | result = ICertificatesContainer.providedBy(container) |
---|
[6222] | 68 | assert result is True |
---|
| 69 | |
---|
| 70 | def test_interfaces(self): |
---|
| 71 | # class and instances comply with their main interface |
---|
[7333] | 72 | container = CertificatesContainer() |
---|
| 73 | self.assertTrue(verifyClass(ICertificatesContainer, CertificatesContainer)) |
---|
| 74 | self.assertTrue(verifyObject(ICertificatesContainer, container)) |
---|
[6222] | 75 | |
---|
| 76 | def test_add_certificate_non_icertificate(self): |
---|
| 77 | # we cannot add certificates that do not implement ICertificate. |
---|
[7333] | 78 | container = CertificatesContainer() |
---|
[6222] | 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. |
---|
[7333] | 84 | container = CertificatesContainer() |
---|
[6222] | 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( |
---|
[6232] | 91 | DuplicationError, |
---|
[6222] | 92 | container.addCertificate, cert2) |
---|
| 93 | |
---|
| 94 | def test_setitem_duplicate_entry(self): |
---|
| 95 | # we cannot add certificates whose code exists already in catalog |
---|
[7333] | 96 | container1 = CertificatesContainer() |
---|
| 97 | container2 = CertificatesContainer() |
---|
[6241] | 98 | self.app['certs1'] = container1 # enable catalogs |
---|
| 99 | self.app['certs2'] = container2 |
---|
[6222] | 100 | cert1 = Certificate(code="CERT1") |
---|
| 101 | cert2 = Certificate(code="CERT1") |
---|
[6241] | 102 | self.app['certs1']['CERT1'] = cert1 |
---|
[6222] | 103 | self.assertRaises( |
---|
[6232] | 104 | DuplicationError, |
---|
[6241] | 105 | self.app['certs2'].__setitem__, 'CERT1', cert2) |
---|
| 106 | assert len(container2) == 0 |
---|
[6222] | 107 | |
---|
[6241] | 108 | def test_setitem_name_unequal_code(self): |
---|
| 109 | # we cannot add certificates whose code != key |
---|
[7333] | 110 | container = CertificatesContainer() |
---|
[6241] | 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 | |
---|
[6232] | 118 | def test_clear(self): |
---|
[6241] | 119 | # clear() really empties the container. |
---|
[7333] | 120 | container = CertificatesContainer() |
---|
[6232] | 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 |
---|
[6222] | 128 | |
---|
[6232] | 129 | |
---|
[6222] | 130 | def test_suite(): |
---|
[6239] | 131 | suite = unittest.TestSuite(( |
---|
[7333] | 132 | unittest.makeSuite(CertificatesContainerTests), |
---|
[6239] | 133 | doctestsuite_for_module( |
---|
[7811] | 134 | 'waeup.kofa.university.certificatescontainer'), |
---|
[6222] | 135 | )) |
---|
[6239] | 136 | return suite |
---|
[6222] | 137 | |
---|
| 138 | if __name__=='__main__': |
---|
| 139 | unittest.main(defaultTest='test_suite') |
---|