|
| 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 plugins_get, plugins_list, plugins_stats |
| 16 | +from cforge.common import AuthenticationError, CLIError |
| 17 | +from tests.conftest import patch_functions |
| 18 | + |
| 19 | + |
| 20 | +class TestPluginCommands: |
| 21 | + """Tests for plugins commands.""" |
| 22 | + |
| 23 | + def test_plugins_list_success(self, mock_console) -> None: |
| 24 | + """Test plugins list command with table output.""" |
| 25 | + mock_response = { |
| 26 | + "plugins": [{"name": "pii_filter", "version": "1.0.0", "author": "ContextForge", "mode": "enforce", "status": "enabled", "priority": 10, "hooks": ["tool_pre_invoke"], "tags": ["security"]}], |
| 27 | + "total": 1, |
| 28 | + "enabled_count": 1, |
| 29 | + "disabled_count": 0, |
| 30 | + } |
| 31 | + |
| 32 | + with patch_functions( |
| 33 | + "cforge.commands.resources.plugins", |
| 34 | + get_console=mock_console, |
| 35 | + make_authenticated_request={"return_value": mock_response}, |
| 36 | + print_table=None, |
| 37 | + ) as mocks: |
| 38 | + plugins_list(json_output=False) |
| 39 | + mocks.print_table.assert_called_once() |
| 40 | + |
| 41 | + def test_plugins_list_json_output(self, mock_console) -> None: |
| 42 | + """Test plugins list with JSON output.""" |
| 43 | + mock_response = {"plugins": [], "total": 0, "enabled_count": 0, "disabled_count": 0} |
| 44 | + with patch_functions( |
| 45 | + "cforge.commands.resources.plugins", |
| 46 | + get_console=mock_console, |
| 47 | + make_authenticated_request={"return_value": mock_response}, |
| 48 | + print_json=None, |
| 49 | + ) as mocks: |
| 50 | + plugins_list(json_output=True) |
| 51 | + mocks.print_json.assert_called_once() |
| 52 | + |
| 53 | + def test_plugins_list_no_results(self, mock_console) -> None: |
| 54 | + """Test plugins list with no results.""" |
| 55 | + mock_response = {"plugins": [], "total": 0, "enabled_count": 0, "disabled_count": 0} |
| 56 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"return_value": mock_response}): |
| 57 | + plugins_list(json_output=False) |
| 58 | + |
| 59 | + assert any("No plugins found" in str(call) for call in mock_console.print.call_args_list) |
| 60 | + |
| 61 | + def test_plugins_list_with_filters(self, mock_console) -> None: |
| 62 | + """Test plugins list with all filters.""" |
| 63 | + with patch_functions( |
| 64 | + "cforge.commands.resources.plugins", |
| 65 | + get_console=mock_console, |
| 66 | + make_authenticated_request={"return_value": {"plugins": [], "total": 0, "enabled_count": 0, "disabled_count": 0}}, |
| 67 | + print_table=None, |
| 68 | + ) as mocks: |
| 69 | + plugins_list(search="pii", mode="enforce", hook="tool_pre_invoke", tag="security", json_output=False) |
| 70 | + |
| 71 | + call_args = mocks.make_authenticated_request.call_args |
| 72 | + assert call_args[0][0] == "GET" |
| 73 | + assert call_args[0][1] == "/admin/plugins" |
| 74 | + assert call_args[1]["params"] == {"search": "pii", "mode": "enforce", "hook": "tool_pre_invoke", "tag": "security"} |
| 75 | + |
| 76 | + def test_plugins_list_error(self, mock_console) -> None: |
| 77 | + """Test plugins list error handling.""" |
| 78 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"side_effect": Exception("API error")}): |
| 79 | + with pytest.raises(typer.Exit): |
| 80 | + plugins_list(json_output=False) |
| 81 | + |
| 82 | + def test_plugins_get_success(self, mock_console) -> None: |
| 83 | + """Test plugins get command.""" |
| 84 | + mock_plugin = {"name": "pii_filter", "version": "1.0.0"} |
| 85 | + with patch_functions( |
| 86 | + "cforge.commands.resources.plugins", |
| 87 | + get_console=mock_console, |
| 88 | + make_authenticated_request={"return_value": mock_plugin}, |
| 89 | + print_json=None, |
| 90 | + ) as mocks: |
| 91 | + plugins_get(name="pii_filter") |
| 92 | + mocks.print_json.assert_called_once() |
| 93 | + |
| 94 | + def test_plugins_get_error(self, mock_console) -> None: |
| 95 | + """Test plugins get error handling.""" |
| 96 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"side_effect": Exception("API error")}): |
| 97 | + with pytest.raises(typer.Exit): |
| 98 | + plugins_get(name="pii_filter") |
| 99 | + |
| 100 | + def test_plugins_stats_success(self, mock_console) -> None: |
| 101 | + """Test plugins stats command.""" |
| 102 | + 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}} |
| 103 | + with patch_functions( |
| 104 | + "cforge.commands.resources.plugins", |
| 105 | + get_console=mock_console, |
| 106 | + make_authenticated_request={"return_value": mock_stats}, |
| 107 | + print_json=None, |
| 108 | + ) as mocks: |
| 109 | + plugins_stats() |
| 110 | + mocks.print_json.assert_called_once() |
| 111 | + |
| 112 | + def test_plugins_stats_error(self, mock_console) -> None: |
| 113 | + """Test plugins stats error handling.""" |
| 114 | + with patch_functions("cforge.commands.resources.plugins", get_console=mock_console, make_authenticated_request={"side_effect": Exception("API error")}): |
| 115 | + with pytest.raises(typer.Exit): |
| 116 | + plugins_stats() |
| 117 | + |
| 118 | + def test_plugins_list_forbidden_shows_permission_hint(self, mock_console) -> None: |
| 119 | + """Test plugins list shows a targeted hint on forbidden/admin failures.""" |
| 120 | + with patch_functions( |
| 121 | + "cforge.commands.resources.plugins", |
| 122 | + get_console=mock_console, |
| 123 | + make_authenticated_request={"side_effect": AuthenticationError("Authentication required but not configured")}, |
| 124 | + ): |
| 125 | + with pytest.raises(typer.Exit): |
| 126 | + plugins_list(json_output=False) |
| 127 | + |
| 128 | + assert any("Requires admin.plugins permission" in str(call) for call in mock_console.print.call_args_list) |
| 129 | + |
| 130 | + def test_plugins_list_not_found_shows_admin_api_hint(self, mock_console) -> None: |
| 131 | + """Test plugins list shows an admin-api hint on 404.""" |
| 132 | + with patch_functions( |
| 133 | + "cforge.commands.resources.plugins", |
| 134 | + get_console=mock_console, |
| 135 | + make_authenticated_request={"side_effect": CLIError("API request failed (404): Not Found")}, |
| 136 | + ): |
| 137 | + with pytest.raises(typer.Exit): |
| 138 | + plugins_list(json_output=False) |
| 139 | + |
| 140 | + assert any("Admin plugin API unavailable" in str(call) for call in mock_console.print.call_args_list) |
0 commit comments