|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Location: ./tests/commands/resources/test_plugins.py |
| 3 | +Copyright 2025 |
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +Authors: Matthew Grigsby |
| 6 | +
|
| 7 | +Tests for the plugins commands. |
| 8 | +""" |
| 9 | + |
| 10 | +# Third-Party |
| 11 | +import pytest |
| 12 | +import typer |
| 13 | + |
| 14 | +# First-Party |
| 15 | +from cforge.commands.resources.plugins import PluginMode, plugins_get, plugins_list, plugins_stats |
| 16 | +from cforge.common import AuthenticationError, CLIError |
| 17 | +from tests.conftest import invoke_typer_command, patch_functions |
| 18 | + |
| 19 | + |
| 20 | +class TestPluginCommands: |
| 21 | + """Tests for plugins commands.""" |
| 22 | + |
| 23 | + def test_plugin_mode_enum_is_case_insensitive(self) -> None: |
| 24 | + """Typer Enum choices should accept case-insensitive values.""" |
| 25 | + assert PluginMode("EnFoRcE") == PluginMode.ENFORCE |
| 26 | + |
| 27 | + def test_plugin_mode_enum_missing_non_string(self) -> None: |
| 28 | + """Non-string values should not be coerced into Enum members.""" |
| 29 | + assert PluginMode._missing_(123) is None |
| 30 | + |
| 31 | + def test_plugin_mode_enum_missing_unknown_value(self) -> None: |
| 32 | + """Unknown strings should not be coerced into Enum members.""" |
| 33 | + assert PluginMode._missing_("nope") is None |
| 34 | + |
| 35 | + def test_plugins_list_success(self, mock_console) -> None: |
| 36 | + """Test plugins list command with table output.""" |
| 37 | + mock_response = { |
| 38 | + "plugins": [ |
| 39 | + {"name": "pii_filter", "version": "1.0.0", "author": "ContextForge", "mode": "enforce", "status": "enabled", "priority": 10, "hooks": ["tool_pre_invoke"], "tags": ["security"]} |
| 40 | + ], |
| 41 | + "total": 1, |
| 42 | + "enabled_count": 1, |
| 43 | + "disabled_count": 0, |
| 44 | + } |
| 45 | + |
| 46 | + with patch_functions( |
| 47 | + "cforge.commands.resources.plugins", |
| 48 | + get_console=mock_console, |
| 49 | + make_authenticated_request={"return_value": mock_response}, |
| 50 | + print_table=None, |
| 51 | + ) as mocks: |
| 52 | + invoke_typer_command(plugins_list) |
| 53 | + mocks.print_table.assert_called_once() |
| 54 | + |
| 55 | + def test_plugins_list_json_output(self, mock_console) -> None: |
| 56 | + """Test plugins list with JSON output.""" |
| 57 | + mock_response = {"plugins": [], "total": 0, "enabled_count": 0, "disabled_count": 0} |
| 58 | + with patch_functions( |
| 59 | + "cforge.commands.resources.plugins", |
| 60 | + get_console=mock_console, |
| 61 | + make_authenticated_request={"return_value": mock_response}, |
| 62 | + print_json=None, |
| 63 | + ) as mocks: |
| 64 | + invoke_typer_command(plugins_list, json_output=True) |
| 65 | + mocks.print_json.assert_called_once() |
| 66 | + |
| 67 | + def test_plugins_list_no_results(self, mock_console) -> None: |
| 68 | + """Test plugins list with no results.""" |
| 69 | + mock_response = {"plugins": [], "total": 0, "enabled_count": 0, "disabled_count": 0} |
| 70 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"return_value": mock_response}): |
| 71 | + invoke_typer_command(plugins_list) |
| 72 | + |
| 73 | + assert any("No plugins found" in str(call) for call in mock_console.print.call_args_list) |
| 74 | + |
| 75 | + def test_plugins_list_with_filters(self, mock_console) -> None: |
| 76 | + """Test plugins list with all filters.""" |
| 77 | + with patch_functions( |
| 78 | + "cforge.commands.resources.plugins", |
| 79 | + get_console=mock_console, |
| 80 | + make_authenticated_request={"return_value": {"plugins": [], "total": 0, "enabled_count": 0, "disabled_count": 0}}, |
| 81 | + print_table=None, |
| 82 | + ) as mocks: |
| 83 | + invoke_typer_command(plugins_list, search="pii", mode=PluginMode.ENFORCE, hook="tool_pre_invoke", tag="security") |
| 84 | + |
| 85 | + call_args = mocks.make_authenticated_request.call_args |
| 86 | + assert call_args[0][0] == "GET" |
| 87 | + assert call_args[0][1] == "/admin/plugins" |
| 88 | + assert call_args[1]["params"] == {"search": "pii", "mode": "enforce", "hook": "tool_pre_invoke", "tag": "security"} |
| 89 | + |
| 90 | + def test_plugins_list_error(self, mock_console) -> None: |
| 91 | + """Test plugins list error handling.""" |
| 92 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"side_effect": Exception("API error")}): |
| 93 | + with pytest.raises(typer.Exit): |
| 94 | + invoke_typer_command(plugins_list) |
| 95 | + |
| 96 | + def test_plugins_get_success(self, mock_console) -> None: |
| 97 | + """Test plugins get command.""" |
| 98 | + mock_plugin = {"name": "pii_filter", "version": "1.0.0"} |
| 99 | + with patch_functions( |
| 100 | + "cforge.commands.resources.plugins", |
| 101 | + get_console=mock_console, |
| 102 | + make_authenticated_request={"return_value": mock_plugin}, |
| 103 | + print_json=None, |
| 104 | + ) as mocks: |
| 105 | + invoke_typer_command(plugins_get, name="pii_filter") |
| 106 | + mocks.print_json.assert_called_once() |
| 107 | + |
| 108 | + def test_plugins_get_error(self, mock_console) -> None: |
| 109 | + """Test plugins get error handling.""" |
| 110 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"side_effect": Exception("API error")}): |
| 111 | + with pytest.raises(typer.Exit): |
| 112 | + invoke_typer_command(plugins_get, name="pii_filter") |
| 113 | + |
| 114 | + def test_plugins_stats_success(self, mock_console) -> None: |
| 115 | + """Test plugins stats command.""" |
| 116 | + mock_stats = {"total_plugins": 4, "enabled_plugins": 3, "disabled_plugins": 1, "plugins_by_hook": {"tool_pre_invoke": 3}, "plugins_by_mode": {"enforce": 3, "disabled": 1}} |
| 117 | + with patch_functions( |
| 118 | + "cforge.commands.resources.plugins", |
| 119 | + get_console=mock_console, |
| 120 | + make_authenticated_request={"return_value": mock_stats}, |
| 121 | + print_json=None, |
| 122 | + ) as mocks: |
| 123 | + invoke_typer_command(plugins_stats) |
| 124 | + mocks.print_json.assert_called_once() |
| 125 | + |
| 126 | + def test_plugins_stats_error(self, mock_console) -> None: |
| 127 | + """Test plugins stats error handling.""" |
| 128 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"side_effect": Exception("API error")}): |
| 129 | + with pytest.raises(typer.Exit): |
| 130 | + invoke_typer_command(plugins_stats) |
| 131 | + |
| 132 | + def test_plugins_list_forbidden_shows_permission_hint(self, mock_console) -> None: |
| 133 | + """Test plugins list shows a targeted hint on forbidden/admin failures.""" |
| 134 | + with patch_functions( |
| 135 | + "cforge.commands.resources.plugins", |
| 136 | + get_console=mock_console, |
| 137 | + make_authenticated_request={"side_effect": AuthenticationError("Authentication required but not configured")}, |
| 138 | + ): |
| 139 | + with pytest.raises(typer.Exit): |
| 140 | + invoke_typer_command(plugins_list) |
| 141 | + |
| 142 | + assert any("Requires admin.plugins permission" in str(call) for call in mock_console.print.call_args_list) |
| 143 | + |
| 144 | + def test_plugins_list_not_found_shows_admin_api_hint(self, mock_console) -> None: |
| 145 | + """Test plugins list shows an admin-api hint on 404.""" |
| 146 | + with patch_functions( |
| 147 | + "cforge.commands.resources.plugins", |
| 148 | + get_console=mock_console, |
| 149 | + make_authenticated_request={"side_effect": CLIError("API request failed (404): Not Found")}, |
| 150 | + ): |
| 151 | + with pytest.raises(typer.Exit): |
| 152 | + invoke_typer_command(plugins_list) |
| 153 | + |
| 154 | + assert any("Admin plugin API unavailable" in str(call) for call in mock_console.print.call_args_list) |
0 commit comments