2017-03-08 12:07:51 +01:00
|
|
|
import filecmp
|
|
|
|
import os
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
from django.core import mail
|
2018-02-13 11:47:40 +01:00
|
|
|
from mock import patch, MagicMock
|
2019-08-18 12:56:21 +02:00
|
|
|
from typing import Any, Dict, List, Mapping, Optional
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2018-02-13 11:54:16 +01:00
|
|
|
from zerver.lib.actions import do_change_stream_invite_only, do_deactivate_user
|
2019-08-18 12:56:21 +02:00
|
|
|
from zerver.lib.bot_config import get_bot_config, ConfigError
|
2017-05-23 20:57:59 +02:00
|
|
|
from zerver.models import get_realm, get_stream, \
|
2019-02-02 23:53:44 +01:00
|
|
|
Realm, UserProfile, get_user, get_bot_services, Service, \
|
2017-12-07 20:47:10 +01:00
|
|
|
is_cross_realm_bot_email
|
2017-03-22 05:58:54 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase, UploadSerializeMixin
|
2017-03-08 12:07:51 +01:00
|
|
|
from zerver.lib.test_helpers import (
|
2017-10-09 20:56:09 +02:00
|
|
|
avatar_disk_path,
|
|
|
|
get_test_image_file,
|
|
|
|
queries_captured,
|
|
|
|
tornado_redirected_to_list,
|
2017-03-08 12:07:51 +01:00
|
|
|
)
|
2019-08-18 12:56:21 +02:00
|
|
|
from zerver.lib.integrations import EMBEDDED_BOTS, WebhookIntegration
|
2017-07-21 17:54:34 +02:00
|
|
|
from zerver.lib.bot_lib import get_bot_handler
|
2018-02-13 11:47:40 +01:00
|
|
|
from zulip_bots.custom_exceptions import ConfigValidationError
|
|
|
|
|
2019-08-18 12:56:21 +02:00
|
|
|
|
|
|
|
# A test validator
|
|
|
|
def _check_string(var_name: str, val: object) -> Optional[str]:
|
|
|
|
if str(val).startswith("_"):
|
|
|
|
return ('%s starts with a "_" and is hence invalid.') % (var_name,)
|
|
|
|
return None
|
|
|
|
|
|
|
|
stripe_sample_config_options = [
|
|
|
|
WebhookIntegration('stripe', ['financial'], display_name='Stripe',
|
|
|
|
config_options=[("Stripe API Key", "stripe_api_key", _check_string)])
|
|
|
|
]
|
|
|
|
|
2017-03-22 05:58:54 +01:00
|
|
|
class BotTest(ZulipTestCase, UploadSerializeMixin):
|
2018-05-15 15:26:04 +02:00
|
|
|
def get_bot_user(self, email: str) -> UserProfile:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
bot = get_user(email, realm)
|
|
|
|
return bot
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def assert_num_bots_equal(self, count: int) -> None:
|
2017-03-08 12:07:51 +01:00
|
|
|
result = self.client_get("/json/bots")
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual(count, len(result.json()['bots']))
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def create_bot(self, **extras: Any) -> Dict[str, Any]:
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
2017-06-12 19:50:03 +02:00
|
|
|
'bot_type': '1',
|
2017-03-08 12:07:51 +01:00
|
|
|
}
|
|
|
|
bot_info.update(extras)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:29:14 +02:00
|
|
|
return result.json()
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_bot_domain(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-05 04:17:12 +01:00
|
|
|
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
|
|
|
|
# the wrong subdomain, and because 'testserver.example.com' is not a valid domain for the bot's email.
|
|
|
|
# So we just test the Raelm.get_bot_domain function.
|
|
|
|
realm = get_realm('zulip')
|
2017-10-02 08:32:09 +02:00
|
|
|
self.assertEqual(realm.get_bot_domain(), 'zulip.testserver')
|
2017-03-05 04:17:12 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def deactivate_bot(self) -> None:
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_delete("/json/bots/{}".format(self.get_bot_user(email).id))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_bad_username(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
2017-06-21 13:46:58 +02:00
|
|
|
|
|
|
|
# Invalid username
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = dict(
|
2017-05-12 04:21:49 +02:00
|
|
|
full_name='My bot name',
|
|
|
|
short_name='@',
|
2017-03-08 12:07:51 +01:00
|
|
|
)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Bad name or username')
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2017-06-21 13:46:58 +02:00
|
|
|
# Empty username
|
|
|
|
bot_info = dict(
|
|
|
|
full_name='My bot name',
|
|
|
|
short_name='',
|
|
|
|
)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Bad name or username')
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_no_name(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-05-12 04:21:49 +02:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
bot_info = dict(
|
|
|
|
full_name='a',
|
|
|
|
short_name='bot',
|
|
|
|
)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Name too short!')
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_json_users_with_bots(self) -> None:
|
2017-10-09 20:56:09 +02:00
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
self.login(hamlet.email)
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2019-02-22 23:32:08 +01:00
|
|
|
num_bots = 3
|
2017-10-09 20:56:09 +02:00
|
|
|
for i in range(num_bots):
|
|
|
|
full_name = 'Bot %d' % (i,)
|
|
|
|
short_name = 'bot-%d' % (i,)
|
|
|
|
bot_info = dict(
|
|
|
|
full_name=full_name,
|
|
|
|
short_name=short_name,
|
|
|
|
bot_type=1
|
|
|
|
)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assert_num_bots_equal(num_bots)
|
|
|
|
|
|
|
|
with queries_captured() as queries:
|
|
|
|
users_result = self.client_get('/json/users')
|
|
|
|
|
|
|
|
self.assert_json_success(users_result)
|
|
|
|
|
2018-10-19 10:00:38 +02:00
|
|
|
self.assert_length(queries, 3)
|
2017-10-09 20:56:09 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2017-03-08 12:07:51 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
2018-05-15 15:26:04 +02:00
|
|
|
bot = self.get_bot_user(email)
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
|
|
|
|
self.assertEqual(
|
|
|
|
dict(
|
|
|
|
type='realm_bot',
|
|
|
|
op='add',
|
2017-03-05 04:17:12 +01:00
|
|
|
bot=dict(email='hambot-bot@zulip.testserver',
|
2017-03-08 12:07:51 +01:00
|
|
|
user_id=bot.id,
|
2017-06-12 19:50:03 +02:00
|
|
|
bot_type=bot.bot_type,
|
2017-03-08 12:07:51 +01:00
|
|
|
full_name='The Bot of Hamlet',
|
|
|
|
is_active=True,
|
|
|
|
api_key=result['api_key'],
|
|
|
|
avatar_url=result['avatar_url'],
|
|
|
|
default_sending_stream=None,
|
|
|
|
default_events_register_stream=None,
|
|
|
|
default_all_public_streams=False,
|
2018-01-16 20:34:12 +01:00
|
|
|
services=[],
|
2017-05-24 01:24:29 +02:00
|
|
|
owner=self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
),
|
|
|
|
event['event']
|
|
|
|
)
|
|
|
|
|
|
|
|
users_result = self.client_get('/json/users')
|
|
|
|
members = ujson.loads(users_result.content)['members']
|
2017-03-05 04:17:12 +01:00
|
|
|
bots = [m for m in members if m['email'] == 'hambot-bot@zulip.testserver']
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(len(bots), 1)
|
|
|
|
bot = bots[0]
|
2017-05-24 01:24:29 +02:00
|
|
|
self.assertEqual(bot['bot_owner'], self.example_email('hamlet'))
|
2018-05-15 15:26:04 +02:00
|
|
|
self.assertEqual(bot['user_id'], self.get_bot_user(email).id)
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_username_in_use(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
bots: Prevent bots from having duplicate full names.
Bots are not allowed to use the same name as
other users in the realm (either bot or human).
This is kind of a big commit, but I wanted to
combine the post/patch (aka add/edit) checks
into one commit, since it's a change in policy
that affects both codepaths.
A lot of the noise is in tests. We had good
coverage on the previous code, including some places
like event testing where we were expediently
not bothering to use different names for
different bots in some longer tests. And then
of course I test some new scenarios that are relevant
with the new policy.
There are two new functions:
check_bot_name_available:
very simple Django query
check_change_bot_full_name:
this diverges from the 3-line
check_change_full_name, where the latter
is still used for the "humans" use case
And then we just call those in appropriate places.
Note that there is still a loophole here
where you can get two bots with the same
name if you reactivate a bot named Fred
that was inactive when the second bot named
Fred was created. Also, we don't attempt
to fix historical data. So this commit
shouldn't be considered any kind of lockdown,
it's just meant to help people from
inadvertently creating two bots of the same
name where they don't intend to. For more
context, we are continuing to allow two
human users in the same realm to have the
same full name, and our code should generally
be tolerant of that possibility. (A good
example is our new mention syntax, which disambiguates
same-named people using ids.)
It's also worth noting that our web app client
doesn't try to scrub full_name from its payload in
situations where the user has actually only modified other
fields in the "Edit bot" UI. Starting here
we just handle this on the server, since it's
easy to fix there, and even if we fixed it in the web
app, there's no guarantee that other clients won't be
just as brute force. It wasn't exactly broken before,
but we'd needlessly write rows to audit tables.
Fixes #10509
2018-09-27 19:25:18 +02:00
|
|
|
# The short_name is used in the email, which we call
|
|
|
|
# "Username" for legacy reasons.
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = dict(
|
bots: Prevent bots from having duplicate full names.
Bots are not allowed to use the same name as
other users in the realm (either bot or human).
This is kind of a big commit, but I wanted to
combine the post/patch (aka add/edit) checks
into one commit, since it's a change in policy
that affects both codepaths.
A lot of the noise is in tests. We had good
coverage on the previous code, including some places
like event testing where we were expediently
not bothering to use different names for
different bots in some longer tests. And then
of course I test some new scenarios that are relevant
with the new policy.
There are two new functions:
check_bot_name_available:
very simple Django query
check_change_bot_full_name:
this diverges from the 3-line
check_change_full_name, where the latter
is still used for the "humans" use case
And then we just call those in appropriate places.
Note that there is still a loophole here
where you can get two bots with the same
name if you reactivate a bot named Fred
that was inactive when the second bot named
Fred was created. Also, we don't attempt
to fix historical data. So this commit
shouldn't be considered any kind of lockdown,
it's just meant to help people from
inadvertently creating two bots of the same
name where they don't intend to. For more
context, we are continuing to allow two
human users in the same realm to have the
same full name, and our code should generally
be tolerant of that possibility. (A good
example is our new mention syntax, which disambiguates
same-named people using ids.)
It's also worth noting that our web app client
doesn't try to scrub full_name from its payload in
situations where the user has actually only modified other
fields in the "Edit bot" UI. Starting here
we just handle this on the server, since it's
easy to fix there, and even if we fixed it in the web
app, there's no guarantee that other clients won't be
just as brute force. It wasn't exactly broken before,
but we'd needlessly write rows to audit tables.
Fixes #10509
2018-09-27 19:25:18 +02:00
|
|
|
full_name='whatever',
|
2017-03-08 12:07:51 +01:00
|
|
|
short_name='hambot',
|
|
|
|
)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Username already in use')
|
|
|
|
|
bots: Prevent bots from having duplicate full names.
Bots are not allowed to use the same name as
other users in the realm (either bot or human).
This is kind of a big commit, but I wanted to
combine the post/patch (aka add/edit) checks
into one commit, since it's a change in policy
that affects both codepaths.
A lot of the noise is in tests. We had good
coverage on the previous code, including some places
like event testing where we were expediently
not bothering to use different names for
different bots in some longer tests. And then
of course I test some new scenarios that are relevant
with the new policy.
There are two new functions:
check_bot_name_available:
very simple Django query
check_change_bot_full_name:
this diverges from the 3-line
check_change_full_name, where the latter
is still used for the "humans" use case
And then we just call those in appropriate places.
Note that there is still a loophole here
where you can get two bots with the same
name if you reactivate a bot named Fred
that was inactive when the second bot named
Fred was created. Also, we don't attempt
to fix historical data. So this commit
shouldn't be considered any kind of lockdown,
it's just meant to help people from
inadvertently creating two bots of the same
name where they don't intend to. For more
context, we are continuing to allow two
human users in the same realm to have the
same full name, and our code should generally
be tolerant of that possibility. (A good
example is our new mention syntax, which disambiguates
same-named people using ids.)
It's also worth noting that our web app client
doesn't try to scrub full_name from its payload in
situations where the user has actually only modified other
fields in the "Edit bot" UI. Starting here
we just handle this on the server, since it's
easy to fix there, and even if we fixed it in the web
app, there's no guarantee that other clients won't be
just as brute force. It wasn't exactly broken before,
but we'd needlessly write rows to audit tables.
Fixes #10509
2018-09-27 19:25:18 +02:00
|
|
|
dup_full_name = 'The Bot of Hamlet'
|
|
|
|
|
|
|
|
bot_info = dict(
|
|
|
|
full_name=dup_full_name,
|
|
|
|
short_name='whatever',
|
|
|
|
)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Name is already in use!')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_user_avatar(self) -> None:
|
2017-05-23 20:57:59 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
realm = get_realm('zulip')
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
with get_test_image_file('img.png') as fp:
|
|
|
|
self.create_bot(file=fp)
|
2017-05-23 20:57:59 +02:00
|
|
|
profile = get_user(email, realm)
|
2017-03-08 12:07:51 +01:00
|
|
|
# Make sure that avatar image that we've uploaded is same with avatar image in the server
|
|
|
|
self.assertTrue(filecmp.cmp(fp.name,
|
|
|
|
os.path.splitext(avatar_disk_path(profile))[0] +
|
|
|
|
".original"))
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER)
|
|
|
|
self.assertTrue(os.path.exists(avatar_disk_path(profile)))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_too_many_files(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
with get_test_image_file('img.png') as fp1, \
|
|
|
|
get_test_image_file('img.gif') as fp2:
|
|
|
|
bot_info = dict(
|
|
|
|
full_name='whatever',
|
|
|
|
short_name='whatever',
|
|
|
|
file1=fp1,
|
|
|
|
file2=fp2,
|
|
|
|
)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'You may only upload one file at a time')
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_sending_stream(self) -> None:
|
2017-05-23 20:57:59 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
realm = get_realm('zulip')
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_sending_stream='Denmark')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_sending_stream'], 'Denmark')
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
profile = get_user(email, realm)
|
2018-02-19 02:47:03 +01:00
|
|
|
assert(profile.default_sending_stream is not None)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.default_sending_stream.name, 'Denmark')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_sending_stream_not_subscribed(self) -> None:
|
2017-05-23 20:57:59 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
realm = get_realm('zulip')
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_sending_stream='Rome')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_sending_stream'], 'Rome')
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
profile = get_user(email, realm)
|
2018-02-19 02:47:03 +01:00
|
|
|
assert(profile.default_sending_stream is not None)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.default_sending_stream.name, 'Rome')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_bot_add_subscription(self) -> None:
|
2017-03-08 12:07:51 +01:00
|
|
|
"""
|
|
|
|
Calling POST /json/users/me/subscriptions should successfully add
|
|
|
|
streams, and a stream to the
|
|
|
|
list of subscriptions and confirm the right number of events
|
|
|
|
are generated.
|
|
|
|
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.
|
|
|
|
"""
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
# Normal user i.e. not a bot.
|
|
|
|
request_data = {
|
2017-05-24 01:24:29 +02:00
|
|
|
'principals': '["' + self.example_email('iago') + '"]'
|
2017-03-08 12:07:51 +01:00
|
|
|
}
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2017-03-08 12:07:51 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-05-24 01:24:29 +02:00
|
|
|
result = self.common_subscribe_to_streams(self.example_email('hamlet'), ['Rome'], request_data)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
msg_event = [e for e in events if e['event']['type'] == 'message']
|
2017-05-07 17:17:56 +02:00
|
|
|
self.assert_length(msg_event, 1) # Notification message event is sent.
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
# Create a bot.
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
# A bot
|
|
|
|
bot_request_data = {
|
2017-03-05 04:17:12 +01:00
|
|
|
'principals': '["hambot-bot@zulip.testserver"]'
|
2017-03-08 12:07:51 +01:00
|
|
|
}
|
2017-08-04 02:25:38 +02:00
|
|
|
events_bot = [] # type: List[Mapping[str, Any]]
|
2017-03-08 12:07:51 +01:00
|
|
|
with tornado_redirected_to_list(events_bot):
|
2017-05-24 01:24:29 +02:00
|
|
|
result = self.common_subscribe_to_streams(self.example_email('hamlet'), ['Rome'], bot_request_data)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# No notification message event or invitation email is sent because of bot.
|
|
|
|
msg_event = [e for e in events_bot if e['event']['type'] == 'message']
|
|
|
|
self.assert_length(msg_event, 0)
|
|
|
|
self.assertEqual(len(events_bot), len(events) - 1)
|
|
|
|
|
|
|
|
# Test runner automatically redirects all sent email to a dummy 'outbox'.
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_sending_stream_private_allowed(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:07:51 +01:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2017-08-25 06:01:29 +02:00
|
|
|
self.subscribe(user_profile, stream.name)
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
self.assert_num_bots_equal(0)
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2017-03-08 12:07:51 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.create_bot(default_sending_stream='Denmark')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_sending_stream'], 'Denmark')
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
profile = get_user(email, realm)
|
2018-02-19 02:47:03 +01:00
|
|
|
assert(profile.default_sending_stream is not None)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.default_sending_stream.name, 'Denmark')
|
|
|
|
|
|
|
|
event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
|
|
|
|
self.assertEqual(
|
|
|
|
dict(
|
|
|
|
type='realm_bot',
|
|
|
|
op='add',
|
2017-03-05 04:17:12 +01:00
|
|
|
bot=dict(email='hambot-bot@zulip.testserver',
|
2017-03-08 12:07:51 +01:00
|
|
|
user_id=profile.id,
|
|
|
|
full_name='The Bot of Hamlet',
|
2017-06-12 19:50:03 +02:00
|
|
|
bot_type=profile.bot_type,
|
2017-03-08 12:07:51 +01:00
|
|
|
is_active=True,
|
|
|
|
api_key=result['api_key'],
|
|
|
|
avatar_url=result['avatar_url'],
|
|
|
|
default_sending_stream='Denmark',
|
|
|
|
default_events_register_stream=None,
|
|
|
|
default_all_public_streams=False,
|
2018-01-16 20:34:12 +01:00
|
|
|
services=[],
|
2017-05-24 01:24:29 +02:00
|
|
|
owner=self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
),
|
|
|
|
event['event']
|
|
|
|
)
|
2017-03-21 07:32:59 +01:00
|
|
|
self.assertEqual(event['users'], {user_profile.id, })
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_sending_stream_private_denied(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-07-12 21:10:18 +02:00
|
|
|
realm = self.example_user('hamlet').realm
|
|
|
|
stream = get_stream("Denmark", realm)
|
2017-08-25 06:23:11 +02:00
|
|
|
self.unsubscribe(self.example_user('hamlet'), "Denmark")
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
'default_sending_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_events_register_stream(self) -> None:
|
2017-05-23 20:57:59 +02:00
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_events_register_stream='Denmark')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_events_register_stream'], 'Denmark')
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
profile = get_user(bot_email, bot_realm)
|
2018-02-19 02:47:03 +01:00
|
|
|
assert(profile.default_events_register_stream is not None)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.default_events_register_stream.name, 'Denmark')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_events_register_stream_private_allowed(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-08-25 06:01:29 +02:00
|
|
|
stream = self.subscribe(user_profile, 'Denmark')
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
self.assert_num_bots_equal(0)
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2017-03-08 12:07:51 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.create_bot(default_events_register_stream='Denmark')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_events_register_stream'], 'Denmark')
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
bot_profile = get_user(bot_email, bot_realm)
|
2018-02-19 02:47:03 +01:00
|
|
|
assert(bot_profile.default_events_register_stream is not None)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(bot_profile.default_events_register_stream.name, 'Denmark')
|
|
|
|
|
|
|
|
event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
|
|
|
|
self.assertEqual(
|
|
|
|
dict(
|
|
|
|
type='realm_bot',
|
|
|
|
op='add',
|
2017-03-05 04:17:12 +01:00
|
|
|
bot=dict(email='hambot-bot@zulip.testserver',
|
2017-03-08 12:07:51 +01:00
|
|
|
full_name='The Bot of Hamlet',
|
|
|
|
user_id=bot_profile.id,
|
2017-06-12 19:50:03 +02:00
|
|
|
bot_type=bot_profile.bot_type,
|
2017-03-08 12:07:51 +01:00
|
|
|
is_active=True,
|
|
|
|
api_key=result['api_key'],
|
|
|
|
avatar_url=result['avatar_url'],
|
|
|
|
default_sending_stream=None,
|
|
|
|
default_events_register_stream='Denmark',
|
|
|
|
default_all_public_streams=False,
|
2018-01-16 20:34:12 +01:00
|
|
|
services=[],
|
2017-05-24 01:24:29 +02:00
|
|
|
owner=self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
),
|
|
|
|
event['event']
|
|
|
|
)
|
2017-03-21 07:32:59 +01:00
|
|
|
self.assertEqual(event['users'], {user_profile.id, })
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_events_register_stream_private_denied(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-07-12 21:10:18 +02:00
|
|
|
realm = self.example_user('hamlet').realm
|
|
|
|
stream = get_stream("Denmark", realm)
|
2017-08-25 06:23:11 +02:00
|
|
|
self.unsubscribe(self.example_user('hamlet'), "Denmark")
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
'default_events_register_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_default_all_public_streams(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_all_public_streams=ujson.dumps(True))
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertTrue(result['default_all_public_streams'])
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
profile = get_user(bot_email, bot_realm)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.default_all_public_streams, True)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_deactivate_bot(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.deactivate_bot()
|
|
|
|
# You can deactivate the same bot twice.
|
|
|
|
self.deactivate_bot()
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_deactivate_bogus_bot(self) -> None:
|
2017-03-08 12:07:51 +01:00
|
|
|
"""Deleting a bogus bot will succeed silently."""
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
2018-05-15 15:26:04 +02:00
|
|
|
invalid_user_id = 1000
|
|
|
|
result = self.client_delete("/json/bots/{}".format(invalid_user_id))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, 'No such bot')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2018-02-04 19:47:12 +01:00
|
|
|
def test_deactivate_bot_with_owner_deactivation(self) -> None:
|
|
|
|
email = self.example_email("hamlet")
|
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': u'The Bot of Hamlet',
|
|
|
|
'short_name': u'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': u'The Another Bot of Hamlet',
|
|
|
|
'short_name': u'hambot-another',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
all_bots = UserProfile.objects.filter(is_bot=True, bot_owner=user, is_active=True)
|
|
|
|
bots = [bot for bot in all_bots]
|
|
|
|
self.assertEqual(len(bots), 2)
|
|
|
|
|
|
|
|
result = self.client_delete('/json/users/me')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.assertFalse(user.is_active)
|
|
|
|
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
all_bots = UserProfile.objects.filter(is_bot=True, bot_owner=user, is_active=True)
|
|
|
|
bots = [bot for bot in all_bots]
|
|
|
|
self.assertEqual(len(bots), 0)
|
|
|
|
|
2018-05-15 15:26:04 +02:00
|
|
|
def test_cannot_deactivate_other_realm_bot(self) -> None:
|
|
|
|
realm = get_realm("zephyr")
|
|
|
|
self.login(self.mit_email("starnine"), realm=realm)
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot in zephyr',
|
|
|
|
'short_name': 'starn-bot',
|
|
|
|
'bot_type': '1',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info, subdomain="zephyr")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
result = self.client_get("/json/bots", subdomain="zephyr")
|
|
|
|
bot_email = result.json()['bots'][0]['username']
|
|
|
|
bot = get_user(bot_email, realm)
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
result = self.client_delete("/json/bots/{}".format(bot.id))
|
2018-05-17 21:59:04 +02:00
|
|
|
self.assert_json_error(result, 'No such bot')
|
2018-05-15 15:26:04 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_bot_deactivation_attacks(self) -> None:
|
2017-03-08 12:07:51 +01:00
|
|
|
"""You cannot deactivate somebody else's bot."""
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
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.
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('othello'))
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2018-04-03 00:36:31 +02:00
|
|
|
# Cannot deactivate a user as a bot
|
2018-05-15 15:26:04 +02:00
|
|
|
result = self.client_delete("/json/bots/{}".format(self.example_user("hamlet").id))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, 'No such bot')
|
|
|
|
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_delete("/json/bots/{}".format(self.get_bot_user(email).id))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
|
|
|
# But we don't actually deactivate the other person's bot.
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2018-04-03 00:36:31 +02:00
|
|
|
# Cannot deactivate a bot as a user
|
2018-05-17 19:36:33 +02:00
|
|
|
result = self.client_delete("/json/users/{}".format(self.get_bot_user(email).id))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, 'No such user')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_bot_permissions(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
# Have Othello try to mess with Hamlet's bots.
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('othello'))
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2018-05-15 18:13:07 +02:00
|
|
|
result = self.client_post("/json/bots/{}/api_key/regenerate".format(self.get_bot_user(email).id))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_bot(self) -> Dict[str, Any]:
|
2017-03-08 12:07:51 +01:00
|
|
|
result = self.client_get("/json/bots")
|
2017-08-17 08:29:14 +02:00
|
|
|
bots = result.json()['bots']
|
2017-03-08 12:07:51 +01:00
|
|
|
return bots[0]
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_api_key(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.create_bot()
|
|
|
|
bot = self.get_bot()
|
|
|
|
old_api_key = bot['api_key']
|
2018-05-15 18:13:07 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_post('/json/bots/{}/api_key/regenerate'.format(self.get_bot_user(email).id))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:50:14 +02:00
|
|
|
new_api_key = result.json()['api_key']
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertNotEqual(old_api_key, new_api_key)
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(new_api_key, bot['api_key'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_api_key_for_invalid_user(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2018-05-15 18:13:07 +02:00
|
|
|
invalid_user_id = 1000
|
|
|
|
result = self.client_post('/json/bots/{}/api_key/regenerate'.format(invalid_user_id))
|
2018-05-28 20:42:31 +02:00
|
|
|
self.assert_json_error(result, 'No such bot')
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_bot_type_default(self) -> None:
|
2017-05-30 19:19:48 +02:00
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot(bot_type=UserProfile.DEFAULT_BOT)
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
profile = get_user(bot_email, bot_realm)
|
|
|
|
self.assertEqual(profile.bot_type, UserProfile.DEFAULT_BOT)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_bot_type_incoming_webhook(self) -> None:
|
2017-05-30 19:19:48 +02:00
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot(bot_type=UserProfile.INCOMING_WEBHOOK_BOT)
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
profile = get_user(bot_email, bot_realm)
|
|
|
|
self.assertEqual(profile.bot_type, UserProfile.INCOMING_WEBHOOK_BOT)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_bot_with_bot_type_invalid(self) -> None:
|
2017-06-07 06:16:18 +02:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
'bot_type': 7,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.assert_json_error(result, 'Invalid bot type')
|
|
|
|
|
2018-01-29 16:10:54 +01:00
|
|
|
def test_no_generic_bots_allowed_for_non_admins(self) -> None:
|
2017-11-24 16:24:24 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
'bot_type': 1,
|
|
|
|
}
|
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
2018-01-29 16:10:54 +01:00
|
|
|
bot_realm.bot_creation_policy = Realm.BOT_CREATION_LIMIT_GENERIC_BOTS
|
|
|
|
bot_realm.save(update_fields=['bot_creation_policy'])
|
2017-11-24 16:24:24 +01:00
|
|
|
|
|
|
|
# A regular user cannot create a generic bot
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_num_bots_equal(0)
|
2018-01-29 16:10:54 +01:00
|
|
|
self.assert_json_error(result, 'Must be an organization administrator')
|
2017-11-24 16:24:24 +01:00
|
|
|
|
|
|
|
# But can create an incoming webhook
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot(bot_type=UserProfile.INCOMING_WEBHOOK_BOT)
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
profile = get_user(bot_email, bot_realm)
|
|
|
|
self.assertEqual(profile.bot_type, UserProfile.INCOMING_WEBHOOK_BOT)
|
|
|
|
|
2018-07-27 18:18:33 +02:00
|
|
|
def test_no_generic_bot_reactivation_allowed_for_non_admins(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.create_bot(bot_type=UserProfile.DEFAULT_BOT)
|
|
|
|
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
bot_realm.bot_creation_policy = Realm.BOT_CREATION_LIMIT_GENERIC_BOTS
|
|
|
|
bot_realm.save(update_fields=['bot_creation_policy'])
|
|
|
|
|
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_user = get_user(bot_email, bot_realm)
|
|
|
|
do_deactivate_user(bot_user)
|
|
|
|
|
|
|
|
# A regular user cannot reactivate a generic bot
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.client_post("/json/users/%s/reactivate" % (bot_user.id,))
|
|
|
|
self.assert_json_error(result, 'Must be an organization administrator')
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2018-01-29 16:10:54 +01:00
|
|
|
def test_no_generic_bots_allowed_for_admins(self) -> None:
|
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
bot_realm.bot_creation_policy = Realm.BOT_CREATION_LIMIT_GENERIC_BOTS
|
|
|
|
bot_realm.save(update_fields=['bot_creation_policy'])
|
|
|
|
|
|
|
|
# An administrator can create any type of bot
|
|
|
|
self.login(self.example_email('iago'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot(bot_type=UserProfile.DEFAULT_BOT)
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
profile = get_user(bot_email, bot_realm)
|
|
|
|
self.assertEqual(profile.bot_type, UserProfile.DEFAULT_BOT)
|
|
|
|
|
|
|
|
def test_no_bots_allowed_for_non_admins(self) -> None:
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
'bot_type': 1,
|
|
|
|
}
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
bot_realm.bot_creation_policy = Realm.BOT_CREATION_ADMINS_ONLY
|
|
|
|
bot_realm.save(update_fields=['bot_creation_policy'])
|
|
|
|
|
|
|
|
# A regular user cannot create a generic bot
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.assert_json_error(result, 'Must be an organization administrator')
|
|
|
|
|
|
|
|
# Also, a regular user cannot create a incoming bot
|
|
|
|
bot_info['bot_type'] = 2
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.assert_json_error(result, 'Must be an organization administrator')
|
|
|
|
|
|
|
|
def test_no_bots_allowed_for_admins(self) -> None:
|
2017-11-24 16:24:24 +01:00
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
2018-01-29 16:10:54 +01:00
|
|
|
bot_realm.bot_creation_policy = Realm.BOT_CREATION_ADMINS_ONLY
|
|
|
|
bot_realm.save(update_fields=['bot_creation_policy'])
|
2017-11-24 16:24:24 +01:00
|
|
|
|
|
|
|
# An administrator can create any type of bot
|
|
|
|
self.login(self.example_email('iago'))
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot(bot_type=UserProfile.DEFAULT_BOT)
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
profile = get_user(bot_email, bot_realm)
|
|
|
|
self.assertEqual(profile.bot_type, UserProfile.DEFAULT_BOT)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_full_name(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual('Fred', result.json()['full_name'])
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Fred', bot['full_name'])
|
|
|
|
|
bots: Prevent bots from having duplicate full names.
Bots are not allowed to use the same name as
other users in the realm (either bot or human).
This is kind of a big commit, but I wanted to
combine the post/patch (aka add/edit) checks
into one commit, since it's a change in policy
that affects both codepaths.
A lot of the noise is in tests. We had good
coverage on the previous code, including some places
like event testing where we were expediently
not bothering to use different names for
different bots in some longer tests. And then
of course I test some new scenarios that are relevant
with the new policy.
There are two new functions:
check_bot_name_available:
very simple Django query
check_change_bot_full_name:
this diverges from the 3-line
check_change_full_name, where the latter
is still used for the "humans" use case
And then we just call those in appropriate places.
Note that there is still a loophole here
where you can get two bots with the same
name if you reactivate a bot named Fred
that was inactive when the second bot named
Fred was created. Also, we don't attempt
to fix historical data. So this commit
shouldn't be considered any kind of lockdown,
it's just meant to help people from
inadvertently creating two bots of the same
name where they don't intend to. For more
context, we are continuing to allow two
human users in the same realm to have the
same full name, and our code should generally
be tolerant of that possibility. (A good
example is our new mention syntax, which disambiguates
same-named people using ids.)
It's also worth noting that our web app client
doesn't try to scrub full_name from its payload in
situations where the user has actually only modified other
fields in the "Edit bot" UI. Starting here
we just handle this on the server, since it's
easy to fix there, and even if we fixed it in the web
app, there's no guarantee that other clients won't be
just as brute force. It wasn't exactly broken before,
but we'd needlessly write rows to audit tables.
Fixes #10509
2018-09-27 19:25:18 +02:00
|
|
|
def test_patch_bot_full_name_in_use(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
|
|
|
|
original_name = 'The Bot of Hamlet'
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': original_name,
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot = self.get_bot_user(bot_email)
|
|
|
|
url = "/json/bots/{}".format(bot.id)
|
|
|
|
|
|
|
|
# It doesn't matter whether a name is taken by a human
|
|
|
|
# or a bot, we can't use it.
|
|
|
|
already_taken_name = self.example_user('cordelia').full_name
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': already_taken_name,
|
|
|
|
}
|
|
|
|
result = self.client_patch(url, bot_info)
|
|
|
|
self.assert_json_error(result, "Name is already in use!")
|
|
|
|
|
|
|
|
# We can use our own name (with extra whitespace), and the
|
|
|
|
# server should silently do nothing.
|
|
|
|
original_name_with_padding = ' ' + original_name + ' '
|
|
|
|
bot_info = {
|
|
|
|
'full_name': original_name_with_padding,
|
|
|
|
}
|
|
|
|
result = self.client_patch(url, bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot = self.get_bot_user(bot_email)
|
|
|
|
self.assertEqual(bot.full_name, original_name)
|
|
|
|
|
|
|
|
# And let's do a sanity check with an actual name change
|
|
|
|
# after our various attempts that either failed or did
|
|
|
|
# nothing.
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Hal',
|
|
|
|
}
|
|
|
|
result = self.client_patch(url, bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot = self.get_bot_user(bot_email)
|
|
|
|
self.assertEqual(bot.full_name, 'Hal')
|
|
|
|
|
2018-04-03 03:55:51 +02:00
|
|
|
def test_patch_bot_full_name_non_bot(self) -> None:
|
|
|
|
self.login(self.example_email('iago'))
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
result = self.client_patch("/json/bots/{}".format(self.example_user("hamlet").id), bot_info)
|
2018-04-03 03:55:51 +02:00
|
|
|
self.assert_json_error(result, "No such bot")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_owner(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
2017-05-25 00:17:02 +02:00
|
|
|
'full_name': u'The Bot of Hamlet',
|
|
|
|
'short_name': u'hambot',
|
2017-03-08 12:07:51 +01:00
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
2018-06-19 07:42:04 +02:00
|
|
|
'bot_owner_id': self.example_user('othello').id,
|
2017-03-08 12:07:51 +01:00
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Test bot's owner has been changed successfully.
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual(result.json()['bot_owner'], self.example_email('othello'))
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('othello'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('The Bot of Hamlet', bot['full_name'])
|
|
|
|
|
2018-06-19 07:42:04 +02:00
|
|
|
def test_patch_bot_owner_bad_user_id(self) -> None:
|
2018-02-10 14:17:04 +01:00
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
2018-02-10 14:17:04 +01:00
|
|
|
profile = get_user('hambot-bot@zulip.testserver', get_realm('zulip'))
|
|
|
|
|
2018-06-26 22:27:10 +02:00
|
|
|
bad_bot_owner_id = 999999
|
2018-02-10 14:17:04 +01:00
|
|
|
bot_info = {
|
2018-06-26 22:27:10 +02:00
|
|
|
'bot_owner_id': bad_bot_owner_id,
|
2018-02-10 14:17:04 +01:00
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2018-02-10 14:17:04 +01:00
|
|
|
self.assert_json_error(result, "Failed to change owner, no such user")
|
|
|
|
profile = get_user('hambot-bot@zulip.testserver', get_realm('zulip'))
|
|
|
|
self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
|
|
|
|
|
2018-02-13 11:54:16 +01:00
|
|
|
def test_patch_bot_owner_deactivated(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
target_user_profile = self.example_user("othello")
|
|
|
|
do_deactivate_user(target_user_profile)
|
|
|
|
target_user_profile = self.example_user('othello')
|
|
|
|
self.assertFalse(target_user_profile.is_active)
|
|
|
|
bot_info = {
|
2018-06-19 07:42:04 +02:00
|
|
|
'bot_owner_id': self.example_user('othello').id,
|
2018-02-13 11:54:16 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2018-02-13 11:54:16 +01:00
|
|
|
self.assert_json_error(result, "Failed to change owner, user is deactivated")
|
2018-05-15 15:26:04 +02:00
|
|
|
profile = self.get_bot_user(email)
|
2018-02-13 11:54:16 +01:00
|
|
|
self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
|
|
|
|
|
2018-06-19 07:42:04 +02:00
|
|
|
def test_patch_bot_owner_must_be_in_same_realm(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'bot_owner_id': self.mit_user("starnine").id,
|
|
|
|
}
|
|
|
|
|
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
|
|
|
self.assert_json_error(result, "Failed to change owner, no such user")
|
|
|
|
profile = self.get_bot_user(email)
|
|
|
|
self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
|
|
|
|
|
2018-05-23 00:03:31 +02:00
|
|
|
def test_patch_bot_owner_noop(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
bot_info = {
|
2018-06-19 07:42:04 +02:00
|
|
|
'bot_owner_id': self.example_user('hamlet').id,
|
2018-05-23 00:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
|
|
|
|
|
|
|
# Check that we're still the owner
|
|
|
|
self.assert_json_success(result)
|
|
|
|
profile = self.get_bot_user(email)
|
|
|
|
self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
|
|
|
|
|
2018-02-13 11:54:16 +01:00
|
|
|
def test_patch_bot_owner_a_bot(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': u'Another Bot of Hamlet',
|
|
|
|
'short_name': u'hamelbot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_info = {
|
2018-06-19 07:42:04 +02:00
|
|
|
'bot_owner_id': self.get_bot_user('hamelbot-bot@zulip.testserver').id,
|
2018-02-13 11:54:16 +01:00
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2018-02-13 11:54:16 +01:00
|
|
|
self.assert_json_error(result, "Failed to change owner, bots can't own other bots")
|
2018-05-15 15:26:04 +02:00
|
|
|
profile = get_user(email, get_realm('zulip'))
|
2018-02-13 11:54:16 +01:00
|
|
|
self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_avatar(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
bot_email = 'hambot-bot@zulip.testserver'
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
profile = get_user(bot_email, bot_realm)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_GRAVATAR)
|
|
|
|
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
2017-03-08 12:07:51 +01:00
|
|
|
# Try error case first (too many files):
|
|
|
|
with get_test_image_file('img.png') as fp1, \
|
|
|
|
get_test_image_file('img.gif') as fp2:
|
|
|
|
result = self.client_patch_multipart(
|
2018-05-15 15:26:04 +02:00
|
|
|
'/json/bots/{}'.format(self.get_bot_user(email).id),
|
2017-03-08 12:07:51 +01:00
|
|
|
dict(file1=fp1, file2=fp2))
|
|
|
|
self.assert_json_error(result, 'You may only upload one file at a time')
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
profile = get_user(bot_email, bot_realm)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.avatar_version, 1)
|
|
|
|
|
|
|
|
# HAPPY PATH
|
|
|
|
with get_test_image_file('img.png') as fp:
|
|
|
|
result = self.client_patch_multipart(
|
2018-05-15 15:26:04 +02:00
|
|
|
'/json/bots/{}'.format(self.get_bot_user(email).id),
|
2017-03-08 12:07:51 +01:00
|
|
|
dict(file=fp))
|
2017-05-23 20:57:59 +02:00
|
|
|
profile = get_user(bot_email, bot_realm)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(profile.avatar_version, 2)
|
|
|
|
# Make sure that avatar image that we've uploaded is same with avatar image in the server
|
|
|
|
self.assertTrue(filecmp.cmp(fp.name,
|
|
|
|
os.path.splitext(avatar_disk_path(profile))[0] +
|
|
|
|
".original"))
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER)
|
|
|
|
self.assertTrue(os.path.exists(avatar_disk_path(profile)))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_to_stream(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Denmark',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual('Denmark', result.json()['default_sending_stream'])
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Denmark', bot['default_sending_stream'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_to_stream_not_subscribed(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Rome',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual('Rome', result.json()['default_sending_stream'])
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Rome', bot['default_sending_stream'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_to_stream_none(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': '',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
bot_email = "hambot-bot@zulip.testserver"
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
default_sending_stream = get_user(bot_email, bot_realm).default_sending_stream
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(None, default_sending_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(None, bot['default_sending_stream'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_to_stream_private_allowed(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-08-25 06:01:29 +02:00
|
|
|
stream = self.subscribe(user_profile, "Denmark")
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Denmark',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual('Denmark', result.json()['default_sending_stream'])
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Denmark', bot['default_sending_stream'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_to_stream_private_denied(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-07-12 21:10:18 +02:00
|
|
|
realm = self.example_user('hamlet').realm
|
|
|
|
stream = get_stream("Denmark", realm)
|
2017-08-25 06:23:11 +02:00
|
|
|
self.unsubscribe(self.example_user('hamlet'), "Denmark")
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Denmark',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_to_stream_not_found(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'missing',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'missing'")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_events_register_stream(self) -> None:
|
2018-08-15 18:06:53 +02:00
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
self.login(hamlet.email)
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
2018-08-15 18:06:53 +02:00
|
|
|
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
2018-08-15 18:06:53 +02:00
|
|
|
bot_user = self.get_bot_user(email)
|
|
|
|
url = "/json/bots/{}".format(bot_user.id)
|
|
|
|
|
|
|
|
# Successfully give the bot a default stream.
|
|
|
|
stream_name = 'Denmark'
|
|
|
|
bot_info = dict(default_events_register_stream=stream_name)
|
|
|
|
result = self.client_patch(url, bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2018-08-15 18:06:53 +02:00
|
|
|
self.assertEqual(stream_name, result.json()['default_events_register_stream'])
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
2018-08-15 18:06:53 +02:00
|
|
|
self.assertEqual(stream_name, bot['default_events_register_stream'])
|
|
|
|
|
|
|
|
# Make sure we are locked out of an unsubscribed private stream.
|
|
|
|
# We'll subscribe the bot but not the owner (since the check is
|
|
|
|
# on owner).
|
|
|
|
stream_name = 'private_stream'
|
|
|
|
self.make_stream(stream_name, hamlet.realm, invite_only=True)
|
|
|
|
self.subscribe(bot_user, stream_name)
|
|
|
|
bot_info = dict(default_events_register_stream=stream_name)
|
|
|
|
result = self.client_patch(url, bot_info)
|
|
|
|
self.assert_json_error_contains(result, 'Invalid stream name')
|
|
|
|
|
|
|
|
# Subscribing the owner allows us to patch the stream.
|
|
|
|
self.subscribe(hamlet, stream_name)
|
|
|
|
bot_info = dict(default_events_register_stream=stream_name)
|
|
|
|
result = self.client_patch(url, bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Make sure the bot cannot create their own default stream.
|
|
|
|
url = "/api/v1/bots/{}".format(bot_user.id)
|
|
|
|
result = self.api_patch(bot_user.email, url, bot_info)
|
|
|
|
self.assert_json_error_contains(result, 'endpoint does not accept')
|
2017-03-08 12:07:51 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_events_register_stream_allowed(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-08-25 06:01:29 +02:00
|
|
|
stream = self.subscribe(user_profile, "Denmark")
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': 'Denmark',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual('Denmark', result.json()['default_events_register_stream'])
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Denmark', bot['default_events_register_stream'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_events_register_stream_denied(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-07-12 21:10:18 +02:00
|
|
|
realm = self.example_user('hamlet').realm
|
|
|
|
stream = get_stream("Denmark", realm)
|
2017-08-25 06:23:11 +02:00
|
|
|
self.unsubscribe(self.example_user('hamlet'), "Denmark")
|
2017-03-08 12:07:51 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': 'Denmark',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_events_register_stream_none(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': '',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-23 20:57:59 +02:00
|
|
|
bot_email = "hambot-bot@zulip.testserver"
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
default_events_register_stream = get_user(bot_email, bot_realm).default_events_register_stream
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assertEqual(None, default_events_register_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(None, bot['default_events_register_stream'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_events_register_stream_not_found(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': 'missing',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'missing'")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_default_all_public_streams_true(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_all_public_streams': ujson.dumps(True),
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual(result.json()['default_all_public_streams'], True)
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(bot['default_all_public_streams'], True)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_default_all_public_streams_false(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_all_public_streams': ujson.dumps(False),
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual(result.json()['default_all_public_streams'], False)
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(bot['default_all_public_streams'], False)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bot_via_post(self) -> None:
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
'method': 'PATCH'
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
# Important: We intentionally use the wrong method, post, here.
|
|
|
|
result = self.client_post("/json/bots/{}".format(self.get_bot_user(email).id),
|
|
|
|
bot_info)
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:50:14 +02:00
|
|
|
self.assertEqual('Fred', result.json()['full_name'])
|
2017-03-08 12:07:51 +01:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Fred', bot['full_name'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_bogus_bot(self) -> None:
|
2017-03-08 12:07:51 +01:00
|
|
|
"""Deleting a bogus bot will succeed silently."""
|
2017-05-24 01:24:29 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-03-08 12:07:51 +01:00
|
|
|
self.create_bot()
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
invalid_user_id = 1000
|
|
|
|
result = self.client_patch("/json/bots/{}".format(invalid_user_id), bot_info)
|
2018-05-28 20:42:31 +02:00
|
|
|
self.assert_json_error(result, 'No such bot')
|
2017-03-08 12:07:51 +01:00
|
|
|
self.assert_num_bots_equal(1)
|
2017-06-10 18:43:31 +02:00
|
|
|
|
2018-01-16 20:34:12 +01:00
|
|
|
def test_patch_outgoing_webhook_bot(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_info = {
|
|
|
|
'full_name': u'The Bot of Hamlet',
|
|
|
|
'short_name': u'hambot',
|
|
|
|
'bot_type': UserProfile.OUTGOING_WEBHOOK_BOT,
|
|
|
|
'payload_url': ujson.dumps("http://foo.bar.com"),
|
|
|
|
'service_interface': Service.GENERIC,
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'service_payload_url': ujson.dumps("http://foo.bar2.com"),
|
|
|
|
'service_interface': Service.SLACK,
|
|
|
|
}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'hambot-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2018-01-16 20:34:12 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
service_interface = ujson.loads(result.content)['service_interface']
|
|
|
|
self.assertEqual(service_interface, Service.SLACK)
|
|
|
|
|
|
|
|
service_payload_url = ujson.loads(result.content)['service_payload_url']
|
|
|
|
self.assertEqual(service_payload_url, "http://foo.bar2.com")
|
|
|
|
|
2018-02-13 11:47:40 +01:00
|
|
|
@patch('zulip_bots.bots.giphy.giphy.GiphyHandler.validate_config')
|
|
|
|
def test_patch_bot_config_data(self, mock_validate_config: MagicMock) -> None:
|
2018-01-30 19:21:13 +01:00
|
|
|
self.create_test_bot('test', self.example_user("hamlet"),
|
|
|
|
full_name=u'Bot with config data',
|
|
|
|
bot_type=UserProfile.EMBEDDED_BOT,
|
|
|
|
service_name='giphy',
|
|
|
|
config_data=ujson.dumps({'key': '12345678'}))
|
|
|
|
bot_info = {'config_data': ujson.dumps({'key': '87654321'})}
|
2018-05-15 15:26:04 +02:00
|
|
|
email = 'test-bot@zulip.testserver'
|
|
|
|
result = self.client_patch("/json/bots/{}".format(self.get_bot_user(email).id), bot_info)
|
2018-01-30 19:21:13 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
config_data = ujson.loads(result.content)['config_data']
|
|
|
|
self.assertEqual(config_data, ujson.loads(bot_info['config_data']))
|
|
|
|
|
2018-01-16 20:34:12 +01:00
|
|
|
def test_outgoing_webhook_invalid_interface(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Outgoing Webhook test bot',
|
|
|
|
'short_name': 'outgoingservicebot',
|
|
|
|
'bot_type': UserProfile.OUTGOING_WEBHOOK_BOT,
|
2018-05-28 18:24:32 +02:00
|
|
|
'payload_url': ujson.dumps('http://127.0.0.1:5002'),
|
2018-01-16 20:34:12 +01:00
|
|
|
'interface_type': -1,
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Invalid interface type')
|
|
|
|
|
|
|
|
bot_info['interface_type'] = Service.GENERIC
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_create_outgoing_webhook_bot(self, **extras: Any) -> None:
|
2017-06-10 18:43:31 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Outgoing Webhook test bot',
|
|
|
|
'short_name': 'outgoingservicebot',
|
|
|
|
'bot_type': UserProfile.OUTGOING_WEBHOOK_BOT,
|
2018-05-28 18:24:32 +02:00
|
|
|
'payload_url': ujson.dumps('http://127.0.0.1:5002'),
|
2017-06-10 18:43:31 +02:00
|
|
|
}
|
|
|
|
bot_info.update(extras)
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_email = "outgoingservicebot-bot@zulip.testserver"
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
bot = get_user(bot_email, bot_realm)
|
|
|
|
services = get_bot_services(bot.id)
|
|
|
|
service = services[0]
|
|
|
|
|
|
|
|
self.assertEqual(len(services), 1)
|
|
|
|
self.assertEqual(service.name, "outgoingservicebot")
|
2018-05-28 18:24:32 +02:00
|
|
|
self.assertEqual(service.base_url, "http://127.0.0.1:5002")
|
2017-06-10 18:43:31 +02:00
|
|
|
self.assertEqual(service.user_profile, bot)
|
|
|
|
|
|
|
|
# invalid URL test case.
|
2018-05-28 18:24:32 +02:00
|
|
|
bot_info['payload_url'] = ujson.dumps('http://127.0.0.:5002')
|
2017-06-10 18:43:31 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2018-03-16 06:22:14 +01:00
|
|
|
self.assert_json_error(result, "payload_url is not a URL")
|
2017-07-21 17:54:34 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_bot_handler(self) -> None:
|
2017-07-25 19:03:09 +02:00
|
|
|
# Test for valid service.
|
|
|
|
test_service_name = 'converter'
|
|
|
|
test_bot_handler = get_bot_handler(test_service_name)
|
|
|
|
self.assertEqual(str(type(test_bot_handler)), "<class 'zulip_bots.bots.converter.converter.ConverterHandler'>")
|
|
|
|
|
|
|
|
# Test for invalid service.
|
|
|
|
test_service_name = "incorrect_bot_service_foo"
|
|
|
|
test_bot_handler = get_bot_handler(test_service_name)
|
|
|
|
self.assertEqual(test_bot_handler, None)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_if_each_embedded_bot_service_exists(self) -> None:
|
2017-07-21 17:54:34 +02:00
|
|
|
for embedded_bot in EMBEDDED_BOTS:
|
2017-10-12 16:52:53 +02:00
|
|
|
self.assertIsNotNone(get_bot_handler(embedded_bot.name))
|
2017-07-03 18:35:12 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_outgoing_webhook_interface_type(self) -> None:
|
2017-07-03 18:35:12 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Outgoing Webhook test bot',
|
|
|
|
'short_name': 'outgoingservicebot',
|
|
|
|
'bot_type': UserProfile.OUTGOING_WEBHOOK_BOT,
|
2018-05-28 18:24:32 +02:00
|
|
|
'payload_url': ujson.dumps('http://127.0.0.1:5002'),
|
2017-07-03 18:35:12 +02:00
|
|
|
'interface_type': -1,
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Invalid interface type')
|
|
|
|
|
|
|
|
bot_info['interface_type'] = Service.GENERIC
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
2017-07-14 16:44:07 +02:00
|
|
|
|
2018-02-25 19:09:44 +01:00
|
|
|
def test_create_embedded_bot_with_disabled_embedded_bots(self, **extras: Any) -> None:
|
2017-07-14 16:44:07 +02:00
|
|
|
with self.settings(EMBEDDED_BOTS_ENABLED=False):
|
2018-02-25 19:09:44 +01:00
|
|
|
self.create_test_bot(short_name='embeddedservicebot',
|
|
|
|
user_profile=self.example_user("hamlet"),
|
|
|
|
bot_type=UserProfile.EMBEDDED_BOT,
|
|
|
|
service_name='followup',
|
|
|
|
config_data=ujson.dumps({'key': 'value'}),
|
2018-02-25 18:42:38 +01:00
|
|
|
assert_json_error_msg='Embedded bots are not enabled.',
|
2018-02-25 19:09:44 +01:00
|
|
|
**extras)
|
2017-07-14 16:44:07 +02:00
|
|
|
|
2018-02-25 19:09:44 +01:00
|
|
|
def test_create_embedded_bot(self, **extras: Any) -> None:
|
|
|
|
bot_config_info = {'key': 'value'}
|
|
|
|
self.create_test_bot(short_name='embeddedservicebot',
|
|
|
|
user_profile=self.example_user("hamlet"),
|
|
|
|
bot_type=UserProfile.EMBEDDED_BOT,
|
|
|
|
service_name='followup',
|
|
|
|
config_data=ujson.dumps(bot_config_info),
|
|
|
|
**extras)
|
2017-07-14 16:44:07 +02:00
|
|
|
bot_email = "embeddedservicebot-bot@zulip.testserver"
|
|
|
|
bot_realm = get_realm('zulip')
|
|
|
|
bot = get_user(bot_email, bot_realm)
|
|
|
|
services = get_bot_services(bot.id)
|
|
|
|
service = services[0]
|
2018-01-07 19:24:14 +01:00
|
|
|
bot_config = get_bot_config(bot)
|
|
|
|
self.assertEqual(bot_config, bot_config_info)
|
2017-07-14 16:44:07 +02:00
|
|
|
self.assertEqual(len(services), 1)
|
2018-01-07 19:24:14 +01:00
|
|
|
self.assertEqual(service.name, "followup")
|
2017-07-14 16:44:07 +02:00
|
|
|
self.assertEqual(service.user_profile, bot)
|
|
|
|
|
2018-02-25 19:09:44 +01:00
|
|
|
def test_create_embedded_bot_with_incorrect_service_name(self, **extras: Any) -> None:
|
|
|
|
self.create_test_bot(short_name='embeddedservicebot',
|
|
|
|
user_profile=self.example_user("hamlet"),
|
|
|
|
bot_type=UserProfile.EMBEDDED_BOT,
|
|
|
|
service_name='not_existing_service',
|
2018-02-25 18:42:38 +01:00
|
|
|
assert_json_error_msg='Invalid embedded bot name.',
|
2018-02-25 19:09:44 +01:00
|
|
|
**extras)
|
2017-12-07 20:47:10 +01:00
|
|
|
|
2018-02-25 19:09:44 +01:00
|
|
|
def test_create_embedded_bot_with_invalid_config_value(self, **extras: Any) -> None:
|
|
|
|
self.create_test_bot(short_name='embeddedservicebot',
|
|
|
|
user_profile=self.example_user("hamlet"),
|
|
|
|
service_name='followup',
|
|
|
|
config_data=ujson.dumps({'invalid': ['config', 'value']}),
|
2018-02-25 18:42:38 +01:00
|
|
|
assert_json_error_msg='config_data contains a value that is not a string',
|
2018-02-25 19:09:44 +01:00
|
|
|
**extras)
|
2018-01-07 19:24:14 +01:00
|
|
|
|
2018-02-13 11:47:40 +01:00
|
|
|
# Test to create embedded bot with an incorrect config value
|
|
|
|
incorrect_bot_config_info = {'key': 'incorrect key'}
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Embedded test bot',
|
|
|
|
'short_name': 'embeddedservicebot3',
|
|
|
|
'bot_type': UserProfile.EMBEDDED_BOT,
|
|
|
|
'service_name': 'giphy',
|
|
|
|
'config_data': ujson.dumps(incorrect_bot_config_info)
|
|
|
|
}
|
|
|
|
bot_info.update(extras)
|
|
|
|
with patch('zulip_bots.bots.giphy.giphy.GiphyHandler.validate_config', side_effect=ConfigValidationError):
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_error(result, 'Invalid configuration data!')
|
|
|
|
|
2017-12-07 20:47:10 +01:00
|
|
|
def test_is_cross_realm_bot_email(self) -> None:
|
|
|
|
self.assertTrue(is_cross_realm_bot_email("notification-bot@zulip.com"))
|
|
|
|
self.assertTrue(is_cross_realm_bot_email("notification-BOT@zulip.com"))
|
|
|
|
self.assertFalse(is_cross_realm_bot_email("random-bot@zulip.com"))
|
|
|
|
|
|
|
|
with self.settings(CROSS_REALM_BOT_EMAILS={"random-bot@zulip.com"}):
|
|
|
|
self.assertTrue(is_cross_realm_bot_email("random-bot@zulip.com"))
|
|
|
|
self.assertFalse(is_cross_realm_bot_email("notification-bot@zulip.com"))
|
2019-08-18 12:56:21 +02:00
|
|
|
|
|
|
|
@patch("zerver.lib.integrations.WEBHOOK_INTEGRATIONS", stripe_sample_config_options)
|
|
|
|
def test_create_incoming_webhook_bot_with_service_name_and_with_keys(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_metadata = {
|
|
|
|
"full_name": "My Stripe Bot",
|
|
|
|
"short_name": "my-stripe",
|
|
|
|
"bot_type": UserProfile.INCOMING_WEBHOOK_BOT,
|
|
|
|
"service_name": "stripe",
|
|
|
|
"config_data": ujson.dumps({"stripe_api_key": "sample-api-key"})
|
|
|
|
}
|
|
|
|
self.create_bot(**bot_metadata)
|
|
|
|
new_bot = UserProfile.objects.get(full_name="My Stripe Bot")
|
|
|
|
config_data = get_bot_config(new_bot)
|
|
|
|
self.assertEqual(config_data, {"integration_id": "stripe",
|
|
|
|
"stripe_api_key": "sample-api-key"})
|
|
|
|
|
|
|
|
@patch("zerver.lib.integrations.WEBHOOK_INTEGRATIONS", stripe_sample_config_options)
|
|
|
|
def test_create_incoming_webhook_bot_with_service_name_incorrect_keys(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_metadata = {
|
|
|
|
"full_name": "My Stripe Bot",
|
|
|
|
"short_name": "my-stripe",
|
|
|
|
"bot_type": UserProfile.INCOMING_WEBHOOK_BOT,
|
|
|
|
"service_name": "stripe",
|
|
|
|
"config_data": ujson.dumps({"stripe_api_key": "_invalid_key"})
|
|
|
|
}
|
|
|
|
response = self.client_post("/json/bots", bot_metadata)
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
expected_error_message = 'Invalid stripe_api_key value _invalid_key (stripe_api_key starts with a "_" and is hence invalid.)'
|
|
|
|
self.assertEqual(ujson.loads(response.content.decode('utf-8'))["msg"], expected_error_message)
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
UserProfile.objects.get(full_name="My Stripe Bot")
|
|
|
|
|
|
|
|
@patch("zerver.lib.integrations.WEBHOOK_INTEGRATIONS", stripe_sample_config_options)
|
|
|
|
def test_create_incoming_webhook_bot_with_service_name_without_keys(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_metadata = {
|
|
|
|
"full_name": "My Stripe Bot",
|
|
|
|
"short_name": "my-stripe",
|
|
|
|
"bot_type": UserProfile.INCOMING_WEBHOOK_BOT,
|
|
|
|
"service_name": "stripe",
|
|
|
|
}
|
|
|
|
response = self.client_post("/json/bots", bot_metadata)
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
expected_error_message = "Missing configuration parameters: {'stripe_api_key'}"
|
|
|
|
self.assertEqual(ujson.loads(response.content.decode('utf-8'))["msg"], expected_error_message)
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
UserProfile.objects.get(full_name="My Stripe Bot")
|
|
|
|
|
|
|
|
@patch("zerver.lib.integrations.WEBHOOK_INTEGRATIONS", stripe_sample_config_options)
|
|
|
|
def test_create_incoming_webhook_bot_without_service_name(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_metadata = {
|
|
|
|
"full_name": "My Stripe Bot",
|
|
|
|
"short_name": "my-stripe",
|
|
|
|
"bot_type": UserProfile.INCOMING_WEBHOOK_BOT,
|
|
|
|
}
|
|
|
|
self.create_bot(**bot_metadata)
|
|
|
|
new_bot = UserProfile.objects.get(full_name="My Stripe Bot")
|
|
|
|
with self.assertRaises(ConfigError):
|
|
|
|
get_bot_config(new_bot)
|
|
|
|
|
|
|
|
@patch("zerver.lib.integrations.WEBHOOK_INTEGRATIONS", stripe_sample_config_options)
|
|
|
|
def test_create_incoming_webhook_bot_with_incorrect_service_name(self) -> None:
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
bot_metadata = {
|
|
|
|
"full_name": "My Stripe Bot",
|
|
|
|
"short_name": "my-stripe",
|
|
|
|
"bot_type": UserProfile.INCOMING_WEBHOOK_BOT,
|
|
|
|
"service_name": "stripes",
|
|
|
|
}
|
|
|
|
response = self.client_post("/json/bots", bot_metadata)
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
expected_error_message = "Invalid integration 'stripes'."
|
|
|
|
self.assertEqual(ujson.loads(response.content.decode('utf-8'))["msg"], expected_error_message)
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
UserProfile.objects.get(full_name="My Stripe Bot")
|