Back to index

plone3  3.1.7
test_InlineAuthHelper.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 import unittest
00016 
00017 from Products.PluggableAuthService.tests.conformance \
00018      import ILoginPasswordHostExtractionPlugin_conformance
00019 from Products.PluggableAuthService.tests.conformance \
00020      import IChallengePlugin_conformance
00021 from Products.PluggableAuthService.tests.conformance \
00022      import ICredentialsUpdatePlugin_conformance
00023 from Products.PluggableAuthService.tests.conformance \
00024      import ICredentialsResetPlugin_conformance
00025 
00026 from Products.PluggableAuthService.tests.test_PluggableAuthService \
00027      import FauxRequest, FauxResponse, FauxObject, FauxRoot, FauxContainer
00028 
00029 class FauxSettableRequest(FauxRequest):
00030 
00031     def set(self, name, value):
00032         self._dict[name] = value
00033 
00034 class FauxInlineResponse(FauxResponse):
00035 
00036     def __init__(self):
00037         self.setBody("Should never see this.")
00038         self.status = '200'
00039         self.headers = {}
00040 
00041     def setStatus(self, status, reason=None):
00042         self.status = status
00043 
00044     def setBody(self, body, *args, **kw):
00045         self.body = body
00046 
00047 class InlineAuthHelperTests( unittest.TestCase
00048                            , ILoginPasswordHostExtractionPlugin_conformance
00049                            , IChallengePlugin_conformance
00050                            ):
00051 
00052     def _getTargetClass( self ):
00053 
00054         from Products.PluggableAuthService.plugins.InlineAuthHelper \
00055             import InlineAuthHelper
00056 
00057         return InlineAuthHelper
00058 
00059     def _makeOne( self, id='test', *args, **kw ):
00060 
00061         return self._getTargetClass()( id=id, *args, **kw )
00062 
00063     def _makeTree( self ):
00064 
00065         rc = FauxObject( 'rc' )
00066         root = FauxRoot( 'root' ).__of__( rc )
00067         folder = FauxContainer( 'folder' ).__of__( root )
00068         object = FauxObject( 'object' ).__of__( folder )
00069 
00070         return rc, root, folder, object
00071 
00072     def test_extractCredentials_no_creds( self ):
00073 
00074         helper = self._makeOne()
00075         response = FauxInlineResponse()
00076         request = FauxRequest(RESPONSE=response)
00077 
00078         self.assertEqual( helper.extractCredentials( request ), {} )
00079 
00080     def test_extractCredentials_with_form_creds( self ):
00081 
00082         helper = self._makeOne()
00083         response = FauxInlineResponse()
00084         request = FauxSettableRequest(__ac_name='foo',
00085                                       __ac_password='bar',
00086                                       RESPONSE=response)
00087 
00088         self.assertEqual(helper.extractCredentials(request),
00089                         {'login': 'foo',
00090                          'password': 'bar',
00091                          'remote_host': '',
00092                          'remote_address': ''})
00093 
00094     def test_challenge( self ):
00095         from zExceptions import Unauthorized
00096         rc, root, folder, object = self._makeTree()
00097         response = FauxInlineResponse()
00098         request = FauxRequest(RESPONSE=response)
00099         root.REQUEST = request
00100 
00101         helper = self._makeOne().__of__(root)
00102         helper.body = "Overridden"
00103 
00104         self.assertEqual(response.body, "Should never see this.")
00105         helper.challenge(request, response)
00106         self.assertEqual(response.body, "Overridden")
00107 
00108 if __name__ == "__main__":
00109     unittest.main()
00110 
00111 def test_suite():
00112     return unittest.TestSuite((
00113         unittest.makeSuite( InlineAuthHelperTests ),
00114         ))
00115