Ticket #15098: svft.diff

File svft.diff, 2.9 KB (added by tkolar, 4 years ago)

Patch that implements the proposed change, test included

  • django/contrib/auth/models.py

     
    5151user_logged_in.connect(update_last_login)
    5252
    5353class SiteProfileNotAvailable(Exception):
    54     pass
     54    silent_variable_failure = True
    5555
    5656class PermissionManager(models.Manager):
    5757    def get_by_natural_key(self, codename, app_label, model):
  • tests/regressiontests/templates/tests.py

     
    332332        settings.SETTINGS_MODULE = old_settings_module
    333333        settings.TEMPLATE_DEBUG = old_template_debug
    334334
     335    def test_usergetprofile_silentfailure(self):
     336        # See #15098
     337        from django.template import Template, Context
     338        from django.contrib.auth.models import User, SiteProfileNotAvailable
     339
     340        # Unset AUTH_PROFILE_MODULE so that User.get_profile raises SiteProfileNotAvailable
     341        reassign = False
     342        if hasattr(settings, "AUTH_PROFILE_MODULE"):
     343            old_profile = settings.AUTH_PROFILE_MODULE
     344            del settings.AUTH_PROFILE_MODULE
     345            reassign = True
     346
     347        # Set TEMPLATE_STRING_IF_INVALID to a known string.
     348        old_invalid = settings.TEMPLATE_STRING_IF_INVALID
     349        settings.TEMPLATE_STRING_IF_INVALID = 'INVALID'
     350
     351        try:
     352            user=User()
     353            try:
     354                p=user.get_profile()
     355                #if this does not raise SiteProfileNotAvailable, we have a problem.
     356                #We can't be entirely sure that creating a user object will not automatically also create a profile, but if it does,
     357                #User.get_profile should still fail if settings.AUTH_PROFILE_MODULE is unusable.
     358                #If it doesn't, I don't see a way to ensure that the user will not have a profile.
     359                self.fail(
     360                    "FAILED - Couldn't ensure that user.get_profile would fail, probably because the implementation of get_profile changed.")
     361            except SiteProfileNotAvailable:
     362                pass
     363                #all is well
     364            t = Template("{{ user.get_profile }}")
     365            c = Context({ "user" : user })
     366            try:
     367                rendered = t.render(c)
     368            except SiteProfileNotAvailable:
     369                self.fail("FAILED - User.get_profile should fail silently from within templates.")
     370            self.assertEquals(rendered, "INVALID")
     371        finally:
     372            settings.TEMPLATE_STRING_IF_INVALID = old_invalid
     373            if reassign:
     374                settings.AUTH_PROFILE_MODULE = old_profile
     375
    335376    def test_invalid_block_suggestion(self):
    336377        # See #7876
    337378        from django.template import Template, TemplateSyntaxError
Back to Top