source: main/waeup.sirp/trunk/src/waeup/sirp/university/tests/test_certificatecontainer.py @ 6230

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

Reorder imports.

File size: 4.2 KB
Line 
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"""
23Tests for certificate containers and related components.
24"""
25import shutil
26import tempfile
27import os
28import unittest
29from zope.app.testing.functional import FunctionalTestCase
30from zope.catalog.tests import PlacelessSetup
31from zope.component.hooks import setSite, clearSite
32from zope.interface.verify import verifyClass, verifyObject
33from waeup.sirp.app import University
34from waeup.sirp.testing import FunctionalLayer, WAeUPSIRPUnitTestLayer
35from waeup.sirp.university.certificate import Certificate
36from waeup.sirp.university.certificatecontainer import CertificateContainer
37from waeup.sirp.university.interfaces import ICertificateContainer
38
39
40class 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            ValueError,
96            container.addCertificate, cert2)
97
98    def test_setitem_duplicate_entry(self):
99        # we cannot add certificates whose code exists already in catalog
100        container = CertificateContainer()
101        self.app['certs'] = container # enable catalogs
102        cert1 = Certificate(code="CERT1")
103        cert2 = Certificate(code="CERT1")
104        self.app['certs']['SomeKey'] = cert1
105        self.assertRaises(
106            ValueError,
107            container.__setitem__, 'OtherKey', cert2)
108        assert len(container) == 1
109
110
111def test_suite():
112
113    return unittest.TestSuite((
114        unittest.makeSuite(CertificateContainerTests),
115        ))
116
117if __name__=='__main__':
118    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.