From 1ed50ee8588187671e1d931d6a9bfac140d9720e Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 17 Nov 2017 15:11:24 -0800 Subject: [PATCH] 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. --- zerver/lib/test_classes.py | 12 ++++++++---- zerver/tests/test_messages.py | 22 +++++++++++----------- zerver/tests/test_narrow.py | 8 +++++--- zerver/tests/test_presence.py | 9 +++++---- zerver/tests/test_realm_domains.py | 2 +- zerver/tests/test_sessions.py | 30 +++++++++++++++++++++--------- zerver/tests/test_upload.py | 4 ++-- zerver/tests/test_zephyr.py | 2 +- 8 files changed, 54 insertions(+), 35 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index a7e946c539..bdf5c76931 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -284,14 +284,18 @@ class ZulipTestCase(TestCase): {'username': email, 'password': password}, **kwargs) - def login(self, email, password=None, fails=False): - # type: (Text, Optional[Text], bool) -> HttpResponse + def login(self, email, password=None, fails=False, realm=None): + # type: (Text, Optional[Text], bool, Optional[Realm]) -> HttpResponse + if realm is None: + realm = get_realm("zulip") if password is None: password = initial_password(email) 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: - 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): # type: () -> None diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index feaa52089b..ec25905ac9 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -1050,7 +1050,7 @@ class MessagePOSTTest(ZulipTestCase): """ 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", "sender": self.mit_email("sipbtest"), "content": "Test message", @@ -1064,7 +1064,7 @@ class MessagePOSTTest(ZulipTestCase): """ 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", "sender": self.mit_email("sipbtest"), "content": "Test message", @@ -1077,7 +1077,7 @@ class MessagePOSTTest(ZulipTestCase): """ 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", "sender": self.mit_email("sipbtest"), "content": "Test message", @@ -1098,11 +1098,11 @@ class MessagePOSTTest(ZulipTestCase): self.mit_email("starnine")])} 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, subdomain="zephyr") 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, subdomain="zephyr") self.assertEqual(ujson.loads(result1.content)['id'], @@ -1200,7 +1200,7 @@ class MessagePOSTTest(ZulipTestCase): self.assert_json_error(result, "Unknown realm non-existing") 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", "content": "Test message", "client": "zephyr_mirror", @@ -1209,7 +1209,7 @@ class MessagePOSTTest(ZulipTestCase): self.assert_json_error(result, "Missing sender") 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", "sender": self.mit_email("sipbtest"), "content": "Test message", @@ -1222,7 +1222,7 @@ class MessagePOSTTest(ZulipTestCase): def test_send_message_create_mirrored_message_user_returns_invalid_input( self, create_mirrored_message_users_mock: Any) -> None: 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", "sender": self.mit_email("sipbtest"), "content": "Test message", @@ -1239,7 +1239,7 @@ class MessagePOSTTest(ZulipTestCase): email = user.email user.realm.string_id = 'notzephyr' user.realm.save() - self.login(email) + self.login(email, realm=get_realm("notzephyr")) result = self.client_post("/json/messages", {"type": "private", "sender": self.mit_email("sipbtest"), "content": "Test message", @@ -1353,7 +1353,7 @@ class EditMessageTest(ZulipTestCase): result = self.client_get('/json/messages/' + str(msg_id)) 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") self.assert_json_error(result, 'Invalid message(s)') @@ -2066,7 +2066,7 @@ class StarTests(ZulipTestCase): self.assert_json_success(result) # 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") self.assert_json_error(result, 'Invalid message(s)') diff --git a/zerver/tests/test_narrow.py b/zerver/tests/test_narrow.py index f6d15d17c3..35091f0bb2 100644 --- a/zerver/tests/test_narrow.py +++ b/zerver/tests/test_narrow.py @@ -683,7 +683,7 @@ class GetOldMessagesTest(ZulipTestCase): 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. """ - 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 # it to ensure that we actually have a stream message in this # narrow view. @@ -718,7 +718,7 @@ class GetOldMessagesTest(ZulipTestCase): """ mit_user_profile = self.mit_user("starnine") 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 # it to ensure that we actually have a stream message in this # narrow view. @@ -754,9 +754,11 @@ class GetOldMessagesTest(ZulipTestCase): """ mit_user_profile = self.mit_user("starnine") 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 # narrow view. + self.login(email, realm=mit_user_profile.realm) self.subscribe(mit_user_profile, "Scotland") self.send_stream_message(email, "Scotland", diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.py index ef8a96b5e9..72a821069a 100644 --- a/zerver/tests/test_presence.py +++ b/zerver/tests/test_presence.py @@ -23,6 +23,7 @@ from zerver.models import ( UserProfile, UserPresence, flush_per_request_caches, + get_realm, ) import datetime @@ -172,7 +173,7 @@ class UserPresenceTests(ZulipTestCase): def test_no_mit(self) -> None: """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'}, subdomain="zephyr") self.assert_json_success(result) @@ -182,7 +183,7 @@ class UserPresenceTests(ZulipTestCase): """Zephyr mirror realms find out the status of their mirror bot""" user_profile = self.mit_user('espuser') email = user_profile.email - self.login(email) + self.login(email, realm=user_profile.realm) def post_presence() -> Dict[str, Any]: result = self.client_post("/json/users/me/presence", {'status': 'idle'}, @@ -211,7 +212,7 @@ class UserPresenceTests(ZulipTestCase): ) 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'}, subdomain="zephyr") self.logout() @@ -254,7 +255,7 @@ class SingleUserPresenceTests(ZulipTestCase): 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.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", subdomain="zephyr") self.assert_json_error(result, "No such user") diff --git a/zerver/tests/test_realm_domains.py b/zerver/tests/test_realm_domains.py index 99c7b3c441..362df20b1f 100644 --- a/zerver/tests/test_realm_domains.py +++ b/zerver/tests/test_realm_domains.py @@ -55,7 +55,7 @@ class RealmDomainTest(ZulipTestCase): self.assert_json_error(result, 'The domain acme.com is already a part of your organization.') 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) diff --git a/zerver/tests/test_sessions.py b/zerver/tests/test_sessions.py index ca025beea3..8ab83fd74e 100644 --- a/zerver/tests/test_sessions.py +++ b/zerver/tests/test_sessions.py @@ -10,7 +10,7 @@ from zerver.lib.sessions 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 @@ -20,12 +20,13 @@ class TestSessions(ZulipTestCase): def do_test_session(self, user: Text, action: Callable[[], Any], + realm: Realm, expected_result: bool) -> None: - self.login(user) + self.login(user, realm=realm) self.assertIn('_auth_user_id', self.client.session) action() if expected_result: - result = self.client_get('/') + result = self.client_get('/', subdomain=realm.subdomain) self.assertEqual('/login', result.url) else: self.assertIn('_auth_user_id', self.client.session) @@ -43,17 +44,28 @@ class TestSessions(ZulipTestCase): def test_delete_user_sessions(self) -> None: user_profile = self.example_user('hamlet') email = user_profile.email - self.do_test_session(str(email), lambda: delete_user_sessions(user_profile), True) - self.do_test_session(str(self.example_email("othello")), lambda: delete_user_sessions(user_profile), False) + self.do_test_session(str(email), lambda: delete_user_sessions(user_profile), + 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: realm = get_realm('zulip') - self.do_test_session(self.example_email("hamlet"), lambda: delete_realm_user_sessions(realm), True) - self.do_test_session(self.mit_email("sipbtest"), lambda: delete_realm_user_sessions(realm), False) + self.do_test_session(self.example_email("hamlet"), + 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: - self.do_test_session(self.example_email("hamlet"), lambda: delete_all_user_sessions(), True) - self.do_test_session(self.mit_email("sipbtest"), lambda: delete_all_user_sessions(), True) + self.do_test_session(self.example_email("hamlet"), + 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: diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py index 9581725cc5..25bb3ea527 100644 --- a/zerver/tests/test_upload.py +++ b/zerver/tests/test_upload.py @@ -452,7 +452,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): content=body, ) - self.login(user1_email, 'test') + self.login(user1_email, 'test', realm=r1) response = self.client_get(uri, subdomain=test_subdomain) self.assertEqual(response.status_code, 200) data = b"".join(response.streaming_content) @@ -460,7 +460,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): self.logout() # 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) self.assertEqual(response.status_code, 403) self.assert_in_response("You are not authorized to view this file.", response) diff --git a/zerver/tests/test_zephyr.py b/zerver/tests/test_zephyr.py index 9165577b14..046c2f6be3 100644 --- a/zerver/tests/test_zephyr.py +++ b/zerver/tests/test_zephyr.py @@ -29,7 +29,7 @@ class ZephyrTest(ZulipTestCase): email = str(self.mit_email("starnine")) realm = get_realm('zephyr') - self.login(email) + self.login(email, realm=realm) def ccache_mock(**kwargs): # type: (**Any) -> Any