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
|
|
|
|
|
2022-04-14 23:50:10 +02:00
|
|
|
from zerver.actions.message_send import create_mirror_user_if_needed
|
2020-07-05 20:22:29 +02:00
|
|
|
from zerver.lib.create_user import create_user_profile
|
2020-07-04 22:16:52 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2022-03-30 20:17:53 +02:00
|
|
|
from zerver.lib.test_helpers import reset_emails_in_zulip_realm
|
|
|
|
from zerver.models import UserProfile, get_client, get_realm, get_user
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.views.message_send import InvalidMirrorInputError, create_mirrored_message_users
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MirroredMessageUsersTest(ZulipTestCase):
|
|
|
|
def test_invalid_client(self) -> None:
|
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] = []
|
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
message_type = "private"
|
|
|
|
client = get_client("banned_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
with self.assertRaises(InvalidMirrorInputError):
|
2022-03-30 20:17:53 +02:00
|
|
|
create_mirrored_message_users(client, user, recipients, sender.email, message_type)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
message_type = "private"
|
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"]:
|
2022-03-30 20:17:53 +02:00
|
|
|
client = get_client(client_name)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
with self.assertRaises(InvalidMirrorInputError):
|
2022-03-30 20:17:53 +02:00
|
|
|
create_mirrored_message_users(client, user, recipients, sender.email, message_type)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
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
|
|
|
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]
|
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
message_type = "private"
|
|
|
|
client = get_client("zephyr_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
mirror_sender = create_mirrored_message_users(
|
|
|
|
client, user, recipients, sender.email, message_type
|
|
|
|
)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
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
|
|
|
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
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
message_type = "stream"
|
|
|
|
client = get_client("zephyr_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
mirror_sender = create_mirrored_message_users(
|
|
|
|
client, user, recipients, sender_email, message_type
|
|
|
|
)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
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()
|
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
sender = user
|
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
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
message_type = "private"
|
|
|
|
client = get_client("irc_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
mirror_sender = create_mirrored_message_users(
|
|
|
|
client, user, recipients, sender.email, message_type
|
|
|
|
)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
sender = user
|
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"),
|
|
|
|
self.nonreg_email("bob"),
|
|
|
|
self.nonreg_email("cordelia"),
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
message_type = "private"
|
|
|
|
client = get_client("jabber_mirror")
|
2020-07-04 22:16:52 +02:00
|
|
|
|
2022-03-30 20:17:53 +02:00
|
|
|
mirror_sender = create_mirrored_message_users(
|
|
|
|
client, user, recipients, sender.email, message_type
|
|
|
|
)
|
2020-07-04 22:16:52 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-04-14 23:50:10 +02:00
|
|
|
with mock.patch("zerver.actions.message_send.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()
|