mute_topic: Update tests to not use lib functions directly.

The "add_topic_mute" and "remove_topic_mute" library functions
shouldn't be called directly from tests.
They should instead call "do_mute_topic" and "do_unmute_topic"

The reason being:
Library functions are meant to be internal interfaces
for just changing the database, and shouldn't generally be
called elsewhere.
This commit is contained in:
Prakhar Pratyush 2023-03-03 22:30:27 +05:30 committed by Tim Abbott
parent e161b36b21
commit 9ecc610681
4 changed files with 36 additions and 65 deletions

View File

@ -48,7 +48,6 @@ from zerver.lib.test_helpers import (
use_s3_backend, use_s3_backend,
) )
from zerver.lib.upload import claim_attachment, upload_avatar_image, upload_message_attachment from zerver.lib.upload import claim_attachment, upload_avatar_image, upload_message_attachment
from zerver.lib.user_topics import add_topic_mute
from zerver.lib.utils import assert_is_not_none from zerver.lib.utils import assert_is_not_none
from zerver.models import ( from zerver.models import (
AlertWord, AlertWord,
@ -773,13 +772,10 @@ class RealmImportExportTest(ExportFile):
# data to test import of muted topic # data to test import of muted topic
stream = get_stream("Verona", original_realm) stream = get_stream("Verona", original_realm)
recipient = stream.recipient do_mute_topic(
assert recipient is not None sample_user,
add_topic_mute( stream,
user_profile=sample_user, "Verona2",
stream_id=stream.id,
recipient_id=recipient.id,
topic_name="Verona2",
) )
# data to test import of muted users # data to test import of muted users

View File

@ -6,6 +6,7 @@ from django.db import connection, transaction
from zerver.actions.message_flags import do_update_message_flags from zerver.actions.message_flags import do_update_message_flags
from zerver.actions.streams import do_change_stream_permission from zerver.actions.streams import do_change_stream_permission
from zerver.actions.user_topics import do_mute_topic
from zerver.lib.fix_unreads import fix, fix_unsubscribed from zerver.lib.fix_unreads import fix, fix_unsubscribed
from zerver.lib.message import ( from zerver.lib.message import (
MessageDetailsDict, MessageDetailsDict,
@ -23,7 +24,6 @@ from zerver.lib.message import (
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import get_subscription, timeout_mock from zerver.lib.test_helpers import get_subscription, timeout_mock
from zerver.lib.timeout import TimeoutExpiredError from zerver.lib.timeout import TimeoutExpiredError
from zerver.lib.user_topics import add_topic_mute
from zerver.models import ( from zerver.models import (
Message, Message,
Recipient, Recipient,
@ -470,14 +470,11 @@ class FixUnreadTests(ZulipTestCase):
def mute_topic(stream_name: str, topic_name: str) -> None: def mute_topic(stream_name: str, topic_name: str) -> None:
stream = get_stream(stream_name, realm) stream = get_stream(stream_name, realm)
recipient = stream.recipient
assert recipient is not None
add_topic_mute( do_mute_topic(
user_profile=user, user,
stream_id=stream.id, stream,
recipient_id=recipient.id, topic_name,
topic_name=topic_name,
) )
def force_unsubscribe(stream_name: str) -> None: def force_unsubscribe(stream_name: str) -> None:
@ -711,14 +708,11 @@ class GetUnreadMsgsTest(ZulipTestCase):
def mute_topic(self, user_profile: UserProfile, stream_name: str, topic_name: str) -> None: def mute_topic(self, user_profile: UserProfile, stream_name: str, topic_name: str) -> None:
realm = user_profile.realm realm = user_profile.realm
stream = get_stream(stream_name, realm) stream = get_stream(stream_name, realm)
recipient = stream.recipient
assert recipient is not None
add_topic_mute( do_mute_topic(
user_profile=user_profile, user_profile,
stream_id=stream.id, stream,
recipient_id=recipient.id, topic_name,
topic_name=topic_name,
) )
def test_raw_unread_stream(self) -> None: def test_raw_unread_stream(self) -> None:

View File

@ -4,12 +4,11 @@ from unittest import mock
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.actions.user_topics import do_mute_topic, do_unmute_topic
from zerver.lib.stream_topic import StreamTopicTarget from zerver.lib.stream_topic import StreamTopicTarget
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.user_topics import ( from zerver.lib.user_topics import (
add_topic_mute,
get_topic_mutes, get_topic_mutes,
remove_topic_mute,
topic_is_muted, topic_is_muted,
) )
from zerver.models import UserProfile, UserTopic, get_stream from zerver.models import UserProfile, UserTopic, get_stream
@ -21,16 +20,13 @@ class MutedTopicsTests(ZulipTestCase):
self.login_user(user) self.login_user(user)
stream = get_stream("Verona", user.realm) stream = get_stream("Verona", user.realm)
recipient = stream.recipient
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp() mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
assert recipient is not None do_mute_topic(
add_topic_mute( user,
user_profile=user, stream,
stream_id=stream.id, "Verona3",
recipient_id=recipient.id,
topic_name="Verona3",
date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc), date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc),
) )
@ -45,7 +41,6 @@ class MutedTopicsTests(ZulipTestCase):
cordelia = self.example_user("cordelia") cordelia = self.example_user("cordelia")
realm = hamlet.realm realm = hamlet.realm
stream = get_stream("Verona", realm) stream = get_stream("Verona", realm)
recipient = stream.recipient
topic_name = "teST topic" topic_name = "teST topic"
stream_topic_target = StreamTopicTarget( stream_topic_target = StreamTopicTarget(
@ -57,12 +52,10 @@ class MutedTopicsTests(ZulipTestCase):
self.assertEqual(user_ids, set()) self.assertEqual(user_ids, set())
def mute_topic_for_user(user: UserProfile) -> None: def mute_topic_for_user(user: UserProfile) -> None:
assert recipient is not None do_mute_topic(
add_topic_mute( user,
user_profile=user, stream,
stream_id=stream.id, "test TOPIC",
recipient_id=recipient.id,
topic_name="test TOPIC",
date_muted=timezone_now(), date_muted=timezone_now(),
) )
@ -107,10 +100,10 @@ class MutedTopicsTests(ZulipTestCase):
self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user)) self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
self.assertTrue(topic_is_muted(user, stream.id, "verona3")) self.assertTrue(topic_is_muted(user, stream.id, "verona3"))
remove_topic_mute( do_unmute_topic(
user_profile=user, user,
stream_id=stream.id, stream,
topic_name="Verona3", "Verona3",
) )
assert stream.recipient is not None assert stream.recipient is not None
@ -128,7 +121,6 @@ class MutedTopicsTests(ZulipTestCase):
self.login_user(user) self.login_user(user)
stream = get_stream("Verona", realm) stream = get_stream("Verona", realm)
recipient = stream.recipient
url = "/api/v1/users/me/subscriptions/muted_topics" url = "/api/v1/users/me/subscriptions/muted_topics"
payloads: List[Dict[str, object]] = [ payloads: List[Dict[str, object]] = [
@ -137,13 +129,11 @@ class MutedTopicsTests(ZulipTestCase):
] ]
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp() mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
assert recipient is not None
for data in payloads: for data in payloads:
add_topic_mute( do_mute_topic(
user_profile=user, user,
stream_id=stream.id, stream,
recipient_id=recipient.id, "Verona3",
topic_name="Verona3",
date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc), date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc),
) )
self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user)) self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
@ -160,15 +150,7 @@ class MutedTopicsTests(ZulipTestCase):
self.login_user(user) self.login_user(user)
stream = get_stream("Verona", realm) stream = get_stream("Verona", realm)
recipient = stream.recipient do_mute_topic(user, stream, "Verona3", date_muted=timezone_now())
assert recipient is not None
add_topic_mute(
user_profile=user,
stream_id=stream.id,
recipient_id=recipient.id,
topic_name="Verona3",
date_muted=timezone_now(),
)
url = "/api/v1/users/me/subscriptions/muted_topics" url = "/api/v1/users/me/subscriptions/muted_topics"

View File

@ -18,6 +18,7 @@ from zerver.actions.message_send import RecipientInfoResult, get_recipient_info
from zerver.actions.muted_users import do_mute_user from zerver.actions.muted_users import do_mute_user
from zerver.actions.realm_settings import do_set_realm_property from zerver.actions.realm_settings import do_set_realm_property
from zerver.actions.user_settings import bulk_regenerate_api_keys, do_change_user_setting from zerver.actions.user_settings import bulk_regenerate_api_keys, do_change_user_setting
from zerver.actions.user_topics import do_mute_topic
from zerver.actions.users import ( from zerver.actions.users import (
change_user_is_active, change_user_is_active,
do_change_can_create_users, do_change_can_create_users,
@ -46,7 +47,6 @@ from zerver.lib.test_helpers import (
) )
from zerver.lib.upload import upload_avatar_image from zerver.lib.upload import upload_avatar_image
from zerver.lib.user_groups import get_system_user_group_for_user from zerver.lib.user_groups import get_system_user_group_for_user
from zerver.lib.user_topics import add_topic_mute
from zerver.lib.users import Accounts, access_user_by_id, get_accounts_for_email, user_ids_to_users from zerver.lib.users import Accounts, access_user_by_id, get_accounts_for_email, user_ids_to_users
from zerver.lib.utils import assert_is_not_none from zerver.lib.utils import assert_is_not_none
from zerver.models import ( from zerver.models import (
@ -1841,11 +1841,10 @@ class RecipientInfoTest(ZulipTestCase):
self.assertEqual(info.stream_push_user_ids, {hamlet.id}) self.assertEqual(info.stream_push_user_ids, {hamlet.id})
# Now have Hamlet mute the topic to omit him from stream_push_user_ids. # Now have Hamlet mute the topic to omit him from stream_push_user_ids.
add_topic_mute( do_mute_topic(
user_profile=hamlet, hamlet,
stream_id=stream.id, stream,
recipient_id=recipient.id, topic_name,
topic_name=topic_name,
) )
info = get_recipient_info( info = get_recipient_info(