Back to index

plone3  3.1.7
test_DomainAuthHelper.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 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 IExtractionPlugin_conformance
00019 from Products.PluggableAuthService.tests.conformance \
00020      import IAuthenticationPlugin_conformance
00021 from Products.PluggableAuthService.tests.conformance \
00022      import IRolesPlugin_conformance
00023 
00024 class FauxRequest:
00025 
00026     def __init__(self, **kw):
00027         self._data = dict(kw)
00028 
00029     def get(self, key, default=None):
00030         return self._data.get(key, default)
00031 
00032 class FauxRequestWithClientAddr(FauxRequest):
00033 
00034     def getClientAddr(self):
00035         return self._data.get('CLIENT_ADDR')
00036 
00037 class DomainAuthHelperTests( unittest.TestCase
00038                            , IExtractionPlugin_conformance
00039                            , IAuthenticationPlugin_conformance
00040                            , IRolesPlugin_conformance
00041                            ):
00042 
00043     def _getTargetClass( self ):
00044 
00045         from Products.PluggableAuthService.plugins.DomainAuthHelper \
00046             import DomainAuthHelper
00047 
00048         return DomainAuthHelper
00049 
00050     def _makeOne( self, id='test', *args, **kw ):
00051 
00052         return self._getTargetClass()( id=id, *args, **kw )
00053 
00054     def test_extractCredentials_no_creds( self ):
00055 
00056         helper = self._makeOne()
00057         request = FauxRequest()
00058 
00059         self.assertEqual( helper.extractCredentials( request ), {} )
00060 
00061     def test_extractCredentials_with_getClientAddr( self ):
00062 
00063         helper = self._makeOne()
00064         request = FauxRequestWithClientAddr(REMOTE_HOST='foo',
00065                                             CLIENT_ADDR='bar')
00066 
00067         self.assertEqual(helper.extractCredentials(request),
00068                         {'remote_host': 'foo',
00069                          'remote_address': 'bar'})
00070 
00071     def test_extractCredentials_no_getClientAddr_with_REMOTE_ADDR( self ):
00072 
00073         helper = self._makeOne()
00074         request = FauxRequest(REMOTE_HOST='foo',
00075                               REMOTE_ADDR='bam')
00076 
00077         self.assertEqual(helper.extractCredentials(request),
00078                         {'remote_host': 'foo',
00079                          'remote_address': 'bam'})
00080 
00081     def test_extractCredentials_with_getClientAddr_no_REMOTE_HSOT( self ):
00082 
00083         helper = self._makeOne()
00084         request = FauxRequestWithClientAddr(CLIENT_ADDR='bar')
00085 
00086         self.assertEqual(helper.extractCredentials(request),
00087                         {'remote_host': '',
00088                          'remote_address': 'bar'})
00089 
00090     def test_extractCredentials_with_REMOTE_ADDR_no_REMOTE_HOST( self ):
00091 
00092         helper = self._makeOne()
00093         request = FauxRequest(REMOTE_ADDR='bam')
00094 
00095         self.assertEqual(helper.extractCredentials(request),
00096                         {'remote_host': '',
00097                          'remote_address': 'bam'})
00098 
00099     def test_extractCredentials_no_getClientAddr_no_REMOTE_ADDR( self ):
00100 
00101         helper = self._makeOne()
00102         request = FauxRequest(REMOTE_HOST='foo')
00103 
00104         self.assertEqual(helper.extractCredentials(request),
00105                         {'remote_host': 'foo',
00106                          'remote_address': ''})
00107 
00108     def test_authenticateCredentials_empty_mapping_empty_creds(self):
00109         creds = {}
00110         helper = self._makeOne()
00111         self.assertEqual(helper.authenticateCredentials(creds), (None, None))
00112 
00113     def test_authenticateCredentials_empty_mapping_nonesuch_remote_host(self):
00114         creds = {'remote_host': 'foo'}
00115         helper = self._makeOne()
00116         self.assertEqual(helper.authenticateCredentials(creds), (None, None))
00117 
00118     def test_authenticateCredentials_empty_mapping_nonesuch_remote_addr(self):
00119         creds = {'remote_address': 'bam'}
00120         helper = self._makeOne()
00121         self.assertEqual(helper.authenticateCredentials(creds), (None, None))
00122 
00123     def test_authenticateCredentials_w_mapping_known_remote_host(self):
00124         from Products.PluggableAuthService.plugins.DomainAuthHelper \
00125             import _MATCH_EQUALS
00126 
00127         creds = {'login': 'qux', 'remote_host': 'bam'}
00128         helper = self._makeOne()
00129         helper.manage_addMapping(match_type=_MATCH_EQUALS, match_string='bam')
00130 
00131         self.assertEqual(helper.authenticateCredentials(creds), ('qux', 'qux'))
00132 
00133     def test_authenticateCredentials_w_mapping_known_remote_addr(self):
00134         from Products.PluggableAuthService.plugins.DomainAuthHelper \
00135             import _MATCH_ENDSWITH
00136 
00137         creds = {'login': 'qux', 'remote_address': 'baz'}
00138         helper = self._makeOne()
00139         helper.manage_addMapping(match_type=_MATCH_ENDSWITH,
00140                                  match_string='z',
00141                                  username='foo',
00142                                 )
00143 
00144         self.assertEqual(helper.authenticateCredentials(creds), ('qux', 'qux'))
00145 
00146     def test_authenticateCredentials_w_mapping_no_login_known_remote_host(self):
00147         from Products.PluggableAuthService.plugins.DomainAuthHelper \
00148             import _MATCH_EQUALS
00149 
00150         creds = {'remote_host': 'baz'}
00151         helper = self._makeOne()
00152         helper.manage_addMapping(match_type=_MATCH_EQUALS,
00153                                  match_string='baz',
00154                                  username='foo',
00155                                 )
00156 
00157         self.assertEqual(helper.authenticateCredentials(creds), ('foo', 'foo'))
00158 
00159     # TODO  add tests for getRolesForPrincipal, etc.
00160 
00161 
00162 if __name__ == "__main__":
00163     unittest.main()
00164 
00165 def test_suite():
00166     return unittest.TestSuite((
00167         unittest.makeSuite( DomainAuthHelperTests ),
00168         ))
00169