From 334f14105fa5483b09dc7adaa381143ecdb4e7f9 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Tue, 12 Nov 2024 17:25:30 +0100 Subject: [PATCH] test send_magic_link --- .../tests/__snapshots__/tests_view.ambr | 11 ++++++++ lacommunaute/forum_member/tests/tests_view.py | 26 ++++++++++++++++++- lacommunaute/forum_member/views.py | 4 +-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lacommunaute/forum_member/tests/__snapshots__/tests_view.ambr b/lacommunaute/forum_member/tests/__snapshots__/tests_view.ambr index c78de6d5e..3b03e119b 100644 --- a/lacommunaute/forum_member/tests/__snapshots__/tests_view.ambr +++ b/lacommunaute/forum_member/tests/__snapshots__/tests_view.ambr @@ -336,3 +336,14 @@ ''' # --- +# name: TestSendMagicLink.test_send_magic_link[send_magic_link_email_body] + ''' +

Bonjour Samuel J.

+

Cliquez sur le lien ci-dessous pour vous connecter à la communauté de l'inclusion :

+

http://127.0.0.1:8000MAGIC_LINK?next=%2Ftopics%2F

+

Si vous n'êtes pas à l'origine de cette demande, vous pouvez ignorer ce message.

+

A bientôt,

+

L'équipe de la communauté

+ + ''' +# --- diff --git a/lacommunaute/forum_member/tests/tests_view.py b/lacommunaute/forum_member/tests/tests_view.py index 0d9c950f2..d63685eae 100644 --- a/lacommunaute/forum_member/tests/tests_view.py +++ b/lacommunaute/forum_member/tests/tests_view.py @@ -3,7 +3,7 @@ from django.urls import reverse from machina.core.loading import get_class from pytest_django.asserts import assertContains, assertNotContains - +from django.core import mail from lacommunaute.forum_member.factories import ForumProfileFactory from lacommunaute.forum_member.models import ForumProfile from lacommunaute.forum_member.shortcuts import get_forum_member_display_name @@ -16,6 +16,7 @@ from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode from django.contrib.auth.tokens import default_token_generator +from lacommunaute.forum_member.views import send_magic_link PermissionHandler = get_class("forum_permission.handler", "PermissionHandler") assign_perm = get_class("forum_permission.shortcuts", "assign_perm") @@ -71,6 +72,29 @@ def test_show_linkedin_link(self, client, db): assertContains(response, forum_profile.linkedin) +class TestSendMagicLink: + def test_send_magic_link(self, client, db, snapshot): + user = UserFactory(first_name="Samuel", last_name="Jackson") + next_url = "/topics/" + send_magic_link(client, user, next_url) + + assert len(mail.outbox) == 1 + email = mail.outbox[0] + assert email.subject == "Votre lien pour vous connecter à la communauté de l'inclusion" + assert email.to == [user.email] + assert email.from_email == "noreply@inclusion.gouv.fr" + + expected_magic_link = reverse( + "members:login_with_link", + kwargs={ + "uidb64": urlsafe_base64_encode(force_bytes(user.pk)), + "token": default_token_generator.make_token(user), + }, + ) + assert expected_magic_link in email.body + assert email.body.replace(expected_magic_link, "MAGIC_LINK") == snapshot(name="send_magic_link_email_body") + + class TestLoginView: @pytest.mark.parametrize("next", [None, "/", "/topics/"]) def test_content(self, client, db, next, snapshot): diff --git a/lacommunaute/forum_member/views.py b/lacommunaute/forum_member/views.py index d9e453492..3f0dae9c4 100644 --- a/lacommunaute/forum_member/views.py +++ b/lacommunaute/forum_member/views.py @@ -60,8 +60,8 @@ def get_context_data(self, **kwargs): def send_magic_link(request, user, next_url): token = default_token_generator.make_token(user) - uid = urlsafe_base64_encode(force_bytes(user.pk)) - url = reverse("members:login_with_link", kwargs={"uidb64": uid, "token": token}) + uidb64 = urlsafe_base64_encode(force_bytes(user.pk)) + url = reverse("members:login_with_link", kwargs={"uidb64": uidb64, "token": token}) query_params = urlencode({"next": next_url}) login_link = f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{url}?{query_params}" message = render_to_string(