Skip to content

Commit

Permalink
feat: POC, autoconnection les emplois vers la commu via Pro Connect (#…
Browse files Browse the repository at this point in the history
…851)

# AutoConnect les emplois <> la commu

Partie 1 : les emplois > la commu
Scope : Orienteurs et SIAE

## Description

🎸 Permettre à un utilisateur authentifié dans le site des emplois via
ProConnect, d'être automatiquement connecté via ProConnect dans la
communauté

## Type de changement

🎢 Nouvelle fonctionnalité (changement non cassant qui ajoute une
fonctionnalité).

### Points d'attention

🦺 capture du paramètre `proconnect_login` dans l'url par un `middleware`
dédié
🦺 retraitement de l'url pour supprimer le paramètre `proconnect_login`
dans tous les cas
  • Loading branch information
vincentporte authored Dec 9, 2024
1 parent 3486f6e commit 56e3163
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
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")

0 comments on commit 56e3163

Please sign in to comment.