Streamline / clarify shell command control configuration (#4628)

* Streamline / clarify shell command control configuration

* Fix lint
This commit is contained in:
Erik Peterson
2023-06-09 11:48:20 -07:00
committed by GitHub
parent cce50bef50
commit ff4e53d0e6
4 changed files with 71 additions and 40 deletions

View File

@@ -5,18 +5,12 @@ import tempfile
from typing import Callable
import pytest
from pytest_mock import MockerFixture
import autogpt.commands.execute_code as sut # system under testing
from autogpt.config import Config
from autogpt.config.ai_config import AIConfig
@pytest.fixture
def config_allow_execute(config: Config, mocker: MockerFixture) -> Callable:
yield mocker.patch.object(config, "execute_local_commands", True)
@pytest.fixture
def random_code(random_string) -> Callable:
return f"print('Hello {random_string}!')"
@@ -92,14 +86,43 @@ def test_execute_python_file_not_found(config: Config):
)
def test_execute_shell(config_allow_execute: bool, random_string: str, config: Config):
def test_execute_shell(random_string: str, config: Config):
result = sut.execute_shell(f"echo 'Hello {random_string}!'", config)
assert f"Hello {random_string}!" in result
def test_execute_shell_deny_command(
python_test_file: str, config_allow_execute: bool, config: Config
):
config.deny_commands = ["echo"]
def test_execute_shell_local_commands_not_allowed(random_string: str, config: Config):
result = sut.execute_shell(f"echo 'Hello {random_string}!'", config)
assert f"Hello {random_string}!" in result
def test_execute_shell_denylist_should_deny(config: Config, random_string: str):
config.shell_denylist = ["echo"]
result = sut.execute_shell(f"echo 'Hello {random_string}!'", config)
assert "Error:" in result and "not allowed" in result
def test_execute_shell_denylist_should_allow(config: Config, random_string: str):
config.shell_denylist = ["cat"]
result = sut.execute_shell(f"echo 'Hello {random_string}!'", config)
assert "Hello" in result and random_string in result
assert "Error" not in result
def test_execute_shell_allowlist_should_deny(config: Config, random_string: str):
config.shell_command_control = sut.ALLOWLIST_CONTROL
config.shell_allowlist = ["cat"]
result = sut.execute_shell(f"echo 'Hello {random_string}!'", config)
assert "Error:" in result and "not allowed" in result
def test_execute_shell_allowlist_should_allow(config: Config, random_string: str):
config.shell_command_control = sut.ALLOWLIST_CONTROL
config.shell_allowlist = ["echo"]
result = sut.execute_shell(f"echo 'Hello {random_string}!'", config)
assert "Hello" in result and random_string in result
assert "Error" not in result