-
-
Save vitorfs/03e2f20a4c1e693c9b22b343503fb461 to your computer and use it in GitHub Desktop.
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')) |
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))
Thank you a lot. =)
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?
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.
self.assertRedirects(response,'{}?next={}'.format(login_url, url))
Thanks Vitorfs
i have this failure for a while... could someone help with it?
FAIL: test_board_topics_view_contains_navigation_links (boards.tests.test_views.BoardTopicsTests)
Traceback (most recent call last):
File "C:\Users\User\myproject\myproject\boards\tests\test_views.py", line 52, in test_board_topics_view_contains_navigation_links
self.assertContains(response, 'href="{0}"'.format(new_topic_url))
File "C:\Users\User\myproject\venv\lib\site-packages\django\test\testcases.py", line 454, in assertContains
self.assertTrue(real_count != 0, msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: False is not true : Couldn't find 'href="/boards/1/new/"' in response
Using "next" doesn't really seem to grasp the idea behind it, can you elaborate...
Since the user is not logged in while changing the password, it will first get the user to the login page, then normally after login, it redirects the user back to the home page, but since the user requested for a change in the password then it will directly redirect the user to password_change page.
i have this error that i cant seem to solve can somone plz help me
Traceback (most recent call last):
File "C:\Users\T-chris\myproject\myproject\next\test\test_views.py", line 33, in test_board_topics_view_contains_link_back_to_homepage
self.assertContains(response,'href="{0}"'.format(homepage_url))
File "C:\Users\T-chris\Envs\final\lib\site-packages\django\test\testcases.py", line 445, in assertContains
text_repr, real_count, msg_prefix = self._assert_contains(
File "C:\Users\T-chris\Envs\final\lib\site-packages\django\test\testcases.py", line 415, in _assert_contains
self.assertEqual(
AssertionError: 404 != 200 : Couldn't retrieve content: Response code was 404 (expected 200)
======================================================================
FAIL: test_board_topics_view_contains_navigation_links (next.test.test_views.BoardTopicsTests)
Traceback (most recent call last):
File "C:\Users\T-chris\myproject\myproject\next\test\test_views.py", line 42, in test_board_topics_view_contains_navigation_links
self.assertContains(response, 'href="{0}"'.format(homepage_url))
File "C:\Users\T-chris\Envs\final\lib\site-packages\django\test\testcases.py", line 445, in assertContains
text_repr, real_count, msg_prefix = self._assert_contains(
File "C:\Users\T-chris\Envs\final\lib\site-packages\django\test\testcases.py", line 415, in _assert_contains
self.assertEqual(
AssertionError: 404 != 200 : Couldn't retrieve content: Response code was 404 (expected 200)
Using "next" dont really seem to grasp the idea behind it, can you elaborate....