tests: Add wrapper for client.logout in ZulipTestCase.

In this commit we add a logout wrapper so as to enable developers
to just do self.logout instead of doing a post request at API
endpoint for logout. This is achieved by adding a wrapper function
for the Django's client.logout contained in TestCase. We add this
by extending ZulipTestCase to have a logout function.
This commit is contained in:
Aditya Bansal 2017-04-18 06:53:32 +05:30 committed by Tim Abbott
parent cf001876d4
commit bdcddd35d0
5 changed files with 18 additions and 12 deletions

View File

@ -223,6 +223,10 @@ class ZulipTestCase(TestCase):
else: else:
self.assertFalse(self.client.login(username=email, password=password)) self.assertFalse(self.client.login(username=email, password=password))
def logout(self):
# type: () -> None
self.client.logout()
def register(self, email, password): def register(self, email, password):
# type: (Text, Text) -> HttpResponse # type: (Text, Text) -> HttpResponse
self.client_post('/accounts/home/', {'email': email}) self.client_post('/accounts/home/', {'email': email})

View File

@ -199,7 +199,7 @@ class UserPresenceTests(ZulipTestCase):
# type: () -> None # type: () -> None
self.login("espuser@mit.edu") self.login("espuser@mit.edu")
self.client_post("/json/users/me/presence", {'status': 'idle'}) self.client_post("/json/users/me/presence", {'status': 'idle'})
result = self.client_post("/accounts/logout/") self.logout()
# Ensure we don't see hamlet@zulip.com information leakage # Ensure we don't see hamlet@zulip.com information leakage
self.login("hamlet@zulip.com") self.login("hamlet@zulip.com")

View File

@ -78,7 +78,7 @@ class ChangeSettingsTest(ZulipTestCase):
self.check_well_formed_change_settings_response(result) self.check_well_formed_change_settings_response(result)
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com"). self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
full_name, "Foo Bar") full_name, "Foo Bar")
self.client_post('/accounts/logout/') self.logout()
self.login("hamlet@zulip.com", "foobar1") self.login("hamlet@zulip.com", "foobar1")
user_profile = get_user_profile_by_email('hamlet@zulip.com') user_profile = get_user_profile_by_email('hamlet@zulip.com')
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id) self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)

View File

@ -242,6 +242,8 @@ class LoginTest(ZulipTestCase):
def test_logout(self): def test_logout(self):
# type: () -> None # type: () -> None
self.login("hamlet@zulip.com") self.login("hamlet@zulip.com")
# We use the logout API, not self.logout, to make sure we test
# the actual logout code path.
self.client_post('/accounts/logout/') self.client_post('/accounts/logout/')
self.assertIsNone(get_session_dict_user(self.client.session)) self.assertIsNone(get_session_dict_user(self.client.session))
@ -257,11 +259,11 @@ class LoginTest(ZulipTestCase):
self.register("test@zulip.com", password) self.register("test@zulip.com", password)
user_profile = get_user_profile_by_email(email) user_profile = get_user_profile_by_email(email)
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id) self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
self.client_post('/accounts/logout/') self.logout()
self.assertIsNone(get_session_dict_user(self.client.session)) self.assertIsNone(get_session_dict_user(self.client.session))
# Logging in succeeds. # Logging in succeeds.
self.client_post('/accounts/logout/') self.logout()
self.login(email, password) self.login(email, password)
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id) self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)

View File

@ -77,7 +77,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
self.assertEqual(base, uri[:len(base)]) self.assertEqual(base, uri[:len(base)])
# Download file via API # Download file via API
self.client_post('/accounts/logout/') self.logout()
response = self.client_get(uri, **auth_headers) response = self.client_get(uri, **auth_headers)
data = b"".join(response.streaming_content) data = b"".join(response.streaming_content)
self.assertEqual(b"zulip!", data) self.assertEqual(b"zulip!", data)
@ -213,7 +213,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
json = ujson.loads(result.content) json = ujson.loads(result.content)
uri = json["uri"] uri = json["uri"]
self.client_post('/accounts/logout/') self.logout()
response = self.client_get(uri) response = self.client_get(uri)
self.assert_json_error(response, "Not logged in: API authentication or user session required", self.assert_json_error(response, "Not logged in: API authentication or user session required",
status_code=401) status_code=401)
@ -506,7 +506,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = b"".join(response.streaming_content) data = b"".join(response.streaming_content)
self.assertEqual(b"zulip!", data) self.assertEqual(b"zulip!", data)
self.client_post('/accounts/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')
@ -535,7 +535,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
fp_path_id = re.sub('/user_uploads/', '', uri) fp_path_id = re.sub('/user_uploads/', '', uri)
body = "First message ...[zulip.txt](http://localhost:9991/user_uploads/" + fp_path_id + ")" body = "First message ...[zulip.txt](http://localhost:9991/user_uploads/" + fp_path_id + ")"
self.send_message("hamlet@zulip.com", "test-subscribe", Recipient.STREAM, body, "test") self.send_message("hamlet@zulip.com", "test-subscribe", Recipient.STREAM, body, "test")
self.client_post('/accounts/logout/') self.logout()
# Subscribed user should be able to view file # Subscribed user should be able to view file
for user in subscribed_users: for user in subscribed_users:
@ -544,7 +544,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = b"".join(response.streaming_content) data = b"".join(response.streaming_content)
self.assertEqual(b"zulip!", data) self.assertEqual(b"zulip!", data)
self.client_post('/accounts/logout/') self.logout()
# Unsubscribed user should not be able to view file # Unsubscribed user should not be able to view file
for user in unsubscribed_users: for user in unsubscribed_users:
@ -552,7 +552,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
response = self.client_get(uri) response = self.client_get(uri)
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)
self.client_post('/accounts/logout/') self.logout()
def test_file_download_authorization_public(self): def test_file_download_authorization_public(self):
# type: () -> None # type: () -> None
@ -570,7 +570,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
fp_path_id = re.sub('/user_uploads/', '', uri) fp_path_id = re.sub('/user_uploads/', '', uri)
body = "First message ...[zulip.txt](http://localhost:9991/user_uploads/" + fp_path_id + ")" body = "First message ...[zulip.txt](http://localhost:9991/user_uploads/" + fp_path_id + ")"
self.send_message("hamlet@zulip.com", "test-subscribe", Recipient.STREAM, body, "test") self.send_message("hamlet@zulip.com", "test-subscribe", Recipient.STREAM, body, "test")
self.client_post('/accounts/logout/') self.logout()
# Now all users should be able to access the files # Now all users should be able to access the files
for user in subscribed_users + unsubscribed_users: for user in subscribed_users + unsubscribed_users:
@ -578,7 +578,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
response = self.client_get(uri) response = self.client_get(uri)
data = b"".join(response.streaming_content) data = b"".join(response.streaming_content)
self.assertEqual(b"zulip!", data) self.assertEqual(b"zulip!", data)
self.client_post('/accounts/logout/') self.logout()
def tearDown(self): def tearDown(self):
# type: () -> None # type: () -> None