test_bots.py: Use example_email instead of using the emails directly.

This commit is contained in:
Vishnu Ks
2017-05-23 16:24:29 -07:00
committed by Tim Abbott
parent dbdc0220b9
commit b5c4a95ef1

View File

@@ -40,7 +40,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_bot_domain(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.create_bot()
self.assertTrue(UserProfile.objects.filter(email='hambot-bot@zulip.testserver').exists())
# The other cases are hard to test directly, since we don't allow creating bots from
@@ -59,7 +59,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_bad_username(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
bot_info = dict(
full_name='My bot name',
@@ -71,7 +71,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_no_name(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
bot_info = dict(
full_name='a',
@@ -83,7 +83,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
events = [] # type: List[Dict[str, Any]]
with tornado_redirected_to_list(events):
@@ -106,7 +106,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
default_sending_stream=None,
default_events_register_stream=None,
default_all_public_streams=False,
owner='hamlet@zulip.com')
owner=self.example_email('hamlet'))
),
event['event']
)
@@ -116,12 +116,12 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bots = [m for m in members if m['email'] == 'hambot-bot@zulip.testserver']
self.assertEqual(len(bots), 1)
bot = bots[0]
self.assertEqual(bot['bot_owner'], 'hamlet@zulip.com')
self.assertEqual(bot['bot_owner'], self.example_email('hamlet'))
self.assertEqual(bot['user_id'], get_user_profile_by_email('hambot-bot@zulip.testserver').id)
def test_add_bot_with_username_in_use(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
result = self.create_bot()
self.assert_num_bots_equal(1)
@@ -135,7 +135,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_user_avatar(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
with get_test_image_file('img.png') as fp:
self.create_bot(file=fp)
@@ -151,7 +151,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_too_many_files(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
with get_test_image_file('img.png') as fp1, \
get_test_image_file('img.gif') as fp2:
@@ -167,7 +167,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_sending_stream(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
result = self.create_bot(default_sending_stream='Denmark')
self.assert_num_bots_equal(1)
@@ -178,7 +178,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_sending_stream_not_subscribed(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
result = self.create_bot(default_sending_stream='Rome')
self.assert_num_bots_equal(1)
@@ -197,15 +197,15 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
When 'principals' has a bot, no notification message event or invitation email
is sent when add_subscriptions_backend is called in the above api call.
"""
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
# Normal user i.e. not a bot.
request_data = {
'principals': '["iago@zulip.com"]'
'principals': '["' + self.example_email('iago') + '"]'
}
events = [] # type: List[Dict[str, Any]]
with tornado_redirected_to_list(events):
result = self.common_subscribe_to_streams("hamlet@zulip.com", ['Rome'], request_data)
result = self.common_subscribe_to_streams(self.example_email('hamlet'), ['Rome'], request_data)
self.assert_json_success(result)
msg_event = [e for e in events if e['event']['type'] == 'message']
@@ -222,7 +222,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
}
events_bot = [] # type: List[Dict[str, Any]]
with tornado_redirected_to_list(events_bot):
result = self.common_subscribe_to_streams("hamlet@zulip.com", ['Rome'], bot_request_data)
result = self.common_subscribe_to_streams(self.example_email('hamlet'), ['Rome'], bot_request_data)
self.assert_json_success(result)
# No notification message event or invitation email is sent because of bot.
@@ -235,7 +235,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_sending_stream_private_allowed(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = get_stream("Denmark", user_profile.realm)
self.subscribe_to_stream(user_profile.email, stream.name)
@@ -265,7 +265,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
default_sending_stream='Denmark',
default_events_register_stream=None,
default_all_public_streams=False,
owner='hamlet@zulip.com')
owner=self.example_email('hamlet'))
),
event['event']
)
@@ -273,10 +273,10 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_sending_stream_private_denied(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = get_stream("Denmark", user_profile.realm)
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
self.unsubscribe_from_stream(self.example_email('hamlet'), "Denmark")
do_change_stream_invite_only(stream, True)
bot_info = {
@@ -289,7 +289,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_events_register_stream(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
result = self.create_bot(default_events_register_stream='Denmark')
self.assert_num_bots_equal(1)
@@ -300,7 +300,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_events_register_stream_private_allowed(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = self.subscribe_to_stream(user_profile.email, 'Denmark')
do_change_stream_invite_only(stream, True)
@@ -329,7 +329,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
default_sending_stream=None,
default_events_register_stream='Denmark',
default_all_public_streams=False,
owner='hamlet@zulip.com')
owner=self.example_email('hamlet'))
),
event['event']
)
@@ -337,10 +337,10 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_events_register_stream_private_denied(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = get_stream("Denmark", user_profile.realm)
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
self.unsubscribe_from_stream(self.example_email('hamlet'), "Denmark")
do_change_stream_invite_only(stream, True)
self.assert_num_bots_equal(0)
@@ -354,7 +354,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_add_bot_with_default_all_public_streams(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
result = self.create_bot(default_all_public_streams=ujson.dumps(True))
self.assert_num_bots_equal(1)
@@ -365,7 +365,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_deactivate_bot(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
self.create_bot()
self.assert_num_bots_equal(1)
@@ -377,7 +377,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_deactivate_bogus_bot(self):
# type: () -> None
"""Deleting a bogus bot will succeed silently."""
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
self.create_bot()
self.assert_num_bots_equal(1)
@@ -388,24 +388,24 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_bot_deactivation_attacks(self):
# type: () -> None
"""You cannot deactivate somebody else's bot."""
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
self.create_bot()
self.assert_num_bots_equal(1)
# Have Othello try to deactivate both Hamlet and
# Hamlet's bot.
self.login("othello@zulip.com")
self.login(self.example_email('othello'))
# Can not deactivate a user as a bot
result = self.client_delete("/json/bots/hamlet@zulip.com")
result = self.client_delete("/json/bots/" + self.example_email("hamlet"))
self.assert_json_error(result, 'No such bot')
result = self.client_delete("/json/bots/hambot-bot@zulip.testserver")
self.assert_json_error(result, 'Insufficient permission')
# But we don't actually deactivate the other person's bot.
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(1)
# Can not deactivate a bot as a user
@@ -415,13 +415,13 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_bot_permissions(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.assert_num_bots_equal(0)
self.create_bot()
self.assert_num_bots_equal(1)
# Have Othello try to mess with Hamlet's bots.
self.login("othello@zulip.com")
self.login(self.example_email('othello'))
result = self.client_post("/json/bots/hambot-bot@zulip.testserver/api_key/regenerate")
self.assert_json_error(result, 'Insufficient permission')
@@ -440,7 +440,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_update_api_key(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.create_bot()
bot = self.get_bot()
old_api_key = bot['api_key']
@@ -453,13 +453,13 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_update_api_key_for_invalid_user(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
result = self.client_post('/json/bots/nonexistentuser@zulip.com/api_key/regenerate')
self.assert_json_error(result, 'No such user')
def test_patch_bot_full_name(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -480,7 +480,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_owner(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -488,23 +488,23 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_post("/json/bots", bot_info)
self.assert_json_success(result)
bot_info = {
'bot_owner': 'othello@zulip.com',
'bot_owner': self.example_email('othello'),
}
result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result)
# Test bot's owner has been changed successfully.
bot_owner = ujson.loads(result.content)['bot_owner']
self.assertEqual(bot_owner, 'othello@zulip.com')
self.assertEqual(bot_owner, self.example_email('othello'))
self.login('othello@zulip.com')
self.login(self.example_email('othello'))
bot = self.get_bot()
self.assertEqual('The Bot of Hamlet', bot['full_name'])
@override_settings(LOCAL_UPLOADS_DIR='var/bot_avatar')
def test_patch_bot_avatar(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -544,7 +544,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_to_stream(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -565,7 +565,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_to_stream_not_subscribed(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -586,7 +586,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_to_stream_none(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -608,7 +608,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_to_stream_private_allowed(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = self.subscribe_to_stream(user_profile.email, "Denmark")
do_change_stream_invite_only(stream, True)
@@ -634,10 +634,10 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_to_stream_private_denied(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = get_stream("Denmark", user_profile.realm)
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
self.unsubscribe_from_stream(self.example_email('hamlet'), "Denmark")
do_change_stream_invite_only(stream, True)
bot_info = {
@@ -655,7 +655,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_to_stream_not_found(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -670,7 +670,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_events_register_stream(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -691,7 +691,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_events_register_stream_allowed(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = self.subscribe_to_stream(user_profile.email, "Denmark")
do_change_stream_invite_only(stream, True)
@@ -716,10 +716,10 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_events_register_stream_denied(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
user_profile = self.example_user('hamlet')
stream = get_stream("Denmark", user_profile.realm)
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
self.unsubscribe_from_stream(self.example_email('hamlet'), "Denmark")
do_change_stream_invite_only(stream, True)
bot_info = {
@@ -736,7 +736,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_events_register_stream_none(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -758,7 +758,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_events_register_stream_not_found(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -773,7 +773,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_default_all_public_streams_true(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -794,7 +794,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_default_all_public_streams_false(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -815,7 +815,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bot_via_post(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
@@ -838,7 +838,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_patch_bogus_bot(self):
# type: () -> None
"""Deleting a bogus bot will succeed silently."""
self.login("hamlet@zulip.com")
self.login(self.example_email('hamlet'))
self.create_bot()
bot_info = {
'full_name': 'Fred',