Skip to content

Commit ca671ef

Browse files
committed
Do not send e-mails when using become/hijack.
1 parent b06460d commit ca671ef

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

hypha/apply/users/signals.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@
77

88
from hypha.core.mail import MarkdownMail
99

10+
HIJACK_VIEW_NAMES = {
11+
"hijack-become",
12+
"users:hijack",
13+
"hijack:acquire",
14+
"hijack:release",
15+
}
16+
1017

1118
@receiver(user_logged_in)
1219
def send_login_notification(sender, request, user, **kwargs):
1320
if not settings.SEND_MESSAGES or not user.email:
1421
return
1522

23+
if request and getattr(request, "resolver_match", None):
24+
if request.resolver_match.view_name in HIJACK_VIEW_NAMES:
25+
return
26+
1627
subject = _("Successful login to %(org)s") % {"org": settings.ORG_LONG_NAME}
1728
if settings.EMAIL_SUBJECT_PREFIX:
1829
subject = str(settings.EMAIL_SUBJECT_PREFIX) + str(subject)

hypha/apply/users/tests/test_signals.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import MagicMock
2+
13
from django.contrib.auth.signals import user_logged_in
24
from django.core import mail
35
from django.test import RequestFactory, TestCase, override_settings
@@ -51,3 +53,28 @@ def test_no_email_when_request_is_none(self):
5153
def test_email_body_contains_login_time(self):
5254
self._fire_signal()
5355
self.assertTrue(any("Login time" in part for part in [mail.outbox[0].body]))
56+
57+
def _fire_signal_with_view_name(self, view_name):
58+
request = self.factory.get("/")
59+
request.resolver_match = MagicMock(view_name=view_name)
60+
self._fire_signal(request=request)
61+
62+
def test_no_email_on_hijack_acquire(self):
63+
self._fire_signal_with_view_name("hijack:acquire")
64+
self.assertEqual(len(mail.outbox), 0)
65+
66+
def test_no_email_on_hijack_release(self):
67+
self._fire_signal_with_view_name("hijack:release")
68+
self.assertEqual(len(mail.outbox), 0)
69+
70+
def test_no_email_on_hijack_become(self):
71+
self._fire_signal_with_view_name("hijack-become")
72+
self.assertEqual(len(mail.outbox), 0)
73+
74+
def test_no_email_on_users_hijack_view(self):
75+
self._fire_signal_with_view_name("users:hijack")
76+
self.assertEqual(len(mail.outbox), 0)
77+
78+
def test_email_sent_for_non_hijack_view(self):
79+
self._fire_signal_with_view_name("account_login")
80+
self.assertEqual(len(mail.outbox), 1)

0 commit comments

Comments
 (0)