From fc37ad7c17553684bf5d6159c3e6951480732ed6 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 27 Jul 2023 15:02:51 +0200 Subject: [PATCH] Adapt logout behaviour in ajax/not ajax modes --- umap/tests/test_views.py | 18 ++++++++++++++++++ umap/views.py | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/umap/tests/test_views.py b/umap/tests/test_views.py index 101c4956..f2d1e78c 100644 --- a/umap/tests/test_views.py +++ b/umap/tests/test_views.py @@ -261,3 +261,21 @@ def test_user_dashboard_display_user_maps(client, map): assert f"{map.get_absolute_url()}?download" in body assert "Everyone (public)" in body assert "Owner only" in body + + +@pytest.mark.django_db +def test_logout_should_return_json_in_ajax(client, user, settings): + client.login(username=user.username, password="123123") + response = client.get( + reverse("logout"), headers={"X_REQUESTED_WITH": "XMLHttpRequest"} + ) + data = json.loads(response.content.decode()) + assert data["redirect"] == "/" + + +@pytest.mark.django_db +def test_logout_should_return_redirect(client, user, settings): + client.login(username=user.username, password="123123") + response = client.get(reverse("logout")) + assert response.status_code == 302 + assert response["Location"] == "/" diff --git a/umap/views.py b/umap/views.py index 8ebb8374..4e667cc5 100644 --- a/umap/views.py +++ b/umap/views.py @@ -945,7 +945,9 @@ def stats(request): def logout(request): do_logout(request) - return simple_json_response(redirect="/") + if is_ajax(request): + return simple_json_response(redirect="/") + return HttpResponseRedirect("/") class LoginPopupEnd(TemplateView):