From 63a037c15a9bb21636b2a456206866e5804691c2 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 13 Mar 2020 17:18:50 +0100
Subject: [PATCH] python3: update idp login redirection (#40730)

---
 bijoe/views.py      |  4 ++--
 tests/test_views.py | 12 ++++++++++++
 tox.ini             |  1 +
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/bijoe/views.py b/bijoe/views.py
index a811436..0b7839d 100644
--- a/bijoe/views.py
+++ b/bijoe/views.py
@@ -10,23 +10,23 @@
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Affero General Public License for more details.
 #
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import json
-import urllib
 
 from django.conf import settings
 from django.shortcuts import resolve_url
 from django.core.urlresolvers import reverse
 from django.views.generic import ListView, View
 from django.http import HttpResponse, HttpResponseRedirect
+from django.utils.http import quote
 from django.utils.translation import ugettext as _
 from django.contrib.auth import logout as auth_logout
 from django.contrib.auth import views as auth_views
 from django.contrib.auth.views import redirect_to_login
 from django.core.exceptions import PermissionDenied
 
 try:
     from mellon.utils import get_idps
@@ -96,17 +96,17 @@ homepage = HomepageView.as_view()
 menu_json = MenuJSONView.as_view()
 
 
 def login(request, *args, **kwargs):
     if any(get_idps()):
         if not 'next' in request.GET:
             return HttpResponseRedirect(resolve_url('mellon_login'))
         return HttpResponseRedirect(resolve_url('mellon_login') + '?next='
-                                    + urllib.quote(request.GET.get('next')))
+                                    + quote(request.GET.get('next')))
     return auth_views.login(request, template_name='bijoe/login.html')
 
 
 def logout(request, next_page=None):
     if any(get_idps()):
         return HttpResponseRedirect(resolve_url('mellon_logout'))
     auth_logout(request)
     if next_page is not None:
diff --git a/tests/test_views.py b/tests/test_views.py
index 000bbfc..a91b399 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -13,16 +13,17 @@
 #
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import copy
 import hashlib
 import json
 
+import mock
 import pytest
 from webtest import Upload
 
 from django.urls import reverse
 from django.utils.encoding import force_bytes
 
 from bijoe.visualization.models import Visualization
 from bijoe.visualization.signature import sign_url
@@ -270,8 +271,19 @@ def test_ods_view(schema1, app, admin, visualization, settings):
     assert resp.content_type == 'application/vnd.oasis.opendocument.spreadsheet'
 
 
 def test_geojson_view(schema1, app, admin, visualization, settings):
     login(app, admin)
     resp = app.get('/visualization/%s/geojson/' % visualization.id)
     assert resp.content_type == 'application/json'
     assert len(resp.json) == 8
+
+
+@mock.patch('bijoe.views.get_idps', return_value='foo')
+@mock.patch('bijoe.views.resolve_url', return_value='foo-url')
+def test_idp_redirections(mocked_resolv_url, mocked_get_idps, app):
+    resp = app.get('/accounts/login/', status=302)
+    assert resp.location == 'foo-url'
+    resp = app.get('/accounts/login/?next=bar-url', status=302)
+    assert resp.location == 'foo-url?next=bar-url'
+    resp = app.get('/accounts/logout/', status=302)
+    assert resp.location == 'foo-url'
diff --git a/tox.ini b/tox.ini
index f263c75..8c5612e 100644
--- a/tox.ini
+++ b/tox.ini
@@ -18,16 +18,17 @@ deps =
 	dj111: django>=1.11,<1.12
 	coverage
 	pytest
 	pytest-cov
 	pytest-django<3.4.6
 	pytest-freezegun
 	WebTest
 	django-webtest<1.9.3
+        mock
 	pyquery
 	tabulate
 	http://git.entrouvert.org/hobo.git/snapshot/hobo-master.tar.gz
 commands =
         dj111: py.test {posargs: --junitxml=test_{envname}_results.xml --cov-report xml --cov-report html --cov=bijoe tests/}
 [pytest]
 filterwarnings =
  once:.*
-- 
2.25.1

