File size: 4,166 Bytes
ad4c1f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pytest

from autogpt.config import Config
from autogpt.plugins import (
    denylist_allowlist_check,
    inspect_zip_for_modules,
    scan_plugins,
)

PLUGINS_TEST_DIR = "tests/unit/data/test_plugins"
PLUGIN_TEST_ZIP_FILE = "Auto-GPT-Plugin-Test-master.zip"
PLUGIN_TEST_INIT_PY = "Auto-GPT-Plugin-Test-master/src/auto_gpt_vicuna/__init__.py"
PLUGIN_TEST_OPENAI = "https://weathergpt.vercel.app/"


def test_inspect_zip_for_modules():
    result = inspect_zip_for_modules(str(f"{PLUGINS_TEST_DIR}/{PLUGIN_TEST_ZIP_FILE}"))
    assert result == [PLUGIN_TEST_INIT_PY]


@pytest.fixture
def mock_config_denylist_allowlist_check():
    class MockConfig:
        """Mock config object for testing the denylist_allowlist_check function"""

        plugins_denylist = ["BadPlugin"]
        plugins_allowlist = ["GoodPlugin"]
        authorise_key = "y"
        exit_key = "n"

    return MockConfig()


def test_denylist_allowlist_check_denylist(
    mock_config_denylist_allowlist_check, monkeypatch
):
    # Test that the function returns False when the plugin is in the denylist
    monkeypatch.setattr("builtins.input", lambda _: "y")
    assert not denylist_allowlist_check(
        "BadPlugin", mock_config_denylist_allowlist_check
    )


def test_denylist_allowlist_check_allowlist(
    mock_config_denylist_allowlist_check, monkeypatch
):
    # Test that the function returns True when the plugin is in the allowlist
    monkeypatch.setattr("builtins.input", lambda _: "y")
    assert denylist_allowlist_check("GoodPlugin", mock_config_denylist_allowlist_check)


def test_denylist_allowlist_check_user_input_yes(
    mock_config_denylist_allowlist_check, monkeypatch
):
    # Test that the function returns True when the user inputs "y"
    monkeypatch.setattr("builtins.input", lambda _: "y")
    assert denylist_allowlist_check(
        "UnknownPlugin", mock_config_denylist_allowlist_check
    )


def test_denylist_allowlist_check_user_input_no(
    mock_config_denylist_allowlist_check, monkeypatch
):
    # Test that the function returns False when the user inputs "n"
    monkeypatch.setattr("builtins.input", lambda _: "n")
    assert not denylist_allowlist_check(
        "UnknownPlugin", mock_config_denylist_allowlist_check
    )


def test_denylist_allowlist_check_user_input_invalid(
    mock_config_denylist_allowlist_check, monkeypatch
):
    # Test that the function returns False when the user inputs an invalid value
    monkeypatch.setattr("builtins.input", lambda _: "invalid")
    assert not denylist_allowlist_check(
        "UnknownPlugin", mock_config_denylist_allowlist_check
    )


@pytest.fixture
def config_with_plugins():
    """Mock config object for testing the scan_plugins function"""
    # Test that the function returns the correct number of plugins
    cfg = Config()
    cfg.plugins_dir = PLUGINS_TEST_DIR
    cfg.plugins_openai = ["https://weathergpt.vercel.app/"]
    return cfg


@pytest.fixture
def mock_config_openai_plugin():
    """Mock config object for testing the scan_plugins function"""

    class MockConfig:
        """Mock config object for testing the scan_plugins function"""

        plugins_dir = PLUGINS_TEST_DIR
        plugins_openai = [PLUGIN_TEST_OPENAI]
        plugins_denylist = ["AutoGPTPVicuna"]
        plugins_allowlist = [PLUGIN_TEST_OPENAI]

    return MockConfig()


def test_scan_plugins_openai(mock_config_openai_plugin):
    # Test that the function returns the correct number of plugins
    result = scan_plugins(mock_config_openai_plugin, debug=True)
    assert len(result) == 1


@pytest.fixture
def mock_config_generic_plugin():
    """Mock config object for testing the scan_plugins function"""

    # Test that the function returns the correct number of plugins
    class MockConfig:
        plugins_dir = PLUGINS_TEST_DIR
        plugins_openai = []
        plugins_denylist = []
        plugins_allowlist = ["AutoGPTPVicuna"]

    return MockConfig()


def test_scan_plugins_generic(mock_config_generic_plugin):
    # Test that the function returns the correct number of plugins
    result = scan_plugins(mock_config_generic_plugin, debug=True)
    assert len(result) == 1