Skip to content

Instantly share code, notes, and snippets.

@vitorfs vitorfs/test_view_password_change.py Secret
Last active Jun 18, 2018

Embed
What would you like to do?
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.models import User
from django.contrib.auth import views as auth_views
from django.core.urlresolvers import reverse
from django.urls import resolve
from django.test import TestCase
class PasswordChangeTests(TestCase):
def setUp(self):
username = 'john'
password = 'secret123'
user = User.objects.create_user(username=username, email='john@doe.com', password=password)
url = reverse('password_change')
self.client.login(username=username, password=password)
self.response = self.client.get(url)
def test_status_code(self):
self.assertEquals(self.response.status_code, 200)
def test_url_resolves_correct_view(self):
view = resolve('/settings/password/')
self.assertEquals(view.func.view_class, auth_views.PasswordChangeView)
def test_csrf(self):
self.assertContains(self.response, 'csrfmiddlewaretoken')
def test_contains_form(self):
form = self.response.context.get('form')
self.assertIsInstance(form, PasswordChangeForm)
def test_form_inputs(self):
'''
The view must contain four inputs: csrf, old_password, new_password1, new_password2
'''
self.assertContains(self.response, '<input', 4)
self.assertContains(self.response, 'type="password"', 3)
class LoginRequiredPasswordChangeTests(TestCase):
def test_redirection(self):
url = reverse('password_change')
login_url = reverse('login')
response = self.client.get(url)
self.assertRedirects(response, f'{login_url}?next={url}')
class PasswordChangeTestCase(TestCase):
'''
Base test case for form processing
accepts a `data` dict to POST to the view.
'''
def setUp(self, data={}):
self.user = User.objects.create_user(username='john', email='john@doe.com', password='old_password')
self.url = reverse('password_change')
self.client.login(username='john', password='old_password')
self.response = self.client.post(self.url, data)
class SuccessfulPasswordChangeTests(PasswordChangeTestCase):
def setUp(self):
super().setUp({
'old_password': 'old_password',
'new_password1': 'new_password',
'new_password2': 'new_password',
})
def test_redirection(self):
'''
A valid form submission should redirect the user
'''
self.assertRedirects(self.response, reverse('password_change_done'))
def test_password_changed(self):
'''
refresh the user instance from database to get the new password
hash updated by the change password view.
'''
self.user.refresh_from_db()
self.assertTrue(self.user.check_password('new_password'))
def test_user_authentication(self):
'''
Create a new request to an arbitrary page.
The resulting response should now have an `user` to its context, after a successful sign up.
'''
response = self.client.get(reverse('home'))
user = response.context.get('user')
self.assertTrue(user.is_authenticated)
class InvalidPasswordChangeTests(PasswordChangeTestCase):
def test_status_code(self):
'''
An invalid form submission should return to the same page
'''
self.assertEquals(self.response.status_code, 200)
def test_form_errors(self):
form = self.response.context.get('form')
self.assertTrue(form.errors)
def test_didnt_change_password(self):
'''
refresh the user instance from the database to make
sure we have the latest data.
'''
self.user.refresh_from_db()
self.assertTrue(self.user.check_password('old_password'))
@officialgabzz

This comment has been minimized.

Copy link

officialgabzz commented Nov 30, 2017

Thanks man!!!

@officialgabzz

This comment has been minimized.

Copy link

officialgabzz commented Nov 30, 2017

Using "next" dont really seem to grasp the idea behind it, can you elaborate....

@asaguado

This comment has been minimized.

Copy link

asaguado commented Feb 13, 2018

I have sintax error in line 45: self.assertRedirects(response, f'{login_url}?next={url}')

What's is this?--> f'{login_url}?next={url}'

[SOLVED!]
Correct line 45 is:
self.assertRedirects(response,'{login_url}?next={url}'.format(login_url=login_url, url=url))

@yeongseon

This comment has been minimized.

Copy link

yeongseon commented Mar 25, 2018

Thank you a lot. =)

@kotrotko

This comment has been minimized.

Copy link

kotrotko commented May 18, 2018

I have the same problem with f'{login_url}?next={url}'
I used 'asaguado''s correction, thank you, but could you kindly explain what it was?

@KYDronePilot

This comment has been minimized.

Copy link

KYDronePilot commented May 26, 2018

Python 3.6 introduced "f-strings", defined by prefixing a string with the letter 'f' (much like 'r' for raw strings). The 'f' indicates that the string is for formatting, so any variable names placed within brackets, within the string will be replaced with the value of each respective variable. For instance:

text = 'some text'
formatted_text = f'This is {text}'
print(formatted_text)

The output will be This is some text

You were likely having this issue because of not running Python 3.6.

@skimpa

This comment has been minimized.

Copy link

skimpa commented Jun 18, 2018

self.assertRedirects(response,'{}?next={}'.format(login_url, url))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.