Back to index

plone3  3.1.7
testCookieAuth.py
Go to the documentation of this file.
00001 #
00002 # CookieAuth tests
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 
00007 import base64
00008 from urlparse import urlparse
00009 from urllib import quote
00010 from urllib import urlencode
00011 
00012 default_user = PloneTestCase.default_user
00013 default_password = PloneTestCase.default_password
00014 
00015 
00016 class TestCookieAuth(PloneTestCase.FunctionalTestCase):
00017 
00018     def afterSetUp(self):
00019         self.portal_url = self.portal.absolute_url()
00020         self.portal_path = '/%s' % self.portal.absolute_url(1)
00021         self.folder_path = '/%s' % self.folder.absolute_url(1)
00022         self.auth_info = '%s:%s' % (default_user, default_password)
00023         self.cookie = base64.encodestring(self.auth_info)[:-1]
00024         self.folder.manage_permission('View', ['Manager'], acquire=0)
00025 
00026     def testAutoLoginPage(self):
00027         # Should send us to login_form
00028         response = self.publish(self.folder_path)
00029         self.assertEqual(response.getStatus(), 302)
00030 
00031         location = response.getHeader('Location')
00032         self.failUnless(location.startswith(self.portal_url))
00033         self.failUnless(urlparse(location)[2].endswith('/require_login'))
00034 
00035     def testInsufficientPrivileges(self):
00036         # Should send us to login_form
00037         response = self.publish(self.folder_path, extra={'__ac': self.cookie})
00038         self.assertEqual(response.getStatus(), 302)
00039 
00040         location = response.getHeader('Location')
00041         self.failUnless(location.startswith(self.portal_url))
00042         self.failUnless(urlparse(location)[2].endswith('/require_login'))
00043 
00044     def testSetSessionCookie(self):
00045         # The __ac cookie should be set for the session only
00046         form = {'__ac_name': default_user, '__ac_password': default_password}
00047 
00048         response = self.publish(self.portal_path + '/logged_in',
00049                                 env={'QUERY_STRING': urlencode(form)})
00050 
00051         self.assertEqual(response.getStatus(), 200)
00052 
00053         cookie = response.getCookie('__ac')
00054         self.assertEqual(cookie.get('path'), '/')
00055         self.assertEqual(cookie.get('expires'), None)
00056 
00057 
00058 def test_suite():
00059     from unittest import TestSuite, makeSuite
00060     suite = TestSuite()
00061     suite.addTest(makeSuite(TestCookieAuth))
00062     return suite