diff --git a/frappe/tests/test_auth.py b/frappe/tests/test_auth.py index 086602ea01..bbe9c36aea 100644 --- a/frappe/tests/test_auth.py +++ b/frappe/tests/test_auth.py @@ -110,7 +110,7 @@ class TestLoginAttemptTracker(unittest.TestCase): def test_account_unlock(self): """Make sure that locked account gets unlocked after lock_interval of time. """ - lock_interval = 10 # In sec + lock_interval = 2 # In sec tracker = LoginAttemptTracker(user_name='tester', max_consecutive_login_attempts=1, lock_interval=lock_interval) # Clear the cache by setting attempt as success tracker.add_success_attempt() diff --git a/frappe/tests/test_twofactor.py b/frappe/tests/test_twofactor.py index 6c4cfc07c7..709b88b8f3 100644 --- a/frappe/tests/test_twofactor.py +++ b/frappe/tests/test_twofactor.py @@ -8,7 +8,7 @@ from frappe.utils import cint from frappe.utils import set_request from frappe.auth import validate_ip_address, get_login_attempt_tracker from frappe.twofactor import (should_run_2fa, authenticate_for_2factor, get_cached_user_pass, - two_factor_is_enabled_for_, confirm_otp_token, get_otpsecret_for_, get_verification_obj) + two_factor_is_enabled_for_, confirm_otp_token, get_otpsecret_for_, get_verification_obj, ExpiredLoginException) from . import update_system_settings, get_system_setting import time @@ -111,6 +111,7 @@ class TestTwoFactor(unittest.TestCase): def test_confirm_otp_token(self): '''Ensure otp is confirmed''' + frappe.flags.otp_expiry = 2 authenticate_for_2factor(self.user) tmp_id = frappe.local.response['tmp_id'] otp = 'wrongotp' @@ -118,10 +119,11 @@ class TestTwoFactor(unittest.TestCase): confirm_otp_token(self.login_manager,otp=otp,tmp_id=tmp_id) otp = get_otp(self.user) self.assertTrue(confirm_otp_token(self.login_manager,otp=otp,tmp_id=tmp_id)) + frappe.flags.otp_expiry = None if frappe.flags.tests_verbose: - print('Sleeping for 30secs to confirm token expires..') - time.sleep(30) - with self.assertRaises(frappe.AuthenticationError): + print('Sleeping for 2 secs to confirm token expires..') + time.sleep(2) + with self.assertRaises(ExpiredLoginException): confirm_otp_token(self.login_manager,otp=otp,tmp_id=tmp_id) def test_get_verification_obj(self): diff --git a/frappe/twofactor.py b/frappe/twofactor.py index 0a120d5287..4e098c3075 100644 --- a/frappe/twofactor.py +++ b/frappe/twofactor.py @@ -73,11 +73,11 @@ def cache_2fa_data(user, token, otp_secret, tmp_id): # set increased expiry time for SMS and Email if verification_method in ['SMS', 'Email']: - expiry_time = 300 + expiry_time = frappe.flags.token_expiry or 300 frappe.cache().set(tmp_id + '_token', token) frappe.cache().expire(tmp_id + '_token', expiry_time) else: - expiry_time = 180 + expiry_time = frappe.flags.otp_expiry or 180 for k, v in iteritems({'_usr': user, '_pwd': pwd, '_otp_secret': otp_secret}): frappe.cache().set("{0}{1}".format(tmp_id, k), v) frappe.cache().expire("{0}{1}".format(tmp_id, k), expiry_time)