tests: Add assert_message_stream_name

The get_display_recipient helper is a clumsy way to get
stream names, and it's not even representative of how
most of our code retrieves stream names.

The new helper also double-checks that the Stream
object has the correct recipient id.
This commit is contained in:
Steve Howell 2023-07-16 10:08:57 +00:00 committed by Tim Abbott
parent df068ae7a5
commit a54760da0e
6 changed files with 25 additions and 37 deletions

View File

@ -96,7 +96,6 @@ from zerver.models import (
UserStatus,
clear_supported_auth_backends_cache,
flush_per_request_caches,
get_display_recipient,
get_realm,
get_realm_stream,
get_stream,
@ -1284,6 +1283,13 @@ Output:
"""
self.assertEqual(get_session_dict_user(self.client.session), user_id)
def assert_message_stream_name(self, message: Message, stream_name: str) -> None:
self.assertEqual(message.recipient.type, Recipient.STREAM)
stream_id = message.recipient.type_id
stream = Stream.objects.get(id=stream_id)
self.assertEqual(stream.recipient_id, message.recipient_id)
self.assertEqual(stream.name, stream_name)
def webhook_fixture_data(self, type: str, action: str, file_type: str = "json") -> str:
fn = os.path.join(
os.path.dirname(__file__),
@ -2139,7 +2145,7 @@ one or more new messages.
topic_name: str,
content: str,
) -> None:
self.assertEqual(get_display_recipient(message.recipient), stream_name)
self.assert_message_stream_name(message, stream_name)
self.assertEqual(message.topic_name(), topic_name)
self.assertEqual(message.content, content)

View File

@ -42,7 +42,6 @@ from zerver.models import (
Recipient,
Stream,
UserProfile,
get_display_recipient,
get_realm,
get_stream,
get_system_bot,
@ -275,7 +274,7 @@ class TestStreamEmailMessagesSuccess(ZulipTestCase):
message = most_recent_message(user_profile)
self.assertEqual(message.content, "TestStreamEmailMessages body")
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
# Test receiving an email with the address on an UnstructuredHeader
@ -305,7 +304,7 @@ class TestStreamEmailMessagesSuccess(ZulipTestCase):
message = most_recent_message(user_profile)
self.assertEqual(message.content, "TestStreamEmailMessages body")
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
def test_receive_stream_email_messages_blank_subject_success(self) -> None:
@ -330,7 +329,7 @@ class TestStreamEmailMessagesSuccess(ZulipTestCase):
message = most_recent_message(user_profile)
self.assertEqual(message.content, "TestStreamEmailMessages body")
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), "(no topic)")
def test_receive_stream_email_messages_subject_with_nonprintable_chars(
@ -388,7 +387,7 @@ class TestStreamEmailMessagesSuccess(ZulipTestCase):
message = most_recent_message(user_profile)
self.assertEqual(message.content, "TestStreamEmailMessages body")
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
def test_receive_stream_email_multiple_recipient_success(self) -> None:
@ -417,7 +416,7 @@ class TestStreamEmailMessagesSuccess(ZulipTestCase):
message = most_recent_message(user_profile)
self.assertEqual(message.content, "TestStreamEmailMessages body")
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
def test_receive_stream_email_show_sender_success(self) -> None:
@ -437,7 +436,7 @@ class TestStreamEmailMessagesSuccess(ZulipTestCase):
message.content,
"From: {}\n{}".format(self.example_email("hamlet"), msgtext),
)
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
def test_receive_stream_email_forwarded_success(self) -> None:
@ -469,7 +468,7 @@ and other things
message = most_recent_message(user_profile)
expected = "From: {}\n{}".format(self.example_email("hamlet"), expected_body)
self.assertEqual(message.content, expected.strip())
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
# include_quotes=True: expect the From:... to be preserved
@ -511,7 +510,7 @@ and other things
"Test Useróąę <hamlet_ę@zulip.com>", "TestStreamEmailMessages body"
),
)
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
def test_receive_stream_email_include_footer_success(self) -> None:
@ -539,7 +538,7 @@ and other things
message = most_recent_message(user_profile)
self.assertEqual(message.content, text)
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
def test_receive_stream_email_include_quotes_success(self) -> None:
@ -569,7 +568,7 @@ and other things
message = most_recent_message(user_profile)
self.assertEqual(message.content, text)
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assert_message_stream_name(message, stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])

View File

@ -49,8 +49,7 @@ class TestEmbeddedBotMessaging(ZulipTestCase):
self.assertEqual(last_message.content, "beep boop")
self.assertEqual(last_message.sender_id, self.bot_profile.id)
self.assertEqual(last_message.topic_name(), "bar")
display_recipient = get_display_recipient(last_message.recipient)
self.assertEqual(display_recipient, "Denmark")
self.assert_message_stream_name(last_message, "Denmark")
def test_stream_message_not_to_embedded_bot(self) -> None:
self.send_stream_message(self.user_profile, "Denmark", content="foo", topic_name="bar")

View File

@ -10,14 +10,7 @@ from zerver.lib.streams import access_stream_for_send_message
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import most_recent_message
from zerver.lib.users import is_administrator_role
from zerver.models import (
UserProfile,
UserStatus,
get_display_recipient,
get_realm,
get_stream,
get_user_by_delivery_email,
)
from zerver.models import UserProfile, UserStatus, get_realm, get_stream, get_user_by_delivery_email
# Most Zulip tests use ZulipTestCase, which inherits from django.test.TestCase.
@ -329,7 +322,7 @@ class TestMessageHelpers(ZulipTestCase):
# extended to send multiple similar messages.
self.assertEqual(iago_message.id, sent_message_id)
self.assertEqual(iago_message.sender_id, hamlet.id)
self.assertEqual(get_display_recipient(iago_message.recipient), "Denmark")
self.assert_message_stream_name(iago_message, "Denmark")
self.assertEqual(iago_message.topic_name(), "lunch")
self.assertEqual(iago_message.content, "I want pizza!")

View File

@ -2295,7 +2295,7 @@ class GetOldMessagesTest(ZulipTestCase):
for i in range(num_messages_per_stream):
message_id = self.send_stream_message(hamlet, stream_name, content=f"test {i}")
message = Message.objects.get(id=message_id)
self.assertEqual(get_display_recipient(message.recipient), stream_name)
self.assert_message_stream_name(message, stream_name)
send_messages_to_all_streams()

View File

@ -19,14 +19,7 @@ from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.topic import TOPIC_NAME
from zerver.lib.url_encoding import near_message_url
from zerver.lib.users import add_service
from zerver.models import (
Recipient,
Service,
UserProfile,
get_display_recipient,
get_realm,
get_stream,
)
from zerver.models import Recipient, Service, UserProfile, get_realm, get_stream
class ResponseMock:
@ -554,8 +547,7 @@ class TestOutgoingWebhookMessaging(ZulipTestCase):
self.assertEqual(last_message.content, "Hidley ho, I'm a webhook responding!")
self.assertEqual(last_message.sender_id, bot.id)
self.assertEqual(last_message.topic_name(), "bar")
display_recipient = get_display_recipient(last_message.recipient)
self.assertEqual(display_recipient, "Denmark")
self.assert_message_stream_name(last_message, "Denmark")
@responses.activate
def test_stream_message_failure_to_outgoing_webhook_bot(self) -> None:
@ -609,8 +601,7 @@ class TestOutgoingWebhookMessaging(ZulipTestCase):
self.assertEqual(stream_message.content, "Failure! Bot is unavailable")
self.assertEqual(stream_message.sender_id, bot.id)
self.assertEqual(stream_message.topic_name(), "bar")
display_recipient = get_display_recipient(stream_message.recipient)
self.assertEqual(display_recipient, "Denmark")
self.assert_message_stream_name(stream_message, "Denmark")
@responses.activate
def test_stream_message_failure_deactivated_to_outgoing_webhook_bot(self) -> None: