2020-07-04 22:16:52 +02:00
|
|
|
from collections import namedtuple
|
2020-07-05 20:22:29 +02:00
|
|
|
from typing import Any, List
|
2020-07-04 22:16:52 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
2020-07-05 20:22:29 +02:00
|
|
|
from django.db import IntegrityError
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from zerver.lib.actions import create_mirror_user_if_needed, get_client
|
|
|
|
from zerver.lib.create_user import create_user_profile
|
2020-07-04 22:16:52 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.test_helpers import reset_emails_in_zulip_realm
|
|
|
|
from zerver.models import UserProfile, get_realm, get_user
|
|
|
|
from zerver.views.message_send import InvalidMirrorInput, create_mirrored_message_users
|
|
|
|
|
|
|
|
|
|
|
|
class MirroredMessageUsersTest(ZulipTestCase):
|
|
|
|
def test_invalid_sender(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-07-04 22:16:52 +02:00
|
|
|
recipients: List[str] = []
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
Request = namedtuple("Request", ["POST"])
|
2020-09-02 08:14:51 +02:00
|
|
|
request = Request(POST={}) # no sender
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
with self.assertRaises(InvalidMirrorInput):
|
|
|
|
create_mirrored_message_users(request, user, recipients)
|
|
|
|
|
|
|
|
def test_invalid_client(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
client = get_client(name="banned_mirror") # Invalid!!!
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-07-04 22:16:52 +02:00
|
|
|
sender = user
|
|
|
|
|
|
|
|
recipients: List[str] = []
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
Request = namedtuple("Request", ["POST", "client"])
|
|
|
|
request = Request(POST=dict(sender=sender.email, type="private"), client=client)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
with self.assertRaises(InvalidMirrorInput):
|
|
|
|
create_mirrored_message_users(request, user, recipients)
|
|
|
|
|
|
|
|
def test_invalid_email(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
invalid_email = "alice AT example.com"
|
2020-07-04 22:16:52 +02:00
|
|
|
recipients = [invalid_email]
|
|
|
|
|
|
|
|
# We use an MIT user here to maximize code coverage
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.mit_user("starnine")
|
2020-07-04 22:16:52 +02:00
|
|
|
sender = user
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
Request = namedtuple("Request", ["POST", "client"])
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
for client_name in ["zephyr_mirror", "irc_mirror", "jabber_mirror"]:
|
2020-07-04 22:16:52 +02:00
|
|
|
client = get_client(name=client_name)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
request = Request(POST=dict(sender=sender.email, type="private"), client=client)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
with self.assertRaises(InvalidMirrorInput):
|
|
|
|
create_mirrored_message_users(request, user, recipients)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
@mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup",
|
|
|
|
return_value=[["sipbtest:*:20922:101:Fred Sipb,,,:/mit/sipbtest:/bin/athena/tcsh"]],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-07-04 22:16:52 +02:00
|
|
|
def test_zephyr_mirror_new_recipient(self, ignored: object) -> None:
|
|
|
|
"""Test mirror dummy user creation for PM recipients"""
|
2021-02-12 08:20:45 +01:00
|
|
|
client = get_client(name="zephyr_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.mit_user("starnine")
|
|
|
|
sender = self.mit_user("sipbtest")
|
|
|
|
new_user_email = "bob_the_new_user@mit.edu"
|
2020-07-04 22:16:52 +02:00
|
|
|
new_user_realm = get_realm("zephyr")
|
|
|
|
|
|
|
|
recipients = [user.email, new_user_email]
|
|
|
|
|
|
|
|
# Now make the request.
|
2021-02-12 08:20:45 +01:00
|
|
|
Request = namedtuple("Request", ["POST", "client"])
|
|
|
|
request = Request(POST=dict(sender=sender.email, type="private"), client=client)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
mirror_sender = create_mirrored_message_users(request, user, recipients)
|
|
|
|
|
|
|
|
self.assertEqual(mirror_sender, sender)
|
|
|
|
|
|
|
|
realm_users = UserProfile.objects.filter(realm=sender.realm)
|
|
|
|
realm_emails = {user.email for user in realm_users}
|
|
|
|
self.assertIn(user.email, realm_emails)
|
|
|
|
self.assertIn(new_user_email, realm_emails)
|
|
|
|
|
|
|
|
bob = get_user(new_user_email, new_user_realm)
|
|
|
|
self.assertTrue(bob.is_mirror_dummy)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
@mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup",
|
|
|
|
return_value=[["sipbtest:*:20922:101:Fred Sipb,,,:/mit/sipbtest:/bin/athena/tcsh"]],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-07-04 22:16:52 +02:00
|
|
|
def test_zephyr_mirror_new_sender(self, ignored: object) -> None:
|
|
|
|
"""Test mirror dummy user creation for sender when sending to stream"""
|
2021-02-12 08:20:45 +01:00
|
|
|
client = get_client(name="zephyr_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.mit_user("starnine")
|
|
|
|
sender_email = "new_sender@mit.edu"
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
recipients = ["stream_name"]
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
# Now make the request.
|
2021-02-12 08:20:45 +01:00
|
|
|
Request = namedtuple("Request", ["POST", "client"])
|
|
|
|
request = Request(POST=dict(sender=sender_email, type="stream"), client=client)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
mirror_sender = create_mirrored_message_users(request, user, recipients)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
assert mirror_sender is not None
|
2020-07-04 22:16:52 +02:00
|
|
|
self.assertEqual(mirror_sender.email, sender_email)
|
|
|
|
self.assertTrue(mirror_sender.is_mirror_dummy)
|
|
|
|
|
|
|
|
def test_irc_mirror(self) -> None:
|
|
|
|
reset_emails_in_zulip_realm()
|
2021-02-12 08:20:45 +01:00
|
|
|
client = get_client(name="irc_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("hamlet")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
recipients = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.nonreg_email("alice"),
|
|
|
|
"bob@irc.zulip.com",
|
|
|
|
self.nonreg_email("cordelia"),
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
# Now make the request.
|
2021-02-12 08:20:45 +01:00
|
|
|
Request = namedtuple("Request", ["POST", "client"])
|
|
|
|
request = Request(POST=dict(sender=sender.email, type="private"), client=client)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
mirror_sender = create_mirrored_message_users(request, sender, recipients)
|
|
|
|
|
|
|
|
self.assertEqual(mirror_sender, sender)
|
|
|
|
|
|
|
|
realm_users = UserProfile.objects.filter(realm=sender.realm)
|
|
|
|
realm_emails = {user.email for user in realm_users}
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn(self.nonreg_email("alice"), realm_emails)
|
|
|
|
self.assertIn("bob@irc.zulip.com", realm_emails)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
bob = get_user("bob@irc.zulip.com", sender.realm)
|
2020-07-04 22:16:52 +02:00
|
|
|
self.assertTrue(bob.is_mirror_dummy)
|
|
|
|
|
|
|
|
def test_jabber_mirror(self) -> None:
|
|
|
|
reset_emails_in_zulip_realm()
|
2021-02-12 08:20:45 +01:00
|
|
|
client = get_client(name="jabber_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("hamlet")
|
2020-07-04 22:16:52 +02:00
|
|
|
user = sender
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
recipients = [
|
2021-02-12 08:20:45 +01:00
|
|
|
self.nonreg_email("alice"),
|
|
|
|
self.nonreg_email("bob"),
|
|
|
|
self.nonreg_email("cordelia"),
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
# Now make the request.
|
2021-02-12 08:20:45 +01:00
|
|
|
Request = namedtuple("Request", ["POST", "client"])
|
|
|
|
request = Request(POST=dict(sender=sender.email, type="private"), client=client)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
mirror_sender = create_mirrored_message_users(request, user, recipients)
|
|
|
|
|
|
|
|
self.assertEqual(mirror_sender, sender)
|
|
|
|
|
|
|
|
realm_users = UserProfile.objects.filter(realm=sender.realm)
|
|
|
|
realm_emails = {user.email for user in realm_users}
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn(self.nonreg_email("alice"), realm_emails)
|
|
|
|
self.assertIn(self.nonreg_email("bob"), realm_emails)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
bob = get_user(self.nonreg_email("bob"), sender.realm)
|
2020-07-04 22:16:52 +02:00
|
|
|
self.assertTrue(bob.is_mirror_dummy)
|
2020-07-05 20:22:29 +02:00
|
|
|
|
|
|
|
def test_create_mirror_user_despite_race(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2020-07-05 20:22:29 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
email = "fred@example.com"
|
2020-07-05 20:22:29 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
email_to_full_name = lambda email: "fred"
|
2020-07-05 20:22:29 +02:00
|
|
|
|
|
|
|
def create_user(**kwargs: Any) -> UserProfile:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(kwargs["full_name"], "fred")
|
|
|
|
self.assertEqual(kwargs["email"], email)
|
|
|
|
self.assertEqual(kwargs["active"], False)
|
|
|
|
self.assertEqual(kwargs["is_mirror_dummy"], True)
|
2020-07-05 20:22:29 +02:00
|
|
|
# We create an actual user here to simulate a race.
|
|
|
|
# We use the minimal, un-mocked function.
|
2021-02-12 08:20:45 +01:00
|
|
|
kwargs["bot_type"] = None
|
|
|
|
kwargs["bot_owner"] = None
|
|
|
|
kwargs["tos_version"] = None
|
|
|
|
kwargs["timezone"] = timezone_now()
|
2020-07-05 20:22:29 +02:00
|
|
|
create_user_profile(**kwargs).save()
|
|
|
|
raise IntegrityError()
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("zerver.lib.actions.create_user", side_effect=create_user) as m:
|
2020-07-05 20:22:29 +02:00
|
|
|
mirror_fred_user = create_mirror_user_if_needed(
|
|
|
|
realm,
|
|
|
|
email,
|
|
|
|
email_to_full_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(mirror_fred_user.delivery_email, email)
|
|
|
|
m.assert_called()
|