11# -*- coding: utf-8 -*-
2- """Tests for plugin."""
2+ """Test cases for OPA plugin
3+
4+ Copyright 2025
5+ SPDX-License-Identifier: Apache-2.0
6+ Authors: Shriti Priya
7+
8+ This module contains test cases for running opa plugin.
9+ """
10+
311
412# Third-Party
513import pytest
1321 GlobalContext
1422)
1523
24+ from tests .server .opa_server import run_mock_opa
25+
1626
1727@pytest .mark .asyncio
18- async def test_opapluginfilter ():
28+ # Test for when opaplugin is not applied to a tool
29+ async def test_benign_opapluginfilter ():
1930 """Test plugin prompt prefetch hook."""
2031 config = PluginConfig (
2132 name = "test" ,
2233 kind = "opapluginfilter.OPAPluginFilter" ,
2334 hooks = ["tool_pre_invoke" ],
24- config = {"setting_one" : "test_value" },
35+ applied_to = {"tools" : [{"tool_name" : "fast-time-git-status" , "context" : ["global.opa_policy_context.git_context" ], "extensions" : {"policy" : "example" , "policy_endpoint" : "allow" }}]},
36+ config = {"opa_base_url" : "http://127.0.0.1:8181/v1/data/" }
2537 )
38+ mock_server = run_mock_opa ()
39+
2640
2741 plugin = OPAPluginFilter (config )
2842
2943 # Test your plugin logic
30- payload = ToolPreInvokePayload (name = "test_tool " , args = {"repo_path" : "This is an argument " })
44+ payload = ToolPreInvokePayload (name = "fast-time-git-status " , args = {"repo_path" : "/path/IBM " })
3145 context = PluginContext (global_context = GlobalContext (request_id = "1" , server_id = "2" ))
3246 result = await plugin .tool_pre_invoke (payload , context )
33- import pdb
34- pdb .set_trace ()
47+ mock_server .shutdown ()
3548 assert result .continue_processing
49+
50+
51+ @pytest .mark .asyncio
52+ # Test for when opaplugin is not applied to a tool
53+ async def test_malign_opapluginfilter ():
54+ """Test plugin prompt prefetch hook."""
55+ config = PluginConfig (
56+ name = "test" ,
57+ kind = "opapluginfilter.OPAPluginFilter" ,
58+ hooks = ["tool_pre_invoke" ],
59+ applied_to = {"tools" : [{"tool_name" : "fast-time-git-status" , "context" : ["global.opa_policy_context.git_context" ], "extensions" : {"policy" : "example" , "policy_endpoint" : "allow" }}]},
60+ config = {"opa_base_url" : "http://127.0.0.1:8181/v1/data/" }
61+ )
62+ mock_server = run_mock_opa ()
63+ plugin = OPAPluginFilter (config )
64+
65+ # Test your plugin logic
66+ payload = ToolPreInvokePayload (name = "fast-time-git-status" , args = {"repo_path" : "/path/IM" })
67+ context = PluginContext (global_context = GlobalContext (request_id = "1" , server_id = "2" ))
68+ result = await plugin .tool_pre_invoke (payload , context )
69+ mock_server .shutdown ()
70+ assert not result .continue_processing and result .violation .code == "deny"
71+
72+ @pytest .mark .asyncio
73+ # Test for opa plugin not applied to any of the tools
74+ async def test_applied_to_opaplugin ():
75+ """Test plugin prompt prefetch hook."""
76+ config = PluginConfig (
77+ name = "test" ,
78+ kind = "opapluginfilter.OPAPluginFilter" ,
79+ hooks = ["tool_pre_invoke" ],
80+ applied_to = {},
81+ config = {"opa_base_url" : "http://127.0.0.1:8181/v1/data/" }
82+ )
83+ mock_server = run_mock_opa ()
84+ plugin = OPAPluginFilter (config )
85+
86+ # Test your plugin logic
87+ payload = ToolPreInvokePayload (name = "fast-time-git-status" , args = {"repo_path" : "/path/IM" })
88+ context = PluginContext (global_context = GlobalContext (request_id = "1" , server_id = "2" ))
89+ result = await plugin .tool_pre_invoke (payload , context )
90+ mock_server .shutdown ()
91+ assert result .continue_processing
0 commit comments