Skip to content

Commit

Permalink
test send_magic_link
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Nov 12, 2024
1 parent 05e6c36 commit 334f141
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
11 changes: 11 additions & 0 deletions lacommunaute/forum_member/tests/__snapshots__/tests_view.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,14 @@
</main>
'''
# ---
# name: TestSendMagicLink.test_send_magic_link[send_magic_link_email_body]
'''
<p>Bonjour Samuel J.</p>
<p>Cliquez sur le lien ci-dessous pour vous connecter à la communauté de l'inclusion :</p>
<p>http://127.0.0.1:8000MAGIC_LINK?next=%2Ftopics%2F</p>
<p>Si vous n'êtes pas à l'origine de cette demande, vous pouvez ignorer ce message.</p>
<p>A bientôt,</p>
<p>L'équipe de la communauté</p>

'''
# ---
26 changes: 25 additions & 1 deletion lacommunaute/forum_member/tests/tests_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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 == "[email protected]"

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):
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/forum_member/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 334f141

Please sign in to comment.