Ticket #34066: patch.diff

File patch.diff, 2.9 KB (added by Simon Kern, 2 years ago)

Patch including tests

  • django/contrib/auth/forms.py

    diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
    index 9d6a9918bb..78101b447c 100644
    a b class UserChangeForm(forms.ModelForm):  
    163163        super().__init__(*args, **kwargs)
    164164        password = self.fields.get("password")
    165165        if password:
    166             password.help_text = password.help_text.format("../password/")
     166            password.help_text = password.help_text.format(
     167                f"../../{self.instance.pk}/password/"
     168            )
    167169        user_permissions = self.fields.get("user_permissions")
    168170        if user_permissions:
    169171            user_permissions.queryset = user_permissions.queryset.select_related(
  • tests/auth_tests/test_forms.py

    diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py
    index 98bf264040..b0f015dee7 100644
    a b  
    11import datetime
    22import re
     3import urllib.parse
    34from unittest import mock
    45
    56from django.contrib.auth.forms import (
    from django.core.mail import EmailMultiAlternatives  
    2223from django.forms import forms
    2324from django.forms.fields import CharField, Field, IntegerField
    2425from django.test import SimpleTestCase, TestCase, override_settings
     26from django.urls import reverse
    2527from django.utils import translation
    2628from django.utils.text import capfirst
    2729from django.utils.translation import gettext as _
    class UserChangeFormTest(TestDataMixin, TestCase):  
    867869        # value to render correctly
    868870        self.assertEqual(form.initial["password"], form["password"].value())
    869871
     872    @override_settings(ROOT_URLCONF="auth_tests.urls_admin")
     873    def test_bug_34066_link_to_password_reset_in_helptext(self):
     874        user = User.objects.get(username="testclient")
     875        form = UserChangeForm(data={}, instance=user)
     876        passsword_help_text = form.fields["password"].help_text
     877        matches = re.search('<a href="(.*?)">', passsword_help_text)
     878
     879        # Get URL to UserChangeForm in admin via to_field (instead of pk)
     880        admin_user_change_url = f"""{reverse(
     881            "admin:%s_%s_change" % (user._meta.app_label, user._meta.model_name),
     882            args=(user.username,),
     883        )}?to_field=username"""
     884
     885        # join relative url found in password's help_text to admin_user_change_url
     886        joined_url = urllib.parse.urljoin(admin_user_change_url, matches.group(1))
     887
     888        # Get URL to PasswordChangeForm in admin
     889        pw_change_url = reverse(
     890            "admin:%s_%s_password_change"
     891            % (user._meta.app_label, user._meta.model_name),
     892            args=(user.pk,),
     893        )
     894        # assert joined_url and pw_change_url are ideential
     895        self.assertEqual(joined_url, pw_change_url)
     896
    870897    def test_custom_form(self):
    871898        class CustomUserChangeForm(UserChangeForm):
    872899            class Meta(UserChangeForm.Meta):
Back to Top