Skip to content

Commit 4e80b51

Browse files
authored
Add plugin wrappers for plugin related utils functions (CTFd#410)
1 parent 126144f commit 4e80b51

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ CTFd/uploads
6666

6767
# Vagrant
6868
.vagrant
69+
70+
# CTFd Exports
71+
*.zip

CTFd/plugins/__init__.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import os
44

55
from flask.helpers import safe_join
6-
from flask import send_file, send_from_directory, abort
7-
from CTFd.utils import admins_only as admins_only_wrapper
6+
from flask import current_app as app, send_file, send_from_directory, abort
7+
from CTFd.utils import (
8+
admins_only as admins_only_wrapper,
9+
override_template as utils_override_template,
10+
register_plugin_script as utils_register_plugin_script,
11+
register_plugin_stylesheet as utils_register_plugin_stylesheet
12+
)
813

914

1015
def register_plugin_assets_directory(app, base_path, admins_only=False):
@@ -48,6 +53,29 @@ def asset_handler():
4853
app.add_url_rule(rule=rule, endpoint=asset_path, view_func=asset_handler)
4954

5055

56+
def override_template(*args, **kwargs):
57+
"""
58+
Overrides a template with the provided html content.
59+
60+
e.g. override_template('scoreboard.html', '<h1>scores</h1>')
61+
"""
62+
utils_override_template(*args, **kwargs)
63+
64+
65+
def register_plugin_script(*args, **kwargs):
66+
"""
67+
Adds a given script to the base.html template which all pages inherit from
68+
"""
69+
utils_register_plugin_script(*args, **kwargs)
70+
71+
72+
def register_plugin_stylesheet(*args, **kwargs):
73+
"""
74+
Adds a given stylesheet to the base.html template which all pages inherit from.
75+
"""
76+
utils_register_plugin_stylesheet(*args, **kwargs)
77+
78+
5179
def init_plugins(app):
5280
"""
5381
Searches for the load function in modules in the CTFd/plugins folder. This function is called with the current CTFd

tests/test_plugin_utils.py

+62-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44
from tests.helpers import *
55
from CTFd.models import ip2long, long2ip
6-
from CTFd.plugins import register_plugin_assets_directory, register_plugin_asset
6+
from CTFd.plugins import (
7+
register_plugin_assets_directory,
8+
register_plugin_asset,
9+
register_plugin_script,
10+
register_plugin_stylesheet,
11+
override_template
12+
)
713
from freezegun import freeze_time
814
from mock import patch
915
import json
@@ -37,3 +43,58 @@ def test_register_plugin_assets_directory():
3743
assert len(r.get_data(as_text=True)) > 0
3844
assert r.status_code == 200
3945
destroy_ctfd(app)
46+
47+
48+
def test_override_template():
49+
"""Does override_template work properly for regular themes when used from a plugin"""
50+
app = create_ctfd()
51+
with app.app_context():
52+
override_template('login.html', 'LOGIN OVERRIDE')
53+
with app.test_client() as client:
54+
r = client.get('/login')
55+
assert r.status_code == 200
56+
output = r.get_data(as_text=True)
57+
assert 'LOGIN OVERRIDE' in output
58+
destroy_ctfd(app)
59+
60+
61+
def test_admin_override_template():
62+
"""Does override_template work properly for the admin panel when used from a plugin"""
63+
app = create_ctfd()
64+
with app.app_context():
65+
override_template('admin/team.html', 'ADMIN TEAM OVERRIDE')
66+
67+
client = login_as_user(app, name="admin", password="password")
68+
r = client.get('/admin/team/1')
69+
assert r.status_code == 200
70+
output = r.get_data(as_text=True)
71+
assert 'ADMIN TEAM OVERRIDE' in output
72+
destroy_ctfd(app)
73+
74+
75+
def test_register_plugin_script():
76+
'''Test that register_plugin_script adds script paths to the original theme when used from a plugin'''
77+
app = create_ctfd()
78+
with app.app_context():
79+
register_plugin_script('/fake/script/path.js')
80+
register_plugin_script('http://ctfd.io/fake/script/path.js')
81+
with app.test_client() as client:
82+
r = client.get('/')
83+
output = r.get_data(as_text=True)
84+
assert '/fake/script/path.js' in output
85+
assert 'http://ctfd.io/fake/script/path.js' in output
86+
destroy_ctfd(app)
87+
88+
89+
def test_register_plugin_stylesheet():
90+
'''Test that register_plugin_stylesheet adds stylesheet paths to the original theme when used from a plugin'''
91+
app = create_ctfd()
92+
with app.app_context():
93+
register_plugin_script('/fake/stylesheet/path.css')
94+
register_plugin_script('http://ctfd.io/fake/stylesheet/path.css')
95+
with app.test_client() as client:
96+
r = client.get('/')
97+
output = r.get_data(as_text=True)
98+
assert '/fake/stylesheet/path.css' in output
99+
assert 'http://ctfd.io/fake/stylesheet/path.css' in output
100+
destroy_ctfd(app)

0 commit comments

Comments
 (0)