Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30121 closed Bug (fixed)

assertURLEqual() should coerce URLs built with reverse_lazy()

Reported by: Jon Dufresne Owned by: nobody
Component: Testing framework Version: 2.2
Severity: Release blocker Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Calling assertRedirects() now uses the new assertURLEqual(). If a URL is built with reverse_lazy(), assertRedirects() now results in the exception:

Traceback (most recent call last):
  ... # My project calls assertRedirects()
  File "django/django/test/testcases.py", line 383, in assertRedirects
    msg_prefix + "Response redirected to '%s', expected '%s'" % (url, expected_url)
  File "django/django/test/testcases.py", line 404, in assertURLEqual
    normalize(url1), normalize(url2),
  File "django/django/test/testcases.py", line 399, in normalize
    scheme, netloc, path, params, query, fragment = urlparse(url)
  File "/usr/lib64/python3.7/urllib/parse.py", line 367, in urlparse
    url, scheme, _coerce_result = _coerce_args(url, scheme)
  File "/usr/lib64/python3.7/urllib/parse.py", line 123, in _coerce_args
    return _decode_args(args) + (_encode_result,)
  File "/usr/lib64/python3.7/urllib/parse.py", line 107, in _decode_args
    return tuple(x.decode(encoding, errors) if x else '' for x in args)
  File "/usr/lib64/python3.7/urllib/parse.py", line 107, in <genexpr>
    return tuple(x.decode(encoding, errors) if x else '' for x in args)
AttributeError: '__proxy__' object has no attribute 'decode'

This does not happen with Django 2.1. Bisected to commit 24959e48d949a20be969f649ece3576dbc7ce422. A reverse_lazy() URL may be used as a class or module attribute to be reused by multiple tests methods.

To solve this, the inner function in assertURLequal() can coerce the url argument using str().

https://github.com/django/django/blob/2.2a1/django/test/testcases.py#L394-L398

Change History (3)

comment:1 by Jon Dufresne, 6 years ago

Has patch: set

comment:2 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In d15c61ca:

Fixed #30121 -- Fixed assertURLEqual() crash with reverse_lazy() URLs.

Regression in 24959e48d949a20be969f649ece3576dbc7ce422.

comment:3 by Tim Graham <timograham@…>, 6 years ago

In 28fb4ed:

[2.2.x] Fixed #30121 -- Fixed assertURLEqual() crash with reverse_lazy() URLs.

Regression in 24959e48d949a20be969f649ece3576dbc7ce422.
Backport of d15c61cabbe1c15068ffeb58c64035057f0c7d5c from master.

Note: See TracTickets for help on using tickets.
Back to Top