From 87895918f5e8464c9d6ebedbe24fb97d21a61aca Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 30 Apr 2020 02:45:12 -0400 Subject: [PATCH] Fix tracker code to fix tests --- CTFd/cache/__init__.py | 6 ++++++ CTFd/utils/initialization/__init__.py | 18 +++++++++++------- tests/admin/test_config.py | 3 +++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CTFd/cache/__init__.py b/CTFd/cache/__init__.py index 78a63b5..601616a 100644 --- a/CTFd/cache/__init__.py +++ b/CTFd/cache/__init__.py @@ -44,3 +44,9 @@ def clear_pages(): cache.delete_memoized(get_pages) cache.delete_memoized(get_page) + + +def clear_user_ips(user_id): + from CTFd.utils.user import get_user_ips + + cache.delete_memoized(get_user_ips, user_id=user_id) diff --git a/CTFd/utils/initialization/__init__.py b/CTFd/utils/initialization/__init__.py index 0cafbef..7e79721 100644 --- a/CTFd/utils/initialization/__init__.py +++ b/CTFd/utils/initialization/__init__.py @@ -7,6 +7,7 @@ from flask import abort, redirect, render_template, request, session, url_for from sqlalchemy.exc import IntegrityError, InvalidRequestError from werkzeug.wsgi import DispatcherMiddleware +from CTFd.cache import clear_user_ips from CTFd.exceptions import UserNotFoundException, UserTokenExpiredException from CTFd.models import Tracking, db from CTFd.utils import config, get_config, markdown @@ -179,9 +180,10 @@ def init_request_processors(app): if authed(): user_ips = get_current_user_ips() ip = get_ip() + track = None if ip not in user_ips: - visit = Tracking(ip=get_ip(), user_id=session["id"]) - db.session.add(visit) + track = Tracking(ip=get_ip(), user_id=session["id"]) + db.session.add(track) else: if request.method != "GET": track = Tracking.query.filter_by( @@ -189,11 +191,13 @@ def init_request_processors(app): ).first() track.date = datetime.datetime.utcnow() - try: - db.session.commit() - except (InvalidRequestError, IntegrityError): - db.session.rollback() - logout_user() + if track: + try: + db.session.commit() + except (InvalidRequestError, IntegrityError): + db.session.rollback() + logout_user() + clear_user_ips(user_id=session["id"]) if authed(): user = get_current_user() diff --git a/tests/admin/test_config.py b/tests/admin/test_config.py index d33d791..dd0da3a 100644 --- a/tests/admin/test_config.py +++ b/tests/admin/test_config.py @@ -58,6 +58,9 @@ def test_reset(): gen_fail(app.db, user_id=user_obj.id, challenge_id=random.randint(1, 10)) gen_tracking(app.db, user_id=user_obj.id) + print("eet") + print(Tracking.query.all()) + # Add PageFiles for x in range(5): gen_file(