def assertCanLogin(self, user): response = self.app.get('/template/index.html', user=user) res_user = response.context['user'] assert is_authenticated(res_user) if isinstance(user, User): self.assertEqual(res_user, user) else: self.assertEqual(res_user.username, user)
def assertCanLogin(self, user): response = self.app.get('/template/index.html', user=user) res_user = response.context['user'] assert is_authenticated(res_user) if isinstance(user, User): self.assertEqual(res_user, user) else: self.assertEqual(res_user.username, user)
def test_reusing_custom_user(self): if django.VERSION >= (1, 5): from django_webtest_tests.testapp_tests.models import MyCustomUser with self.settings(AUTH_USER_MODEL = 'testapp_tests.MyCustomUser'): custom_user = MyCustomUser.objects.create( email="*****@*****.**") custom_user.set_password("123") custom_user.save() # Let the middleware logs the user in self.app.get('/template/index.html', user=custom_user) # Middleware authentication check shouldn't crash response = self.app.get('/template/index.html', user=custom_user) user = response.context['user'] assert is_authenticated(user) self.assertEqual(user, custom_user)
def test_reusing_custom_user(self): if django.VERSION >= (1, 5): from django_webtest_tests.testapp_tests.models import MyCustomUser with self.settings(AUTH_USER_MODEL='testapp_tests.MyCustomUser'): custom_user = MyCustomUser.objects.create( email="*****@*****.**") custom_user.set_password("123") custom_user.save() # Let the middleware logs the user in self.app.get('/template/index.html', user=custom_user) # Middleware authentication check shouldn't crash response = self.app.get('/template/index.html', user=custom_user) user = response.context['user'] assert is_authenticated(user) self.assertEqual(user, custom_user)
def process_request(self, request): # AuthenticationMiddleware is required so that request.user exists. if not hasattr(request, 'user'): raise ImproperlyConfigured( "The django-webtest auth middleware requires the " "'django.contrib.auth.middleware.AuthenticationMiddleware' " "to be installed. Add it to your MIDDLEWARE_CLASSES setting " "or disable django-webtest auth support " "by setting 'setup_auth' property of your WebTest subclass " "to False." ) try: username = request.META[self.header] except KeyError: # If specified header doesn't exist then return (leaving # request.user set to AnonymousUser by the # AuthenticationMiddleware). return # If the user is already authenticated and that user is the user we are # getting passed in the headers, then the correct user is already # persisted in the session and we don't need to continue. if is_authenticated(request.user): if hasattr(request.user, "get_username"): authenticated_username = request.user.get_username() else: authenticated_username = request.user.username clean_username = self.clean_username(username, request) if authenticated_username == clean_username: return # We are seeing this user for the first time in this session, attempt # to authenticate the user. user = auth.authenticate(django_webtest_user=username) if user: # User is valid. Set request.user and persist user in the session # by logging the user in. request.user = user auth.login(request, user)
def process_request(self, request): # AuthenticationMiddleware is required so that request.user exists. if not hasattr(request, 'user'): raise ImproperlyConfigured( "The django-webtest auth middleware requires the " "'django.contrib.auth.middleware.AuthenticationMiddleware' " "to be installed. Add it to your MIDDLEWARE_CLASSES setting " "or disable django-webtest auth support " "by setting 'setup_auth' property of your WebTest subclass " "to False.") try: username = request.META[self.header] except KeyError: # If specified header doesn't exist then return (leaving # request.user set to AnonymousUser by the # AuthenticationMiddleware). return # If the user is already authenticated and that user is the user we are # getting passed in the headers, then the correct user is already # persisted in the session and we don't need to continue. if is_authenticated(request.user): if hasattr(request.user, "get_username"): authenticated_username = request.user.get_username() else: authenticated_username = request.user.username clean_username = self.clean_username(username, request) if authenticated_username == clean_username: return # We are seeing this user for the first time in this session, attempt # to authenticate the user. user = auth.authenticate(django_webtest_user=username) if user: # User is valid. Set request.user and persist user in the session # by logging the user in. request.user = user auth.login(request, user)
def test_not_logged_in(self): response = self.app.get('/template/index.html') user = response.context['user'] assert not is_authenticated(user)
def test_not_logged_in(self): response = self.app.get('/template/index.html') user = response.context['user'] assert not is_authenticated(user)