def test_session_ttl(app): """Test actual/working session expiration/TTL settings.""" if type(app.kvsession_store) is not RedisStore: pytest.skip('TTL support needed, this test requires Redis.') ttl_seconds = 1 # Set ttl to "0 days, 1 seconds" ttl_delta = datetime.timedelta(0, ttl_seconds) assert app.kvsession_store.ttl_support # _THIS_ is what flask_kvsession uses to determine default ttl # sets default ttl to `ttl_seconds` seconds app.config['PERMANENT_SESSION_LIFETIME'] = ttl_delta assert app.permanent_session_lifetime.total_seconds() == ttl_seconds with app.app_context(): user = testutils.create_test_user() with app.test_client() as client: testutils.login_user_via_view(client, user=user) assert len(app.kvsession_store.keys()) == 1 sid = testutils.unserialize_session(flask.session.sid_s) time.sleep(ttl_seconds + 1) assert sid.has_expired(ttl_delta) assert not testutils.client_authenticated(client) # Expired sessions are automagically removed from the sessionstore # Although not _instantly_. while len(app.kvsession_store.keys()) > 0: pass assert len(app.kvsession_store.keys()) == 0
def test_session_ttl(app): """Test actual/working session expiration/TTL settings.""" ttl_seconds = 1 # Set ttl to "0 days, 1 seconds" ttl_delta = datetime.timedelta(0, ttl_seconds) ext = InvenioAccounts(app) app.register_blueprint(blueprint) assert ext.sessionstore.ttl_support # _THIS_ is what flask_kvsession uses to determine default ttl # sets default ttl to `ttl_seconds` seconds app.config['PERMANENT_SESSION_LIFETIME'] = ttl_delta assert app.permanent_session_lifetime.total_seconds() == ttl_seconds user = testutils.create_test_user() with app.test_client() as client: testutils.login_user_via_view(client, user=user) assert len(testutils.get_kvsession_keys()) == 1 sid = testutils.unserialize_session(flask.session.sid_s) testutils.let_session_expire() assert sid.has_expired(ttl_delta) assert not testutils.client_authenticated(client) # Expired sessions are automagically removed from the sessionstore # Although not _instantly_. while len(testutils.get_kvsession_keys()) > 0: pass assert len(testutils.get_kvsession_keys()) == 0
def test_sessionstore_default_ttl_secs(app): """Test the `default_ttl_secs` field for simplekv sessionstore. See http://pythonhosted.org/simplekv/index.html#simplekv.TimeToLiveMixin. """ if type(app.kvsession_store) is not RedisStore: pytest.skip('TTL support needed, this test requires Redis.') ttl_seconds = 1 ttl_delta = datetime.timedelta(0, ttl_seconds) sessionstore = app.kvsession_store sessionstore.default_ttl_secs = ttl_seconds # Verify that the backend supports ttl assert sessionstore.ttl_support app.kvsession_store = sessionstore with app.app_context(): user = testutils.create_test_user() with app.test_client() as client: testutils.login_user_via_view(client, user=user) sid = testutils.unserialize_session(flask.session.sid_s) while not sid.has_expired(ttl_delta): pass # When we get here the session should have expired. # But the client is still authenticated. assert testutils.client_authenticated(client)
def test_session_ttl(app): """Test actual/working session expiration/TTL settings.""" if type(app.kvsession_store) is not RedisStore: pytest.skip('TTL support needed, this test requires Redis.') ttl_seconds = 3 # Set ttl to "0 days, 1 seconds" ttl_delta = datetime.timedelta(0, ttl_seconds) assert app.kvsession_store.ttl_support # _THIS_ is what flask_kvsession uses to determine default ttl # sets default ttl to `ttl_seconds` seconds app.config['PERMANENT_SESSION_LIFETIME'] = ttl_delta assert app.permanent_session_lifetime.total_seconds() == ttl_seconds with app.app_context(): user = testutils.create_test_user('*****@*****.**') with app.test_client() as client: testutils.login_user_via_view(client, user=user) assert len(app.kvsession_store.keys()) == 1 sid = testutils.unserialize_session(session.sid_s) time.sleep(ttl_seconds + 1) assert sid.has_expired(ttl_delta) assert not testutils.client_authenticated(client) # Expired sessions are automagically removed from the sessionstore # Although not _instantly_. while len(app.kvsession_store.keys()) > 0: pass assert len(app.kvsession_store.keys()) == 0
def test_sessionstore_default_ttl_secs(app): """Test the `default_ttl_secs` field for simplekv sessionstore backends using the TimeToLive-mixin (http://pythonhosted.org/simplekv/index.html#simplekv.TimeToLiveMixin)""" ttl_seconds = 1 ttl_delta = datetime.timedelta(0, ttl_seconds) sessionstore = RedisStore(redis.StrictRedis()) sessionstore.default_ttl_secs = ttl_seconds ext = InvenioAccounts(app, sessionstore=sessionstore) app.register_blueprint(blueprint) # Verify that the backend supports ttl assert ext.sessionstore.ttl_support user = testutils.create_test_user() with app.test_client() as client: testutils.login_user_via_view(client, user=user) sid = testutils.unserialize_session(flask.session.sid_s) while not sid.has_expired(ttl_delta): pass # When we get here the session should have expired. # But the client is still authenticated. assert testutils.client_authenticated(client)
def test_sessionstore_default_ttl_secs(app): """Test the `default_ttl_secs` field for simplekv sessionstore. See http://simplekv.readthedocs.io/index.html#simplekv.TimeToLiveMixin. """ if type(app.kvsession_store) is not RedisStore: pytest.skip('TTL support needed, this test requires Redis.') ttl_seconds = 3 ttl_delta = datetime.timedelta(0, ttl_seconds) sessionstore = app.kvsession_store sessionstore.default_ttl_secs = ttl_seconds # Verify that the backend supports ttl assert sessionstore.ttl_support app.kvsession_store = sessionstore with app.app_context(): user = testutils.create_test_user('*****@*****.**') with app.test_client() as client: testutils.login_user_via_view(client, user=user) sid = testutils.unserialize_session(session.sid_s) while not sid.has_expired(ttl_delta): pass # When we get here the session should have expired. # But the client is still authenticated. assert testutils.client_authenticated(client)
def test_set_app_session_ttl(app): """Test testutils.py:set_app_session_ttl.""" InvenioAccounts(app) app.register_blueprint(blueprint) ttl_seconds = 1 ttl_delta = testutils.set_app_session_ttl(app, ttl_seconds) assert ttl_delta == datetime.timedelta(0, ttl_seconds) user = testutils.create_test_user() with app.test_client() as client: testutils.login_user_via_view(client, user=user) login_at = datetime.datetime.utcnow() sid = testutils.unserialize_session(flask.session.sid_s) while not sid.has_expired(ttl_delta): pass assert not testutils.client_authenticated(client)