2012-07-03 11:50:40 +02: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-09 23:31:58 +02:00
|
|
|
PasswordChangeDoneView, PasswordChangeView, PasswordResetCompleteView,
|
2017-01-13 15:17:54 +01:00
|
|
|
PasswordResetDoneView, PasswordResetView,
|
2012-07-03 11:50:40 +02:00
|
|
|
)
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.test import RequestFactory, TestCase, override_settings
|
2017-01-13 15:17:54 +01:00
|
|
|
from django.urls import reverse
|
2017-01-24 12:22:42 +01:00
|
|
|
from django.utils.encoding import force_bytes
|
2012-07-03 11:50:40 +02:00
|
|
|
from django.utils.http import urlsafe_base64_encode
|
|
|
|
|
2017-01-13 15:17:54 +01:00
|
|
|
from .client import PasswordResetConfirmClient
|
|
|
|
|
2012-07-03 11:50:40 +02:00
|
|
|
|
2016-02-05 21:56:52 +01:00
|
|
|
@override_settings(ROOT_URLCONF='auth_tests.urls')
|
2012-07-03 11:50:40 +02:00
|
|
|
class AuthTemplateTests(TestCase):
|
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2012-07-03 11:50:40 +02:00
|
|
|
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
|
2017-01-12 00:12:06 +01:00
|
|
|
cls.user, cls.request = user, request
|
2012-07-03 11:50:40 +02:00
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
def test_PasswordResetView(self):
|
|
|
|
response = PasswordResetView.as_view(success_url='dummy/')(self.request)
|
2012-07-03 11:50:40 +02:00
|
|
|
self.assertContains(response, '<title>Password reset</title>')
|
|
|
|
self.assertContains(response, '<h1>Password reset</h1>')
|
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
def test_PasswordResetDoneView(self):
|
|
|
|
response = PasswordResetDoneView.as_view()(self.request)
|
2014-11-15 17:05:24 +01:00
|
|
|
self.assertContains(response, '<title>Password reset sent</title>')
|
|
|
|
self.assertContains(response, '<h1>Password reset sent</h1>')
|
2012-07-03 11:50:40 +02:00
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
def test_PasswordResetConfirmView_invalid_token(self):
|
2013-04-09 23:31:58 +02:00
|
|
|
# PasswordResetConfirmView invalid token
|
2017-01-13 15:17:54 +01:00
|
|
|
client = PasswordResetConfirmClient()
|
|
|
|
url = reverse('password_reset_confirm', kwargs={'uidb64': 'Bad', 'token': 'Bad-Token'})
|
|
|
|
response = client.get(url)
|
2012-07-03 11:50:40 +02:00
|
|
|
self.assertContains(response, '<title>Password reset unsuccessful</title>')
|
|
|
|
self.assertContains(response, '<h1>Password reset unsuccessful</h1>')
|
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
def test_PasswordResetConfirmView_valid_token(self):
|
2013-04-09 23:31:58 +02:00
|
|
|
# PasswordResetConfirmView valid token
|
2017-01-13 15:17:54 +01:00
|
|
|
client = PasswordResetConfirmClient()
|
2012-07-03 11:50:40 +02:00
|
|
|
default_token_generator = PasswordResetTokenGenerator()
|
2017-01-12 00:12:06 +01:00
|
|
|
token = default_token_generator.make_token(self.user)
|
2017-01-24 12:22:42 +01:00
|
|
|
uidb64 = urlsafe_base64_encode(force_bytes(self.user.pk)).decode()
|
2017-01-13 15:17:54 +01:00
|
|
|
url = reverse('password_reset_confirm', kwargs={'uidb64': uidb64, 'token': token})
|
|
|
|
response = client.get(url)
|
2012-07-03 11:50:40 +02:00
|
|
|
self.assertContains(response, '<title>Enter new password</title>')
|
|
|
|
self.assertContains(response, '<h1>Enter new password</h1>')
|
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
def test_PasswordResetCompleteView(self):
|
|
|
|
response = PasswordResetCompleteView.as_view()(self.request)
|
2012-07-03 11:50:40 +02:00
|
|
|
self.assertContains(response, '<title>Password reset complete</title>')
|
|
|
|
self.assertContains(response, '<h1>Password reset complete</h1>')
|
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
def test_PasswordResetChangeView(self):
|
|
|
|
response = PasswordChangeView.as_view(success_url='dummy/')(self.request)
|
2012-07-03 11:50:40 +02:00
|
|
|
self.assertContains(response, '<title>Password change</title>')
|
|
|
|
self.assertContains(response, '<h1>Password change</h1>')
|
|
|
|
|
2017-01-12 00:12:06 +01:00
|
|
|
def test_PasswordChangeDoneView(self):
|
|
|
|
response = PasswordChangeDoneView.as_view()(self.request)
|
2012-07-03 11:50:40 +02:00
|
|
|
self.assertContains(response, '<title>Password change successful</title>')
|
|
|
|
self.assertContains(response, '<h1>Password change successful</h1>')
|