Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: POC, autoconnection les emplois vers la commu via Pro Connect #851

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"lacommunaute.utils.middleware.ParkingPageMiddleware",
"lacommunaute.openid_connect.middleware.ProConnectLoginMiddleware",
]

THIRD_PARTIES_MIDDLEWARE = [
Expand Down
24 changes: 24 additions & 0 deletions lacommunaute/openid_connect/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.deprecation import MiddlewareMixin
from django.utils.http import urlencode


class ProConnectLoginMiddleware(MiddlewareMixin):
def process_request(self, request):
query_params = request.GET.copy()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je commencerais plutot la méthode par

if "proconnect_login" not in request.GET:
    return

afin d'éviter de copier le query_dict à chaque passage dans le middleware


if "proconnect_login" not in query_params:
return

query_params.pop("proconnect_login")
new_url = (
f"{request.path}?{urlencode({k: v for k, v in query_params.items() if v})}"
if query_params
else request.path
)

if not request.user.is_authenticated:
return HttpResponseRedirect(reverse("openid_connect:authorize") + f"?next={new_url}")

return HttpResponseRedirect(new_url)
51 changes: 51 additions & 0 deletions lacommunaute/openid_connect/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from django.urls import reverse

from lacommunaute.users.factories import UserFactory


@pytest.mark.parametrize(
"params,expected",
[
("?proconnect_login=true", ""),
("?proconnect_login=true&param=1", "?param=1"),
("?param=1&proconnect_login=true", "?param=1"),
],
)
def test_redirect_for_authenticated_user(client, db, params, expected):
client.force_login(UserFactory())
response = client.get(f"/{params}")
assert response.url == f"/{expected}"
assert response.status_code == 302
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert response.url == f"/{expected}"
assert response.status_code == 302
from pytest_django.asserts import assertRedirects
assertRedirects(response, f"/{expected}")



@pytest.mark.parametrize(
"params,expected",
[
("?proconnect_login=true", "/"),
("?proconnect_login=true&param=1", "/?param=1"),
("?param=1&proconnect_login=true", "/?param=1"),
],
)
def test_redirect_for_anonymous_user(client, db, params, expected):
response = client.get(f"/{params}")
assert response.url == f"{reverse('openid_connect:authorize')}?next={expected}"
assert response.status_code == 302


@pytest.mark.parametrize(
"params, logged",
[
("", True),
("?param=1", True),
("?param=1&key=2", True),
("", False),
("?param=1", False),
("?param=1&key=2", False),
],
)
def test_wo_proconnect_login_param(client, db, logged, params):
if logged:
client.force_login(UserFactory())
response = client.get(f"/{params}")
assert response.status_code == 200
Loading