tests: Pass a realm option to login for non-zulip realms.

This better matches the model of how having multiple realms should
work: you need to specify which realm you're logging into.
This commit is contained in:
Tim Abbott 2017-11-17 15:11:24 -08:00
parent 1e334f41ed
commit 1ed50ee858
8 changed files with 54 additions and 35 deletions

View File

@ -284,14 +284,18 @@ class ZulipTestCase(TestCase):
{'username': email, 'password': password}, {'username': email, 'password': password},
**kwargs) **kwargs)
def login(self, email, password=None, fails=False): def login(self, email, password=None, fails=False, realm=None):
# type: (Text, Optional[Text], bool) -> HttpResponse # type: (Text, Optional[Text], bool, Optional[Realm]) -> HttpResponse
if realm is None:
realm = get_realm("zulip")
if password is None: if password is None:
password = initial_password(email) password = initial_password(email)
if not fails: if not fails:
self.assertTrue(self.client.login(username=email, password=password)) self.assertTrue(self.client.login(username=email, password=password,
realm_subdomain=realm.subdomain))
else: else:
self.assertFalse(self.client.login(username=email, password=password)) self.assertFalse(self.client.login(username=email, password=password,
realm_subdomain=realm.subdomain))
def logout(self): def logout(self):
# type: () -> None # type: () -> None

View File

