bots: Pass realm to self.notification_bot test helper.

This commit is contained in:
Mateusz Mandera
2021-03-08 11:39:48 +01:00
committed by Tim Abbott
parent a01594e72b
commit 994ee70497
9 changed files with 51 additions and 42 deletions

View File

@@ -470,8 +470,8 @@ Output:
def mit_email(self, name: str) -> str:
return self.mit_user_map[name]
def notification_bot(self) -> UserProfile:
return get_system_bot(settings.NOTIFICATION_BOT)
def notification_bot(self, realm: Realm) -> UserProfile:
return get_system_bot(settings.NOTIFICATION_BOT, realm.id)
def create_test_bot(
self, short_name: str, user_profile: UserProfile, full_name: str = "Foo Bot", **extras: Any

View File

@@ -231,7 +231,7 @@ class TestRealmAuditLog(ZulipTestCase):
def test_change_bot_owner(self) -> None:
now = timezone_now()
admin = self.example_user("iago")
bot = self.notification_bot()
bot = self.notification_bot(admin.realm)
bot_owner = self.example_user("hamlet")
do_change_bot_owner(bot, bot_owner, admin)
self.assertEqual(

View File

@@ -430,7 +430,7 @@ class FetchInitialStateDataTest(ZulipTestCase):
self.assert_length(result["realm_bots"], 0)
# additionally the API key for a random bot is not present in the data
api_key = get_api_key(self.notification_bot())
api_key = get_api_key(self.notification_bot(user_profile.realm))
self.assertNotIn(api_key, str(result))
# Admin users have access to all bots in the realm_bots field

View File

@@ -605,9 +605,10 @@ class HomeTest(ZulipTestCase):
del cross_bot["avatar_url"]
del cross_bot["date_joined"]
notification_bot = self.notification_bot()
email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT)
welcome_bot = get_system_bot(settings.WELCOME_BOT)
admin_realm = get_realm(settings.SYSTEM_BOT_REALM)
cross_realm_notification_bot = self.notification_bot(admin_realm)
cross_realm_email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT, admin_realm.id)
cross_realm_welcome_bot = get_system_bot(settings.WELCOME_BOT, admin_realm.id)
by_email = lambda d: d["email"]
@@ -616,50 +617,50 @@ class HomeTest(ZulipTestCase):
sorted(
[
dict(
avatar_version=email_gateway_bot.avatar_version,
avatar_version=cross_realm_email_gateway_bot.avatar_version,
bot_owner_id=None,
bot_type=1,
email=email_gateway_bot.email,
user_id=email_gateway_bot.id,
full_name=email_gateway_bot.full_name,
email=cross_realm_email_gateway_bot.email,
user_id=cross_realm_email_gateway_bot.id,
full_name=cross_realm_email_gateway_bot.full_name,
is_active=True,
is_bot=True,
is_admin=False,
is_owner=False,
is_billing_admin=False,
role=email_gateway_bot.role,
role=cross_realm_email_gateway_bot.role,
is_system_bot=True,
is_guest=False,
),
dict(
avatar_version=notification_bot.avatar_version,
avatar_version=cross_realm_notification_bot.avatar_version,
bot_owner_id=None,
bot_type=1,
email=notification_bot.email,
user_id=notification_bot.id,
full_name=notification_bot.full_name,
email=cross_realm_notification_bot.email,
user_id=cross_realm_notification_bot.id,
full_name=cross_realm_notification_bot.full_name,
is_active=True,
is_bot=True,
is_admin=False,
is_owner=False,
is_billing_admin=False,
role=notification_bot.role,
role=cross_realm_notification_bot.role,
is_system_bot=True,
is_guest=False,
),
dict(
avatar_version=welcome_bot.avatar_version,
avatar_version=cross_realm_welcome_bot.avatar_version,
bot_owner_id=None,
bot_type=1,
email=welcome_bot.email,
user_id=welcome_bot.id,
full_name=welcome_bot.full_name,
email=cross_realm_welcome_bot.email,
user_id=cross_realm_welcome_bot.id,
full_name=cross_realm_welcome_bot.full_name,
is_active=True,
is_bot=True,
is_admin=False,
is_owner=False,
is_billing_admin=False,
role=welcome_bot.role,
role=cross_realm_welcome_bot.role,
is_system_bot=True,
is_guest=False,
),

View File

@@ -272,11 +272,11 @@ class MessageDictTest(ZulipTestCase):
# Send messages before and after saving the realm filter from each user.
assert_topic_links([], get_message(self.example_user("othello"), zulip_realm))
assert_topic_links([], get_message(self.lear_user("cordelia"), lear_realm))
assert_topic_links([], get_message(self.notification_bot(), zulip_realm))
assert_topic_links([], get_message(self.notification_bot(zulip_realm), zulip_realm))
linkifier.save()
assert_topic_links([links], get_message(self.example_user("othello"), zulip_realm))
assert_topic_links([], get_message(self.lear_user("cordelia"), lear_realm))
assert_topic_links([links], get_message(self.notification_bot(), zulip_realm))
assert_topic_links([links], get_message(self.notification_bot(zulip_realm), zulip_realm))
def test_reaction(self) -> None:
sender = self.example_user("othello")

View File

@@ -230,6 +230,7 @@ class EditMessageTest(EditMessageTestCase):
# tested in this class. Hence, its performance is tested here.
# Generate 2 messages
user = self.example_user("hamlet")
realm = user.realm
self.login_user(user)
stream_name = "public_stream"
self.subscribe(user, stream_name)
@@ -238,9 +239,9 @@ class EditMessageTest(EditMessageTestCase):
user_2 = self.example_user("cordelia")
self.subscribe(user_2, stream_name)
message_ids.append(self.send_stream_message(user_2, stream_name, "Message two"))
self.subscribe(self.notification_bot(), stream_name)
self.subscribe(self.notification_bot(realm), stream_name)
message_ids.append(
self.send_stream_message(self.notification_bot(), stream_name, "Message three")
self.send_stream_message(self.notification_bot(realm), stream_name, "Message three")
)
messages = [
Message.objects.select_related().get(id=message_id) for message_id in message_ids

View File

@@ -1286,8 +1286,9 @@ class MessagePOSTTest(ZulipTestCase):
def test_cross_realm_bots_can_use_api_on_own_subdomain(self) -> None:
# Cross realm bots should use internal_send_*_message, not the API:
notification_bot = self.notification_bot()
stream = self.make_stream("notify_channel", get_realm("zulipinternal"))
internal_realm = get_realm("zulipinternal")
notification_bot = self.notification_bot(internal_realm)
stream = self.make_stream("notify_channel", internal_realm)
result = self.api_post(
notification_bot,
@@ -2516,7 +2517,8 @@ class CheckMessageTest(ZulipTestCase):
do_change_can_forge_sender(forwarder_user_profile, True)
mit_user = self.mit_user("sipbtest")
notification_bot = self.notification_bot()
internal_realm = get_realm("zulipinternal")
notification_bot = self.notification_bot(internal_realm)
client = make_client(name="test suite")
stream = get_stream("Denmark", forwarder_user_profile.realm)
@@ -2553,7 +2555,8 @@ class CheckMessageTest(ZulipTestCase):
do_change_can_forge_sender(forwarder_user_profile, True)
mit_user = self.mit_user("sipbtest")
notification_bot = self.notification_bot()
internal_realm = get_realm("zulipinternal")
notification_bot = self.notification_bot(internal_realm)
client = make_client(name="test suite")
stream_name = "España y Francia"

View File

@@ -3011,7 +3011,8 @@ class SubscriptionAPITest(ZulipTestCase):
msg = self.get_second_to_last_message()
self.assertEqual(msg.recipient.type, Recipient.STREAM)
self.assertEqual(msg.sender_id, self.notification_bot().id)
self.assertEqual(msg.recipient.type_id, notifications_stream.id)
self.assertEqual(msg.sender_id, self.notification_bot(self.test_realm).id)
expected_msg = (
f"@_**{invitee_full_name}|{invitee.id}** created a new stream #**{invite_streams[0]}**."
)
@@ -3045,7 +3046,8 @@ class SubscriptionAPITest(ZulipTestCase):
msg = self.get_second_to_last_message()
self.assertEqual(msg.recipient.type, Recipient.STREAM)
self.assertEqual(msg.sender_id, self.notification_bot().id)
self.assertEqual(msg.recipient.type_id, notifications_stream.id)
self.assertEqual(msg.sender_id, self.notification_bot(realm).id)
stream_id = Stream.objects.latest("id").id
expected_rendered_msg = f'<p><span class="user-mention silent" data-user-id="{user.id}">{user.full_name}</span> created a new stream <a class="stream" data-stream-id="{stream_id}" href="/#narrow/stream/{stream_id}-{invite_streams[0]}">#{invite_streams[0]}</a>.</p>'
self.assertEqual(msg.rendered_content, expected_rendered_msg)
@@ -3073,7 +3075,7 @@ class SubscriptionAPITest(ZulipTestCase):
)
msg = self.get_second_to_last_message()
self.assertEqual(msg.sender_id, self.notification_bot().id)
self.assertEqual(msg.sender_id, self.notification_bot(notifications_stream.realm).id)
expected_msg = (
f"@_**{invitee_full_name}|{invitee.id}** created a new stream #**{invite_streams[0]}**."
)
@@ -4564,11 +4566,11 @@ class GetSubscribersTest(ZulipTestCase):
self.user_profile = self.example_user("hamlet")
self.login_user(self.user_profile)
def assert_user_got_subscription_notification(self, expected_msg: str) -> None:
def assert_user_got_subscription_notification(self, expected_msg: str, realm: Realm) -> None:
# verify that the user was sent a message informing them about the subscription
msg = self.get_last_message()
self.assertEqual(msg.recipient.type, msg.recipient.PERSONAL)
self.assertEqual(msg.sender_id, self.notification_bot().id)
self.assertEqual(msg.sender_id, self.notification_bot(realm).id)
def non_ws(s: str) -> str:
return s.replace("\n", "").replace(" ", "")
@@ -4648,7 +4650,7 @@ class GetSubscribersTest(ZulipTestCase):
* #**stream_9**
"""
self.assert_user_got_subscription_notification(msg)
self.assert_user_got_subscription_notification(msg, self.user_profile.realm)
# Subscribe ourself first.
self.common_subscribe_to_streams(
@@ -4670,7 +4672,7 @@ class GetSubscribersTest(ZulipTestCase):
msg = """
@**King Hamlet** subscribed you to the stream #**stream_invite_only_1**.
"""
self.assert_user_got_subscription_notification(msg)
self.assert_user_got_subscription_notification(msg, self.user_profile.realm)
with queries_captured() as queries:
subscribed_streams, _ = gather_subscriptions(

View File

@@ -36,9 +36,10 @@ class WebhooksCommonTestCase(ZulipTestCase):
self.assertEqual(header_value, "custom_value")
def test_webhook_http_header_header_does_not_exist(self) -> None:
webhook_bot = get_user("webhook-bot@zulip.com", get_realm("zulip"))
realm = get_realm("zulip")
webhook_bot = get_user("webhook-bot@zulip.com", realm)
webhook_bot.last_reminder = None
notification_bot = self.notification_bot()
notification_bot = self.notification_bot(realm)
request = HostRequestMock()
request.user = webhook_bot
request.path = "some/random/path"
@@ -90,7 +91,7 @@ class WebhooksCommonTestCase(ZulipTestCase):
my_webhook_notify(request)
msg = self.get_last_message()
self.assertNotEqual(msg.id, last_message_id)
self.assertEqual(msg.sender.id, self.notification_bot().id)
self.assertEqual(msg.sender.id, self.notification_bot(webhook_bot_realm).id)
self.assertEqual(msg.content, expected_msg.strip())
@patch("zerver.lib.webhooks.common.importlib.import_module")
@@ -187,9 +188,10 @@ class MissingEventHeaderTestCase(WebhookTestCase):
)
self.assert_json_error(result, "Missing the HTTP event header 'X_GROOVE_EVENT'")
webhook_bot = get_user("webhook-bot@zulip.com", get_realm("zulip"))
realm = get_realm("zulip")
webhook_bot = get_user("webhook-bot@zulip.com", realm)
webhook_bot.last_reminder = None
notification_bot = self.notification_bot()
notification_bot = self.notification_bot(realm)
msg = self.get_last_message()
expected_message = MISSING_EVENT_HEADER_MESSAGE.format(
bot_name=webhook_bot.full_name,