tests: Replace api_auth usages with auth methods in zerver/tests.

This commit is contained in:
Alena Volkova 2017-12-14 13:02:31 -05:00 committed by showell
parent 4a303d1778
commit 26eb23afc7
14 changed files with 237 additions and 376 deletions

View File

@ -1118,10 +1118,10 @@ class BugdownTest(ZulipTestCase):
class BugdownApiTests(ZulipTestCase):
def test_render_message_api(self) -> None:
content = 'That is a **bold** statement'
result = self.client_post(
result = self.api_post(
self.example_email("othello"),
'/api/v1/messages/render',
dict(content=content),
**self.api_auth(self.example_email("othello"))
dict(content=content)
)
self.assert_json_success(result)
self.assertEqual(result.json()['rendered'],
@ -1130,10 +1130,10 @@ class BugdownApiTests(ZulipTestCase):
def test_render_mention_stream_api(self) -> None:
"""Determines whether we're correctly passing the realm context"""
content = 'This mentions #**Denmark** and @**King Hamlet**.'
result = self.client_post(
result = self.api_post(
self.example_email("othello"),
'/api/v1/messages/render',
dict(content=content),
**self.api_auth(self.example_email("othello"))
dict(content=content)
)
self.assert_json_success(result)
user_id = self.example_user('hamlet').id

View File

@ -614,11 +614,11 @@ class DeactivatedRealmTest(ZulipTestCase):
"to": self.example_email("othello")})
self.assert_json_error_contains(result, "has been deactivated", status_code=400)
result = self.client_post("/api/v1/messages", {"type": "private",
"content": "Test message",
"client": "test suite",
"to": self.example_email("othello")},
**self.api_auth(self.example_email("hamlet")))
result = self.api_post(self.example_email("hamlet"),
"/api/v1/messages", {"type": "private",
"content": "Test message",
"client": "test suite",
"to": self.example_email("othello")})
self.assert_json_error_contains(result, "has been deactivated", status_code=401)
def test_fetch_api_key_deactivated_realm(self) -> None:
@ -731,11 +731,11 @@ class InactiveUserTest(ZulipTestCase):
"to": self.example_email("othello")})
self.assert_json_error_contains(result, "Account not active", status_code=400)
result = self.client_post("/api/v1/messages", {"type": "private",
"content": "Test message",
"client": "test suite",
"to": self.example_email("othello")},
**self.api_auth(self.example_email("hamlet")))
result = self.api_post(self.example_email("hamlet"),
"/api/v1/messages", {"type": "private",
"content": "Test message",
"client": "test suite",
"to": self.example_email("othello")})
self.assert_json_error_contains(result, "Account not active", status_code=401)
def test_fetch_api_key_deactivated_user(self) -> None:
@ -832,16 +832,14 @@ class TestIncomingWebhookBot(ZulipTestCase):
self.webhook_bot = get_user('webhook-bot@zulip.com', zulip_realm)
def test_webhook_bot_permissions(self) -> None:
result = self.client_post("/api/v1/messages", {
"type": "private",
"content": "Test message",
"client": "test suite",
"to": self.example_email("othello")
}, **self.api_auth("webhook-bot@zulip.com"))
result = self.api_post("webhook-bot@zulip.com",
"/api/v1/messages", {"type": "private",
"content": "Test message",
"client": "test suite",
"to": self.example_email("othello")})
self.assert_json_success(result)
post_params = {"anchor": 1, "num_before": 1, "num_after": 1}
result = self.client_get("/api/v1/messages", dict(post_params),
**self.api_auth("webhook-bot@zulip.com"))
result = self.api_get("webhook-bot@zulip.com", "/api/v1/messages", dict(post_params))
self.assert_json_error(result, 'This API is not available to incoming webhook bots.',
status_code=401)
@ -985,7 +983,7 @@ class TestHumanUsersOnlyDecorator(ZulipTestCase):
"/api/v1/report/unnarrow_times",
]
for endpoint in post_endpoints:
result = self.client_post(endpoint, **self.api_auth('default-bot@zulip.com'))
result = self.api_post('default-bot@zulip.com', endpoint)
self.assert_json_error(result, "This endpoint does not accept bot requests.")
patch_endpoints = [
@ -996,7 +994,7 @@ class TestHumanUsersOnlyDecorator(ZulipTestCase):
"/api/v1/users/me/profile_data"
]
for endpoint in patch_endpoints:
result = self.client_patch(endpoint, **self.api_auth('default-bot@zulip.com'))
result = self.api_patch('default-bot@zulip.com', endpoint)
self.assert_json_error(result, "This endpoint does not accept bot requests.")
delete_endpoints = [
@ -1004,7 +1002,7 @@ class TestHumanUsersOnlyDecorator(ZulipTestCase):
"/api/v1/users/me/android_gcm_reg_id",
]
for endpoint in delete_endpoints:
result = self.client_delete(endpoint, **self.api_auth('default-bot@zulip.com'))
result = self.api_delete('default-bot@zulip.com', endpoint)
self.assert_json_error(result, "This endpoint does not accept bot requests.")
class TestAuthenticatedJsonPostViewDecorator(ZulipTestCase):

View File

