Suppress deprecation warnings under Py3k.
| | |
| | | encodestring = base64.encodestring |
| | | |
| | | try: |
| | | from urllib.parse import parse_qs |
| | | except ImportError: #pragma NO COVER Python < 3.0 |
| | | from cgi import parse_qs |
| | | from cgi import parse_qsl |
| | | else: #pragma NO COVER Python >= 3.0 |
| | | from urllib.parse import parse_qsl |
| | | |
| | | try: |
| | | from ConfigParser import ConfigParser |
| | | except ImportError: #pragma NO COVER Python >= 3.0 |
| | | from configparser import ConfigParser |
| | |
| | | import unittest |
| | | |
| | | |
| | | class TestAuthTktCookiePlugin(unittest.TestCase): |
| | | tempdir = None |
| | | _now_testing = None |
| | |
| | | if self._now_testing is not None: |
| | | self._setNowTesting(self._now_testing) |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin |
| | | return AuthTktCookiePlugin |
| | |
| | | plugin = self._getTargetClass()(*arg, **kw) |
| | | return plugin |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | def _makeEnviron(self, kw=None): |
| | | from wsgiref.util import setup_testing_defaults |
| | | environ = {} |
| | |
| | | import unittest |
| | | |
| | | |
| | | class TestHTPasswdPlugin(unittest.TestCase): |
| | | |
| | | def _getTargetClass(self): |
| | |
| | | environ.update(kw) |
| | | return environ |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | def test_implements(self): |
| | | from zope.interface.verify import verifyClass |
| | | from repoze.who.interfaces import IAuthenticator |
| | |
| | | import unittest |
| | | |
| | | class _Base(unittest.TestCase): |
| | | |
| | | class TestRedirectorPlugin(unittest.TestCase): |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | |
| | | class TestRedirectorPlugin(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.plugins.redirector import RedirectorPlugin |
| | |
| | | reason_header='X-Reason') |
| | | |
| | | def test_challenge(self): |
| | | import cgi |
| | | from repoze.who._compat import urlparse |
| | | from ..._compat import parse_qsl |
| | | from ..._compat import urlparse |
| | | plugin = self._makeOne(came_from_param='came_from', |
| | | reason_param='reason', |
| | | reason_header='X-Authorization-Failure-Reason', |
| | |
| | | self.assertEqual(sr.headers[1][0], 'Location') |
| | | url = sr.headers[1][1] |
| | | parts = urlparse(url) |
| | | parts_qsl = cgi.parse_qsl(parts[4]) |
| | | parts_qsl = parse_qsl(parts[4]) |
| | | self.assertEqual(len(parts_qsl), 1) |
| | | came_from_key, came_from_value = parts_qsl[0] |
| | | self.assertEqual(parts[0], 'http') |
| | |
| | | self.assertEqual(sr.status, '302 Found') |
| | | |
| | | def test_challenge_with_reason_header(self): |
| | | import cgi |
| | | from repoze.who._compat import urlparse |
| | | from ..._compat import parse_qsl |
| | | from ..._compat import urlparse |
| | | plugin = self._makeOne(came_from_param='came_from', |
| | | reason_param='reason', |
| | | reason_header='X-Authorization-Failure-Reason', |
| | |
| | | self.assertEqual(sr.headers[1][0], 'Location') |
| | | url = sr.headers[1][1] |
| | | parts = urlparse(url) |
| | | parts_qsl = cgi.parse_qsl(parts[4]) |
| | | parts_qsl = parse_qsl(parts[4]) |
| | | self.assertEqual(len(parts_qsl), 2) |
| | | parts_qsl.sort() |
| | | came_from_key, came_from_value = parts_qsl[0] |
| | |
| | | self.assertEqual(reason_value, 'you are ugly') |
| | | |
| | | def test_challenge_with_custom_reason_header(self): |
| | | import cgi |
| | | from repoze.who._compat import urlparse |
| | | from ..._compat import parse_qsl |
| | | from ..._compat import urlparse |
| | | plugin = self._makeOne(came_from_param='came_from', |
| | | reason_param='reason', |
| | | reason_header='X-Custom-Auth-Failure', |
| | |
| | | self.assertEqual(sr.headers[1][0], 'Location') |
| | | url = sr.headers[1][1] |
| | | parts = urlparse(url) |
| | | parts_qsl = cgi.parse_qsl(parts[4]) |
| | | parts_qsl = parse_qsl(parts[4]) |
| | | self.assertEqual(len(parts_qsl), 1) |
| | | came_from_key, came_from_value = parts_qsl[0] |
| | | self.assertEqual(parts[0], 'http') |
| | |
| | | self.assertEqual(came_from_value, 'http://www.example.com/?default=1') |
| | | |
| | | def test_challenge_w_reason_no_reason_param_no_came_from_param(self): |
| | | import cgi |
| | | from repoze.who._compat import urlparse |
| | | from ..._compat import parse_qsl |
| | | from ..._compat import urlparse |
| | | plugin = self._makeOne() |
| | | environ = self._makeEnviron() |
| | | app = plugin.challenge( |
| | |
| | | self.assertEqual(sr.headers[1][0], 'Location') |
| | | url = sr.headers[1][1] |
| | | parts = urlparse(url) |
| | | parts_qsl = cgi.parse_qsl(parts[4]) |
| | | parts_qsl = parse_qsl(parts[4]) |
| | | self.assertEqual(len(parts_qsl), 0) |
| | | self.assertEqual(parts[0], 'http') |
| | | self.assertEqual(parts[1], 'example.com') |
| | |
| | | self.assertEqual(parts[3], '') |
| | | |
| | | def test_challenge_w_reason_no_reason_param_w_came_from_param(self): |
| | | import cgi |
| | | from repoze.who._compat import urlparse |
| | | from ..._compat import parse_qsl |
| | | from ..._compat import urlparse |
| | | plugin = self._makeOne(came_from_param='came_from', |
| | | ) |
| | | environ = self._makeEnviron() |
| | |
| | | self.assertEqual(sr.headers[1][0], 'Location') |
| | | url = sr.headers[1][1] |
| | | parts = urlparse(url) |
| | | parts_qsl = cgi.parse_qsl(parts[4]) |
| | | parts_qsl = parse_qsl(parts[4]) |
| | | self.assertEqual(len(parts_qsl), 1) |
| | | came_from_key, came_from_value = parts_qsl[0] |
| | | self.assertEqual(parts[0], 'http') |
| | |
| | | self.assertEqual(came_from_value, 'http://www.example.com/?default=1') |
| | | |
| | | def test_challenge_with_reason_and_custom_reason_param(self): |
| | | import cgi |
| | | from repoze.who._compat import urlparse |
| | | from ..._compat import parse_qsl |
| | | from ..._compat import urlparse |
| | | plugin = self._makeOne(came_from_param='came_from', |
| | | reason_param='auth_failure', |
| | | reason_header='X-Custom-Auth-Failure', |
| | |
| | | self.assertEqual(sr.headers[1][0], 'Location') |
| | | url = sr.headers[1][1] |
| | | parts = urlparse(url) |
| | | parts_qsl = cgi.parse_qsl(parts[4]) |
| | | parts_qsl = parse_qsl(parts[4]) |
| | | self.assertEqual(len(parts_qsl), 2) |
| | | parts_qsl.sort() |
| | | reason_key, reason_value = parts_qsl[0] |
| | |
| | | self.assertEqual(reason_value, 'you are ugly') |
| | | |
| | | def test_challenge_wo_reason_w_came_from_param(self): |
| | | import cgi |
| | | from repoze.who._compat import urlparse |
| | | from ..._compat import parse_qsl |
| | | from ..._compat import urlparse |
| | | plugin = self._makeOne(came_from_param='came_from') |
| | | environ = self._makeEnviron() |
| | | app = plugin.challenge( |
| | |
| | | self.assertEqual(sr.headers[1][0], 'Location') |
| | | url = sr.headers[1][1] |
| | | parts = urlparse(url) |
| | | parts_qsl = cgi.parse_qsl(parts[4]) |
| | | parts_qsl = parse_qsl(parts[4]) |
| | | self.assertEqual(len(parts_qsl), 1) |
| | | came_from_key, came_from_value = parts_qsl[0] |
| | | self.assertEqual(parts[0], 'http') |
| | |
| | | self.assertEqual(sr.headers[1][0], 'set-cookie') |
| | | self.assertEqual(sr.headers[1][1], 'b') |
| | | |
| | | class Test_make_redirecting_plugin(unittest.TestCase): |
| | | class Test_make_redirecting_plugin(_Base): |
| | | |
| | | def _callFUT(self, *args, **kw): |
| | | from repoze.who.plugins.redirector import make_plugin |
| | |
| | | import unittest |
| | | |
| | | class TestSQLAuthenticatorPlugin(unittest.TestCase): |
| | | class _Base(unittest.TestCase): |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | class TestSQLAuthenticatorPlugin(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.plugins.sql import SQLAuthenticatorPlugin |
| | |
| | | self.assertEqual(dummy_factory.query, None) |
| | | self.assertEqual(dummy_factory.closed, False) |
| | | |
| | | class TestDefaultPasswordCompare(unittest.TestCase): |
| | | class TestDefaultPasswordCompare(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.plugins.sql import default_password_compare |
| | |
| | | result = compare('notpassword', stored) |
| | | self.assertEqual(result, False) |
| | | |
| | | class TestSQLMetadataProviderPlugin(unittest.TestCase): |
| | | class TestSQLMetadataProviderPlugin(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.plugins.sql import SQLMetadataProviderPlugin |
| | |
| | | self.assertEqual(dummy_factory.query, 'select foo from bar') |
| | | self.failIf('__userid' in identity) |
| | | |
| | | class TestMakeSQLAuthenticatorPlugin(unittest.TestCase): |
| | | class TestMakeSQLAuthenticatorPlugin(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.plugins.sql import make_authenticator_plugin |
| | |
| | | self.assertEqual(plugin.conn_factory, DummyConnFactory) |
| | | self.assertEqual(plugin.compare_fn, make_dummy_connfactory) |
| | | |
| | | class TestMakeSQLMetadataProviderPlugin(unittest.TestCase): |
| | | class TestMakeSQLMetadataProviderPlugin(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.plugins.sql import make_metadata_plugin |
| | |
| | | import unittest |
| | | |
| | | class AuthTicketTests(unittest.TestCase): |
| | | class _Base(unittest.TestCase): |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | class AuthTicketTests(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from .._auth_tkt import AuthTicket |
| | |
| | | self.assertEqual(cookie['oatmeal']['secure'], 'true') |
| | | |
| | | |
| | | class BadTicketTests(unittest.TestCase): |
| | | class BadTicketTests(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from .._auth_tkt import BadTicket |
| | |
| | | self.assertEqual(exc.expected, 'foo') |
| | | |
| | | |
| | | class Test_parse_ticket(unittest.TestCase): |
| | | class Test_parse_ticket(_Base): |
| | | |
| | | def _callFUT(self, secret='SEEKRIT', ticket=None, ip='1.2.3.4'): |
| | | from .._auth_tkt import parse_ticket |
| | |
| | | self.assertEqual(user_data, 'DATA') |
| | | |
| | | |
| | | class Test_helpers(unittest.TestCase): |
| | | class Test_helpers(_Base): |
| | | |
| | | # calculate_digest is not very testable, and fully exercised throug callers. |
| | | |
| | |
| | | import unittest |
| | | |
| | | class Test_get_api(unittest.TestCase): |
| | | class _Base(unittest.TestCase): |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | class Test_get_api(_Base): |
| | | |
| | | def _callFUT(self, environ): |
| | | from repoze.who.api import get_api |
| | |
| | | api = self._callFUT(environ) |
| | | self.failUnless(api is expected) |
| | | |
| | | class APIFactoryTests(unittest.TestCase): |
| | | class APIFactoryTests(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.api import APIFactory |
| | |
| | | self.failUnless(api is expected) |
| | | |
| | | |
| | | class TestMakeRegistries(unittest.TestCase): |
| | | class TestMakeRegistries(_Base): |
| | | |
| | | def _callFUT(self, identifiers, authenticators, challengers, mdproviders): |
| | | from repoze.who.api import make_registries |
| | |
| | | self.assertEqual(name_reg['challenger'], dummy_challenger) |
| | | self.assertEqual(name_reg['mdprovider'], dummy_mdprovider) |
| | | |
| | | class TestMatchClassification(unittest.TestCase): |
| | | class TestMatchClassification(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.api import match_classification |
| | |
| | | # any for either |
| | | self.assertEqual(f(IAuthenticator, plugins, 'buz'), [multi1, multi2]) |
| | | |
| | | class APITests(unittest.TestCase): |
| | | class APITests(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.api import API |
| | |
| | | self.assertEqual(identity.get('fuz'), None) |
| | | |
| | | |
| | | class TestIdentityDict(unittest.TestCase): |
| | | class TestIdentityDict(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.api import Identity |
| | |
| | | import unittest |
| | | |
| | | class TestDefaultRequestClassifier(unittest.TestCase): |
| | | class _Base(unittest.TestCase): |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | class TestDefaultRequestClassifier(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.classifiers import default_request_classifier |
| | |
| | | self.assertEqual(result, 'browser') |
| | | |
| | | |
| | | class TestDefaultChallengeDecider(unittest.TestCase): |
| | | class TestDefaultChallengeDecider(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.classifiers import default_challenge_decider |
| | |
| | | decider = self._getFUT() |
| | | self.failIf(decider({}, '200 Ok', [])) |
| | | |
| | | class TestPassthroughChallengeDecider(unittest.TestCase): |
| | | class TestPassthroughChallengeDecider(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.classifiers import passthrough_challenge_decider |
| | |
| | | import unittest |
| | | |
| | | class TestWhoConfig(unittest.TestCase): |
| | | class _Base(unittest.TestCase): |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | class TestWhoConfig(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.config import WhoConfig |
| | |
| | | iface = iface |
| | | """ |
| | | |
| | | class TestConfigMiddleware(unittest.TestCase): |
| | | class TestConfigMiddleware(_Base): |
| | | _tempdir = None |
| | | |
| | | def setUp(self): |
| | |
| | | self.failUnless(isinstance(handlers[0], NullHandler)) |
| | | logging.shutdown() |
| | | |
| | | class NullHandlerTests(unittest.TestCase): |
| | | class NullHandlerTests(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.config import NullHandler |
| | |
| | | handler = self._makeOne() |
| | | handler.emit(object()) |
| | | |
| | | class Test_make_api_factory_with_config(unittest.TestCase): |
| | | class Test_make_api_factory_with_config(_Base): |
| | | _tempdir = None |
| | | _warning_filters = None |
| | | |
| | |
| | | import unittest |
| | | |
| | | class TestMiddleware(unittest.TestCase): |
| | | class _Base(unittest.TestCase): |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | class TestMiddleware(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.middleware import PluggableAuthenticationMiddleware |
| | |
| | | # XXX need more call tests: |
| | | # - auth_id sorting |
| | | |
| | | class TestStartResponseWrapper(unittest.TestCase): |
| | | class TestStartResponseWrapper(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.middleware import StartResponseWrapper |
| | |
| | | self.assertEqual(datases[0], 'written') |
| | | self.assertEqual(closededs[0], True) |
| | | |
| | | class WrapGeneratorTests(unittest.TestCase): |
| | | class WrapGeneratorTests(_Base): |
| | | |
| | | def _callFUT(self, iterable): |
| | | from repoze.who.middleware import wrap_generator |
| | |
| | | self.assertEqual(list(newgen), ['a', 'b']) |
| | | self.failUnless(iterable._closed) |
| | | |
| | | class TestMakeTestMiddleware(unittest.TestCase): |
| | | class TestMakeTestMiddleware(_Base): |
| | | |
| | | def setUp(self): |
| | | import os |
| | |
| | | import unittest |
| | | |
| | | class AuthenticatedPredicateTests(unittest.TestCase): |
| | | class _Base(unittest.TestCase): |
| | | |
| | | def failUnless(self, predicate, message=''): |
| | | self.assertTrue(predicate, message) # Nannies go home! |
| | | |
| | | def failIf(self, predicate, message=''): |
| | | self.assertFalse(predicate, message) # Nannies go home! |
| | | |
| | | class AuthenticatedPredicateTests(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.restrict import authenticated_predicate |
| | |
| | | environ = {'repoze.who.identity': {'login': 'fred'}} |
| | | self.failUnless(predicate(environ)) |
| | | |
| | | class MakeAuthenticatedRestrictionTests(unittest.TestCase): |
| | | class MakeAuthenticatedRestrictionTests(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.restrict import make_authenticated_restriction |
| | |
| | | self.failUnless(predicate({'REMOTE_USER': 'fred'})) |
| | | self.failUnless(predicate({'repoze.who.identity': {'login': 'fred'}})) |
| | | |
| | | class PredicateRestrictionTests(unittest.TestCase): |
| | | class PredicateRestrictionTests(_Base): |
| | | |
| | | def _getTargetClass(self): |
| | | from repoze.who.restrict import PredicateRestriction |
| | |
| | | self.assertEqual(len(_started), 0) |
| | | self.assertEqual(restrict.app.environ, environ) |
| | | |
| | | class MakePredicateRestrictionTests(unittest.TestCase): |
| | | class MakePredicateRestrictionTests(_Base): |
| | | |
| | | def _getFUT(self): |
| | | from repoze.who.restrict import make_predicate_restriction |