Add API support for setting defaults in the add bot API

Support setting default_to_stream, default_events_register_stream, and
default_all_public_streams during in the bot creation API.

(imported from commit bef484dd8be9f8aacd65a959594075aea8bdf271)
This commit is contained in:
Jason Michalski
2014-02-11 12:43:30 -05:00
parent f3180b774b
commit 50db83508b
4 changed files with 163 additions and 12 deletions

View File

@@ -106,7 +106,9 @@ def notify_created_user(user_profile):
def do_create_user(email, password, realm, full_name, short_name, def do_create_user(email, password, realm, full_name, short_name,
active=True, bot=False, bot_owner=None, active=True, bot=False, bot_owner=None,
avatar_source=UserProfile.AVATAR_FROM_GRAVATAR): avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,
default_sending_stream=None, default_events_register_stream=None,
default_all_public_streams=None):
event = {'type': 'user_created', event = {'type': 'user_created',
'timestamp': time.time(), 'timestamp': time.time(),
'full_name': full_name, 'full_name': full_name,
@@ -118,8 +120,13 @@ def do_create_user(email, password, realm, full_name, short_name,
event['bot_owner'] = bot_owner.email event['bot_owner'] = bot_owner.email
log_event(event) log_event(event)
user_profile = create_user(email, password, realm, full_name, short_name, user_profile = create_user(email=email, password=password, realm=realm,
active, bot, bot_owner, avatar_source) full_name=full_name, short_name=short_name,
active=active, bot=bot, bot_owner=bot_owner,
avatar_source=avatar_source,
default_sending_stream=default_sending_stream,
default_events_register_stream=default_events_register_stream,
default_all_public_streams=default_all_public_streams)
notify_created_user(user_profile) notify_created_user(user_profile)
return user_profile return user_profile

View File

@@ -42,11 +42,20 @@ def create_user_profile(realm, email, password, active, bot, full_name,
def create_user(email, password, realm, full_name, short_name, def create_user(email, password, realm, full_name, short_name,
active=True, bot=False, bot_owner=None, active=True, bot=False, bot_owner=None,
avatar_source=UserProfile.AVATAR_FROM_GRAVATAR, avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,
is_mirror_dummy=False): is_mirror_dummy=False, default_sending_stream=None,
default_events_register_stream=None,
default_all_public_streams=None):
user_profile = create_user_profile(realm, email, password, active, bot, user_profile = create_user_profile(realm, email, password, active, bot,
full_name, short_name, bot_owner, full_name, short_name, bot_owner,
is_mirror_dummy) is_mirror_dummy)
user_profile.avatar_source = avatar_source user_profile.avatar_source = avatar_source
user_profile.default_sending_stream = default_sending_stream
user_profile.default_events_register_stream = default_events_register_stream
# Allow the ORM default to be used if not provided
if default_all_public_streams is not None:
user_profile.default_all_public_streams = default_all_public_streams
user_profile.save() user_profile.save()
recipient = Recipient.objects.create(type_id=user_profile.id, recipient = Recipient.objects.create(type_id=user_profile.id,
type=Recipient.PERSONAL) type=Recipient.PERSONAL)

View File

@@ -12,13 +12,14 @@ from zerver.lib.test_helpers import (
from zerver.models import UserProfile, Recipient, \ from zerver.models import UserProfile, Recipient, \
Realm, Client, UserActivity, \ Realm, Client, UserActivity, \
get_user_profile_by_email, split_email_to_domain, get_realm, \ get_user_profile_by_email, split_email_to_domain, get_realm, \
get_client get_client, get_stream
from zerver.lib.initial_password import initial_password from zerver.lib.initial_password import initial_password
from zerver.lib.actions import \ from zerver.lib.actions import \
get_emails_from_user_ids, do_deactivate_user, do_reactivate_user, \ get_emails_from_user_ids, do_deactivate_user, do_reactivate_user, \
do_change_is_admin, extract_recipients, \ do_change_is_admin, extract_recipients, \
do_set_realm_name, get_realm_name, do_deactivate_realm do_set_realm_name, get_realm_name, do_deactivate_realm, \
do_add_subscription, do_remove_subscription, do_make_stream_private
from zerver.lib.alert_words import alert_words_in_realm, user_alert_words, \ from zerver.lib.alert_words import alert_words_in_realm, user_alert_words, \
add_user_alert_words, remove_user_alert_words add_user_alert_words, remove_user_alert_words
from zerver.middleware import is_slow_query from zerver.middleware import is_slow_query
@@ -343,13 +344,15 @@ class BotTest(AuthedTestCase):
json = ujson.loads(result.content) json = ujson.loads(result.content)
self.assertEqual(count, len(json['bots'])) self.assertEqual(count, len(json['bots']))
def create_bot(self): def create_bot(self, **extras):
bot_info = { bot_info = {
'full_name': 'The Bot of Hamlet', 'full_name': 'The Bot of Hamlet',
'short_name': 'hambot', 'short_name': 'hambot',
} }
bot_info.update(extras)
result = self.client.post("/json/bots", bot_info) result = self.client.post("/json/bots", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
return ujson.loads(result.content)
def deactivate_bot(self): def deactivate_bot(self):
result = self.client_delete("/json/bots/hambot-bot@zulip.com") result = self.client_delete("/json/bots/hambot-bot@zulip.com")
@@ -361,6 +364,97 @@ class BotTest(AuthedTestCase):
self.create_bot() self.create_bot()
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
def test_add_bot_with_default_sending_stream(self):
self.login("hamlet@zulip.com")
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')
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_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")
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')
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_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',
'default_sending_stream': 'Denmark',
}
result = self.client.post("/json/bots", bot_info)
self.assert_json_error(result, 'Insufficient permission')
def test_add_bot_with_default_events_register_stream(self):
self.login("hamlet@zulip.com")
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')
profile = get_user_profile_by_email('hambot-bot@zulip.com')
self.assertEqual(profile.default_events_register_stream.name, 'Denmark')
def test_add_bot_with_default_events_register_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")
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')
profile = get_user_profile_by_email('hambot-bot@zulip.com')
self.assertEqual(profile.default_events_register_stream.name, 'Denmark')
def test_add_bot_with_default_events_register_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")
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, 'Insufficient permission')
def test_add_bot_with_default_all_public_streams(self):
self.login("hamlet@zulip.com")
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'])
profile = get_user_profile_by_email('hambot-bot@zulip.com')
self.assertEqual(profile.default_all_public_streams, True)
def test_deactivate_bot(self): def test_deactivate_bot(self):
self.login("hamlet@zulip.com") self.login("hamlet@zulip.com")
self.assert_num_bots_equal(0) self.assert_num_bots_equal(0)

View File

@@ -1886,6 +1886,22 @@ def avatar(request, email):
url += sep + request.META['QUERY_STRING'] url += sep + request.META['QUERY_STRING']
return redirect(url) return redirect(url)
def get_stream_name(stream):
if stream:
name = stream.name
else :
name = None
return name
def stream_or_none(stream_name, realm):
if stream_name == '':
return None
else:
stream = get_stream(stream_name, realm)
if not stream:
raise JsonableError('No such stream \'%s\'' % (stream_name, ))
return stream
@has_request_variables @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):
try: try:
@@ -1961,7 +1977,10 @@ def regenerate_bot_api_key(request, user_profile, email):
return json_success(json_result) return json_success(json_result)
@has_request_variables @has_request_variables
def add_bot_backend(request, user_profile, full_name=REQ, short_name=REQ): def add_bot_backend(request, user_profile, full_name=REQ, short_name=REQ,
default_sending_stream=REQ(default=None),
default_events_register_stream=REQ(default=None),
default_all_public_streams=REQ(validator=check_bool, default=None)):
short_name += "-bot" short_name += "-bot"
email = short_name + "@" + user_profile.realm.domain email = short_name + "@" + user_profile.realm.domain
form = CreateUserForm({'full_name': full_name, 'email': email}) form = CreateUserForm({'full_name': full_name, 'email': email})
@@ -1984,12 +2003,34 @@ def add_bot_backend(request, user_profile, full_name=REQ, short_name=REQ):
upload_avatar_image(user_file, user_profile, email) upload_avatar_image(user_file, user_profile, email)
avatar_source = UserProfile.AVATAR_FROM_USER avatar_source = UserProfile.AVATAR_FROM_USER
bot_profile = do_create_user(email, '', user_profile.realm, full_name, if default_sending_stream is not None:
short_name, True, True, default_sending_stream = stream_or_none(default_sending_stream, user_profile.realm)
user_profile, avatar_source) if default_sending_stream and not default_sending_stream.is_public() and not \
subscribed_to_stream(user_profile, default_sending_stream):
return json_error('Insufficient permission')
if default_events_register_stream is not None:
default_events_register_stream = stream_or_none(default_events_register_stream,
user_profile.realm)
if default_events_register_stream and not default_events_register_stream.is_public() and not \
subscribed_to_stream(user_profile, default_events_register_stream):
return json_error('Insufficient permission')
bot_profile = do_create_user(email=email, password='',
realm=user_profile.realm, full_name=full_name,
short_name=short_name, active=True, bot=True,
bot_owner=user_profile,
avatar_source=avatar_source,
default_sending_stream=default_sending_stream,
default_events_register_stream=default_events_register_stream,
default_all_public_streams=default_all_public_streams)
json_result = dict( json_result = dict(
api_key=bot_profile.api_key, api_key=bot_profile.api_key,
avatar_url=avatar_url(bot_profile) avatar_url=avatar_url(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),
default_all_public_streams=bot_profile.default_all_public_streams,
) )
return json_success(json_result) return json_success(json_result)