test_events.py: Use create_test_bot().

This commit is contained in:
Robert Hönig 2018-01-30 17:08:35 +01:00 committed by Tim Abbott
parent 323284e0b6
commit 760cbcc98e
1 changed files with 20 additions and 20 deletions

View File

@ -14,7 +14,7 @@ from django.utils.timezone import now as timezone_now
from zerver.models import ( from zerver.models import (
get_client, get_realm, get_stream_recipient, get_stream, get_user, get_client, get_realm, get_stream_recipient, get_stream, get_user,
Message, RealmDomain, Recipient, UserMessage, UserPresence, UserProfile, Message, RealmDomain, Recipient, UserMessage, UserPresence, UserProfile,
Realm, Subscription, Stream, flush_per_request_caches, UserGroup Realm, Subscription, Stream, flush_per_request_caches, UserGroup, Service,
) )
from zerver.lib.actions import ( from zerver.lib.actions import (
@ -443,10 +443,8 @@ class EventsRegisterTest(ZulipTestCase):
super().setUp() super().setUp()
self.user_profile = self.example_user('hamlet') self.user_profile = self.example_user('hamlet')
def create_bot(self, email: str) -> UserProfile: def create_bot(self, email: str, **extras: Any) -> Optional[UserProfile]:
return do_create_user(email, '123', return self.create_test_bot(email, self.user_profile, **extras)
get_realm('zulip'), 'Test Bot', 'test',
bot_type=UserProfile.DEFAULT_BOT, bot_owner=self.user_profile)
def realm_bot_schema(self, field_name: str, check: Validator) -> Validator: def realm_bot_schema(self, field_name: str, check: Validator) -> Validator:
return self.check_events_dict([ return self.check_events_dict([
@ -1560,27 +1558,27 @@ class EventsRegisterTest(ZulipTestCase):
]))), ]))),
])), ])),
]) ])
action = lambda: self.create_bot('test-bot@zulip.com') action = lambda: self.create_bot('test')
events = self.do_test(action, num_events=2) events = self.do_test(action, num_events=3)
error = bot_created_checker('events[1]', events[1]) error = bot_created_checker('events[1]', events[1])
self.assert_on_error(error) self.assert_on_error(error)
def test_change_bot_full_name(self) -> None: def test_change_bot_full_name(self) -> None:
bot = self.create_bot('test-bot@zulip.com') bot = self.create_bot('test')
action = lambda: do_change_full_name(bot, 'New Bot Name', self.user_profile) action = lambda: do_change_full_name(bot, 'New Bot Name', self.user_profile)
events = self.do_test(action, num_events=2) events = self.do_test(action, num_events=2)
error = self.realm_bot_schema('full_name', check_string)('events[1]', events[1]) error = self.realm_bot_schema('full_name', check_string)('events[1]', events[1])
self.assert_on_error(error) self.assert_on_error(error)
def test_regenerate_bot_api_key(self) -> None: def test_regenerate_bot_api_key(self) -> None:
bot = self.create_bot('test-bot@zulip.com') bot = self.create_bot('test')
action = lambda: do_regenerate_api_key(bot, self.user_profile) action = lambda: do_regenerate_api_key(bot, self.user_profile)
events = self.do_test(action) events = self.do_test(action)
error = self.realm_bot_schema('api_key', check_string)('events[0]', events[0]) error = self.realm_bot_schema('api_key', check_string)('events[0]', events[0])
self.assert_on_error(error) self.assert_on_error(error)
def test_change_bot_avatar_source(self) -> None: def test_change_bot_avatar_source(self) -> None:
bot = self.create_bot('test-bot@zulip.com') bot = self.create_bot('test')
action = lambda: do_change_avatar_fields(bot, bot.AVATAR_FROM_USER) action = lambda: do_change_avatar_fields(bot, bot.AVATAR_FROM_USER)
events = self.do_test(action, num_events=2) events = self.do_test(action, num_events=2)
error = self.realm_bot_schema('avatar_url', check_string)('events[0]', events[0]) error = self.realm_bot_schema('avatar_url', check_string)('events[0]', events[0])
@ -1604,14 +1602,14 @@ class EventsRegisterTest(ZulipTestCase):
self.assert_on_error(error) self.assert_on_error(error)
def test_change_bot_default_all_public_streams(self) -> None: def test_change_bot_default_all_public_streams(self) -> None:
bot = self.create_bot('test-bot@zulip.com') bot = self.create_bot('test')
action = lambda: do_change_default_all_public_streams(bot, True) action = lambda: do_change_default_all_public_streams(bot, True)
events = self.do_test(action) events = self.do_test(action)
error = self.realm_bot_schema('default_all_public_streams', check_bool)('events[0]', events[0]) error = self.realm_bot_schema('default_all_public_streams', check_bool)('events[0]', events[0])
self.assert_on_error(error) self.assert_on_error(error)
def test_change_bot_default_sending_stream(self) -> None: def test_change_bot_default_sending_stream(self) -> None:
bot = self.create_bot('test-bot@zulip.com') bot = self.create_bot('test')
stream = get_stream("Rome", bot.realm) stream = get_stream("Rome", bot.realm)
action = lambda: do_change_default_sending_stream(bot, stream) action = lambda: do_change_default_sending_stream(bot, stream)
@ -1625,7 +1623,7 @@ class EventsRegisterTest(ZulipTestCase):
self.assert_on_error(error) self.assert_on_error(error)
def test_change_bot_default_events_register_stream(self) -> None: def test_change_bot_default_events_register_stream(self) -> None:
bot = self.create_bot('test-bot@zulip.com') bot = self.create_bot('test')
stream = get_stream("Rome", bot.realm) stream = get_stream("Rome", bot.realm)
action = lambda: do_change_default_events_register_stream(bot, stream) action = lambda: do_change_default_events_register_stream(bot, stream)
@ -1650,7 +1648,7 @@ class EventsRegisterTest(ZulipTestCase):
]) ])
self.user_profile = self.example_user('iago') self.user_profile = self.example_user('iago')
owner = self.example_user('hamlet') owner = self.example_user('hamlet')
bot = self.create_bot('test-bot@zulip.com') bot = self.create_bot('test')
action = lambda: do_change_bot_owner(bot, owner, self.user_profile) action = lambda: do_change_bot_owner(bot, owner, self.user_profile)
events = self.do_test(action) events = self.do_test(action)
error = change_bot_owner_checker('events[0]', events[0]) error = change_bot_owner_checker('events[0]', events[0])
@ -1671,10 +1669,12 @@ class EventsRegisterTest(ZulipTestCase):
])), ])),
]) ])
self.user_profile = self.example_user('iago') self.user_profile = self.example_user('iago')
bot = do_create_user('test-bot@zulip.com', '123', get_realm('zulip'), 'Test Bot', 'test', bot = self.create_test_bot('test', self.user_profile,
bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_owner=self.user_profile) full_name='Test Bot',
add_service(user_profile=bot, name="test", base_url="http://hostname.domain1.com", bot_type=UserProfile.OUTGOING_WEBHOOK_BOT,
interface=1, token="abced") payload_url=ujson.dumps('http://hostname.domain2.com'),
interface_type=Service.GENERIC,
)
action = lambda: do_update_outgoing_webhook_service(bot, 2, 'http://hostname.domain2.com') action = lambda: do_update_outgoing_webhook_service(bot, 2, 'http://hostname.domain2.com')
events = self.do_test(action) events = self.do_test(action)
error = update_outgoing_webhook_service_checker('events[0]', events[0]) error = update_outgoing_webhook_service_checker('events[0]', events[0])
@ -1690,7 +1690,7 @@ class EventsRegisterTest(ZulipTestCase):
('user_id', check_int), ('user_id', check_int),
])), ])),
]) ])
bot = self.create_bot('foo-bot@zulip.com') bot = self.create_bot('test')
action = lambda: do_deactivate_user(bot) action = lambda: do_deactivate_user(bot)
events = self.do_test(action, num_events=2) events = self.do_test(action, num_events=2)
error = bot_deactivate_checker('events[1]', events[1]) error = bot_deactivate_checker('events[1]', events[1])
@ -1718,7 +1718,7 @@ class EventsRegisterTest(ZulipTestCase):
]))), ]))),
])), ])),
]) ])
bot = self.create_bot('foo-bot@zulip.com') bot = self.create_bot('test')
do_deactivate_user(bot) do_deactivate_user(bot)
action = lambda: do_reactivate_user(bot) action = lambda: do_reactivate_user(bot)
events = self.do_test(action, num_events=2) events = self.do_test(action, num_events=2)