@ -154,11 +154,11 @@ class EventsEndpointTest(ZulipTestCase):
# events_register code paths
email = self.example_email("hamlet")
with mock.patch('zerver.views.events_register.do_events_register', return_value={}):
result = self.client_post('/json/register', **self.api_auth(email))
result = self.api_post(email, '/json/register')
self.assert_json_success(result)
with mock.patch('zerver.lib.events.request_event_queue', return_value=None):
result = self.client_post('/json/register', **self.api_auth(email))
result = self.api_post(email, '/json/register')
self.assert_json_error(result, "Could not allocate event queue")
return_event_queue = '15:11'
@ -167,13 +167,11 @@ class EventsEndpointTest(ZulipTestCase):
# Test that call is made to deal with a returning soft deactivated user.
with mock.patch('zerver.lib.events.maybe_catch_up_soft_deactivated_user') as fa:
with stub_event_queue_user_events(return_event_queue, return_user_events):
result = self.client_post('/json/register', dict(event_types=ujson.dumps(['pointer'])),
**self.api_auth(email))
result = self.api_post(email, '/json/register', dict(event_types=ujson.dumps(['pointer'])))
self.assertEqual(fa.call_count, 1)
with stub_event_queue_user_events(return_event_queue, return_user_events):
result = self.client_post('/json/register', dict(event_types=ujson.dumps(['pointer'])),
**self.api_auth(email))
result = self.api_post(email, '/json/register', dict(event_types=ujson.dumps(['pointer'])))
self.assert_json_success(result)
result_dict = result.json()
self.assertEqual(result_dict['last_event_id'], -1)
@ -188,8 +186,7 @@ class EventsEndpointTest(ZulipTestCase):
}
]
with stub_event_queue_user_events(return_event_queue, return_user_events):
result = self.client_post('/json/register', dict(event_types=ujson.dumps(['pointer'])),
**self.api_auth(email))
result = self.api_post(email, '/json/register', dict(event_types=ujson.dumps(['pointer'])))
self.assert_json_success(result)
result_dict = result.json()
@ -200,10 +197,9 @@ class EventsEndpointTest(ZulipTestCase):
# Now test with `fetch_event_types` not matching the event
return_event_queue = '15:13'
with stub_event_queue_user_events(return_event_queue, return_user_events):
result = self.client_post('/json/register',
dict(event_types=ujson.dumps(['pointer']),
fetch_event_types=ujson.dumps(['message'])),
**self.api_auth(email))
result = self.api_post(email, '/json/register',
dict(event_types=ujson.dumps(['pointer']),
fetch_event_types=ujson.dumps(['message'])))
self.assert_json_success(result)
result_dict = result.json()
self.assertEqual(result_dict['last_event_id'], 6)
@ -215,10 +211,9 @@ class EventsEndpointTest(ZulipTestCase):
# Now test with `fetch_event_types` matching the event
with stub_event_queue_user_events(return_event_queue, return_user_events):
result = self.client_post('/json/register',
dict(fetch_event_types=ujson.dumps(['pointer']),
event_types=ujson.dumps(['message'])),
**self.api_auth(email))
result = self.api_post(email, '/json/register',
dict(fetch_event_types=ujson.dumps(['pointer']),
event_types=ujson.dumps(['message'])))
self.assert_json_success(result)
result_dict = result.json()
self.assertEqual(result_dict['last_event_id'], 6)
@ -961,9 +956,8 @@ class EventsRegisterTest(ZulipTestCase):
])),
])),
])
self.client_post("/api/v1/users/me/presence", {'status': 'idle'},
HTTP_USER_AGENT="ZulipAndroid/1.0",
**self.api_auth(self.user_profile.email))
self.api_post(self.user_profile.email, "/api/v1/users/me/presence", {'status': 'idle'},
HTTP_USER_AGENT="ZulipAndroid/1.0")
self.do_test(lambda: do_update_user_presence(
self.user_profile, get_client("website"), timezone_now(), UserPresence.ACTIVE))
events = self.do_test(lambda: do_update_user_presence(

View File

@ -58,12 +58,11 @@ class RateLimitTests(ZulipTestCase):
remove_ratelimit_rule(1, 5)
def send_api_message(self, email: Text, content: Text) -> HttpResponse:
return self.client_post("/api/v1/messages", {"type": "stream",
"to": "Verona",
"client": "test suite",
"content": content,
"subject": "Test subject"},
**self.api_auth(email))
return self.api_post(email, "/api/v1/messages", {"type": "stream",
"to": "Verona",
"client": "test suite",
"content": content,
"subject": "Test subject"})
def test_headers(self) -> None:
user = self.example_user('hamlet')

View File

@ -666,24 +666,22 @@ class StreamMessagesTest(ZulipTestCase):
email = user_profile.email
do_change_is_admin(user_profile, True, 'api_super_user')
result = self.client_post("/api/v1/messages", {"type": "stream",
"to": "Verona",
"sender": self.example_email("cordelia"),
"client": "test suite",
"subject": "announcement",
"content": "Everyone knows Iago rules",
"forged": "true"},
**self.api_auth(email))
result = self.api_post(email, "/api/v1/messages", {"type": "stream",
"to": "Verona",
"sender": self.example_email("cordelia"),
"client": "test suite",
"subject": "announcement",
"content": "Everyone knows Iago rules",
"forged": "true"})
self.assert_json_success(result)
do_change_is_admin(user_profile, False, 'api_super_user')
result = self.client_post("/api/v1/messages", {"type": "stream",
"to": "Verona",
"sender": self.example_email("cordelia"),
"client": "test suite",
"subject": "announcement",
"content": "Everyone knows Iago rules",
"forged": "true"},
**self.api_auth(email))
result = self.api_post(email, "/api/v1/messages", {"type": "stream",
"to": "Verona",
"sender": self.example_email("cordelia"),
"client": "test suite",
"subject": "announcement",
"content": "Everyone knows Iago rules",
"forged": "true"})
self.assert_json_error(result, "User not authorized for this query")
def test_message_to_stream(self) -> None:
@ -914,12 +912,11 @@ class MessagePOSTTest(ZulipTestCase):
Same as above, but for the API view
"""
email = self.example_email("hamlet")
result = self.client_post("/api/v1/messages", {"type": "stream",
"to": "Verona",
"client": "test suite",
"content": "Test message",
"subject": "Test subject"},
**self.api_auth(email))
result = self.api_post(email, "/api/v1/messages", {"type": "stream",
"to": "Verona",
"client": "test suite",
"content": "Test message",
"subject": "Test subject"})
self.assert_json_success(result)
def test_api_message_with_default_to(self) -> None:
@ -931,11 +928,10 @@ class MessagePOSTTest(ZulipTestCase):
email = user_profile.email
user_profile.default_sending_stream_id = get_stream('Verona', user_profile.realm).id
user_profile.save()
result = self.client_post("/api/v1/messages", {"type": "stream",
"client": "test suite",
"content": "Test message no to",
"subject": "Test subject"},
**self.api_auth(email))
result = self.api_post(email, "/api/v1/messages", {"type": "stream",
"client": "test suite",
"content": "Test message no to",
"subject": "Test subject"})
self.assert_json_success(result)
sent_message = self.get_last_message()
@ -1267,15 +1263,13 @@ class MessagePOSTTest(ZulipTestCase):
user.save()
user = get_user(email, get_realm('zulip'))
self.subscribe(user, "IRCland")
result = self.client_post("/api/v1/messages",
{"type": "stream",
"forged": "true",
"sender": "irc-user@irc.zulip.com",
"content": "Test message",
"client": "irc_mirror",
"subject": "from irc",
"to": "IRCLand"},
**self.api_auth(email))
result = self.api_post(email, "/api/v1/messages", {"type": "stream",
"forged": "true",
"sender": "irc-user@irc.zulip.com",
"content": "Test message",
"client": "irc_mirror",
"subject": "from irc",
"to": "IRCLand"})
self.assert_json_success(result)
class EditMessageTest(ZulipTestCase):

View File

@ -62,7 +62,7 @@ class MutedTopicsTests(ZulipTestCase):
url = '/api/v1/users/me/subscriptions/muted_topics'
data = {'stream': 'Verona', 'topic': 'Verona3', 'op': 'add'}
result = self.client_patch(url, data, **self.api_auth(email))
result = self.api_patch(email, url, data)
self.assert_json_success(result)
user = self.example_user('hamlet')
@ -89,7 +89,7 @@ class MutedTopicsTests(ZulipTestCase):
url = '/api/v1/users/me/subscriptions/muted_topics'
data = {'stream': 'Verona', 'topic': 'vERONA3', 'op': 'remove'}
result = self.client_patch(url, data, **self.api_auth(email))
result = self.api_patch(email, url, data)
self.assert_json_success(result)
user = self.example_user('hamlet')
@ -112,7 +112,7 @@ class MutedTopicsTests(ZulipTestCase):
url = '/api/v1/users/me/subscriptions/muted_topics'
data = {'stream': 'Verona', 'topic': 'Verona3', 'op': 'add'}
result = self.client_patch(url, data, **self.api_auth(email))
result = self.api_patch(email, url, data)
self.assert_json_error(result, "Topic already muted")
def test_muted_topic_remove_invalid(self) -> None:
@ -122,9 +122,9 @@ class MutedTopicsTests(ZulipTestCase):
url = '/api/v1/users/me/subscriptions/muted_topics'
data = {'stream': 'BOGUS', 'topic': 'Verona3', 'op': 'remove'}
result = self.client_patch(url, data, **self.api_auth(email))
result = self.api_patch(email, url, data)
self.assert_json_error(result, "Topic is not there in the muted_topics list")
data = {'stream': 'Verona', 'topic': 'BOGUS', 'op': 'remove'}
result = self.client_patch(url, data, **self.api_auth(email))
result = self.api_patch(email, url, data)
self.assert_json_error(result, "Topic is not there in the muted_topics list")

View File

@ -236,9 +236,8 @@ class SingleUserPresenceTests(ZulipTestCase):
result = self.client_post("/json/users/me/presence", {'status': 'active'})
result = self.client_post("/json/users/me/presence", {'status': 'active'},
HTTP_USER_AGENT="ZulipDesktop/1.0")
result = self.client_post("/api/v1/users/me/presence", {'status': 'idle'},
HTTP_USER_AGENT="ZulipAndroid/1.0",
**self.api_auth(email))
result = self.api_post(email, "/api/v1/users/me/presence", {'status': 'idle'},
HTTP_USER_AGENT="ZulipAndroid/1.0")
self.assert_json_success(result)
# Check some error conditions
@ -287,13 +286,11 @@ class UserPresenceAggregationTests(ZulipTestCase):
with mock.patch(timezone_util, return_value=validate_time - datetime.timedelta(seconds=5)):
self.client_post("/json/users/me/presence", {'status': status})
with mock.patch(timezone_util, return_value=validate_time - datetime.timedelta(seconds=2)):
self.client_post("/api/v1/users/me/presence", {'status': status},
HTTP_USER_AGENT="ZulipAndroid/1.0",
**self.api_auth(email))
self.api_post(email, "/api/v1/users/me/presence", {'status': status},
HTTP_USER_AGENT="ZulipAndroid/1.0")
with mock.patch(timezone_util, return_value=validate_time - datetime.timedelta(seconds=7)):
latest_result = self.client_post("/api/v1/users/me/presence", {'status': status},
HTTP_USER_AGENT="ZulipIOS/1.0",
**self.api_auth(email))
latest_result = self.api_post(email, "/api/v1/users/me/presence", {'status': status},
HTTP_USER_AGENT="ZulipIOS/1.0")
latest_result_dict = latest_result.json()
self.assertDictEqual(
latest_result_dict['presences'][email]['aggregated'],
@ -312,9 +309,8 @@ class UserPresenceAggregationTests(ZulipTestCase):
self._send_presence_for_aggregated_tests(str(self.example_email("othello")), 'active', validate_time)
with mock.patch('zerver.views.presence.timezone_now',
return_value=validate_time - datetime.timedelta(seconds=1)):
result = self.client_post("/api/v1/users/me/presence", {'status': 'active'},
HTTP_USER_AGENT="ZulipTestDev/1.0",
**self.api_auth(email))
result = self.api_post(email, "/api/v1/users/me/presence", {'status': 'active'},
HTTP_USER_AGENT="ZulipTestDev/1.0")
result_dict = result.json()
self.assertDictEqual(
result_dict['presences'][email]['aggregated'],
@ -355,9 +351,8 @@ class UserPresenceAggregationTests(ZulipTestCase):
validate_time = timezone_now()
with mock.patch('zerver.views.presence.timezone_now',
return_value=validate_time - datetime.timedelta(seconds=3)):
self.client_post("/api/v1/users/me/presence", {'status': 'active'},
HTTP_USER_AGENT="ZulipTestDev/1.0",
**self.api_auth(email))
self.api_post(email, "/api/v1/users/me/presence", {'status': 'active'},
HTTP_USER_AGENT="ZulipTestDev/1.0")
result_dict = self._send_presence_for_aggregated_tests(str(email), 'idle', validate_time)
self.assertDictEqual(
result_dict['presence']['aggregated'],

View File

@ -106,8 +106,9 @@ class PushBouncerNotificationTest(BouncerTestCase):
realm.string_id = ""
realm.save()
result = self.client_post(endpoint, {'token': token, 'token_kind': token_kind},
**self.api_auth(self.example_email("hamlet"), realm=""))
result = self.api_post(self.example_email("hamlet"), endpoint, {'token': token,
'token_kind': token_kind},
realm="")
self.assert_json_error(result, "Must validate with valid Zulip server API key")
def test_register_remote_push_user_paramas(self) -> None:
@ -131,9 +132,9 @@ class PushBouncerNotificationTest(BouncerTestCase):
**self.get_auth())
self.assert_json_error(result, "Invalid token type")
result = self.client_post(endpoint, {'user_id': user_id, 'token_kind': token_kind,
'token': token},
**self.api_auth(self.example_email("hamlet")))
result = self.api_post(self.example_email("hamlet"), endpoint, {'user_id': user_id,
'token_kind': token_kind,
'token': token})
self.assert_json_error(result, "Account is not associated with this subdomain",
status_code=401)
@ -143,9 +144,9 @@ class PushBouncerNotificationTest(BouncerTestCase):
realm.string_id = ""
realm.save()
result = self.client_post(endpoint, {'user_id': user_id, 'token_kind': token_kind,
'token': token},
**self.api_auth(self.example_email("hamlet")))
result = self.api_post(self.example_email("hamlet"), endpoint, {'user_id': user_id,
'token_kind': token_kind,
'token': token})
self.assert_json_error(result, "Must validate with valid Zulip server API key")
def test_remote_push_user_endpoints(self) -> None:

View File

@ -18,8 +18,7 @@ class ReactionEmojiTest(ZulipTestCase):
Sending reaction without emoji fails
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/')
self.assertEqual(result.status_code, 400)
def test_add_invalid_emoji(self) -> None:
@ -27,8 +26,7 @@ class ReactionEmojiTest(ZulipTestCase):
Sending invalid emoji fails
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/foo',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/foo')
self.assert_json_error(result, "Emoji 'foo' does not exist")
def test_add_deactivated_realm_emoji(self) -> None:
@ -39,8 +37,7 @@ class ReactionEmojiTest(ZulipTestCase):
emoji.deactivated = True
emoji.save(update_fields=['deactivated'])
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/green_tick',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/green_tick')
self.assert_json_error(result, "Emoji 'green_tick' does not exist")
def test_valid_emoji(self) -> None:
@ -48,8 +45,7 @@ class ReactionEmojiTest(ZulipTestCase):
Reacting with valid emoji succeeds
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/smile',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/smile')
self.assert_json_success(result)
self.assertEqual(200, result.status_code)
@ -58,8 +54,7 @@ class ReactionEmojiTest(ZulipTestCase):
Reacting with zulip emoji succeeds
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/zulip',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/zulip')
self.assert_json_success(result)
self.assertEqual(200, result.status_code)
@ -79,8 +74,7 @@ class ReactionEmojiTest(ZulipTestCase):
message_id=message_id).exists())
# Have hamlet react to the message
result = self.client_put('/api/v1/messages/%s/emoji_reactions/smile' % (message_id,),
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/%s/emoji_reactions/smile' % (message_id,))
self.assert_json_success(result)
# Fetch the now-created UserMessage object to confirm it exists and is historical
@ -96,8 +90,7 @@ class ReactionEmojiTest(ZulipTestCase):
sender = self.example_email("hamlet")
emoji_name = 'green_tick'
result = self.client_put('/api/v1/messages/1/emoji_reactions/%s' % (emoji_name,),
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/%s' % (emoji_name,))
self.assert_json_success(result)
def test_emoji_name_to_emoji_code(self) -> None:
@ -161,8 +154,7 @@ class ReactionMessageIDTest(ZulipTestCase):
Reacting without a message_id fails
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages//emoji_reactions/smile',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages//emoji_reactions/smile')
self.assertEqual(result.status_code, 404)
def test_invalid_message_id(self) -> None:
@ -170,8 +162,7 @@ class ReactionMessageIDTest(ZulipTestCase):
Reacting to an invalid message id fails
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/-1/emoji_reactions/smile',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/-1/emoji_reactions/smile')
self.assertEqual(result.status_code, 404)
def test_inaccessible_message_id(self) -> None:
@ -182,14 +173,13 @@ class ReactionMessageIDTest(ZulipTestCase):
pm_recipient = self.example_email("othello")
reaction_sender = self.example_email("iago")
result = self.client_post("/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient},
**self.api_auth(pm_sender))
result = self.api_post(pm_sender,
"/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient})
self.assert_json_success(result)
pm_id = result.json()['id']
result = self.client_put('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender))
result = self.api_put(reaction_sender, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_error(result, "Invalid message(s)")
class ReactionTest(ZulipTestCase):
@ -201,19 +191,17 @@ class ReactionTest(ZulipTestCase):
pm_recipient = self.example_email("othello")
reaction_sender = pm_recipient
pm = self.client_post("/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient},
**self.api_auth(pm_sender))
pm = self.api_post(pm_sender,
"/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient})
self.assert_json_success(pm)
content = ujson.loads(pm.content)
pm_id = content['id']
first = self.client_put('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender))
first = self.api_put(reaction_sender, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_success(first)
second = self.client_put('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender))
second = self.api_put(reaction_sender, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_error(second, "Reaction already exists")
def test_remove_nonexisting_reaction(self) -> None:
@ -224,23 +212,20 @@ class ReactionTest(ZulipTestCase):
pm_recipient = self.example_email("othello")
reaction_sender = pm_recipient
pm = self.client_post("/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient},
**self.api_auth(pm_sender))
pm = self.api_post(pm_sender,
"/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient})
self.assert_json_success(pm)
content = ujson.loads(pm.content)
pm_id = content['id']
add = self.client_put('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender))
add = self.api_put(reaction_sender, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_success(add)
first = self.client_delete('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender))
first = self.api_delete(reaction_sender, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_success(first)
second = self.client_delete('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender))
second = self.api_delete(reaction_sender, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_error(second, "Reaction does not exist")
def test_remove_existing_reaction_with_renamed_emoji(self) -> None:
@ -249,13 +234,11 @@ class ReactionTest(ZulipTestCase):
various emoji infra changes.
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/smile',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/smile')
self.assert_json_success(result)
with mock.patch('zerver.lib.emoji.name_to_codepoint', name_to_codepoint={}):
result = self.client_delete('/api/v1/messages/1/emoji_reactions/smile',
**self.api_auth(sender))
result = self.api_delete(sender, '/api/v1/messages/1/emoji_reactions/smile')
self.assert_json_success(result)
def test_remove_existing_reaction_with_deactivated_realm_emoji(self) -> None:
@ -263,8 +246,7 @@ class ReactionTest(ZulipTestCase):
Removes an old existing reaction but the realm emoji used there has been deactivated.
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/green_tick',
**self.api_auth(sender))
result = self.api_put(sender, '/api/v1/messages/1/emoji_reactions/green_tick')
self.assert_json_success(result)
# Deactivate realm emoji.
@ -272,8 +254,7 @@ class ReactionTest(ZulipTestCase):
emoji.deactivated = True
emoji.save(update_fields=['deactivated'])
result = self.client_delete('/api/v1/messages/1/emoji_reactions/green_tick',
**self.api_auth(sender))
result = self.api_delete(sender, '/api/v1/messages/1/emoji_reactions/green_tick')
self.assert_json_success(result)
class ReactionEventTest(ZulipTestCase):
@ -286,10 +267,10 @@ class ReactionEventTest(ZulipTestCase):
pm_recipient = self.example_user('othello')
reaction_sender = pm_recipient
result = self.client_post("/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient.email},
**self.api_auth(pm_sender.email))
result = self.api_post(pm_sender.email,
"/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient.email})
self.assert_json_success(result)
pm_id = result.json()['id']
@ -297,8 +278,7 @@ class ReactionEventTest(ZulipTestCase):
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_put('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender.email))
result = self.api_put(reaction_sender.email, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_success(result)
self.assertEqual(len(events), 1)
@ -321,24 +301,22 @@ class ReactionEventTest(ZulipTestCase):
pm_recipient = self.example_user('othello')
reaction_sender = pm_recipient
result = self.client_post("/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient.email},
**self.api_auth(pm_sender.email))
result = self.api_post(pm_sender.email,
"/api/v1/messages", {"type": "private",
"content": "Test message",
"to": pm_recipient.email})
self.assert_json_success(result)
content = result.json()
pm_id = content['id']
expected_recipient_ids = set([pm_sender.id, pm_recipient.id])
add = self.client_put('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender.email))
add = self.api_put(reaction_sender.email, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_success(add)
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_delete('/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,),
**self.api_auth(reaction_sender.email))
result = self.api_delete(reaction_sender.email, '/api/v1/messages/%s/emoji_reactions/smile' % (pm_id,))
self.assert_json_success(result)
self.assertEqual(len(events), 1)
@ -362,9 +340,8 @@ class EmojiReactionBase(ZulipTestCase):
if 'reaction_type' not in reaction_info:
reaction_info['reaction_type'] = self.reaction_type
sender = self.example_email(sender)
result = self.client_post('/api/v1/messages/%s/reactions' % (message_id,),
reaction_info,
**self.api_auth(sender))
result = self.api_post(sender, '/api/v1/messages/%s/reactions' % (message_id,),
reaction_info)
return result
def post_zulip_reaction(self, message_id: int=1, sender: str='hamlet') -> HttpResponse:
@ -382,9 +359,8 @@ class EmojiReactionBase(ZulipTestCase):
if 'reaction_type' not in reaction_info:
reaction_info['reaction_type'] = self.reaction_type
sender = self.example_email(sender)
result = self.client_delete('/api/v1/messages/%s/reactions' % (message_id,),
reaction_info,
**self.api_auth(sender))
result = self.api_delete(sender, '/api/v1/messages/%s/reactions' % (message_id,),
reaction_info)
return result
def delete_zulip_reaction(self, message_id: int=1, sender: str='hamlet') -> HttpResponse:
@ -693,9 +669,8 @@ class RealmEmojiReactionTests(EmojiReactionBase):
}
sender = self.example_email("hamlet")
message_id = 1
result = self.client_post('/api/v1/messages/%s/reactions' % (message_id,),
reaction_info,
**self.api_auth(sender))
result = self.api_post(sender, '/api/v1/messages/%s/reactions' % (message_id,),
reaction_info)
self.assert_json_error(result, "Invalid emoji type.")
class ReactionAPIEventTest(EmojiReactionBase):

View File

@ -1037,13 +1037,10 @@ class SubscriptionPropertiesTest(ZulipTestCase):
sub = old_subs[0]
stream_id = sub['stream_id']
new_color = "#ffffff" # TODO: ensure that this is different from old_color
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"stream_id": stream_id,
"value": "#ffffff"}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"stream_id": stream_id,
"value": "#ffffff"}])})
self.assert_json_success(result)
new_subs = gather_subscriptions(get_user(test_email, test_realm))[0]
@ -1071,12 +1068,9 @@ class SubscriptionPropertiesTest(ZulipTestCase):
test_user = self.example_user('hamlet')
test_email = test_user.email
self.login(test_email)
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"value": "#ffffff"}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"value": "#ffffff"}])})
self.assert_json_error(
result, "stream_id key is missing from subscription_data[0]")
@ -1091,12 +1085,10 @@ class SubscriptionPropertiesTest(ZulipTestCase):
subscribed, unsubscribed, never_subscribed = gather_subscriptions_helper(
get_user(test_email, test_realm))
not_subbed = unsubscribed + never_subscribed
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"stream_id": not_subbed[0]["stream_id"],
"value": "#ffffff"}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"stream_id": not_subbed[0]["stream_id"],
"value": "#ffffff"}])})
self.assert_json_error(
result, "Not subscribed to stream id %d" % (not_subbed[0]["stream_id"],))
@ -1108,12 +1100,9 @@ class SubscriptionPropertiesTest(ZulipTestCase):
test_email = test_user.email
self.login(test_email)
subs = gather_subscriptions(test_user)[0]
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"stream_id": subs[0]["stream_id"]}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "color",
"stream_id": subs[0]["stream_id"]}])})
self.assert_json_error(
result, "value key is missing from subscription_data[0]")
@ -1130,13 +1119,10 @@ class SubscriptionPropertiesTest(ZulipTestCase):
sub = old_subs[0]
stream_id = sub['stream_id']
new_pin_to_top = not sub['pin_to_top']
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "pin_to_top",
"stream_id": stream_id,
"value": new_pin_to_top}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "pin_to_top",
"stream_id": stream_id,
"value": new_pin_to_top}])})
self.assert_json_success(result)
updated_sub = get_subscription(sub['name'], user_profile)
@ -1154,57 +1140,42 @@ class SubscriptionPropertiesTest(ZulipTestCase):
subs = gather_subscriptions(test_user)[0]
property_name = "in_home_view"
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])})
self.assert_json_error(result,
'%s is not a boolean' % (property_name,))
property_name = "desktop_notifications"
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])})
self.assert_json_error(result,
'%s is not a boolean' % (property_name,))
property_name = "audible_notifications"
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])})
self.assert_json_error(result,
'%s is not a boolean' % (property_name,))
property_name = "push_notifications"
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": "bad",
"stream_id": subs[0]["stream_id"]}])})
self.assert_json_error(result,
'%s is not a boolean' % (property_name,))
property_name = "color"
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": False,
"stream_id": subs[0]["stream_id"]}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": property_name,
"value": False,
"stream_id": subs[0]["stream_id"]}])})
self.assert_json_error(result,
'%s is not a string' % (property_name,))
@ -1213,12 +1184,10 @@ class SubscriptionPropertiesTest(ZulipTestCase):
self.login(test_email)
stream_id = 1000
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "in_home_view",
"stream_id": stream_id,
"value": False}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "in_home_view",
"stream_id": stream_id,
"value": False}])})
self.assert_json_error(result, "Invalid stream id")
def test_set_invalid_property(self) -> None:
@ -1229,13 +1198,10 @@ class SubscriptionPropertiesTest(ZulipTestCase):
test_email = test_user.email
self.login(test_email)
subs = gather_subscriptions(test_user)[0]
result = self.client_post(
"/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "bad",
"value": "bad",
"stream_id": subs[0]["stream_id"]}])},
**self.api_auth(test_email))
result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties",
{"subscription_data": ujson.dumps([{"property": "bad",
"value": "bad",
"stream_id": subs[0]["stream_id"]}])})
self.assert_json_error(result,
"Unknown subscription property: bad")
@ -1249,11 +1215,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'add': ujson.dumps([{'name': 'my_test_stream_1'}])
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_success(result)
streams = self.get_streams(email, realm)
self.assertTrue('my_test_stream_1' in streams)
@ -1262,11 +1224,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'delete': ujson.dumps(['my_test_stream_1'])
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_success(result)
streams = self.get_streams(email, realm)
self.assertTrue('my_test_stream_1' not in streams)
@ -1280,10 +1238,8 @@ class SubscriptionRestApiTest(ZulipTestCase):
self.login(test_email)
subs = gather_subscriptions(test_user)[0]
result = self.client_patch(
"/api/v1/users/me/subscriptions/%d" % subs[0]["stream_id"],
{'property': 'color', 'value': '#c2c2c2'},
**self.api_auth(test_email))
result = self.api_patch(test_email, "/api/v1/users/me/subscriptions/%d" % subs[0]["stream_id"],
{'property': 'color', 'value': '#c2c2c2'})
self.assert_json_success(result)
def test_api_invalid_property(self) -> None:
@ -1297,10 +1253,8 @@ class SubscriptionRestApiTest(ZulipTestCase):
self.login(test_email)
subs = gather_subscriptions(test_user)[0]
result = self.client_patch(
"/api/v1/users/me/subscriptions/%d" % subs[0]["stream_id"],
{'property': 'invalid', 'value': 'somevalue'},
**self.api_auth(test_email))
result = self.api_patch(test_email, "/api/v1/users/me/subscriptions/%d" % subs[0]["stream_id"],
{'property': 'invalid', 'value': 'somevalue'})
self.assert_json_error(result,
"Unknown subscription property: invalid")
@ -1310,10 +1264,8 @@ class SubscriptionRestApiTest(ZulipTestCase):
"""
test_email = self.example_email("hamlet")
self.login(test_email)
result = self.client_patch(
"/api/v1/users/me/subscriptions/121",
{'property': 'in_home_view', 'value': 'somevalue'},
**self.api_auth(test_email))
result = self.api_patch(test_email, "/api/v1/users/me/subscriptions/121",
{'property': 'in_home_view', 'value': 'somevalue'})
self.assert_json_error(result,
"Invalid stream id")
@ -1325,11 +1277,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'add': ujson.dumps(val)
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_error(result, expected_message)
check_for_error(['foo'], 'add[0] is not a dict')
@ -1344,11 +1292,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
'add': ujson.dumps([{'name': 'my_new_stream'}]),
'principals': ujson.dumps([{}]),
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_error(result, 'principals[0] is not a string')
def test_bad_delete_parameters(self) -> None:
@ -1358,22 +1302,14 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'delete': ujson.dumps([{'name': 'my_test_stream_1'}])
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_error(result, "delete[0] is not a string")
def test_add_or_delete_not_specified(self) -> None:
email = self.example_email('hamlet')
self.login(email)
result = self.client_patch(
"/api/v1/users/me/subscriptions",
{},
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", {})
self.assert_json_error(result,
'Nothing to do. Specify at least one of "add" or "delete".')
@ -1388,11 +1324,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'delete': ujson.dumps([invalid_stream_name])
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_error(result,
"Invalid stream name '%s'" % (invalid_stream_name,))
@ -1404,11 +1336,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'delete': ujson.dumps([long_stream_name])
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_error(result,
"Stream name too long (limit: 60 characters)")
@ -1420,11 +1348,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'delete': ujson.dumps([stream_name])
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_error(result,
"Invalid characters in stream name (disallowed characters: *, @, `, #).")
@ -1436,11 +1360,7 @@ class SubscriptionRestApiTest(ZulipTestCase):
request = {
'delete': ujson.dumps([stream_name])
}
result = self.client_patch(
"/api/v1/users/me/subscriptions",
request,
**self.api_auth(email)
)
result = self.api_patch(email, "/api/v1/users/me/subscriptions", request)
self.assert_json_error(result,
"Stream name '%s' contains NULL (0x00) characters." % (stream_name))
@ -1504,7 +1424,7 @@ class SubscriptionAPITest(ZulipTestCase):
Calling /api/v1/users/me/subscriptions should successfully return your subscriptions.
"""
email = self.test_email
result = self.client_get("/api/v1/users/me/subscriptions", **self.api_auth(email))
result = self.api_get(email, "/api/v1/users/me/subscriptions")
self.assert_json_success(result)
json = result.json()
self.assertIn("subscriptions", json)
@ -2385,8 +2305,8 @@ class GetPublicStreamsTest(ZulipTestCase):
self.login(email)
# Check it correctly lists the user's subs with include_public=false
result = self.client_get("/api/v1/streams?include_public=false", **self.api_auth(email))
result2 = self.client_get("/api/v1/users/me/subscriptions", **self.api_auth(email))
result = self.api_get(email, "/api/v1/streams?include_public=false")
result2 = self.api_get(email, "/api/v1/users/me/subscriptions")
self.assert_json_success(result)
json = result.json()
@ -2402,8 +2322,7 @@ class GetPublicStreamsTest(ZulipTestCase):
sorted([s["name"] for s in json2["subscriptions"]]))
# Check it correctly lists all public streams with include_subscribed=false
result = self.client_get("/api/v1/streams?include_public=true&include_subscribed=false",
**self.api_auth(email))
result = self.api_get(email, "/api/v1/streams?include_public=true&include_subscribed=false")
self.assert_json_success(result)
json = result.json()
@ -2413,8 +2332,7 @@ class GetPublicStreamsTest(ZulipTestCase):
sorted(all_streams))
# Check non-superuser can't use include_all_active
result = self.client_get("/api/v1/streams?include_all_active=true",
**self.api_auth(email))
result = self.api_get(email, "/api/v1/streams?include_all_active=true")
self.assertEqual(result.status_code, 400)
class StreamIdTest(ZulipTestCase):
@ -2461,7 +2379,7 @@ class InviteOnlyStreamTest(ZulipTestCase):
self.assert_json_success(result1)
result2 = self.common_subscribe_to_streams(email, ["Normandy"], invite_only=False)
self.assert_json_success(result2)
result = self.client_get("/api/v1/users/me/subscriptions", **self.api_auth(email))
result = self.api_get(email, "/api/v1/users/me/subscriptions")
self.assert_json_success(result)
self.assertIn("subscriptions", result.json())
for sub in result.json()["subscriptions"]:
@ -2517,8 +2435,7 @@ class InviteOnlyStreamTest(ZulipTestCase):
# Make sure both users are subscribed to this stream
stream_id = get_stream(stream_name, user_profile.realm).id
result = self.client_get("/api/v1/streams/%d/members" % (stream_id,),
**self.api_auth(email))
result = self.api_get(email, "/api/v1/streams/%d/members" % (stream_id,))
self.assert_json_success(result)
json = result.json()
@ -2561,8 +2478,7 @@ class GetSubscribersTest(ZulipTestCase):
def make_subscriber_request(self, stream_id: int, email: Optional[Text]=None) -> HttpResponse:
if email is None:
email = self.email
return self.client_get("/api/v1/streams/%d/members" % (stream_id,),
**self.api_auth(email))
return self.api_get(email, "/api/v1/streams/%d/members" % (stream_id,))
def make_successful_subscriber_request(self, stream_name: Text) -> None:
stream_id = get_stream(stream_name, self.user_profile.realm).id

View File

@ -16,8 +16,7 @@ class TypingNotificationOperatorTest(ZulipTestCase):
"""
sender = self.example_email("hamlet")
recipient = self.example_email("othello")
result = self.client_post('/api/v1/typing', {'to': recipient},
**self.api_auth(sender))
result = self.api_post(sender, '/api/v1/typing', {'to': recipient})
self.assert_json_error(result, 'Missing \'op\' argument')
def test_invalid_parameter(self) -> None:
@ -26,8 +25,7 @@ class TypingNotificationOperatorTest(ZulipTestCase):
"""
sender = self.example_email("hamlet")
recipient = self.example_email("othello")
result = self.client_post('/api/v1/typing', {'to': recipient, 'op': 'foo'},
**self.api_auth(sender))
result = self.api_post(sender, '/api/v1/typing', {'to': recipient, 'op': 'foo'})
self.assert_json_error(result, 'Invalid \'op\' value (should be start or stop)')
class TypingNotificationRecipientsTest(ZulipTestCase):
@ -36,8 +34,7 @@ class TypingNotificationRecipientsTest(ZulipTestCase):
Sending typing notification without recipient fails
"""
sender = self.example_email("hamlet")
result = self.client_post('/api/v1/typing', {'op': 'start'},
**self.api_auth(sender))
result = self.api_post(sender, '/api/v1/typing', {'op': 'start'})
self.assert_json_error(result, 'Missing parameter: \'to\' (recipient)')
def test_invalid_recipient(self) -> None:
@ -46,8 +43,7 @@ class TypingNotificationRecipientsTest(ZulipTestCase):
"""
sender = self.example_email("hamlet")
invalid = 'invalid email'
result = self.client_post('/api/v1/typing', {'op': 'start', 'to': invalid},
**self.api_auth(sender))
result = self.api_post(sender, '/api/v1/typing', {'op': 'start', 'to': invalid})
self.assert_json_error(result, 'Invalid email \'' + invalid + '\'')
def test_single_recipient(self) -> None:
@ -62,9 +58,8 @@ class TypingNotificationRecipientsTest(ZulipTestCase):
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post('/api/v1/typing', {'to': recipient.email,
'op': 'start'},
**self.api_auth(sender.email))
result = self.api_post(sender.email, '/api/v1/typing', {'to': recipient.email,
'op': 'start'})
self.assert_json_success(result)
self.assertEqual(len(events), 1)
@ -91,9 +86,9 @@ class TypingNotificationRecipientsTest(ZulipTestCase):
expected_recipient_ids = set([user.id for user in expected_recipients])
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post('/api/v1/typing', {'to': ujson.dumps([user.email for user in recipient]),
'op': 'start'},
**self.api_auth(sender.email))
result = self.api_post(sender.email, '/api/v1/typing',
{'to': ujson.dumps([user.email for user in recipient]),
'op': 'start'})
self.assert_json_success(result)
self.assertEqual(len(events), 1)
@ -121,9 +116,8 @@ class TypingStartedNotificationTest(ZulipTestCase):
expected_recipient_ids = set([user.id])
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post('/api/v1/typing', {'to': email,
'op': 'start'},
**self.api_auth(email))
result = self.api_post(email, '/api/v1/typing', {'to': email,
'op': 'start'})
self.assert_json_success(result)
self.assertEqual(len(events), 1)
@ -152,9 +146,8 @@ class TypingStartedNotificationTest(ZulipTestCase):
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post('/api/v1/typing', {'to': recipient.email,
'op': 'start'},
**self.api_auth(sender.email))
result = self.api_post(sender.email, '/api/v1/typing', {'to': recipient.email,
'op': 'start'})
self.assert_json_success(result)
self.assertEqual(len(events), 1)
@ -183,9 +176,8 @@ class StoppedTypingNotificationTest(ZulipTestCase):
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post('/api/v1/typing', {'to': email,
'op': 'stop'},
**self.api_auth(email))
result = self.api_post(email, '/api/v1/typing', {'to': email,
'op': 'stop'})
self.assert_json_success(result)
self.assertEqual(len(events), 1)
@ -214,9 +206,8 @@ class StoppedTypingNotificationTest(ZulipTestCase):
events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post('/api/v1/typing', {'to': recipient.email,
'op': 'stop'},
**self.api_auth(sender.email))
result = self.api_post(sender.email, '/api/v1/typing', {'to': recipient.email,
'op': 'stop'})
self.assert_json_success(result)
self.assertEqual(len(events), 1)

View File

@ -54,8 +54,7 @@ class PointerTest(ZulipTestCase):
email = user.email
self.assertEqual(user.pointer, -1)
msg_id = self.send_stream_message(self.example_email("othello"), "Verona")
result = self.client_post("/api/v1/users/me/pointer", {"pointer": msg_id},
**self.api_auth(email))
result = self.api_post(email, "/api/v1/users/me/pointer", {"pointer": msg_id})
self.assert_json_success(result)
self.assertEqual(get_user(email, user.realm).pointer, msg_id)

View File

@ -65,8 +65,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
fp.name = "zulip.txt"
# Upload file via API
auth_headers = self.api_auth(self.example_email("hamlet"))
result = self.client_post('/api/v1/user_uploads', {'file': fp}, **auth_headers)
result = self.api_post(self.example_email("hamlet"), '/api/v1/user_uploads', {'file': fp})
self.assertIn("uri", result.json())
uri = result.json()['uri']
base = '/user_uploads/'
@ -74,7 +73,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
# Download file via API
self.logout()
response = self.client_get(uri, **auth_headers)
response = self.api_get(self.example_email("hamlet"), uri)
data = b"".join(response.streaming_content)
self.assertEqual(b"zulip!", data)

View File

@ -525,7 +525,7 @@ class GetProfileTest(ZulipTestCase):
user_profile = self.example_user(user_id)
self.send_stream_message(user_profile.email, "Verona", "hello")
result = self.client_get("/api/v1/users/me", **self.api_auth(user_profile.email))
result = self.api_get(user_profile.email, "/api/v1/users/me")
max_id = most_recent_message(user_profile).id
@ -607,7 +607,7 @@ class GetProfileTest(ZulipTestCase):
def test_get_all_profiles_avatar_urls(self) -> None:
user_profile = self.example_user('hamlet')
result = self.client_get("/api/v1/users", **self.api_auth(self.example_email("hamlet")))
result = self.api_get(self.example_email("hamlet"), "/api/v1/users")
self.assert_json_success(result)
for user in result.json()['members']: