0

Im testing a redirection (login required) on a view.

Test snippet:

...
expected_url = build_url(
    self.login_url,
    get={'next': self.url}
)
self.assertRedirects(
    response,
    expected_url,
    status_code=302
)

I get AssertionError because expected_url is percent encoded, the one django is displaying through login_required (django.contrib.auth.decorators.login_required) is not percent-encoded.

So I get:

AssertionError: Response redirected to 'http://testserver/log_in/?next=/payments/purchase/'
expected 'http://testserver/log_in/?next=payments%3Apurchase'

I have my build_url func as described below. Im encoding a dict with urllib.urlencode.

build_url source: How do i pass GET parameters using django urlresolvers reverse

def build_url(*args, **kwargs):
    get = kwargs.pop('get', {})
    url = reverse(*args, **kwargs)
    if get:
        url += '?' + urllib.urlencode(get)
    return url

Questions:

  • Is there a way to tell django to display login_url percent-encoded?

  • Should I use something instead urllib.urlencode to build a url from a dict?

  • Is percent-encoding important so I must follow it?
Community
  • 1
  • 1
Laraconda
  • 667
  • 6
  • 15
  • 1
    The unencoded urls are _not_ the same. The redirect contains `next=/payments/purchase/`, while the expected url contains `next=payments:purchase`. I believe `assertRedirects` will decode the urls before comparing them. – knbk Oct 20 '15 at 22:30
  • I forgot to reverse `self.url`. But Im still getting exception. `AssertionError: Response redirected to 'http://testserver/log_in/?next=/payments/purchase/', expected 'http://testserver/log_in/?next=%2Fpayments%2Fpurchase%2F'` – Laraconda Oct 20 '15 at 23:49

0 Answers0