| 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 | |