Skip to content

Commit e9eea86

Browse files
committed
Add protect mode validation
1 parent e73f379 commit e9eea86

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

aikido_zen/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
from aikido_zen.helpers.aikido_disabled_flag_active import aikido_disabled_flag_active
2525

2626

27+
VALID_MODES = ("daemon", "daemon_only", "daemon_disabled")
28+
29+
2730
def protect(mode="daemon", token=""):
2831
"""
2932
Mode can be set to :
@@ -32,6 +35,10 @@ def protect(mode="daemon", token=""):
3235
- daemon_disabled : This will import sinks/sources but won't start a background process
3336
Protect user's application
3437
"""
38+
if mode not in VALID_MODES:
39+
raise ValueError(
40+
f"Invalid mode {mode!r}, expected one of {VALID_MODES}. To pass a token, use protect(token=...)"
41+
)
3542
if aikido_disabled_flag_active():
3643
# Do not run any aikido code when the disabled flag is on
3744
return

aikido_zen/init_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ def test_protect_with_django(monkeypatch, caplog):
1717
def test_protect_sets_token():
1818
aikido_zen.protect(token="MY_TOKEN_1")
1919
assert get_token_from_env().token == "MY_TOKEN_1"
20+
21+
22+
def test_protect_rejects_invalid_mode():
23+
with pytest.raises(ValueError, match=r"Invalid mode .*protect\(token=\.\.\.\)"):
24+
aikido_zen.protect("AIK_RUNTIME_some-token-string")
25+
26+
27+
@pytest.mark.parametrize("mode", ["daemon", "daemon_only", "daemon_disabled"])
28+
def test_protect_accepts_valid_modes(mode):
29+
aikido_zen.protect(mode=mode)

0 commit comments

Comments
 (0)