CoolFace
Apppublic

vishalbhat07/ctfd

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
test_cache.py111 linesDownload Raw Back to cache
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3 4from redis.exceptions import ConnectionError5 6from CTFd.cache import clear_all_user_sessions, clear_user_session7from CTFd.config import TestingConfig8from CTFd.models import Users9from CTFd.utils.security.auth import login_user10from CTFd.utils.user import get_current_user, is_admin11from tests.helpers import create_ctfd, destroy_ctfd, register_user12 13 14def test_clear_user_session():15    app = create_ctfd()16    with app.app_context():17        register_user(app)18 19        # Users by default should have a non-admin type20        user = Users.query.filter_by(id=2).first()21        with app.test_request_context("/"):22            login_user(user)23            user = get_current_user()24            assert user.id == 225            assert user.type == "user"26            assert is_admin() is False27 28            # Set the user's updated type29            user = Users.query.filter_by(id=2).first()30            user.type = "admin"31            app.db.session.commit()32 33            # Should still return False because this is still cached34            assert is_admin() is False35 36            clear_user_session(user_id=2)37 38            # Should now return True after clearing cache39            assert is_admin() is True40    destroy_ctfd(app)41 42 43def test_clear_all_user_sessions():44    app = create_ctfd()45    with app.app_context():46        register_user(app)47 48        # Users by default should have a non-admin type49        user = Users.query.filter_by(id=2).first()50        with app.test_request_context("/"):51            login_user(user)52            user = get_current_user()53            assert user.id == 254            assert user.type == "user"55            assert is_admin() is False56 57            # Set the user's updated type58            user = Users.query.filter_by(id=2).first()59            user.type = "admin"60            app.db.session.commit()61 62            # Should still return False because this is still cached63            assert is_admin() is False64 65            clear_all_user_sessions()66 67            # Should now return True after clearing cache68            assert is_admin() is True69    destroy_ctfd(app)70 71 72def test_cache_subclass_commands():73    app = create_ctfd()74    with app.app_context():75        from CTFd.cache import cache76 77        cache.inc("testing_inc")78        resp = cache.inc("testing_inc")79        assert resp == 280        assert cache.get("testing_inc") == 281        cache.expire("testing_inc", 0)82        assert cache.get("testing_inc") is None83        resp = cache.inc("testing_inc")84        assert resp == 185    destroy_ctfd(app)86 87 88def test_redis_cache_subclass_commands():89    class RedisConfig(TestingConfig):90        REDIS_URL = "redis://localhost:6379/1"91        CACHE_REDIS_URL = "redis://localhost:6379/1"92        CACHE_TYPE = "redis"93 94    try:95        app = create_ctfd(config=RedisConfig)96    except ConnectionError:97        print("Failed to connect to redis. Skipping test.")98    else:99        with app.app_context():100            from CTFd.cache import cache101 102            cache.inc("testing_inc")103            resp = cache.inc("testing_inc")104            assert resp == 2105            assert cache.get("testing_inc") == 2106            cache.expire("testing_inc", 0)107            assert cache.get("testing_inc") is None108            resp = cache.inc("testing_inc")109            assert resp == 1110        destroy_ctfd(app)111