Skip to content

Commit 4026f4a

Browse files
committed
Add protect mode validation
1 parent e73f379 commit 4026f4a

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

aikido_zen/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
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+
2729
def protect(mode="daemon", token=""):
2830
"""
2931
Mode can be set to :
@@ -32,6 +34,10 @@ def protect(mode="daemon", token=""):
3234
- daemon_disabled : This will import sinks/sources but won't start a background process
3335
Protect user's application
3436
"""
37+
if mode not in VALID_MODES:
38+
raise ValueError(
39+
f"Invalid mode {mode!r}, expected one of {VALID_MODES}. To pass a token, use protect(token=...)"
40+
)
3541
if aikido_disabled_flag_active():
3642
# Do not run any aikido code when the disabled flag is on
3743
return

aikido_zen/init_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ 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(
24+
ValueError, match=r"Invalid mode .*protect\(token=\.\.\.\)"
25+
):
26+
aikido_zen.protect("AIK_RUNTIME_some-token-string")
27+
28+
29+
@pytest.mark.parametrize("mode", ["daemon", "daemon_only", "daemon_disabled"])
30+
def test_protect_accepts_valid_modes(mode):
31+
aikido_zen.protect(mode=mode)

0 commit comments

Comments
 (0)