Back to index

plone3  3.1.7
testPAS.py
Go to the documentation of this file.
00001 from zope.publisher.browser import TestRequest
00002 from plone.session.interfaces import ISessionPlugin, ISessionSource
00003 import plone.session
00004 from sessioncase import FunctionalPloneSessionTestCase
00005 
00006 
00007 class TestSessionPlugin(FunctionalPloneSessionTestCase):
00008 
00009     def testInterfaces(self):
00010         session=self.folder.pas.session
00011         self.assertEqual(ISessionPlugin.providedBy(session), True)
00012         source=session.source
00013         self.assertEqual(ISessionSource.providedBy(source), True)
00014 
00015     def makeRequest(self, cookie):
00016         session=self.folder.pas.session
00017         return TestRequest(**{session.cookie_name : cookie})
00018 
00019     def testOneLineCookiesOnly(self):
00020         def createIdentifier(self, *args):
00021             return "x"*256
00022 
00023         class MockResponse:
00024             def setCookie(self, name, value, path):
00025                 self.cookie=value
00026 
00027         response=MockResponse()
00028         session=self.folder.pas.session
00029 
00030         klass=session.source.__class__
00031         org=klass.createIdentifier
00032         klass.createIdentifier=createIdentifier
00033 
00034         session.setupSession(None, response)
00035 
00036         klass.createIdentifier=org
00037         self.assertEqual(len(response.cookie.split()), 1)
00038         
00039 
00040     def testExtraction(self):
00041         session=self.folder.pas.session
00042 
00043         request=self.makeRequest("test string".encode("base64"))
00044         creds=session.extractCredentials(request)
00045         self.assertEqual(creds["source"], "plone.session")
00046         self.assertEqual(creds["cookie"], "test string")
00047 
00048         request=self.makeRequest("test string")
00049         creds=session.extractCredentials(request)
00050         self.assertEqual(creds, {})
00051 
00052     def testCredentialsUpdate(self):
00053         session=self.folder.pas.session
00054         request=self.makeRequest("test string")
00055         session.updateCredentials(request, request.response, "bla", "password")
00056         self.assertEqual(request.response.getCookie(session.cookie_name), None)
00057 
00058         session.updateCredentials(request, request.response,
00059                 "our_user", "password")
00060         self.assertNotEqual(request.response.getCookie(session.cookie_name),
00061                 None)
00062 
00063 
00064 def test_suite():
00065     from unittest import TestSuite, makeSuite
00066     suite=TestSuite()
00067     suite.addTest(makeSuite(TestSessionPlugin))
00068     return suite