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 all 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
23 changes: 23 additions & 0 deletions lacommunaute/openid_connect/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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):
if "proconnect_login" not in request.GET:
return

query_params = request.GET.copy()
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)
50 changes: 50 additions & 0 deletions lacommunaute/openid_connect/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from django.urls import reverse
from pytest_django.asserts import assertRedirects, assertTemplateUsed

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}")
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}")
assertRedirects(response, f"{reverse('openid_connect:authorize')}?next={expected}", fetch_redirect_response=False)


@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}")
assertTemplateUsed(response, "pages/home.html")
Loading