mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 14:35:27 +00:00
tests: Introduce send_personal_message().
The tighter interface prevents the need to specify Recipient.PERSONAL (which can often be inaccurate in the huddle case, anyway), and it prevents tests from confusingly specifying a "subject" field for PMs.
This commit is contained in:
@@ -409,6 +409,18 @@ class ZulipTestCase(TestCase):
|
|||||||
content, forged=False, forged_timestamp=None,
|
content, forged=False, forged_timestamp=None,
|
||||||
forwarder_user_profile=sender, realm=sender.realm, **kwargs)
|
forwarder_user_profile=sender, realm=sender.realm, **kwargs)
|
||||||
|
|
||||||
|
def send_personal_message(self, from_email, to_email, content=u"test content"):
|
||||||
|
# type: (Text, Text, Text) -> int
|
||||||
|
sender = get_user_profile_by_email(from_email)
|
||||||
|
|
||||||
|
recipient_list = [to_email]
|
||||||
|
(sending_client, _) = Client.objects.get_or_create(name="test suite")
|
||||||
|
|
||||||
|
return check_send_message(
|
||||||
|
sender, sending_client, 'private', recipient_list, None,
|
||||||
|
content
|
||||||
|
)
|
||||||
|
|
||||||
def send_stream_message(self, sender_email, stream_name,
|
def send_stream_message(self, sender_email, stream_name,
|
||||||
content=u"test content", topic_name=u"test"):
|
content=u"test content", topic_name=u"test"):
|
||||||
# type: (Text, Text, Text, Text) -> int
|
# type: (Text, Text, Text, Text) -> int
|
||||||
|
|||||||
@@ -220,11 +220,11 @@ class TestCrossRealmPMs(ZulipTestCase):
|
|||||||
support_bot = self.create_user(support_email)
|
support_bot = self.create_user(support_email)
|
||||||
|
|
||||||
# Users can PM themselves
|
# Users can PM themselves
|
||||||
self.send_message(user1_email, user1_email, Recipient.PERSONAL)
|
self.send_personal_message(user1_email, user1_email)
|
||||||
assert_message_received(user1, user1)
|
assert_message_received(user1, user1)
|
||||||
|
|
||||||
# Users on the same realm can PM each other
|
# Users on the same realm can PM each other
|
||||||
self.send_message(user1_email, user1a_email, Recipient.PERSONAL)
|
self.send_personal_message(user1_email, user1a_email)
|
||||||
assert_message_received(user1a, user1)
|
assert_message_received(user1a, user1)
|
||||||
|
|
||||||
# Cross-realm bots in the zulip.com realm can PM any realm
|
# Cross-realm bots in the zulip.com realm can PM any realm
|
||||||
@@ -238,13 +238,13 @@ class TestCrossRealmPMs(ZulipTestCase):
|
|||||||
assert_message_received(user2, feedback_bot)
|
assert_message_received(user2, feedback_bot)
|
||||||
|
|
||||||
# All users can PM cross-realm bots in the zulip.com realm
|
# All users can PM cross-realm bots in the zulip.com realm
|
||||||
self.send_message(user1_email, feedback_email, Recipient.PERSONAL)
|
self.send_personal_message(user1_email, feedback_email)
|
||||||
assert_message_received(feedback_bot, user1)
|
assert_message_received(feedback_bot, user1)
|
||||||
|
|
||||||
# Users can PM cross-realm bots on non-zulip realms.
|
# Users can PM cross-realm bots on non-zulip realms.
|
||||||
# (The support bot represents some theoretical bot that we may
|
# (The support bot represents some theoretical bot that we may
|
||||||
# create in the future that does not have zulip.com as its realm.)
|
# create in the future that does not have zulip.com as its realm.)
|
||||||
self.send_message(user1_email, [support_email], Recipient.PERSONAL)
|
self.send_personal_message(user1_email, support_email)
|
||||||
assert_message_received(support_bot, user1)
|
assert_message_received(support_bot, user1)
|
||||||
|
|
||||||
# Allow sending PMs to two different cross-realm bots simultaneously.
|
# Allow sending PMs to two different cross-realm bots simultaneously.
|
||||||
@@ -253,36 +253,37 @@ class TestCrossRealmPMs(ZulipTestCase):
|
|||||||
# prevent them from sending multiple bots at once. We may revisit
|
# prevent them from sending multiple bots at once. We may revisit
|
||||||
# this if it's a nuisance for huddles.)
|
# this if it's a nuisance for huddles.)
|
||||||
self.send_message(user1_email, [feedback_email, support_email],
|
self.send_message(user1_email, [feedback_email, support_email],
|
||||||
Recipient.PERSONAL)
|
Recipient.HUDDLE)
|
||||||
assert_message_received(feedback_bot, user1)
|
assert_message_received(feedback_bot, user1)
|
||||||
assert_message_received(support_bot, user1)
|
assert_message_received(support_bot, user1)
|
||||||
|
|
||||||
# Prevent old loophole where I could send PMs to other users as long
|
# Prevent old loophole where I could send PMs to other users as long
|
||||||
# as I copied a cross-realm bot from the same realm.
|
# as I copied a cross-realm bot from the same realm.
|
||||||
with assert_invalid_email():
|
with assert_invalid_email():
|
||||||
self.send_message(user1_email, [user3_email, support_email], Recipient.PERSONAL)
|
self.send_message(user1_email, [user3_email, support_email],
|
||||||
|
Recipient.HUDDLE)
|
||||||
|
|
||||||
# Users on three different realms can't PM each other,
|
# Users on three different realms can't PM each other,
|
||||||
# even if one of the users is a cross-realm bot.
|
# even if one of the users is a cross-realm bot.
|
||||||
with assert_invalid_email():
|
with assert_invalid_email():
|
||||||
self.send_message(user1_email, [user2_email, feedback_email],
|
self.send_message(user1_email, [user2_email, feedback_email],
|
||||||
Recipient.PERSONAL)
|
Recipient.HUDDLE)
|
||||||
|
|
||||||
with assert_invalid_email():
|
with assert_invalid_email():
|
||||||
self.send_message(feedback_email, [user1_email, user2_email],
|
self.send_message(feedback_email, [user1_email, user2_email],
|
||||||
Recipient.PERSONAL)
|
Recipient.HUDDLE)
|
||||||
|
|
||||||
# Users on the different realms can not PM each other
|
# Users on the different realms can not PM each other
|
||||||
with assert_invalid_email():
|
with assert_invalid_email():
|
||||||
self.send_message(user1_email, user2_email, Recipient.PERSONAL)
|
self.send_personal_message(user1_email, user2_email)
|
||||||
|
|
||||||
# Users on non-zulip realms can't PM "ordinary" Zulip users
|
# Users on non-zulip realms can't PM "ordinary" Zulip users
|
||||||
with assert_invalid_email():
|
with assert_invalid_email():
|
||||||
self.send_message(user1_email, "hamlet@zulip.com", Recipient.PERSONAL)
|
self.send_personal_message(user1_email, "hamlet@zulip.com")
|
||||||
|
|
||||||
# Users on three different realms can not PM each other
|
# Users on three different realms can not PM each other
|
||||||
with assert_invalid_email():
|
with assert_invalid_email():
|
||||||
self.send_message(user1_email, [user2_email, user3_email], Recipient.PERSONAL)
|
self.send_message(user1_email, [user2_email, user3_email], Recipient.HUDDLE)
|
||||||
|
|
||||||
class ExtractedRecipientsTest(TestCase):
|
class ExtractedRecipientsTest(TestCase):
|
||||||
def test_extract_recipients(self):
|
def test_extract_recipients(self):
|
||||||
@@ -320,7 +321,7 @@ class PersonalMessagesTest(ZulipTestCase):
|
|||||||
self.register(test_email, "test")
|
self.register(test_email, "test")
|
||||||
user_profile = self.nonreg_user('test')
|
user_profile = self.nonreg_user('test')
|
||||||
old_messages_count = message_stream_count(user_profile)
|
old_messages_count = message_stream_count(user_profile)
|
||||||
self.send_message(test_email, test_email, Recipient.PERSONAL)
|
self.send_personal_message(test_email, test_email)
|
||||||
new_messages_count = message_stream_count(user_profile)
|
new_messages_count = message_stream_count(user_profile)
|
||||||
self.assertEqual(new_messages_count, old_messages_count + 1)
|
self.assertEqual(new_messages_count, old_messages_count + 1)
|
||||||
|
|
||||||
@@ -353,7 +354,7 @@ class PersonalMessagesTest(ZulipTestCase):
|
|||||||
for user_profile in old_user_profiles:
|
for user_profile in old_user_profiles:
|
||||||
old_messages.append(message_stream_count(user_profile))
|
old_messages.append(message_stream_count(user_profile))
|
||||||
|
|
||||||
self.send_message(test_email, test_email, Recipient.PERSONAL)
|
self.send_personal_message(test_email, test_email)
|
||||||
|
|
||||||
new_messages = []
|
new_messages = []
|
||||||
for user_profile in old_user_profiles:
|
for user_profile in old_user_profiles:
|
||||||
@@ -384,7 +385,7 @@ class PersonalMessagesTest(ZulipTestCase):
|
|||||||
for user_profile in other_user_profiles:
|
for user_profile in other_user_profiles:
|
||||||
old_other_messages.append(message_stream_count(user_profile))
|
old_other_messages.append(message_stream_count(user_profile))
|
||||||
|
|
||||||
self.send_message(sender_email, receiver_email, Recipient.PERSONAL, content)
|
self.send_personal_message(sender_email, receiver_email, content)
|
||||||
|
|
||||||
# Users outside the conversation don't get the message.
|
# Users outside the conversation don't get the message.
|
||||||
new_other_messages = []
|
new_other_messages = []
|
||||||
@@ -1370,8 +1371,11 @@ class EditMessageTest(ZulipTestCase):
|
|||||||
def test_fetch_raw_message(self):
|
def test_fetch_raw_message(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
self.login(self.example_email("hamlet"))
|
self.login(self.example_email("hamlet"))
|
||||||
msg_id = self.send_message(self.example_email("hamlet"), self.example_email("cordelia"), Recipient.PERSONAL,
|
msg_id = self.send_personal_message(
|
||||||
subject="editing", content="**before** edit")
|
from_email=self.example_email("hamlet"),
|
||||||
|
to_email=self.example_email("cordelia"),
|
||||||
|
content="**before** edit",
|
||||||
|
)
|
||||||
result = self.client_get('/json/messages/' + str(msg_id))
|
result = self.client_get('/json/messages/' + str(msg_id))
|
||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
self.assertEqual(result.json()['raw_content'], '**before** edit')
|
self.assertEqual(result.json()['raw_content'], '**before** edit')
|
||||||
@@ -2015,8 +2019,9 @@ class StarTests(ZulipTestCase):
|
|||||||
POST /json/messages/flags.
|
POST /json/messages/flags.
|
||||||
"""
|
"""
|
||||||
self.login(self.example_email("hamlet"))
|
self.login(self.example_email("hamlet"))
|
||||||
message_ids = [self.send_message(self.example_email("hamlet"), self.example_email("hamlet"),
|
message_ids = [self.send_personal_message(self.example_email("hamlet"),
|
||||||
Recipient.PERSONAL, "test")]
|
self.example_email("hamlet"),
|
||||||
|
"test")]
|
||||||
|
|
||||||
# Star a message.
|
# Star a message.
|
||||||
result = self.change_star(message_ids)
|
result = self.change_star(message_ids)
|
||||||
@@ -2052,14 +2057,24 @@ class StarTests(ZulipTestCase):
|
|||||||
other_message_ids = [
|
other_message_ids = [
|
||||||
self.send_stream_message(self.example_email("hamlet"), stream_name, "test_unused"),
|
self.send_stream_message(self.example_email("hamlet"), stream_name, "test_unused"),
|
||||||
]
|
]
|
||||||
received_message_ids = [self.send_message(self.example_email("hamlet"), [self.example_email("cordelia")],
|
received_message_ids = [
|
||||||
Recipient.PERSONAL, "test_received")]
|
self.send_personal_message(
|
||||||
|
self.example_email("hamlet"),
|
||||||
|
self.example_email("cordelia"),
|
||||||
|
"test_received"
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
# Now login as another user who wasn't on that stream
|
# Now login as another user who wasn't on that stream
|
||||||
self.login(self.example_email("cordelia"))
|
self.login(self.example_email("cordelia"))
|
||||||
# Send a message to yourself to make sure we have at least one with the read flag
|
# Send a message to yourself to make sure we have at least one with the read flag
|
||||||
sent_message_ids = [self.send_message(self.example_email("cordelia"), [self.example_email("cordelia")],
|
sent_message_ids = [
|
||||||
Recipient.PERSONAL, "test_read_message")]
|
self.send_personal_message(
|
||||||
|
self.example_email("cordelia"),
|
||||||
|
self.example_email("cordelia"),
|
||||||
|
"test_read_message",
|
||||||
|
),
|
||||||
|
]
|
||||||
result = self.client_post("/json/messages/flags",
|
result = self.client_post("/json/messages/flags",
|
||||||
{"messages": ujson.dumps(sent_message_ids),
|
{"messages": ujson.dumps(sent_message_ids),
|
||||||
"op": "add",
|
"op": "add",
|
||||||
@@ -2104,8 +2119,13 @@ class StarTests(ZulipTestCase):
|
|||||||
POST /json/messages/flags.
|
POST /json/messages/flags.
|
||||||
"""
|
"""
|
||||||
self.login(self.example_email("hamlet"))
|
self.login(self.example_email("hamlet"))
|
||||||
message_ids = [self.send_message(self.example_email("hamlet"), self.example_email("hamlet"),
|
message_ids = [
|
||||||
Recipient.PERSONAL, "test")]
|
self.send_personal_message(
|
||||||
|
self.example_email("hamlet"),
|
||||||
|
self.example_email("hamlet"),
|
||||||
|
"test",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
# Starring private messages you didn't receive fails.
|
# Starring private messages you didn't receive fails.
|
||||||
self.login(self.example_email("cordelia"))
|
self.login(self.example_email("cordelia"))
|
||||||
@@ -2567,8 +2587,7 @@ class SoftDeactivationMessageTest(ZulipTestCase):
|
|||||||
|
|
||||||
def send_personal_message(content):
|
def send_personal_message(content):
|
||||||
# type: (str) -> None
|
# type: (str) -> None
|
||||||
self.send_message(sender, self.example_email("hamlet"),
|
self.send_personal_message(sender, self.example_email("hamlet"), content)
|
||||||
Recipient.PERSONAL, content)
|
|
||||||
|
|
||||||
long_term_idle_user = self.example_user('hamlet')
|
long_term_idle_user = self.example_user('hamlet')
|
||||||
self.send_stream_message(long_term_idle_user.email, stream_name)
|
self.send_stream_message(long_term_idle_user.email, stream_name)
|
||||||
|
|||||||
Reference in New Issue
Block a user