From 31e8261bad4b927ab5cbf58588b3ee12d073535e Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 26 Jun 2021 15:03:18 -0400 Subject: [PATCH] Add a way to access the challenge plugin class from the Challenges model (#1925) * Add a way to access the challenge plugin class from the Challenges model * Allows templates to access the plugin class more easily * Allows plugins to access the plugin class without having to load the class explicitly * Closes #1879 --- CTFd/models/__init__.py | 6 ++++++ tests/test_plugin_utils.py | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CTFd/models/__init__.py b/CTFd/models/__init__.py index 6b241fcb..f4652c0b 100644 --- a/CTFd/models/__init__.py +++ b/CTFd/models/__init__.py @@ -122,6 +122,12 @@ class Challenges(db.Model): return markup(build_markdown(self.description)) + @property + def plugin_class(self): + from CTFd.plugins.challenges import get_chal_class + + return get_chal_class(self.type) + def __init__(self, *args, **kwargs): super(Challenges, self).__init__(**kwargs) diff --git a/tests/test_plugin_utils.py b/tests/test_plugin_utils.py index c4c95887..24403fb1 100644 --- a/tests/test_plugin_utils.py +++ b/tests/test_plugin_utils.py @@ -14,7 +14,13 @@ from CTFd.plugins import ( register_plugin_script, register_user_page_menu_bar, ) -from tests.helpers import create_ctfd, destroy_ctfd, login_as_user, setup_ctfd +from tests.helpers import ( + create_ctfd, + destroy_ctfd, + gen_challenge, + login_as_user, + setup_ctfd, +) def test_register_plugin_asset(): @@ -204,3 +210,17 @@ def test_bypass_csrf_protection(): assert r.status_code == 200 assert output == "Success" destroy_ctfd(app) + + +def test_challenges_model_access_plugin_class(): + """ + Test that the Challenges model can access its plugin class + """ + app = create_ctfd() + + with app.app_context(): + from CTFd.plugins.challenges import get_chal_class + + chal = gen_challenge(app.db) + assert chal.plugin_class == get_chal_class("standard") + destroy_ctfd(app)