From f5f6c84e89925e271baafe28e247853233e0ddde Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Thu, 7 Feb 2019 15:18:54 +0100
Subject: [PATCH] python3: handle miscellaneous encoding variations in main
 test file (#31173)

---
 tests/test_all.py | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/tests/test_all.py b/tests/test_all.py
index 98ad6d16..f8c06afc 100644
--- a/tests/test_all.py
+++ b/tests/test_all.py
@@ -14,6 +14,7 @@ from django.core.serializers.json import DjangoJSONEncoder
 from django.contrib.auth import get_user_model
 from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import ugettext as _
+from django.utils.six.moves.urllib.parse import quote, unquote
 
 from rest_framework import test
 from rest_framework import status
@@ -141,21 +142,21 @@ class UtilsTests(Authentic2TestCase):
         request = rf.get('/coin', data={'next': '..'})
         request2 = rf.get('/coin', data={'next': '..', 'token': 'xxx'})
         response = redirect(request, '/boob/', keep_params=True)
-        self.assertEqualsURL(response['Location'], '/boob/?next=..')
+        self.assertEqualsURL(unquote(response['Location']), '/boob/?next=..')
         response = redirect(request, '/boob/', keep_params=True,
                             exclude=['next'])
-        self.assertEqualsURL(response['Location'], '/boob/')
+        self.assertEqualsURL(unquote(response['Location']), '/boob/')
         response = redirect(request2, '/boob/', keep_params=True)
-        self.assertEqualsURL(response['Location'], '/boob/?token=xxx&next=..')
+        self.assertEqualsURL(unquote(response['Location']), '/boob/?token=xxx&next=..')
         response = redirect(request, '/boob/', keep_params=True,
                             exclude=['token'])
-        self.assertEqualsURL(response['Location'], '/boob/?next=..')
+        self.assertEqualsURL(unquote(response['Location']), '/boob/?next=..')
         response = redirect(request, '/boob/', keep_params=True,
                             include=['next'])
-        self.assertEqualsURL(response['Location'], '/boob/?next=..')
+        self.assertEqualsURL(unquote(response['Location']), '/boob/?next=..')
         response = redirect(request, '/boob/', keep_params=True,
                             include=['next'], params={'token': 'uuu'})
-        self.assertEqualsURL(response['Location'], '/boob/?token=uuu&next=..')
+        self.assertEqualsURL(unquote(response['Location']), '/boob/?token=uuu&next=..')
 
     def test_redirect_to_login(self):
         from authentic2.utils import redirect_to_login
@@ -163,7 +164,7 @@ class UtilsTests(Authentic2TestCase):
         rf = RequestFactory()
         request = rf.get('/coin', data={'next': '..'})
         response = redirect_to_login(request)
-        self.assertEqualsURL(response['Location'], '/login/?next=..')
+        self.assertEqualsURL(response['Location'], quote('/login/?next=..'))
 
     def test_continue_to_next_url(self):
         from authentic2.utils import continue_to_next_url
@@ -171,7 +172,7 @@ class UtilsTests(Authentic2TestCase):
         rf = RequestFactory()
         request = rf.get('/coin', data={'next': '/zob/', 'nonce': 'xxx'})
         response = continue_to_next_url(request)
-        self.assertEqualsURL(response['Location'], '/zob/?nonce=xxx')
+        self.assertEqualsURL(response['Location'], quote('/zob/?nonce=xxx'))
 
     def test_login_require(self):
         from authentic2.utils import login_require
@@ -179,11 +180,10 @@ class UtilsTests(Authentic2TestCase):
         rf = RequestFactory()
         request = rf.get('/coin', data={'next': '/zob/', 'nonce': 'xxx'})
         response = login_require(request)
-        self.assertEqualsURL(response['Location'].split('?', 1)[0], '/login/')
+        self.assertEqualsURL(unquote(response['Location']).split('?', 1)[0], '/login/')
         self.assertEqualsURL(
-            urlparse.parse_qs(
-                response['Location'].split('?', 1)[1])['next'][0],
-            '/coin?nonce=xxx&next=/zob/')
+            list(urlparse.parse_qs(unquote(response['Location'])).values())[0][0],
+            '/coin?nonce=xxx&next=%2Fzob%2F')
 
 
 class ValidatorsTest(TestCase):
@@ -493,7 +493,7 @@ class APITest(TestCase):
         activation_url = get_link_from_mail(mail.outbox[-1])
         response = client.get(activation_url)
         self.assertEqual(response.status_code, status.HTTP_200_OK)
-        assert utils.make_url(return_url, params={'token': token}) in response.content
+        assert utils.make_url(return_url, params={'token': token}) in response.content.decode('utf-8')
         self.assertEqual(User.objects.count(), user_count + 1)
         response = client.get(reverse('auth_homepage'))
         self.assertContains(response, username)
@@ -620,7 +620,7 @@ class APITest(TestCase):
         activation_url = get_link_from_mail(activation_mail1)
         response = client.get(activation_url)
         self.assertEqual(response.status_code, status.HTTP_200_OK)
-        assert utils.make_url(return_url, params={'token': token}) in response.content
+        assert utils.make_url(return_url, params={'token': token}) in response.content.decode('utf-8')
         self.assertEqual(User.objects.count(), user_count + 1)
         response = client.get(reverse('auth_homepage'))
         self.assertContains(response, username)
@@ -744,7 +744,7 @@ class APITest(TestCase):
         activation_url = get_link_from_mail(activation_mail1)
         response = client.get(activation_url)
         self.assertEqual(response.status_code, status.HTTP_200_OK)
-        assert utils.make_url(return_url, params={'token': token}) in response.content
+        assert utils.make_url(return_url, params={'token': token}) in response.content.decode('utf-8')
         self.assertEqual(User.objects.count(), user_count + 1)
         response = client.get(reverse('auth_homepage'))
         self.assertContains(response, username)
-- 
2.20.1

