mirror of https://github.com/zulip/zulip.git
Update patch bot API to support setting stream defaults
Adds APIs edit a bot's default_to_stream, default_events_register_stream and default_all_public_streams. (imported from commit c848a94b7932311143dad770c901d6688c936b6d)
This commit is contained in:
parent
50db83508b
commit
846dfd5105
|
@ -1289,6 +1289,44 @@ def do_change_full_name(user_profile, full_name, log=True):
|
|||
full_name=user_profile.full_name))
|
||||
send_event(event, active_user_ids(user_profile.realm))
|
||||
|
||||
def _default_stream_permision_check(user_profile, stream):
|
||||
# Any user can have a None default stream
|
||||
if stream is not None:
|
||||
if user_profile.is_bot:
|
||||
user = user_profile.bot_owner
|
||||
else:
|
||||
user = user_profile
|
||||
if stream.invite_only and not subscribed_to_stream(user, stream):
|
||||
raise JsonableError('Insufficient permission')
|
||||
|
||||
def do_change_default_sending_stream(user_profile, stream, log=True):
|
||||
_default_stream_permision_check(user_profile, stream)
|
||||
|
||||
user_profile.default_sending_stream = stream
|
||||
user_profile.save(update_fields=['default_sending_stream'])
|
||||
if log:
|
||||
log_event({'type': 'user_change_default_sending_stream',
|
||||
'user': user_profile.email,
|
||||
'stream': str(stream)})
|
||||
|
||||
def do_change_default_events_register_stream(user_profile, stream, log=True):
|
||||
_default_stream_permision_check(user_profile, stream)
|
||||
|
||||
user_profile.default_events_register_stream = stream
|
||||
user_profile.save(update_fields=['default_events_register_stream'])
|
||||
if log:
|
||||
log_event({'type': 'user_change_default_events_register_stream',
|
||||
'user': user_profile.email,
|
||||
'stream': str(stream)})
|
||||
|
||||
def do_change_default_all_public_streams(user_profile, value, log=True):
|
||||
user_profile.default_all_public_streams = value
|
||||
user_profile.save(update_fields=['default_all_public_streams'])
|
||||
if log:
|
||||
log_event({'type': 'user_change_default_all_public_streams',
|
||||
'user': user_profile.email,
|
||||
'value': str(value)})
|
||||
|
||||
def do_change_is_admin(user_profile, is_admin):
|
||||
if is_admin:
|
||||
assign_perm('administer', user_profile, user_profile.realm)
|
||||
|
|
268
zerver/tests.py
268
zerver/tests.py
|
@ -374,6 +374,16 @@ class BotTest(AuthedTestCase):
|
|||
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
||||
self.assertEqual(profile.default_sending_stream.name, 'Denmark')
|
||||
|
||||
def test_add_bot_with_default_sending_stream_not_subscribed(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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')
|
||||
|
||||
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
||||
self.assertEqual(profile.default_sending_stream.name, 'Rome')
|
||||
|
||||
def test_add_bot_with_default_sending_stream_private_allowed(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
||||
|
@ -557,6 +567,264 @@ class BotTest(AuthedTestCase):
|
|||
bot = self.get_bot()
|
||||
self.assertEqual('Fred', bot['full_name'])
|
||||
|
||||
def test_patch_bot_to_stream(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_sending_stream = ujson.loads(result.content)['default_sending_stream']
|
||||
self.assertEqual('Denmark', default_sending_stream)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual('Denmark', bot['default_sending_stream'])
|
||||
|
||||
def test_patch_bot_to_stream_not_subscribed(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_sending_stream = ujson.loads(result.content)['default_sending_stream']
|
||||
self.assertEqual('Rome', default_sending_stream)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual('Rome', bot['default_sending_stream'])
|
||||
|
||||
def test_patch_bot_to_stream_none(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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': '',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_sending_stream = ujson.loads(result.content)['default_sending_stream']
|
||||
self.assertEqual(None, default_sending_stream)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual(None, bot['default_sending_stream'])
|
||||
|
||||
def test_patch_bot_to_stream_private_allowed(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
||||
stream = get_stream("Denmark", user_profile.realm)
|
||||
do_add_subscription(user_profile, stream)
|
||||
do_make_stream_private(user_profile.realm, "Denmark")
|
||||
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_sending_stream = ujson.loads(result.content)['default_sending_stream']
|
||||
self.assertEqual('Denmark', default_sending_stream)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual('Denmark', bot['default_sending_stream'])
|
||||
|
||||
def test_patch_bot_to_stream_private_denied(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
||||
stream = get_stream("Denmark", user_profile.realm)
|
||||
do_remove_subscription(user_profile, stream)
|
||||
do_make_stream_private(user_profile.realm, "Denmark")
|
||||
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_error(result, 'Insufficient permission')
|
||||
|
||||
def test_patch_bot_to_stream_not_found(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_error(result, 'No such stream \'missing\'')
|
||||
|
||||
def test_patch_bot_events_register_stream(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_events_register_stream = ujson.loads(result.content)['default_events_register_stream']
|
||||
self.assertEqual('Denmark', default_events_register_stream)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual('Denmark', bot['default_events_register_stream'])
|
||||
|
||||
def test_patch_bot_events_register_stream_allowed(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
||||
stream = get_stream("Denmark", user_profile.realm)
|
||||
do_add_subscription(user_profile, stream)
|
||||
do_make_stream_private(user_profile.realm, "Denmark")
|
||||
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_events_register_stream = ujson.loads(result.content)['default_events_register_stream']
|
||||
self.assertEqual('Denmark', default_events_register_stream)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual('Denmark', bot['default_events_register_stream'])
|
||||
|
||||
def test_patch_bot_events_register_stream_denied(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
||||
stream = get_stream("Denmark", user_profile.realm)
|
||||
do_remove_subscription(user_profile, stream)
|
||||
do_make_stream_private(user_profile.realm, "Denmark")
|
||||
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_error(result, 'Insufficient permission')
|
||||
|
||||
def test_patch_bot_events_register_stream_none(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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': '',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_events_register_stream = ujson.loads(result.content)['default_events_register_stream']
|
||||
self.assertEqual(None, default_events_register_stream)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual(None, bot['default_events_register_stream'])
|
||||
|
||||
def test_patch_bot_events_register_stream_not_found(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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',
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_error(result, 'No such stream \'missing\'')
|
||||
|
||||
def test_patch_bot_default_all_public_streams_true(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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),
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_events_register_stream = ujson.loads(result.content)['default_all_public_streams']
|
||||
self.assertEqual(default_events_register_stream, True)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual(bot['default_all_public_streams'], True)
|
||||
|
||||
def test_patch_bot_default_all_public_streams_false(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
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),
|
||||
}
|
||||
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
||||
self.assert_json_success(result)
|
||||
|
||||
default_events_register_stream = ujson.loads(result.content)['default_all_public_streams']
|
||||
self.assertEqual(default_events_register_stream, False)
|
||||
|
||||
bot = self.get_bot()
|
||||
self.assertEqual(bot['default_all_public_streams'], False)
|
||||
|
||||
def test_patch_bot_via_post(self):
|
||||
self.login("hamlet@zulip.com")
|
||||
bot_info = {
|
||||
|
|
|
@ -37,10 +37,13 @@ from zerver.lib.actions import bulk_remove_subscriptions, do_change_password, \
|
|||
do_set_muted_topics, do_rename_stream, clear_followup_emails_queue, \
|
||||
notify_for_streams_by_default, do_change_enable_offline_push_notifications, \
|
||||
do_deactivate_stream, do_change_autoscroll_forever, do_make_stream_public, \
|
||||
do_make_stream_private, do_change_default_desktop_notifications, \
|
||||
do_make_stream_private, do_add_default_stream, \
|
||||
do_change_default_all_public_streams, do_change_default_desktop_notifications, \
|
||||
do_change_default_events_register_stream, do_change_default_sending_stream, \
|
||||
do_change_enable_stream_desktop_notifications, do_change_enable_stream_sounds, \
|
||||
do_change_stream_description, do_update_pointer, do_add_default_stream, do_remove_default_stream, \
|
||||
do_get_streams
|
||||
do_change_stream_description, do_get_streams, do_remove_default_stream, \
|
||||
do_update_pointer
|
||||
|
||||
from zerver.lib.create_user import random_api_key
|
||||
from zerver.lib.push_notifications import num_push_devices_for_user
|
||||
from zerver.forms import RegistrationForm, HomepageForm, ToSForm, \
|
||||
|
@ -1903,7 +1906,11 @@ def stream_or_none(stream_name, realm):
|
|||
return stream
|
||||
|
||||
@has_request_variables
|
||||
def patch_bot_backend(request, user_profile, email, full_name=REQ):
|
||||
def patch_bot_backend(request, user_profile, email,
|
||||
full_name=REQ(default=None),
|
||||
default_sending_stream=REQ(default=None),
|
||||
default_events_register_stream=REQ(default=None),
|
||||
default_all_public_streams=REQ(default=None, validator=check_bool)):
|
||||
try:
|
||||
bot = get_user_profile_by_email(email)
|
||||
except:
|
||||
|
@ -1912,9 +1919,16 @@ def patch_bot_backend(request, user_profile, email, full_name=REQ):
|
|||
if not user_profile.can_admin_user(bot):
|
||||
return json_error('Insufficient permission')
|
||||
|
||||
do_change_full_name(bot, full_name)
|
||||
|
||||
bot_avatar_url = None
|
||||
if full_name is not None:
|
||||
do_change_full_name(bot, full_name)
|
||||
if default_sending_stream is not None:
|
||||
stream = stream_or_none(default_sending_stream, bot.realm)
|
||||
do_change_default_sending_stream(bot, stream)
|
||||
if default_events_register_stream is not None:
|
||||
stream = stream_or_none(default_events_register_stream, bot.realm)
|
||||
do_change_default_events_register_stream(bot, stream)
|
||||
if default_all_public_streams is not None:
|
||||
do_change_default_all_public_streams(bot, default_all_public_streams)
|
||||
|
||||
if len(request.FILES) == 0:
|
||||
pass
|
||||
|
@ -1924,13 +1938,15 @@ def patch_bot_backend(request, user_profile, email, full_name=REQ):
|
|||
avatar_source = UserProfile.AVATAR_FROM_USER
|
||||
bot.avatar_source = avatar_source
|
||||
bot.save(update_fields=["avatar_source"])
|
||||
bot_avatar_url = avatar_url(bot)
|
||||
else:
|
||||
return json_error("You may only upload one file at a time")
|
||||
|
||||
json_result = dict(
|
||||
full_name = full_name,
|
||||
avatar_url = bot_avatar_url
|
||||
full_name=bot.full_name,
|
||||
avatar_url=avatar_url(bot),
|
||||
default_sending_stream=get_stream_name(bot.default_sending_stream),
|
||||
default_events_register_stream=get_stream_name(bot.default_events_register_stream),
|
||||
default_all_public_streams=bot.default_all_public_streams,
|
||||
)
|
||||
return json_success(json_result)
|
||||
|
||||
|
@ -2037,14 +2053,21 @@ def add_bot_backend(request, user_profile, full_name=REQ, short_name=REQ,
|
|||
def get_bots_backend(request, user_profile):
|
||||
bot_profiles = UserProfile.objects.filter(is_bot=True, is_active=True,
|
||||
bot_owner=user_profile)
|
||||
bot_profiles = bot_profiles.select_related('default_sending_stream', 'default_events_register')
|
||||
bot_profiles = bot_profiles.order_by('date_joined')
|
||||
|
||||
def bot_info(bot_profile):
|
||||
default_sending_stream = get_stream_name(bot_profile.default_sending_stream)
|
||||
default_events_register_stream = get_stream_name(bot_profile.default_events_register_stream)
|
||||
|
||||
return dict(
|
||||
username = bot_profile.email,
|
||||
full_name = bot_profile.full_name,
|
||||
api_key = bot_profile.api_key,
|
||||
avatar_url = avatar_url(bot_profile)
|
||||
username=bot_profile.email,
|
||||
full_name=bot_profile.full_name,
|
||||
api_key=bot_profile.api_key,
|
||||
avatar_url=avatar_url(bot_profile),
|
||||
default_sending_stream=default_sending_stream,
|
||||
default_events_register_stream=default_events_register_stream,
|
||||
default_all_public_streams=bot_profile.default_all_public_streams,
|
||||
)
|
||||
|
||||
return json_success({'bots': map(bot_info, bot_profiles)})
|
||||
|
|
Loading…
Reference in New Issue