Back to index

plone3  3.1.7
test_cookieauth.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Cookie crumbler xml adapter and setup handler unit tests.
00014 
00015 $Id: test_cookieauth.py 77004 2007-06-24 08:57:54Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from OFS.Folder import Folder
00022 
00023 from Products.GenericSetup.testing import BodyAdapterTestCase
00024 from Products.GenericSetup.tests.common import BaseRegistryTests
00025 from Products.GenericSetup.tests.common import DummyExportContext
00026 from Products.GenericSetup.tests.common import DummyImportContext
00027 
00028 from Products.CMFCore.CookieCrumbler import CookieCrumbler
00029 from Products.CMFCore.testing import ExportImportZCMLLayer
00030 
00031 _COOKIECRUMBLER_BODY = """\
00032 <?xml version="1.0"?>
00033 <object name="foo_cookiecrumbler" meta_type="Cookie Crumbler">
00034  <property name="auth_cookie">__ac</property>
00035  <property name="name_cookie">__ac_name</property>
00036  <property name="pw_cookie">__ac_password</property>
00037  <property name="persist_cookie">__ac_persistent</property>
00038  <property name="auto_login_page">login_form</property>
00039  <property name="logout_page">logged_out</property>
00040  <property name="unauth_page"></property>
00041  <property name="local_cookie_path">False</property>
00042  <property name="cache_header_value">private</property>
00043  <property name="log_username">True</property>
00044 </object>
00045 """
00046 
00047 _DEFAULT_EXPORT = """\
00048 <?xml version="1.0"?>
00049 <object name="foo_cookiecrumbler" meta_type="Cookie Crumbler">
00050  <property name="auth_cookie">__ac</property>
00051  <property name="name_cookie">__ac_name</property>
00052  <property name="pw_cookie">__ac_password</property>
00053  <property name="persist_cookie">__ac_persistent</property>
00054  <property name="auto_login_page">login_form</property>
00055  <property name="logout_page">logged_out</property>
00056  <property name="unauth_page"></property>
00057  <property name="local_cookie_path">False</property>
00058  <property name="cache_header_value">private</property>
00059  <property name="log_username">True</property>
00060 </object>
00061 """
00062 
00063 _CHANGED_EXPORT = """\
00064 <?xml version="1.0"?>
00065 <object name="foo_cookiecrumbler" meta_type="Cookie Crumbler">
00066  <property name="auth_cookie">value1</property>
00067  <property name="name_cookie">value3</property>
00068  <property name="pw_cookie">value5</property>
00069  <property name="persist_cookie">value4</property>
00070  <property name="auto_login_page">value6</property>
00071  <property name="logout_page">value8</property>
00072  <property name="unauth_page">value7</property>
00073  <property name="local_cookie_path">True</property>
00074  <property name="cache_header_value">value2</property>
00075  <property name="log_username">False</property>
00076 </object>
00077 """
00078 
00079 
00080 class CookieCrumblerXMLAdapterTests(BodyAdapterTestCase):
00081 
00082     layer = ExportImportZCMLLayer
00083 
00084     def _getTargetClass(self):
00085         from Products.CMFCore.exportimport.cookieauth \
00086                 import CookieCrumblerXMLAdapter
00087 
00088         return CookieCrumblerXMLAdapter
00089 
00090     def setUp(self):
00091         BodyAdapterTestCase.setUp(self)
00092         self._obj = CookieCrumbler('foo_cookiecrumbler')
00093         self._BODY = _COOKIECRUMBLER_BODY
00094 
00095 
00096 class _CookieCrumblerSetup(BaseRegistryTests):
00097 
00098     def _initSite(self, use_changed=False):
00099         self.root.site = Folder(id='site')
00100         site = self.root.site
00101         cc = site.cookie_authentication = CookieCrumbler('foo_cookiecrumbler')
00102 
00103         if use_changed:
00104             cc.auth_cookie = 'value1'
00105             cc.cache_header_value = 'value2'
00106             cc.name_cookie = 'value3'
00107             cc.log_username = 0
00108             cc.persist_cookie = 'value4'
00109             cc.pw_cookie = 'value5'
00110             cc.local_cookie_path = 1
00111             cc.auto_login_page = 'value6'
00112             cc.unauth_page = 'value7'
00113             cc.logout_page = 'value8'
00114 
00115         return site
00116 
00117 
00118 class exportCookieCrumblerTests(_CookieCrumblerSetup):
00119 
00120     layer = ExportImportZCMLLayer
00121 
00122     def test_unchanged(self):
00123         from Products.CMFCore.exportimport.cookieauth \
00124                 import exportCookieCrumbler
00125 
00126         site = self._initSite(use_changed=False)
00127         context = DummyExportContext(site)
00128         exportCookieCrumbler(context)
00129 
00130         self.assertEqual(len(context._wrote), 1)
00131         filename, text, content_type = context._wrote[0]
00132         self.assertEqual(filename, 'cookieauth.xml')
00133         self._compareDOM(text, _DEFAULT_EXPORT)
00134         self.assertEqual(content_type, 'text/xml')
00135 
00136     def test_changed(self):
00137         from Products.CMFCore.exportimport.cookieauth \
00138                 import exportCookieCrumbler
00139 
00140         site = self._initSite(use_changed=True)
00141         context = DummyExportContext(site)
00142         exportCookieCrumbler(context)
00143 
00144         self.assertEqual(len(context._wrote), 1)
00145         filename, text, content_type = context._wrote[0]
00146         self.assertEqual(filename, 'cookieauth.xml')
00147         self._compareDOM(text, _CHANGED_EXPORT)
00148         self.assertEqual(content_type, 'text/xml')
00149 
00150 
00151 class importCookieCrumblerTests(_CookieCrumblerSetup):
00152 
00153     layer = ExportImportZCMLLayer
00154 
00155     def test_normal(self):
00156         from Products.CMFCore.exportimport.cookieauth \
00157                 import importCookieCrumbler
00158 
00159         site = self._initSite()
00160         cc = site.cookie_authentication
00161 
00162         context = DummyImportContext(site)
00163         context._files['cookieauth.xml'] = _CHANGED_EXPORT
00164         importCookieCrumbler(context)
00165 
00166         self.assertEqual( cc.auth_cookie, 'value1' )
00167         self.assertEqual( cc.cache_header_value, 'value2' )
00168         self.assertEqual( cc.name_cookie, 'value3' )
00169         self.assertEqual( cc.log_username, 0 )
00170         self.assertEqual( cc.persist_cookie, 'value4' )
00171         self.assertEqual( cc.pw_cookie, 'value5' )
00172         self.assertEqual( cc.local_cookie_path, 1 )
00173         self.assertEqual( cc.auto_login_page, 'value6' )
00174         self.assertEqual( cc.unauth_page, 'value7' )
00175         self.assertEqual( cc.logout_page, 'value8' )
00176 
00177 
00178 def test_suite():
00179     return unittest.TestSuite((
00180         unittest.makeSuite(CookieCrumblerXMLAdapterTests),
00181         unittest.makeSuite(exportCookieCrumblerTests),
00182         unittest.makeSuite(importCookieCrumblerTests),
00183         ))
00184 
00185 if __name__ == '__main__':
00186     from Products.CMFCore.testing import run
00187     run(test_suite())