diff --git a/CTFd/admin/challenges.py b/CTFd/admin/challenges.py index 55de2811..37514563 100644 --- a/CTFd/admin/challenges.py +++ b/CTFd/admin/challenges.py @@ -1,9 +1,11 @@ from flask import current_app as app, render_template, render_template_string, url_for from CTFd.utils.decorators import admins_only +from CTFd.utils import binary_type from CTFd.models import Solves, Challenges, Flags from CTFd.plugins.challenges import get_chal_class from CTFd.admin import admin import os +import six @admin.route('/admin/challenges') @@ -22,9 +24,12 @@ def challenges_detail(challenge_id): flags = Flags.query.filter_by(challenge_id=challenge.id).all() challenge_class = get_chal_class(challenge.type) - with open(os.path.join(app.root_path, challenge_class.templates['update'].lstrip('/'))) as update: + with open(os.path.join(app.root_path, challenge_class.templates['update'].lstrip('/')), 'rb') as update: + tpl = update.read() + if six.PY3 and isinstance(tpl, binary_type): + tpl = tpl.decode('utf-8') update_j2 = render_template_string( - update.read().decode('utf-8'), + tpl, challenge=challenge ) diff --git a/tests/admin/test_challenges.py b/tests/admin/test_challenges.py index 5832d57a..2df8119d 100644 --- a/tests/admin/test_challenges.py +++ b/tests/admin/test_challenges.py @@ -21,6 +21,32 @@ def test_get_admin_challenges_new(): destroy_ctfd(app) +def test_create_new_challenge(): + """Test that an admin can create a challenge properly""" + app = create_ctfd() + with app.app_context(): + register_user(app) + client = login_as_user(app, name="admin", password="password") + + challenge_data = { + "name": "name", + "category": "category", + "description": "description", + "value": 100, + "state": "hidden", + "type": "standard" + } + + r = client.post('/api/v1/challenges', json=challenge_data) + assert r.get_json().get('data')['id'] == 1 + r = client.get('/admin/challenges/1') + assert r.status_code == 200 + r = client.get('/api/v1/challenges/1') + assert r.get_json().get('data')['id'] == 1 + + destroy_ctfd(app) + + def test_hidden_challenge_is_reachable(): """Test that hidden challenges are visible for admins""" app = create_ctfd()