linkding/bookmarks/tests/test_password_change_view.py

58 lines
1.9 KiB
Python
Raw Normal View History

2021-10-16 03:42:04 +00:00
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from bookmarks.tests.helpers import BookmarkFactoryMixin
class PasswordChangeViewTestCase(TestCase, BookmarkFactoryMixin):
def setUp(self) -> None:
2024-01-27 10:29:16 +00:00
self.user = User.objects.create_user(
"testuser", "test@example.com", "initial_password"
)
2021-10-16 03:42:04 +00:00
self.client.force_login(self.user)
def test_change_password(self):
form_data = {
2024-01-27 10:29:16 +00:00
"old_password": "initial_password",
"new_password1": "new_password",
"new_password2": "new_password",
2021-10-16 03:42:04 +00:00
}
2024-01-27 10:29:16 +00:00
response = self.client.post(reverse("change_password"), form_data)
2021-10-16 03:42:04 +00:00
2024-01-27 10:29:16 +00:00
self.assertRedirects(response, reverse("password_change_done"))
2021-10-16 03:42:04 +00:00
def test_change_password_done(self):
form_data = {
2024-01-27 10:29:16 +00:00
"old_password": "initial_password",
"new_password1": "new_password",
"new_password2": "new_password",
2021-10-16 03:42:04 +00:00
}
2024-01-27 10:29:16 +00:00
response = self.client.post(reverse("change_password"), form_data, follow=True)
2021-10-16 03:42:04 +00:00
2024-01-27 10:29:16 +00:00
self.assertContains(response, "Your password was changed successfully")
2021-10-16 03:42:04 +00:00
def test_should_return_error_for_invalid_old_password(self):
form_data = {
2024-01-27 10:29:16 +00:00
"old_password": "wrong_password",
"new_password1": "new_password",
"new_password2": "new_password",
2021-10-16 03:42:04 +00:00
}
2024-01-27 10:29:16 +00:00
response = self.client.post(reverse("change_password"), form_data)
2021-10-16 03:42:04 +00:00
2024-01-27 10:29:16 +00:00
self.assertIn("old_password", response.context_data["form"].errors)
2021-10-16 03:42:04 +00:00
def test_should_return_error_for_mismatching_new_password(self):
form_data = {
2024-01-27 10:29:16 +00:00
"old_password": "initial_password",
"new_password1": "new_password",
"new_password2": "wrong_password",
2021-10-16 03:42:04 +00:00
}
2024-01-27 10:29:16 +00:00
response = self.client.post(reverse("change_password"), form_data)
2021-10-16 03:42:04 +00:00
2024-01-27 10:29:16 +00:00
self.assertIn("new_password2", response.context_data["form"].errors)