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 (
password_change, password_change_done, password_reset,
password_reset_complete, password_reset_confirm, password_reset_done,
)
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(
PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'],
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 = password_reset(request, post_reset_redirect='dummy/')
self.assertContains(response, '
Password reset')
self.assertContains(response, 'Password reset
')
response = password_reset_done(request)
self.assertContains(response, 'Password reset sent')
self.assertContains(response, 'Password reset sent
')
# password_reset_confirm invalid token
response = password_reset_confirm(request, uidb64='Bad', token='Bad', post_reset_redirect='dummy/')
self.assertContains(response, 'Password reset unsuccessful')
self.assertContains(response, 'Password reset unsuccessful
')
# password_reset_confirm valid token
default_token_generator = PasswordResetTokenGenerator()
token = default_token_generator.make_token(user)
uidb64 = force_text(urlsafe_base64_encode(force_bytes(user.pk)))
response = password_reset_confirm(request, uidb64, token, post_reset_redirect='dummy/')
self.assertContains(response, 'Enter new password')
self.assertContains(response, 'Enter new password
')
response = password_reset_complete(request)
self.assertContains(response, 'Password reset complete')
self.assertContains(response, 'Password reset complete
')
response = password_change(request, post_change_redirect='dummy/')
self.assertContains(response, 'Password change')
self.assertContains(response, 'Password change
')
response = password_change_done(request)
self.assertContains(response, 'Password change successful')
self.assertContains(response, 'Password change successful
')