Repository: CTFd/CTFd
Pull request #291 (2017-06-18 22:40:53)

Changing /static to /themes


Patch:
diff --git a/CTFd/utils.py b/CTFd/utils.py
index 77f5609d..2d6f21c9 100644
--- a/CTFd/utils.py
+++ b/CTFd/utils.py
@@ -118,7 +118,7 @@ def init_utils(app):
 
     @app.before_request
     def needs_setup():
-        if request.path == '/setup' or request.path.startswith('/static'):
+        if request.path == '/setup' or request.path.startswith('/themes'):
             return
         if not is_setup():
             return redirect(url_for('views.setup'))
diff --git a/CTFd/views.py b/CTFd/views.py
index 16c4055f..db7e8574 100644
--- a/CTFd/views.py
+++ b/CTFd/views.py
@@ -13,14 +13,6 @@ from CTFd import utils
 views = Blueprint('views', __name__)
 
 
-@views.before_request
-def redirect_setup():
-    if request.path.startswith("/static"):
-        return
-    if not utils.is_setup() and request.path != "/setup":
-        return redirect(url_for('views.setup'))
-
-
 @views.route('/setup', methods=['GET', 'POST'])
 def setup():
     # with app.app_context():


Test patch:
diff --git a/tests/helpers.py b/tests/helpers.py
index d3164387..1d45c1aa 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -4,7 +4,7 @@ from sqlalchemy_utils import database_exists, create_database, drop_database
 from sqlalchemy.engine.url import make_url
 
 
-def create_ctfd(ctf_name="CTFd", name="admin", email="admin@ctfd.io", password="password"):
+def create_ctfd(ctf_name="CTFd", name="admin", email="admin@ctfd.io", password="password", setup=True):
     app = create_app('CTFd.config.TestingConfig')
 
     url = make_url(app.config['SQLALCHEMY_DATABASE_URI'])
@@ -17,19 +17,20 @@ def create_ctfd(ctf_name="CTFd", name="admin", email="admin@ctfd.io", password="
         with app.app_context():
             app.db.create_all()
 
-    with app.app_context():
-        with app.test_client() as client:
-            data = {}
-            r = client.get('/setup')  # Populate session with nonce
-            with client.session_transaction() as sess:
-                data = {
-                    "ctf_name": ctf_name,
-                    "name": name,
-                    "email": email,
-                    "password": password,
-                    "nonce": sess.get('nonce')
-                }
-            client.post('/setup', data=data)
+    if setup:
+        with app.app_context():
+            with app.test_client() as client:
+                data = {}
+                r = client.get('/setup')  # Populate session with nonce
+                with client.session_transaction() as sess:
+                    data = {
+                        "ctf_name": ctf_name,
+                        "name": name,
+                        "email": email,
+                        "password": password,
+                        "nonce": sess.get('nonce')
+                    }
+                client.post('/setup', data=data)
     return app
 
 
diff --git a/tests/test_user_facing.py b/tests/test_user_facing.py
index 8ad24c9b..8452e663 100644
--- a/tests/test_user_facing.py
+++ b/tests/test_user_facing.py
@@ -298,3 +298,17 @@ def test_themes_handler():
             assert r.status_code == 404
             r = client.get('/themes/original/static/../../../utils.py')
             assert r.status_code == 404
+
+
+def test_ctfd_setup_redirect():
+    """Test that a fresh CTFd instance redirects to /setup"""
+    app = create_ctfd(setup=False)
+    with app.app_context():
+        with app.test_client() as client:
+            r = client.get('/teams')
+            assert r.status_code == 302
+            assert r.location == "http://localhost/setup"
+
+            # Files in /themes load properly
+            r = client.get('/themes/original/static/css/style.css')
+            assert r.status_code == 200