Back to index

plone3  3.1.7
test_HTTPBasicAuthHelper.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 from Products.PluggableAuthService.tests.conformance \
00017      import ILoginPasswordHostExtractionPlugin_conformance
00018 from Products.PluggableAuthService.tests.conformance \
00019      import IChallengePlugin_conformance
00020 from Products.PluggableAuthService.tests.conformance \
00021      import ICredentialsUpdatePlugin_conformance
00022 from Products.PluggableAuthService.tests.conformance \
00023      import ICredentialsResetPlugin_conformance
00024 
00025 class FauxHTTPRequest:
00026 
00027     def __init__( self, name=None, password=None ):
00028 
00029         self._name = name
00030         self._password = password
00031 
00032     def _authUserPW( self ):
00033 
00034         if self._name is None:
00035             return None
00036 
00037         return self._name, self._password
00038 
00039     def get(self, name, default=None):
00040         return getattr(self, name, default)
00041 
00042 class FauxHTTPResponse:
00043 
00044     _unauthorized_called = 0
00045     realm = 'unit test'
00046     debug_mode = 0
00047     headers = {}
00048 
00049     def unauthorized( self ):
00050 
00051         self._unauthorized_called = 1
00052 
00053     def setStatus(self, status, reason=None):
00054 
00055         self.status = status
00056 
00057     def setHeader(self, name, value, literal=0):
00058 
00059         self.headers[name] = value
00060 
00061     def addHeader(self, name, value):
00062 
00063         self.headers[name] = value
00064 
00065     def setBody(self, body, is_error=0):
00066         self.body = body
00067 
00068 
00069 class HTTPBasicAuthHelperTests( unittest.TestCase
00070                               , ILoginPasswordHostExtractionPlugin_conformance
00071                               , IChallengePlugin_conformance
00072                               , ICredentialsResetPlugin_conformance
00073                               ):
00074 
00075     def _getTargetClass( self ):
00076 
00077         from Products.PluggableAuthService.plugins.HTTPBasicAuthHelper \
00078             import HTTPBasicAuthHelper
00079 
00080         return HTTPBasicAuthHelper
00081 
00082     def _makeOne( self, id='test', *args, **kw ):
00083 
00084         return self._getTargetClass()( id=id, *args, **kw )
00085 
00086     def test_extractCredentials_no_creds( self ):
00087 
00088         helper = self._makeOne()
00089         request = FauxHTTPRequest()
00090 
00091         self.assertEqual( helper.extractCredentials( request ), {} )
00092 
00093     def test_extractCredentials_with_creds( self ):
00094 
00095         helper = self._makeOne()
00096         request = FauxHTTPRequest( 'foo', 'bar' )
00097 
00098         self.assertEqual( helper.extractCredentials( request )
00099                         , { 'login' : 'foo', 'password' : 'bar',
00100                             'remote_host': '', 'remote_address': '' } )
00101 
00102     def test_challenge( self ):
00103         from zExceptions import Unauthorized
00104 
00105         helper = self._makeOne()
00106         request = FauxHTTPRequest()
00107         response = FauxHTTPResponse()
00108 
00109         self.failIf( response._unauthorized_called )
00110         helper.challenge(request, response)
00111         self.failUnless(response.status, 401)
00112         self.failUnless(response.headers['WWW-Authenticate'],
00113             'basic realm="unit test"')
00114 
00115 
00116     def test_resetCredentials( self ):
00117 
00118         helper = self._makeOne()
00119         request = FauxHTTPRequest()
00120         response = FauxHTTPResponse()
00121 
00122         self.failIf( response._unauthorized_called )
00123         helper.resetCredentials( request, response )
00124         self.failUnless( response._unauthorized_called )
00125 
00126 if __name__ == "__main__":
00127     unittest.main()
00128 
00129 def test_suite():
00130     return unittest.TestSuite((
00131         unittest.makeSuite( HTTPBasicAuthHelperTests ),
00132         ))