@ -1050,7 +1050,7 @@ class MessagePOSTTest(ZulipTestCase):
""" """
Sending a mirrored huddle message works Sending a mirrored huddle message works
""" """
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
result = self.client_post("/json/messages", {"type": "private", result = self.client_post("/json/messages", {"type": "private",
"sender": self.mit_email("sipbtest"), "sender": self.mit_email("sipbtest"),
"content": "Test message", "content": "Test message",
@ -1064,7 +1064,7 @@ class MessagePOSTTest(ZulipTestCase):
""" """
Sending a mirrored personal message works Sending a mirrored personal message works
""" """
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
result = self.client_post("/json/messages", {"type": "private", result = self.client_post("/json/messages", {"type": "private",
"sender": self.mit_email("sipbtest"), "sender": self.mit_email("sipbtest"),
"content": "Test message", "content": "Test message",
@ -1077,7 +1077,7 @@ class MessagePOSTTest(ZulipTestCase):
""" """
Sending a mirrored personal message to someone else is not allowed. Sending a mirrored personal message to someone else is not allowed.
""" """
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
result = self.client_post("/json/messages", {"type": "private", result = self.client_post("/json/messages", {"type": "private",
"sender": self.mit_email("sipbtest"), "sender": self.mit_email("sipbtest"),
"content": "Test message", "content": "Test message",
@ -1098,11 +1098,11 @@ class MessagePOSTTest(ZulipTestCase):
self.mit_email("starnine")])} self.mit_email("starnine")])}
with mock.patch('DNS.dnslookup', return_value=[['starnine:*:84233:101:Athena Consulting Exchange User,,,:/mit/starnine:/bin/bash']]): with mock.patch('DNS.dnslookup', return_value=[['starnine:*:84233:101:Athena Consulting Exchange User,,,:/mit/starnine:/bin/bash']]):
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
result1 = self.client_post("/json/messages", msg, result1 = self.client_post("/json/messages", msg,
subdomain="zephyr") subdomain="zephyr")
with mock.patch('DNS.dnslookup', return_value=[['espuser:*:95494:101:Esp Classroom,,,:/mit/espuser:/bin/athena/bash']]): with mock.patch('DNS.dnslookup', return_value=[['espuser:*:95494:101:Esp Classroom,,,:/mit/espuser:/bin/athena/bash']]):
self.login(self.mit_email("espuser")) self.login(self.mit_email("espuser"), realm=get_realm("zephyr"))
result2 = self.client_post("/json/messages", msg, result2 = self.client_post("/json/messages", msg,
subdomain="zephyr") subdomain="zephyr")
self.assertEqual(ujson.loads(result1.content)['id'], self.assertEqual(ujson.loads(result1.content)['id'],
@ -1200,7 +1200,7 @@ class MessagePOSTTest(ZulipTestCase):
self.assert_json_error(result, "Unknown realm non-existing") self.assert_json_error(result, "Unknown realm non-existing")
def test_send_message_when_sender_is_not_set(self) -> None: def test_send_message_when_sender_is_not_set(self) -> None:
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
result = self.client_post("/json/messages", {"type": "private", result = self.client_post("/json/messages", {"type": "private",
"content": "Test message", "content": "Test message",
"client": "zephyr_mirror", "client": "zephyr_mirror",
@ -1209,7 +1209,7 @@ class MessagePOSTTest(ZulipTestCase):
self.assert_json_error(result, "Missing sender") self.assert_json_error(result, "Missing sender")
def test_send_message_as_not_superuser_when_type_is_not_private(self) -> None: def test_send_message_as_not_superuser_when_type_is_not_private(self) -> None:
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
result = self.client_post("/json/messages", {"type": "not-private", result = self.client_post("/json/messages", {"type": "not-private",
"sender": self.mit_email("sipbtest"), "sender": self.mit_email("sipbtest"),
"content": "Test message", "content": "Test message",
@ -1222,7 +1222,7 @@ class MessagePOSTTest(ZulipTestCase):
def test_send_message_create_mirrored_message_user_returns_invalid_input( def test_send_message_create_mirrored_message_user_returns_invalid_input(
self, create_mirrored_message_users_mock: Any) -> None: self, create_mirrored_message_users_mock: Any) -> None:
create_mirrored_message_users_mock.return_value = (False, True) create_mirrored_message_users_mock.return_value = (False, True)
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
result = self.client_post("/json/messages", {"type": "private", result = self.client_post("/json/messages", {"type": "private",
"sender": self.mit_email("sipbtest"), "sender": self.mit_email("sipbtest"),
"content": "Test message", "content": "Test message",
@ -1239,7 +1239,7 @@ class MessagePOSTTest(ZulipTestCase):
email = user.email email = user.email
user.realm.string_id = 'notzephyr' user.realm.string_id = 'notzephyr'
user.realm.save() user.realm.save()
self.login(email) self.login(email, realm=get_realm("notzephyr"))
result = self.client_post("/json/messages", {"type": "private", result = self.client_post("/json/messages", {"type": "private",
"sender": self.mit_email("sipbtest"), "sender": self.mit_email("sipbtest"),
"content": "Test message", "content": "Test message",
@ -1353,7 +1353,7 @@ class EditMessageTest(ZulipTestCase):
result = self.client_get('/json/messages/' + str(msg_id)) result = self.client_get('/json/messages/' + str(msg_id))
self.assert_json_success(result) self.assert_json_success(result)
self.login(self.mit_email("sipbtest")) self.login(self.mit_email("sipbtest"), realm=get_realm("zephyr"))
result = self.client_get('/json/messages/' + str(msg_id), subdomain="zephyr") result = self.client_get('/json/messages/' + str(msg_id), subdomain="zephyr")
self.assert_json_error(result, 'Invalid message(s)') self.assert_json_error(result, 'Invalid message(s)')
@ -2066,7 +2066,7 @@ class StarTests(ZulipTestCase):
self.assert_json_success(result) self.assert_json_success(result)
# But it still doesn't work if you're in another realm # But it still doesn't work if you're in another realm
self.login(self.mit_email("sipbtest")) self.login(self.mit_email("sipbtest"), realm=get_realm("zephyr"))
result = self.change_star(message_ids, subdomain="zephyr") result = self.change_star(message_ids, subdomain="zephyr")
self.assert_json_error(result, 'Invalid message(s)') self.assert_json_error(result, 'Invalid message(s)')

View File

@ -683,7 +683,7 @@ class GetOldMessagesTest(ZulipTestCase):
A request for old messages for a user in the mit.edu relam with unicode A request for old messages for a user in the mit.edu relam with unicode
stream name should be correctly escaped in the database query. stream name should be correctly escaped in the database query.
""" """
self.login(self.mit_email("starnine")) self.login(self.mit_email("starnine"), realm=get_realm("zephyr"))
# We need to susbcribe to a stream and then send a message to # We need to susbcribe to a stream and then send a message to
# it to ensure that we actually have a stream message in this # it to ensure that we actually have a stream message in this
# narrow view. # narrow view.
@ -718,7 +718,7 @@ class GetOldMessagesTest(ZulipTestCase):
""" """
mit_user_profile = self.mit_user("starnine") mit_user_profile = self.mit_user("starnine")
email = mit_user_profile.email email = mit_user_profile.email
self.login(email) self.login(email, realm=get_realm("zephyr"))
# We need to susbcribe to a stream and then send a message to # We need to susbcribe to a stream and then send a message to
# it to ensure that we actually have a stream message in this # it to ensure that we actually have a stream message in this
# narrow view. # narrow view.
@ -754,9 +754,11 @@ class GetOldMessagesTest(ZulipTestCase):
""" """
mit_user_profile = self.mit_user("starnine") mit_user_profile = self.mit_user("starnine")
email = mit_user_profile.email email = mit_user_profile.email
self.login(email) # We need to susbcribe to a stream and then send a message to
# We need to susbcribe to a stream and then send a message to
# it to ensure that we actually have a stream message in this # it to ensure that we actually have a stream message in this
# narrow view. # narrow view.
self.login(email, realm=mit_user_profile.realm)
self.subscribe(mit_user_profile, "Scotland") self.subscribe(mit_user_profile, "Scotland")
self.send_stream_message(email, "Scotland", self.send_stream_message(email, "Scotland",

View File

@ -23,6 +23,7 @@ from zerver.models import (
UserProfile, UserProfile,
UserPresence, UserPresence,
flush_per_request_caches, flush_per_request_caches,
get_realm,
) )
import datetime import datetime
@ -172,7 +173,7 @@ class UserPresenceTests(ZulipTestCase):
def test_no_mit(self) -> None: def test_no_mit(self) -> None:
"""Zephyr mirror realms such as MIT never get a list of users""" """Zephyr mirror realms such as MIT never get a list of users"""
self.login(self.mit_email("espuser")) self.login(self.mit_email("espuser"), realm=get_realm("zephyr"))
result = self.client_post("/json/users/me/presence", {'status': 'idle'}, result = self.client_post("/json/users/me/presence", {'status': 'idle'},
subdomain="zephyr") subdomain="zephyr")
self.assert_json_success(result) self.assert_json_success(result)
@ -182,7 +183,7 @@ class UserPresenceTests(ZulipTestCase):
"""Zephyr mirror realms find out the status of their mirror bot""" """Zephyr mirror realms find out the status of their mirror bot"""
user_profile = self.mit_user('espuser') user_profile = self.mit_user('espuser')
email = user_profile.email email = user_profile.email
self.login(email) self.login(email, realm=user_profile.realm)
def post_presence() -> Dict[str, Any]: def post_presence() -> Dict[str, Any]:
result = self.client_post("/json/users/me/presence", {'status': 'idle'}, result = self.client_post("/json/users/me/presence", {'status': 'idle'},
@ -211,7 +212,7 @@ class UserPresenceTests(ZulipTestCase):
) )
def test_same_realm(self) -> None: def test_same_realm(self) -> None:
self.login(self.mit_email("espuser")) self.login(self.mit_email("espuser"), realm=get_realm("zephyr"))
self.client_post("/json/users/me/presence", {'status': 'idle'}, self.client_post("/json/users/me/presence", {'status': 'idle'},
subdomain="zephyr") subdomain="zephyr")
self.logout() self.logout()
@ -254,7 +255,7 @@ class SingleUserPresenceTests(ZulipTestCase):
result = self.client_get("/json/users/new-user-bot@zulip.com/presence") result = self.client_get("/json/users/new-user-bot@zulip.com/presence")
self.assert_json_error(result, "Presence is not supported for bot users.") self.assert_json_error(result, "Presence is not supported for bot users.")
self.login(self.mit_email("sipbtest")) self.login(self.mit_email("sipbtest"), realm=get_realm("zephyr"))
result = self.client_get("/json/users/othello@zulip.com/presence", result = self.client_get("/json/users/othello@zulip.com/presence",
subdomain="zephyr") subdomain="zephyr")
self.assert_json_error(result, "No such user") self.assert_json_error(result, "No such user")

View File

@ -55,7 +55,7 @@ class RealmDomainTest(ZulipTestCase):
self.assert_json_error(result, 'The domain acme.com is already a part of your organization.') self.assert_json_error(result, 'The domain acme.com is already a part of your organization.')
mit_user_profile = self.mit_user("sipbtest") mit_user_profile = self.mit_user("sipbtest")
self.login(mit_user_profile.email) self.login(mit_user_profile.email, realm=get_realm("zephyr"))
do_change_is_admin(mit_user_profile, True) do_change_is_admin(mit_user_profile, True)

View File

@ -10,7 +10,7 @@ from zerver.lib.sessions import (
) )
from zerver.models import ( from zerver.models import (
UserProfile, get_user_profile_by_id, get_realm UserProfile, get_user_profile_by_id, get_realm, Realm
) )
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
@ -20,12 +20,13 @@ class TestSessions(ZulipTestCase):
def do_test_session(self, user: Text, def do_test_session(self, user: Text,
action: Callable[[], Any], action: Callable[[], Any],
realm: Realm,
expected_result: bool) -> None: expected_result: bool) -> None:
self.login(user) self.login(user, realm=realm)
self.assertIn('_auth_user_id', self.client.session) self.assertIn('_auth_user_id', self.client.session)
action() action()
if expected_result: if expected_result:
result = self.client_get('/') result = self.client_get('/', subdomain=realm.subdomain)
self.assertEqual('/login', result.url) self.assertEqual('/login', result.url)
else: else:
self.assertIn('_auth_user_id', self.client.session) self.assertIn('_auth_user_id', self.client.session)
@ -43,17 +44,28 @@ class TestSessions(ZulipTestCase):
def test_delete_user_sessions(self) -> None: def test_delete_user_sessions(self) -> None:
user_profile = self.example_user('hamlet') user_profile = self.example_user('hamlet')
email = user_profile.email email = user_profile.email
self.do_test_session(str(email), lambda: delete_user_sessions(user_profile), True) self.do_test_session(str(email), lambda: delete_user_sessions(user_profile),
self.do_test_session(str(self.example_email("othello")), lambda: delete_user_sessions(user_profile), False) get_realm("zulip"), True)
self.do_test_session(str(self.example_email("othello")),
lambda: delete_user_sessions(user_profile),
get_realm("zulip"), False)
def test_delete_realm_user_sessions(self) -> None: def test_delete_realm_user_sessions(self) -> None:
realm = get_realm('zulip') realm = get_realm('zulip')
self.do_test_session(self.example_email("hamlet"), lambda: delete_realm_user_sessions(realm), True) self.do_test_session(self.example_email("hamlet"),
self.do_test_session(self.mit_email("sipbtest"), lambda: delete_realm_user_sessions(realm), False) lambda: delete_realm_user_sessions(realm),
get_realm("zulip"), True)
self.do_test_session(self.mit_email("sipbtest"),
lambda: delete_realm_user_sessions(realm),
get_realm("zephyr"), False)
def test_delete_all_user_sessions(self) -> None: def test_delete_all_user_sessions(self) -> None:
self.do_test_session(self.example_email("hamlet"), lambda: delete_all_user_sessions(), True) self.do_test_session(self.example_email("hamlet"),
self.do_test_session(self.mit_email("sipbtest"), lambda: delete_all_user_sessions(), True) lambda: delete_all_user_sessions(),
get_realm("zulip"), True)
self.do_test_session(self.mit_email("sipbtest"),
lambda: delete_all_user_sessions(),
get_realm("zephyr"), True)
def test_delete_all_deactivated_user_sessions(self) -> None: def test_delete_all_deactivated_user_sessions(self) -> None:

View File

@ -452,7 +452,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
content=body, content=body,
) )
self.login(user1_email, 'test') self.login(user1_email, 'test', realm=r1)
response = self.client_get(uri, subdomain=test_subdomain) response = self.client_get(uri, subdomain=test_subdomain)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = b"".join(response.streaming_content) data = b"".join(response.streaming_content)
@ -460,7 +460,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
self.logout() self.logout()
# Confirm other cross-realm users can't read it. # Confirm other cross-realm users can't read it.
self.login(user3_email, 'test') self.login(user3_email, 'test', realm=r1)
response = self.client_get(uri, subdomain=test_subdomain) response = self.client_get(uri, subdomain=test_subdomain)
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assert_in_response("You are not authorized to view this file.", response) self.assert_in_response("You are not authorized to view this file.", response)

View File

@ -29,7 +29,7 @@ class ZephyrTest(ZulipTestCase):
email = str(self.mit_email("starnine")) email = str(self.mit_email("starnine"))
realm = get_realm('zephyr') realm = get_realm('zephyr')
self.login(email) self.login(email, realm=realm)
def ccache_mock(**kwargs): def ccache_mock(**kwargs):
# type: (**Any) -> Any # type: (**Any) -> Any