2020-11-28 17:20:29 +08:00
|
|
|
from datetime import date
|
|
|
|
|
2012-07-03 17:50:40 +08:00
|
|
|
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 (
|
2013-04-10 05:31:58 +08:00
|
|
|
PasswordChangeDoneView, PasswordChangeView, PasswordResetCompleteView,
|
2017-01-13 22:17:54 +08:00
|
|
|
PasswordResetDoneView, PasswordResetView,
|
2012-07-03 17:50:40 +08:00
|
|
|
)
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import RequestFactory, TestCase, override_settings
|
2017-01-13 22:17:54 +08:00
|
|
|
from django.urls import reverse
|
2012-07-03 17:50:40 +08:00
|
|
|
from django.utils.http import urlsafe_base64_encode
|
|
|
|
|
2017-01-13 22:17:54 +08:00
|
|
|
from .client import PasswordResetConfirmClient
|
2020-11-28 17:20:29 +08:00
|
|
|
from .models import CustomUser
|
2017-01-13 22:17:54 +08:00
|
|
|
|
2012-07-03 17:50:40 +08:00
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='auth_tests.urls')
|
2012-07-03 17:50:40 +08:00
|
|
|
class AuthTemplateTests(TestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
2012-07-03 17:50:40 +08:00
|
|
|
|
2017-01-12 07:12:06 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2012-07-03 17:50:40 +08:00
|
|
|
user = User.objects.create_user('jsmith', 'jsmith@example.com', 'pass')
|
|
|
|
user = authenticate(username=user.username, password='pass')
|
2018-11-27 03:01:27 +08:00
|
|
|
request = cls.request_factory.get('/somepath/')
|
2012-07-03 17:50:40 +08:00
|
|
|
request.user = user
|
2017-01-12 07:12:06 +08:00
|
|
|
cls.user, cls.request = user, request
|
2012-07-03 17:50:40 +08:00
|
|
|
|
2020-11-28 17:28:11 +08:00
|
|
|
def test_password_reset_view(self):
|
2017-01-12 07:12:06 +08:00
|
|
|
response = PasswordResetView.as_view(success_url='dummy/')(self.request)
|
2020-09-10 17:53:09 +08:00
|
|
|
self.assertContains(response, '<title>Password reset | Django site admin</title>')
|
2012-07-03 17:50:40 +08:00
|
|
|
self.assertContains(response, '<h1>Password reset</h1>')
|
|
|
|
|
2020-11-28 17:28:11 +08:00
|
|
|
def test_password_reset_done_view(self):
|
2017-01-12 07:12:06 +08:00
|
|
|
response = PasswordResetDoneView.as_view()(self.request)
|
2020-09-10 17:53:09 +08:00
|
|
|
self.assertContains(response, '<title>Password reset sent | Django site admin</title>')
|
2014-11-16 00:05:24 +08:00
|
|
|
self.assertContains(response, '<h1>Password reset sent</h1>')
|
2012-07-03 17:50:40 +08:00
|
|
|
|
2020-11-28 17:28:11 +08:00
|
|
|
def test_password_reset_confirm_view_invalid_token(self):
|
2013-04-10 05:31:58 +08:00
|
|
|
# PasswordResetConfirmView invalid token
|
2017-01-13 22:17:54 +08:00
|
|
|
client = PasswordResetConfirmClient()
|
|
|
|
url = reverse('password_reset_confirm', kwargs={'uidb64': 'Bad', 'token': 'Bad-Token'})
|
|
|
|
response = client.get(url)
|
2020-09-10 17:53:09 +08:00
|
|
|
self.assertContains(response, '<title>Password reset unsuccessful | Django site admin</title>')
|
2012-07-03 17:50:40 +08:00
|
|
|
self.assertContains(response, '<h1>Password reset unsuccessful</h1>')
|
|
|
|
|
2020-11-28 17:28:11 +08:00
|
|
|
def test_password_reset_confirm_view_valid_token(self):
|
2013-04-10 05:31:58 +08:00
|
|
|
# PasswordResetConfirmView valid token
|
2017-01-13 22:17:54 +08:00
|
|
|
client = PasswordResetConfirmClient()
|
2012-07-03 17:50:40 +08:00
|
|
|
default_token_generator = PasswordResetTokenGenerator()
|
2017-01-12 07:12:06 +08:00
|
|
|
token = default_token_generator.make_token(self.user)
|
2018-10-02 07:18:04 +08:00
|
|
|
uidb64 = urlsafe_base64_encode(str(self.user.pk).encode())
|
2017-01-13 22:17:54 +08:00
|
|
|
url = reverse('password_reset_confirm', kwargs={'uidb64': uidb64, 'token': token})
|
|
|
|
response = client.get(url)
|
2020-09-10 17:53:09 +08:00
|
|
|
self.assertContains(response, '<title>Enter new password | Django site admin</title>')
|
2012-07-03 17:50:40 +08:00
|
|
|
self.assertContains(response, '<h1>Enter new password</h1>')
|
2020-08-20 01:54:30 +08:00
|
|
|
# The username is added to the password reset confirmation form to help
|
|
|
|
# browser's password managers.
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
2020-11-11 04:32:15 +08:00
|
|
|
'<input class="hidden" autocomplete="username" value="jsmith">',
|
2020-08-20 01:54:30 +08:00
|
|
|
)
|
2012-07-03 17:50:40 +08:00
|
|
|
|
2020-11-28 17:20:29 +08:00
|
|
|
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUser')
|
|
|
|
def test_password_reset_confirm_view_custom_username_hint(self):
|
|
|
|
custom_user = CustomUser.custom_objects.create_user(
|
|
|
|
email='joe@example.com',
|
|
|
|
date_of_birth=date(1986, 11, 11),
|
|
|
|
first_name='Joe',
|
|
|
|
)
|
|
|
|
client = PasswordResetConfirmClient()
|
|
|
|
default_token_generator = PasswordResetTokenGenerator()
|
|
|
|
token = default_token_generator.make_token(custom_user)
|
|
|
|
uidb64 = urlsafe_base64_encode(str(custom_user.pk).encode())
|
|
|
|
url = reverse('password_reset_confirm', kwargs={'uidb64': uidb64, 'token': token})
|
|
|
|
response = client.get(url)
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
|
|
|
'<title>Enter new password | Django site admin</title>',
|
|
|
|
)
|
|
|
|
self.assertContains(response, '<h1>Enter new password</h1>')
|
|
|
|
# The username field is added to the password reset confirmation form
|
|
|
|
# to help browser's password managers.
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
|
|
|
'<input class="hidden" autocomplete="username" value="joe@example.com">',
|
|
|
|
)
|
|
|
|
|
2020-11-28 17:28:11 +08:00
|
|
|
def test_password_reset_complete_view(self):
|
2017-01-12 07:12:06 +08:00
|
|
|
response = PasswordResetCompleteView.as_view()(self.request)
|
2020-09-10 17:53:09 +08:00
|
|
|
self.assertContains(response, '<title>Password reset complete | Django site admin</title>')
|
2012-07-03 17:50:40 +08:00
|
|
|
self.assertContains(response, '<h1>Password reset complete</h1>')
|
|
|
|
|
2020-11-28 17:28:11 +08:00
|
|
|
def test_password_reset_change_view(self):
|
2017-01-12 07:12:06 +08:00
|
|
|
response = PasswordChangeView.as_view(success_url='dummy/')(self.request)
|
2020-09-10 17:53:09 +08:00
|
|
|
self.assertContains(response, '<title>Password change | Django site admin</title>')
|
2012-07-03 17:50:40 +08:00
|
|
|
self.assertContains(response, '<h1>Password change</h1>')
|
|
|
|
|
2020-11-28 17:28:11 +08:00
|
|
|
def test_password_change_done_view(self):
|
2017-01-12 07:12:06 +08:00
|
|
|
response = PasswordChangeDoneView.as_view()(self.request)
|
2020-09-10 17:53:09 +08:00
|
|
|
self.assertContains(response, '<title>Password change successful | Django site admin</title>')
|
2012-07-03 17:50:40 +08:00
|
|
|
self.assertContains(response, '<h1>Password change successful</h1>')
|