2020-02-06 18:25:15 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-05-10 19:00:29 +02:00
|
|
|
from typing import Any, Callable
|
2017-04-26 04:15:45 +02:00
|
|
|
|
|
|
|
from zerver.lib.sessions import (
|
|
|
|
user_sessions,
|
|
|
|
delete_session,
|
|
|
|
delete_user_sessions,
|
|
|
|
delete_realm_user_sessions,
|
|
|
|
delete_all_user_sessions,
|
|
|
|
delete_all_deactivated_user_sessions,
|
2020-02-06 18:25:15 +01:00
|
|
|
get_expirable_session_var,
|
|
|
|
set_expirable_session_var,
|
2017-04-26 04:15:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.models import (
|
2020-03-06 18:40:46 +01:00
|
|
|
get_realm, Realm, UserProfile
|
2017-04-26 04:15:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2017-04-26 04:15:45 +02:00
|
|
|
|
|
|
|
class TestSessions(ZulipTestCase):
|
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
def do_test_session(self, user: UserProfile,
|
2017-11-05 10:51:25 +01:00
|
|
|
action: Callable[[], Any],
|
2017-11-18 00:11:24 +01:00
|
|
|
realm: Realm,
|
2017-11-05 10:51:25 +01:00
|
|
|
expected_result: bool) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-04-26 04:15:45 +02:00
|
|
|
self.assertIn('_auth_user_id', self.client.session)
|
|
|
|
action()
|
|
|
|
if expected_result:
|
2017-11-18 00:11:24 +01:00
|
|
|
result = self.client_get('/', subdomain=realm.subdomain)
|
2018-12-04 02:12:08 +01:00
|
|
|
self.assertEqual('/login/', result.url)
|
2017-04-26 04:15:45 +02:00
|
|
|
else:
|
|
|
|
self.assertIn('_auth_user_id', self.client.session)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_session(self) -> None:
|
2017-05-23 23:52:41 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2017-04-26 04:15:45 +02:00
|
|
|
self.assertIn('_auth_user_id', self.client.session)
|
|
|
|
for session in user_sessions(user_profile):
|
|
|
|
delete_session(session)
|
|
|
|
result = self.client_get("/")
|
2018-12-04 02:12:08 +01:00
|
|
|
self.assertEqual('/login/', result.url)
|
2017-04-26 04:15:45 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_user_sessions(self) -> None:
|
2017-05-23 23:52:41 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.do_test_session(user_profile, lambda: delete_user_sessions(user_profile),
|
2017-11-18 00:11:24 +01:00
|
|
|
get_realm("zulip"), True)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.do_test_session(self.example_user("othello"),
|
2017-11-18 00:11:24 +01:00
|
|
|
lambda: delete_user_sessions(user_profile),
|
|
|
|
get_realm("zulip"), False)
|
2017-04-26 04:15:45 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_realm_user_sessions(self) -> None:
|
2017-04-26 04:15:45 +02:00
|
|
|
realm = get_realm('zulip')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.do_test_session(self.example_user("hamlet"),
|
2017-11-18 00:11:24 +01:00
|
|
|
lambda: delete_realm_user_sessions(realm),
|
|
|
|
get_realm("zulip"), True)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.do_test_session(self.mit_user("sipbtest"),
|
2017-11-18 00:11:24 +01:00
|
|
|
lambda: delete_realm_user_sessions(realm),
|
|
|
|
get_realm("zephyr"), False)
|
2017-04-26 04:15:45 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_all_user_sessions(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.do_test_session(self.example_user("hamlet"),
|
2017-11-18 00:11:24 +01:00
|
|
|
lambda: delete_all_user_sessions(),
|
|
|
|
get_realm("zulip"), True)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.do_test_session(self.mit_user("sipbtest"),
|
2017-11-18 00:11:24 +01:00
|
|
|
lambda: delete_all_user_sessions(),
|
|
|
|
get_realm("zephyr"), True)
|
2017-04-26 04:15:45 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_all_deactivated_user_sessions(self) -> None:
|
2017-05-10 19:04:57 +02:00
|
|
|
|
|
|
|
# Test that no exception is thrown with a logged-out session
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2017-04-26 04:15:45 +02:00
|
|
|
self.assertIn('_auth_user_id', self.client.session)
|
|
|
|
self.client_post('/accounts/logout/')
|
|
|
|
delete_all_deactivated_user_sessions()
|
|
|
|
result = self.client_get("/")
|
2018-12-04 02:12:08 +01:00
|
|
|
self.assertEqual('/login/', result.url)
|
2017-04-26 04:15:45 +02:00
|
|
|
|
2017-05-10 19:04:57 +02:00
|
|
|
# Test nothing happens to an active user's session
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2017-04-26 04:15:45 +02:00
|
|
|
self.assertIn('_auth_user_id', self.client.session)
|
|
|
|
delete_all_deactivated_user_sessions()
|
|
|
|
self.assertIn('_auth_user_id', self.client.session)
|
2017-05-10 19:04:57 +02:00
|
|
|
|
|
|
|
# Test that a deactivated session gets logged out
|
2017-05-23 23:52:41 +02:00
|
|
|
user_profile_3 = self.example_user('cordelia')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile_3)
|
2017-05-10 19:04:57 +02:00
|
|
|
self.assertIn('_auth_user_id', self.client.session)
|
|
|
|
user_profile_3.is_active = False
|
|
|
|
user_profile_3.save()
|
|
|
|
delete_all_deactivated_user_sessions()
|
|
|
|
result = self.client_get("/")
|
2018-12-04 02:12:08 +01:00
|
|
|
self.assertEqual('/login/', result.url)
|
2020-02-06 18:25:15 +01:00
|
|
|
|
|
|
|
class TestExpirableSessionVars(ZulipTestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
self.session = self.client.session
|
|
|
|
super().setUp()
|
|
|
|
|
|
|
|
def test_set_and_get_basic(self) -> None:
|
|
|
|
start_time = timezone_now()
|
|
|
|
with mock.patch('zerver.lib.sessions.timezone_now', return_value=start_time):
|
|
|
|
set_expirable_session_var(self.session, 'test_set_and_get_basic', 'some_value', expiry_seconds=10)
|
|
|
|
value = get_expirable_session_var(self.session, 'test_set_and_get_basic')
|
|
|
|
self.assertEqual(value, 'some_value')
|
|
|
|
with mock.patch('zerver.lib.sessions.timezone_now', return_value=start_time + timedelta(seconds=11)):
|
|
|
|
value = get_expirable_session_var(self.session, 'test_set_and_get_basic')
|
|
|
|
self.assertEqual(value, None)
|
|
|
|
|
|
|
|
def test_set_and_get_with_delete(self) -> None:
|
|
|
|
set_expirable_session_var(self.session, 'test_set_and_get_with_delete', 'some_value', expiry_seconds=10)
|
|
|
|
value = get_expirable_session_var(self.session, 'test_set_and_get_with_delete', delete=True)
|
|
|
|
self.assertEqual(value, 'some_value')
|
|
|
|
self.assertEqual(get_expirable_session_var(self.session, 'test_set_and_get_with_delete'), None)
|
|
|
|
|
|
|
|
def test_get_var_not_set(self) -> None:
|
|
|
|
value = get_expirable_session_var(self.session, 'test_get_var_not_set', default_value='default')
|
|
|
|
self.assertEqual(value, 'default')
|
|
|
|
|
|
|
|
def test_get_var_is_not_expirable(self) -> None:
|
|
|
|
self.session["test_get_var_is_not_expirable"] = 0
|
|
|
|
with mock.patch('zerver.lib.sessions.logging.warning') as mock_warn:
|
|
|
|
value = get_expirable_session_var(self.session, 'test_get_var_is_not_expirable', default_value='default')
|
|
|
|
self.assertEqual(value, 'default')
|
|
|
|
mock_warn.assert_called_once()
|