mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-18 14:34:21 +01:00
Add a test for clear_all_user_sessions
This commit is contained in:
33
tests/cache/test_cache.py
vendored
33
tests/cache/test_cache.py
vendored
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from CTFd.cache import clear_user_session, clear_all_user_sessions
|
||||||
from CTFd.models import Users
|
from CTFd.models import Users
|
||||||
from CTFd.utils.user import is_admin, get_current_user
|
from CTFd.utils.user import is_admin, get_current_user
|
||||||
from CTFd.utils.security.auth import login_user
|
from CTFd.utils.security.auth import login_user
|
||||||
@@ -26,12 +27,40 @@ def test_clear_user_session():
|
|||||||
user.type = "admin"
|
user.type = "admin"
|
||||||
app.db.session.commit()
|
app.db.session.commit()
|
||||||
|
|
||||||
# The user's type should now be admin
|
# Should still return False because this is still cached
|
||||||
|
assert is_admin() is False
|
||||||
|
|
||||||
|
clear_user_session(user_id=2)
|
||||||
|
|
||||||
|
# Should now return True after clearing cache
|
||||||
|
assert is_admin() is True
|
||||||
|
destroy_ctfd(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_clear_all_user_sessions():
|
||||||
|
app = create_ctfd()
|
||||||
|
with app.app_context():
|
||||||
|
register_user(app)
|
||||||
|
|
||||||
|
# Users by default should have a non-admin type
|
||||||
user = Users.query.filter_by(id=2).first()
|
user = Users.query.filter_by(id=2).first()
|
||||||
with app.test_request_context("/"):
|
with app.test_request_context("/"):
|
||||||
login_user(user)
|
login_user(user)
|
||||||
user = get_current_user()
|
user = get_current_user()
|
||||||
assert user.id == 2
|
assert user.id == 2
|
||||||
assert user.type == "admin"
|
assert user.type == "user"
|
||||||
|
assert is_admin() is False
|
||||||
|
|
||||||
|
# Set the user's updated type
|
||||||
|
user = Users.query.filter_by(id=2).first()
|
||||||
|
user.type = "admin"
|
||||||
|
app.db.session.commit()
|
||||||
|
|
||||||
|
# Should still return False because this is still cached
|
||||||
|
assert is_admin() is False
|
||||||
|
|
||||||
|
clear_all_user_sessions()
|
||||||
|
|
||||||
|
# Should now return True after clearing cache
|
||||||
assert is_admin() is True
|
assert is_admin() is True
|
||||||
destroy_ctfd(app)
|
destroy_ctfd(app)
|
||||||
Reference in New Issue
Block a user