Back to index

plone3  3.1.7
testKeymanager.py
Go to the documentation of this file.
00001 from unittest import makeSuite
00002 from unittest import TestCase
00003 from unittest import TestSuite
00004 from persistent.mapping import PersistentMapping
00005 from zope.interface.verify import verifyClass
00006 from plone.keyring.interfaces import IKeyManager
00007 from plone.keyring.keymanager import KeyManager
00008 from plone.keyring.keyring import Keyring
00009 
00010 
00011 marker=[]
00012 
00013 
00014 class KeyManagerTests(TestCase):
00015     def setUp(self):
00016         self.mgr=KeyManager()
00017         del self.mgr[u"_system"]
00018         self.mgr[u"_system"]=Keyring()
00019         self.mgr[u"_system"].rotate()
00020         self.mgr[u"one"]=Keyring()
00021         self.mgr[u"one"].rotate()
00022         self.mgr[u"two"]=Keyring()
00023         self.mgr[u"two"].rotate()
00024 
00025 
00026     def testInterface(self):
00027         verifyClass(IKeyManager, KeyManager)
00028 
00029 
00030     def testSystemKeyringCreated(self):
00031         mgr=KeyManager()
00032         self.assertEqual(mgr.keys(), [ u"_system" ])
00033         self.failUnless(mgr[u"_system"].current is not None)
00034 
00035 
00036     def testContainerIsPersistent(self):
00037         mgr=KeyManager()
00038         self.failUnless(isinstance(mgr.__dict__["_SampleContainer__data"],
00039                                    PersistentMapping))
00040 
00041 
00042     def testClear(self):
00043         self.mgr.clear()
00044         self.assertEqual(set(self.mgr[u"_system"]), set([None]))
00045         self.assertNotEqual(set(self.mgr[u"one"]), set([None]))
00046         self.assertNotEqual(set(self.mgr[u"two"]), set([None]))
00047 
00048 
00049     def testClearGivenRing(self):
00050         self.mgr.clear(u"one")
00051         self.assertNotEqual(set(self.mgr[u"_system"]), set([None]))
00052         self.assertEqual(set(self.mgr[u"one"]), set([None]))
00053         self.assertNotEqual(set(self.mgr[u"two"]), set([None]))
00054 
00055 
00056     def testClearAll(self):
00057         self.mgr.clear(None)
00058         self.assertEqual(set(self.mgr[u"_system"]), set([None]))
00059         self.assertEqual(set(self.mgr[u"one"]), set([None]))
00060         self.assertEqual(set(self.mgr[u"two"]), set([None]))
00061 
00062 
00063     def testClearUnknownRing(self):
00064         self.assertRaises(KeyError, self.mgr.clear, u"missing")
00065 
00066 
00067     def testRotate(self):
00068         current_sys = self.mgr[u"_system"].current
00069         current_one = self.mgr[u"one"].current
00070         current_two = self.mgr[u"two"].current
00071         self.mgr.rotate()
00072         self.assertNotEqual(self.mgr[u"_system"].current, current_sys)
00073         self.assertEqual(self.mgr[u"_system"][1], current_sys)
00074         self.assertEqual(self.mgr[u"one"].current, current_one)
00075         self.assertEqual(self.mgr[u"one"][1], None)
00076         self.assertEqual(self.mgr[u"two"].current, current_two)
00077         self.assertEqual(self.mgr[u"two"][1], None)
00078 
00079 
00080     def testRotateGivenRing(self):
00081         current_sys = self.mgr[u"_system"].current
00082         current_one = self.mgr[u"one"].current
00083         current_two = self.mgr[u"two"].current
00084         self.mgr.rotate(u"one")
00085         self.assertEqual(self.mgr[u"_system"].current, current_sys)
00086         self.assertEqual(self.mgr[u"_system"][1], None)
00087         self.assertNotEqual(self.mgr[u"one"].current, current_one)
00088         self.assertEqual(self.mgr[u"one"][1], current_one)
00089         self.assertEqual(self.mgr[u"two"].current, current_two)
00090         self.assertEqual(self.mgr[u"two"][1], None)
00091 
00092 
00093     def testRotateAll(self):
00094         current_sys = self.mgr[u"_system"].current
00095         current_one = self.mgr[u"one"].current
00096         current_two = self.mgr[u"two"].current
00097         self.mgr.rotate(None)
00098         self.assertNotEqual(self.mgr[u"_system"].current, current_sys)
00099         self.assertEqual(self.mgr[u"_system"][1], current_sys)
00100         self.assertNotEqual(self.mgr[u"one"].current, current_one)
00101         self.assertEqual(self.mgr[u"one"][1], current_one)
00102         self.assertNotEqual(self.mgr[u"two"].current, current_two)
00103         self.assertEqual(self.mgr[u"two"][1], current_two)
00104 
00105 
00106     def testRotateUnknownRing(self):
00107         self.assertRaises(KeyError, self.mgr.clear, u"missing")
00108 
00109 
00110     def testSecret(self):
00111         self.mgr[u"_system"][0]=marker
00112         self.failUnless(self.mgr.secret() is marker)
00113 
00114     def testSecretGivenRing(self):
00115         self.mgr[u"one"][0]=marker
00116         self.failUnless(self.mgr.secret(u"one") is marker)
00117 
00118     def testSecretUnknownRing(self):
00119         self.assertRaises(KeyError, self.mgr.secret, u"missing")
00120 
00121 def test_suite():
00122     suite=TestSuite()
00123     suite.addTest(makeSuite(KeyManagerTests))
00124     return suite
00125