from django.contrib.auth import authenticate from django.contrib.auth.models import User from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.contrib.auth.views import ( PasswordChangeDoneView, PasswordChangeView, PasswordResetCompleteView, PasswordResetConfirmView, PasswordResetDoneView, PasswordResetView, ) from django.test import RequestFactory, TestCase, override_settings from django.utils.encoding import force_bytes, force_text from django.utils.http import urlsafe_base64_encode @override_settings(ROOT_URLCONF='auth_tests.urls') class AuthTemplateTests(TestCase): def test_titles(self): rf = RequestFactory() user = User.objects.create_user('jsmith', 'jsmith@example.com', 'pass') user = authenticate(username=user.username, password='pass') request = rf.get('/somepath/') request.user = user response = PasswordResetView.as_view(success_url='dummy/')(request) self.assertContains(response, 'Password reset') self.assertContains(response, '

Password reset

') response = PasswordResetDoneView.as_view()(request) self.assertContains(response, 'Password reset sent') self.assertContains(response, '

Password reset sent

') # PasswordResetConfirmView invalid token response = PasswordResetConfirmView.as_view(success_url='dummy/')(request, uidb64='Bad', token='Bad') self.assertContains(response, 'Password reset unsuccessful') self.assertContains(response, '

Password reset unsuccessful

') # PasswordResetConfirmView valid token default_token_generator = PasswordResetTokenGenerator() token = default_token_generator.make_token(user) uidb64 = force_text(urlsafe_base64_encode(force_bytes(user.pk))) response = PasswordResetConfirmView.as_view(success_url='dummy/')(request, uidb64=uidb64, token=token) self.assertContains(response, 'Enter new password') self.assertContains(response, '

Enter new password

') response = PasswordResetCompleteView.as_view()(request) self.assertContains(response, 'Password reset complete') self.assertContains(response, '

Password reset complete

') response = PasswordChangeView.as_view(success_url='dummy/')(request) self.assertContains(response, 'Password change') self.assertContains(response, '

Password change

') response = PasswordChangeDoneView.as_view()(request) self.assertContains(response, 'Password change successful') self.assertContains(response, '

Password change successful

')