mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 22:14:25 +01:00
Better constant value management (#1335)
* Starts work on #929 * Adds Enum classes that can be accessed from JS, Jinja, and Python code. This allows for the sharing of constant values between the three major codebases in CTFd.
This commit is contained in:
52
tests/constants/test_constants.py
Normal file
52
tests/constants/test_constants.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from CTFd.constants import RawEnum, JSEnum, JinjaEnum
|
||||
from tests.helpers import create_ctfd, destroy_ctfd
|
||||
|
||||
|
||||
def test_RawEnum():
|
||||
class Colors(str, RawEnum):
|
||||
RED = "red"
|
||||
GREEN = "green"
|
||||
BLUE = "blue"
|
||||
|
||||
class Numbers(str, RawEnum):
|
||||
ONE = 1
|
||||
TWO = 2
|
||||
THREE = 3
|
||||
|
||||
assert Colors.RED == "red"
|
||||
assert Colors.GREEN == "green"
|
||||
assert Colors.BLUE == "blue"
|
||||
assert Colors.test("red") is True
|
||||
assert Colors.test("purple") is False
|
||||
assert str(Numbers.ONE) == "1"
|
||||
assert sorted(Colors.keys()) == sorted(["RED", "GREEN", "BLUE"])
|
||||
assert sorted(Colors.values()) == sorted(["red", "green", "blue"])
|
||||
|
||||
|
||||
def test_JSEnum():
|
||||
from CTFd.constants import JS_ENUMS
|
||||
import json
|
||||
|
||||
@JSEnum
|
||||
class Colors(str, RawEnum):
|
||||
RED = "red"
|
||||
GREEN = "green"
|
||||
BLUE = "blue"
|
||||
|
||||
assert JS_ENUMS["Colors"] == {"RED": "red", "GREEN": "green", "BLUE": "blue"}
|
||||
assert json.dumps(JS_ENUMS)
|
||||
|
||||
|
||||
def test_JinjaEnum():
|
||||
app = create_ctfd()
|
||||
with app.app_context():
|
||||
|
||||
@JinjaEnum
|
||||
class Colors(str, RawEnum):
|
||||
RED = "red"
|
||||
GREEN = "green"
|
||||
BLUE = "blue"
|
||||
|
||||
assert app.jinja_env.globals["Colors"] is Colors
|
||||
assert app.jinja_env.globals["Colors"].RED == "red"
|
||||
destroy_ctfd(app)
|
||||
Reference in New Issue
Block a user