-
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.
chore: login in debug/dev mode (#829)
## Description 🎸 lorsque le mode DEBUG est actif: * afficher le lien magique de connexion au lieu d'utiliser les routes `django.contrib.auth.urls` * afficher une bannière rouge ## Type de changement 🎢 Nouvelle fonctionnalité (changement non cassant qui ajoute une fonctionnalité). 🚧 technique ### Points d'attention 🦺 passage de la valeur de la variable `DEBUG` dans le `context_processor` ### Captures d'écran (optionnel) Banniere Debug Mode ![image](https://github.com/user-attachments/assets/bdeb154d-7ec3-4339-8017-727a313967dc) Lien cliquable d'authentification en mode debug ![image](https://github.com/user-attachments/assets/cc8d16ad-941c-4578-84d2-eb1eb159a3f3)
- Loading branch information
1 parent
92bf7d8
commit 0a70301
Showing
11 changed files
with
78 additions
and
30 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
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
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 |
---|---|---|
|
@@ -527,6 +527,9 @@ | |
</main> | ||
''' | ||
# --- | ||
# name: TestSendMagicLink.test_send_magic_link[send_magic_link_payload] | ||
'{"sender": {"name": "La Communaut\\u00e9", "email": "[email protected]"}, "to": [{"email": "[email protected]"}], "params": {"display_name": "Edith M.", "login_link": "LOGIN_LINK"}, "templateId": 31}' | ||
# name: TestSendMagicLink.test_send_magic_link[DEV-1][send_magic_link_payload] | ||
'{"sender": {"name": "La Communaut\\u00e9", "email": "[email protected]"}, "to": [{"email": "[email protected]"}], "params": {"display_name": "Samuel J.", "login_link": "LOGIN_LINK"}, "templateId": 31}' | ||
# --- | ||
# name: TestSendMagicLink.test_send_magic_link[PROD-0][send_magic_link_payload] | ||
'{"sender": {"name": "La Communaut\\u00e9", "email": "[email protected]"}, "to": [{"email": "[email protected]"}], "params": {"display_name": "Samuel J.", "login_link": "LOGIN_LINK"}, "templateId": 31}' | ||
# --- |
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 |
---|---|---|
|
@@ -7,6 +7,10 @@ | |
import respx | ||
from django.conf import settings | ||
from django.contrib.auth.tokens import default_token_generator | ||
from django.contrib.messages.api import get_messages | ||
from django.contrib.messages.middleware import MessageMiddleware | ||
from django.contrib.sessions.middleware import SessionMiddleware | ||
from django.test import RequestFactory, override_settings | ||
from django.urls import reverse | ||
from django.utils.encoding import force_bytes | ||
from django.utils.http import urlsafe_base64_encode | ||
|
@@ -17,6 +21,7 @@ | |
from lacommunaute.users.factories import UserFactory | ||
from lacommunaute.users.models import User | ||
from lacommunaute.users.views import send_magic_link | ||
from lacommunaute.utils.enums import Environment | ||
from lacommunaute.utils.testing import parse_response_to_soup | ||
from lacommunaute.utils.urls import clean_next_url | ||
|
||
|
@@ -79,21 +84,31 @@ def validate_magiclink_payload(payload_as_str, uidb64, token, expected): | |
|
||
|
||
class TestSendMagicLink: | ||
def test_send_magic_link(self, db, snapshot, mock_respx_post_to_sib_smtp_url): | ||
user = UserFactory(first_name="Edith", last_name="Martin", email="[email protected]") | ||
next_url = "/topics/" | ||
send_magic_link(user, next_url) | ||
|
||
token = default_token_generator.make_token(user) | ||
uidb64 = urlsafe_base64_encode(force_bytes(user.pk)) | ||
url = reverse("users:login_with_link", kwargs={"uidb64": uidb64, "token": token}) | ||
query_params = urlencode({"next": clean_next_url(next_url)}) | ||
login_link = f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{url}?{query_params}" | ||
|
||
payload_as_str = respx.calls[0].request.content.decode() | ||
payload = json.loads(payload_as_str) | ||
assert payload["params"]["login_link"] == login_link | ||
assert payload_as_str.replace(login_link, "LOGIN_LINK") == snapshot(name="send_magic_link_payload") | ||
@pytest.mark.parametrize("env,count_msg", [(Environment.PROD, 0), (Environment.DEV, 1)]) | ||
def test_send_magic_link(self, db, user, snapshot, mock_respx_post_to_sib_smtp_url, env, count_msg): | ||
with override_settings(ENVIRONMENT=env): | ||
next_url = "/topics/" | ||
request = RequestFactory().get("/") | ||
SessionMiddleware(lambda request: None).process_request(request) | ||
MessageMiddleware(lambda request: None).process_request(request) | ||
send_magic_link(request, user, next_url) | ||
|
||
token = default_token_generator.make_token(user) | ||
uidb64 = urlsafe_base64_encode(force_bytes(user.pk)) | ||
url = reverse("users:login_with_link", kwargs={"uidb64": uidb64, "token": token}) | ||
query_params = urlencode({"next": clean_next_url(next_url)}) | ||
login_link = f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{url}?{query_params}" | ||
|
||
payload_as_str = respx.calls[0].request.content.decode() | ||
payload = json.loads(payload_as_str) | ||
assert payload["params"]["login_link"] == login_link | ||
assert payload_as_str.replace(login_link, "LOGIN_LINK") == snapshot(name="send_magic_link_payload") | ||
|
||
# we want messages do not appear in the productive environment | ||
msgs = get_messages(request) | ||
assert len(msgs._queued_messages) == count_msg | ||
if msgs._queued_messages: | ||
assert str(msgs._queued_messages[0]) == f'<a href="{login_link}">{login_link}</a> sent to {user.email}' | ||
|
||
|
||
class TestLoginView: | ||
|
@@ -105,6 +120,7 @@ def test_content(self, client, db, next_url, snapshot): | |
content = parse_response_to_soup(response, selector="main") | ||
assert str(content) == snapshot(name="login_view_content") | ||
|
||
@override_settings(ENVIRONMENT=Environment.PROD) | ||
@pytest.mark.parametrize("next_url,expected", next_url_tuples) | ||
def test_post( | ||
self, | ||
|
@@ -152,6 +168,7 @@ def test_user_is_already_authenticated(self, client, db, next_url, expected): | |
|
||
|
||
class TestCreateUserView: | ||
@override_settings(ENVIRONMENT=Environment.PROD) | ||
@pytest.mark.parametrize("next_url,expected", next_url_tuples) | ||
def test_post_new_email( | ||
self, client, db, next_url, expected, snapshot, mock_token_generator, mock_respx_post_to_sib_smtp_url | ||
|
@@ -178,6 +195,7 @@ def test_post_new_email( | |
payload_as_str = respx.calls[0].request.content.decode() | ||
assert validate_magiclink_payload(payload_as_str, uidb64, token, expected) | ||
|
||
@override_settings(ENVIRONMENT=Environment.PROD) | ||
@pytest.mark.parametrize("next_url,expected", next_url_tuples) | ||
def test_post_existing_email( | ||
self, client, db, user, next_url, expected, snapshot, mock_token_generator, mock_respx_post_to_sib_smtp_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
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
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