-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: POC, autoconnection les emplois vers la commu via Pro Connect (#…
…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
1 parent
3486f6e
commit 56e3163
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¶m=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¶m=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") |