def login_required(backend_list=None, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): """ Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary. """ if callable(backend_list): backends = get_backends() else: backends = get_backends(backend_list) backend_names = map(lambda b: b[0], backends) actual_decorator = user_passes_test( lambda u: u.is_authenticated() and (hasattr(u, "_backend_name")) and (u._backend_name in backend_names), login_url=None, redirect_field_name=redirect_field_name, ) if callable(backend_list): return actual_decorator(backend_list) return actual_decorator
def test_get_backends(self): from newauth.backend import BasicUserBackend from newauth.tests.testapp.backends import TestBackend, TestBackend2, TestBackend3 backends = zip(*get_backends()) self.assertTrue('default' in backends[0]) self.assertTrue(isinstance(backends[1][list(backends[0]).index('default')][0], BasicUserBackend), '"default" backend is not a BasicUserBackend') self.assertTrue('testapp' in backends[0]) self.assertTrue(isinstance(backends[1][list(backends[0]).index('testapp')][0], TestBackend), '"testapp" backend is not a TestBackend') self.assertTrue(isinstance(backends[1][list(backends[0]).index('testapp')][1], TestBackend3), '"testapp" backend is not a TestBackend3') self.assertTrue('testapp2' in backends[0]) self.assertTrue(isinstance(backends[1][list(backends[0]).index('testapp2')][0], TestBackend2), '"testapp2" backend is not a TestBackend2') self.assertTrue('testapp3' in backends[0]) self.assertTrue(isinstance(backends[1][list(backends[0]).index('testapp3')][0], TestBackend3), '"testapp3" backend is not a TestBackend3')