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

Last change on this file since 6232 was 6232, checked in by uli, 13 years ago
  • Fix duplication tests.
  • Add working test for clear() method of CertificateContainer?. This one revealed that the old implementation was in fact faulty.
File size: 4.6 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.interfaces import DuplicationError
35from waeup.sirp.testing import FunctionalLayer, WAeUPSIRPUnitTestLayer
36from waeup.sirp.university.certificate import Certificate
37from waeup.sirp.university.certificatecontainer import CertificateContainer
38from waeup.sirp.university.interfaces import ICertificateContainer
39
40
41class CertificateContainerTests(FunctionalTestCase):
42
43    layer = FunctionalLayer
44
45    def setUp(self):
46        super(CertificateContainerTests, self).setUp()
47
48        # Setup a sample site for each test
49        app = University()
50        self.dc_root = tempfile.mkdtemp()
51        app['datacenter'].setStoragePath(self.dc_root)
52
53        # Prepopulate the ZODB...
54        self.getRootFolder()['app'] = app
55        self.app = self.getRootFolder()['app']
56        setSite(self.app)
57
58    def tearDown(self):
59        super(CertificateContainerTests, self).tearDown()
60        clearSite()
61        shutil.rmtree(self.dc_root)
62
63    def test_create(self):
64        # we can create instances
65        container = CertificateContainer()
66        result = isinstance(container, CertificateContainer)
67        assert result is True
68
69    def test_provides_icertificatecontainer(self):
70        # instances tell they provide their main interface
71        container = CertificateContainer()
72        result = ICertificateContainer.providedBy(container)
73        assert result is True
74
75    def test_interfaces(self):
76        # class and instances comply with their main interface
77        container = CertificateContainer()
78        result1 = verifyClass(ICertificateContainer, CertificateContainer)
79        result2 = verifyObject(ICertificateContainer, container)
80
81    def test_add_certificate_non_icertificate(self):
82        # we cannot add certificates that do not implement ICertificate.
83        container = CertificateContainer()
84        self.assertRaises(
85            TypeError, container.addCertificate, 'some_string')
86
87    def test_add_certificate_no_duplicate_entry(self):
88        # we cannot add certificates that are registered already.
89        container = CertificateContainer()
90        # We must place this container in the ZODB to make catalogs work.
91        self.app['certs'] = container
92        cert1 = Certificate(code="CERT1")
93        cert2 = Certificate(code="CERT1")
94        container.addCertificate(cert1)
95        self.assertRaises(
96            DuplicationError,
97            container.addCertificate, cert2)
98
99    def test_setitem_duplicate_entry(self):
100        # we cannot add certificates whose code exists already in catalog
101        container = CertificateContainer()
102        self.app['certs'] = container # enable catalogs
103        cert1 = Certificate(code="CERT1")
104        cert2 = Certificate(code="CERT1")
105        self.app['certs']['SomeKey'] = cert1
106        self.assertRaises(
107            DuplicationError,
108            container.__setitem__, 'OtherKey', cert2)
109        assert len(container) == 1
110
111    def test_clear(self):
112        container = CertificateContainer()
113        self.app['certs'] = container # enable catalogs
114        cert1 = Certificate(code="CERT1")
115        cert2 = Certificate(code="CERT2")
116        container['CERT1'] = cert1
117        container['CERT2'] = cert2
118        container.clear()
119        assert len(container) == 0
120
121
122def test_suite():
123
124    return unittest.TestSuite((
125        unittest.makeSuite(CertificateContainerTests),
126        ))
127
128if __name__=='__main__':
129    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.