diff --git a/lacommunaute/users/tests/tests_views.py b/lacommunaute/users/tests/tests_views.py
index da118e77..70c40d88 100644
--- a/lacommunaute/users/tests/tests_views.py
+++ b/lacommunaute/users/tests/tests_views.py
@@ -1,4 +1,5 @@
import json
+import re
from unittest import mock
from urllib.parse import urlencode
@@ -7,6 +8,7 @@
import respx
from django.conf import settings
from django.contrib.auth.tokens import default_token_generator
+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
@@ -24,6 +26,21 @@
next_url_tuples = [("/", "/"), ("/topics/", "/topics/"), ("http://www.unallowed_host.com", "/")]
+@pytest.fixture(name="override_debug_toolbar_settings")
+def override_debug_toolbar_settings_fixture():
+ with override_settings(
+ DEBUG=True,
+ INSTALLED_APPS=[app for app in settings.INSTALLED_APPS if app != "debug_toolbar"],
+ MIDDLEWARE=[
+ middleware
+ for middleware in settings.MIDDLEWARE
+ if middleware != "debug_toolbar.middleware.DebugToolbarMiddleware"
+ ],
+ DEBUG_TOOLBAR_CONFIG={},
+ ):
+ yield
+
+
@pytest.fixture(name="mock_respx_post_to_sib_smtp_url")
def mock_respx_post_to_sib_smtp_url_fixture():
with respx.mock:
@@ -80,9 +97,10 @@ 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):
+ request = RequestFactory().get("/")
user = UserFactory(first_name="Edith", last_name="Martin", email="edith@martin.co")
next_url = "/topics/"
- send_magic_link(user, next_url)
+ send_magic_link(request, user, next_url)
token = default_token_generator.make_token(user)
uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
@@ -130,6 +148,28 @@ def test_post(
payload_as_str = respx.calls[0].request.content.decode()
assert validate_magiclink_payload(payload_as_str, uidb64, token, expected)
+ def test_post_in_debug_mode(
+ self,
+ client,
+ db,
+ fixed_user_token,
+ snapshot,
+ mock_token_generator,
+ mock_respx_post_to_sib_smtp_url,
+ override_debug_toolbar_settings,
+ ):
+ user, uidb64, token = fixed_user_token
+ response = client.post(
+ reverse("users:login"),
+ data={"email": user.email, "next": "/topics/"},
+ )
+ assert response.status_code == 200
+
+ content = parse_response_to_soup(response, selector="main")
+ pattern = r'http.*?next=%2Ftopics%2F"'
+ content_as_str = re.sub(pattern, "LOGIN_LINK", str(content))
+ assert content_as_str == snapshot(name="debug_login_view_content")
+
@pytest.mark.parametrize("next_url,expected", next_url_tuples)
def test_redirection_when_email_is_unknown(self, client, db, next_url, expected, mock_respx_post_to_sib_smtp_url):
response = client.post(
@@ -201,6 +241,27 @@ def test_post_existing_email(
payload_as_str = respx.calls[0].request.content.decode()
assert validate_magiclink_payload(payload_as_str, uidb64, token, expected)
+ def test_post_new_mail_in_debug_mode(
+ self,
+ client,
+ db,
+ snapshot,
+ mock_token_generator,
+ mock_respx_post_to_sib_smtp_url,
+ override_debug_toolbar_settings,
+ ):
+ email = "john@travolta.lt"
+ response = client.post(
+ reverse("users:create"),
+ data={"email": email, "first_name": "John", "last_name": "Travolta", "next": "/topics/"},
+ )
+ assert response.status_code == 200
+
+ content = parse_response_to_soup(response, selector="main")
+ pattern = r'http.*?next=%2Ftopics%2F"'
+ content_as_str = re.sub(pattern, "LOGIN_LINK", str(content))
+ assert content_as_str == snapshot(name="debug_login_view_content")
+
class TestLoginWithLinkView:
def test_user_not_found(self, client, db):
diff --git a/lacommunaute/users/views.py b/lacommunaute/users/views.py
index 5f0151e1..dcddb8fd 100644
--- a/lacommunaute/users/views.py
+++ b/lacommunaute/users/views.py
@@ -2,6 +2,7 @@
from urllib.parse import urlencode
from django.conf import settings
+from django.contrib import messages
from django.contrib.auth import login, logout
from django.contrib.auth.tokens import default_token_generator
from django.http import HttpResponseRedirect
@@ -9,6 +10,7 @@
from django.urls import reverse
from django.utils.encoding import force_bytes, force_str
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
+from django.utils.safestring import mark_safe
from django.views.generic import FormView
from lacommunaute.forum_member.models import ForumProfile
@@ -24,7 +26,7 @@
logger = logging.getLogger(__name__)
-def send_magic_link(user, next_url):
+def 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})
@@ -38,6 +40,10 @@ def send_magic_link(user, next_url):
template_id=settings.SIB_MAGIC_LINK_TEMPLATE,
)
+ if settings.DEBUG is True:
+ message = mark_safe(f'
Login link sent to {user.email}')
+ messages.success(request, message)
+
class LoginView(FormView):
template_name = "registration/login_with_magic_link.html"
@@ -62,7 +68,7 @@ def form_valid(self, form):
query_params = {"email": email, "next": clean_next_url(next_url)}
return HttpResponseRedirect(f"{base_url}?{urlencode(query_params)}")
- send_magic_link(user, next_url)
+ send_magic_link(self.request, user, next_url)
return render(self.request, "registration/login_link_sent.html", {"email": email})
@@ -91,7 +97,7 @@ def form_valid(self, form):
)
ForumProfile.objects.create(user=user)
- send_magic_link(user, next_url)
+ send_magic_link(self.request, user, next_url)
return render(self.request, "registration/login_link_sent.html", {"email": email})