style: 项目重构

1.项目改名为kilostar(千星)
2.后端部分进行大规模重构
3.node功能进行大规模重新设计
This commit is contained in:
2026-05-11 15:29:16 +00:00
parent 2d8571dee3
commit ee9bbbf676
134 changed files with 2190 additions and 2503 deletions
@@ -1,7 +1,7 @@
import pytest
from unittest.mock import MagicMock, patch
from pretor.adapter.model_adapter.agent_factory import AgentFactory
from pretor.utils.error import ModelNotExistError
from kilostar.adapter.model_adapter.agent_factory import AgentFactory
from kilostar.utils.error import ModelNotExistError
def test_create_agent_success_real():
@@ -11,12 +11,12 @@ def test_create_agent_success_real():
mock_provider.provider_apikey = "key"
mock_provider.provider_url = "url"
with patch("pretor.adapter.model_adapter.agent_factory.Agent") as mock_agent_cls:
with patch("kilostar.adapter.model_adapter.agent_factory.Agent") as mock_agent_cls:
with patch(
"pretor.adapter.model_adapter.agent_factory.OpenAIChatModel"
"kilostar.adapter.model_adapter.agent_factory.OpenAIChatModel"
) as mock_model_cls:
with patch(
"pretor.adapter.model_adapter.agent_factory.OpenAIProvider"
"kilostar.adapter.model_adapter.agent_factory.OpenAIProvider"
) as mock_provider_cls:
factory = AgentFactory()
agent = factory.create_agent(
@@ -2,8 +2,8 @@ import pytest
from unittest.mock import patch
from sqlalchemy.exc import IntegrityError, OperationalError
from pydantic import ValidationError
from pretor.utils.error import UserNotExistError
from pretor.core.postgres_database import database_exception
from kilostar.utils.error import UserNotExistError
from kilostar.core.postgres_database import database_exception
@database_exception
@@ -42,7 +42,7 @@ async def test_success_func():
@pytest.mark.asyncio
@patch("pretor.core.database.database_exception.logger")
@patch("kilostar.core.database.database_exception.logger")
async def test_validation_error(mock_logger):
with pytest.raises(ValidationError):
await validation_error_func()
@@ -51,7 +51,7 @@ async def test_validation_error(mock_logger):
@pytest.mark.asyncio
@patch("pretor.core.database.database_exception.logger")
@patch("kilostar.core.database.database_exception.logger")
async def test_integrity_error(mock_logger):
with pytest.raises(IntegrityError):
await integrity_error_func()
@@ -60,7 +60,7 @@ async def test_integrity_error(mock_logger):
@pytest.mark.asyncio
@patch("pretor.core.database.database_exception.logger")
@patch("kilostar.core.database.database_exception.logger")
async def test_operational_error(mock_logger):
with pytest.raises(OperationalError):
await operational_error_func()
@@ -69,7 +69,7 @@ async def test_operational_error(mock_logger):
@pytest.mark.asyncio
@patch("pretor.core.database.database_exception.logger")
@patch("kilostar.core.database.database_exception.logger")
async def test_user_not_exist_error(mock_logger):
result = await user_not_exist_error_func()
assert result is None
@@ -78,7 +78,7 @@ async def test_user_not_exist_error(mock_logger):
@pytest.mark.asyncio
@patch("pretor.core.database.database_exception.logger")
@patch("kilostar.core.database.database_exception.logger")
async def test_generic_exception(mock_logger):
with pytest.raises(Exception, match="mock generic exception"):
await exception_func()
+13 -13
View File
@@ -4,9 +4,9 @@ from unittest.mock import MagicMock, AsyncMock, patch
@pytest.fixture(autouse=True)
def mock_dependencies():
with patch("pretor.core.database.module.user.User") as mock_user_cls:
with patch("kilostar.core.database.module.user.User") as mock_user_cls:
mock_user_cls.user_name = MagicMock()
with patch("pretor.core.database.module.user.select") as mock_select:
with patch("kilostar.core.database.module.user.select") as mock_select:
yield mock_user_cls, mock_select
@@ -25,7 +25,7 @@ def mock_session_maker():
@pytest.mark.asyncio
async def test_add_user(mock_session_maker, mock_dependencies):
mock_user_cls, _ = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
@@ -51,7 +51,7 @@ async def test_add_user(mock_session_maker, mock_dependencies):
@pytest.mark.asyncio
async def test_change_password_success(mock_session_maker, mock_dependencies):
mock_user_cls, mock_select = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
@@ -59,7 +59,7 @@ async def test_change_password_success(mock_session_maker, mock_dependencies):
mock_statement = MagicMock()
mock_select.return_value.where.return_value = mock_statement
from pretor.utils.access import Accessor
from kilostar.utils.access import Accessor
mock_user = MagicMock()
mock_user.hashed_password = Accessor.hash_password("old_password")
@@ -80,7 +80,7 @@ async def test_change_password_success(mock_session_maker, mock_dependencies):
@pytest.mark.asyncio
async def test_change_password_user_not_exist(mock_session_maker, mock_dependencies):
mock_user_cls, mock_select = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
@@ -96,12 +96,12 @@ async def test_change_password_user_not_exist(mock_session_maker, mock_dependenc
@pytest.mark.asyncio
async def test_change_password_wrong_password(mock_session_maker, mock_dependencies):
mock_user_cls, mock_select = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
from pretor.utils.access import Accessor
from kilostar.utils.access import Accessor
mock_user = MagicMock()
mock_user.hashed_password = Accessor.hash_password("actual_password")
@@ -109,7 +109,7 @@ async def test_change_password_wrong_password(mock_session_maker, mock_dependenc
mock_exec_result.scalar_one_or_none.return_value = mock_user
session.execute = AsyncMock(return_value=mock_exec_result)
from pretor.utils.error import UserPasswordError
from kilostar.utils.error import UserPasswordError
with pytest.raises(UserPasswordError):
await db.change_password("testuser", "old_password", "new_password")
@@ -118,7 +118,7 @@ async def test_change_password_wrong_password(mock_session_maker, mock_dependenc
@pytest.mark.asyncio
async def test_delete_user_success(mock_session_maker, mock_dependencies):
mock_user_cls, mock_select = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
@@ -140,7 +140,7 @@ async def test_delete_user_success(mock_session_maker, mock_dependencies):
@pytest.mark.asyncio
async def test_delete_user_not_exist(mock_session_maker, mock_dependencies):
mock_user_cls, mock_select = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
@@ -156,7 +156,7 @@ async def test_delete_user_not_exist(mock_session_maker, mock_dependencies):
@pytest.mark.asyncio
async def test_login_user_success(mock_session_maker, mock_dependencies):
mock_user_cls, mock_select = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
@@ -177,7 +177,7 @@ async def test_login_user_success(mock_session_maker, mock_dependencies):
@pytest.mark.asyncio
async def test_login_user_not_exist(mock_session_maker, mock_dependencies):
mock_user_cls, mock_select = mock_dependencies
from pretor.core.postgres_database.module import AuthDatabase
from kilostar.core.postgres_database.module import AuthDatabase
maker, session = mock_session_maker
db = AuthDatabase(maker)
@@ -1,4 +1,4 @@
from pretor.core.postgres_database.table import Provider
from kilostar.core.postgres_database.model import Provider
def test_provider_table():
+1 -1
View File
@@ -1,4 +1,4 @@
from pretor.core.postgres_database.table import User
from kilostar.core.postgres_database.model import User
def test_user_table():
@@ -27,10 +27,10 @@ def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
builtins.__import__ = mock_import
for mod in list(sys.modules.keys()):
if "pretor.core.global_state_machine.global_state_machine" in mod or "ray" in mod:
if "kilostar.core.global_state_machine.global_state_machine" in mod or "ray" in mod:
del sys.modules[mod]
from pretor.core.global_state_machine.global_state_machine import GlobalStateMachine # noqa: E402
from kilostar.core.global_state_machine.global_state_machine import GlobalStateMachine # noqa: E402
builtins.__import__ = real_import
@@ -74,7 +74,7 @@ async def test_add_provider_success(gsm, mock_postgres):
@pytest.mark.asyncio
async def test_add_provider_unsupported(gsm):
gsm._global_provider_manager.provider_mapper = {}
with patch("pretor.utils.logger.global_logger.bind") as mock_bind:
with patch("kilostar.utils.logger.global_logger.bind") as mock_bind:
mock_logger = MagicMock()
mock_bind.return_value = mock_logger
await gsm.add_provider_wrap("magic", "title", "url", "key", "1")
@@ -91,8 +91,8 @@ async def test_add_provider_request_error(gsm):
)
gsm._global_provider_manager.provider_mapper = {"openai": mock_provider_class}
with patch("pretor.utils.logger.global_logger.bind") as mock_bind:
from pretor.utils.error import RetryableError
with patch("kilostar.utils.logger.global_logger.bind") as mock_bind:
from kilostar.utils.error import RetryableError
import pytest
mock_logger = MagicMock()
@@ -108,7 +108,7 @@ async def test_add_provider_generic_error(gsm):
mock_provider_class.create_provider.side_effect = ValueError("Some Error")
gsm._global_provider_manager.provider_mapper = {"openai": mock_provider_class}
with patch("pretor.utils.logger.global_logger.bind") as mock_bind:
with patch("kilostar.utils.logger.global_logger.bind") as mock_bind:
mock_logger = MagicMock()
mock_bind.return_value = mock_logger
await gsm.add_provider_wrap("openai", "title", "url", "key", "1")
@@ -1,4 +1,4 @@
from pretor.core.global_state_machine.model_provider.base_provider import (
from kilostar.core.global_state_machine.model_provider.base_provider import (
Provider,
ProviderArgs,
ProviderStatus,
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import patch, MagicMock, AsyncMock
from pretor.core.global_state_machine.model_provider.claude_provider import (
from kilostar.core.global_state_machine.model_provider.claude_provider import (
ClaudeProvider,
ProviderArgs,
)
@@ -18,7 +18,7 @@ def provider_args():
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.claude_provider.httpx.AsyncClient"
"kilostar.core.global_state_machine.model_provider.claude_provider.httpx.AsyncClient"
)
async def test_load_models_success(mock_client, provider_args):
mock_response = MagicMock()
@@ -37,7 +37,7 @@ async def test_load_models_success(mock_client, provider_args):
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.claude_provider.httpx.AsyncClient"
"kilostar.core.global_state_machine.model_provider.claude_provider.httpx.AsyncClient"
)
async def test_load_models_error(mock_client, provider_args):
mock_client_instance = AsyncMock()
@@ -50,7 +50,7 @@ async def test_load_models_error(mock_client, provider_args):
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.claude_provider.ClaudeProvider._load_models",
"kilostar.core.global_state_machine.model_provider.claude_provider.ClaudeProvider._load_models",
return_value=["claude-3"],
)
async def test_create_provider(mock_load, provider_args):
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import patch, MagicMock, AsyncMock
from pretor.core.global_state_machine.model_provider.openai_provider import (
from kilostar.core.global_state_machine.model_provider.openai_provider import (
OpenAIProvider,
ProviderArgs,
)
@@ -28,7 +28,7 @@ def provider_args_no_v1():
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
"kilostar.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
)
async def test_load_models_success(mock_client, provider_args):
mock_response = MagicMock()
@@ -51,7 +51,7 @@ async def test_load_models_success(mock_client, provider_args):
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
"kilostar.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
)
async def test_load_models_no_v1(mock_client, provider_args_no_v1):
mock_response = MagicMock()
@@ -72,7 +72,7 @@ async def test_load_models_no_v1(mock_client, provider_args_no_v1):
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
"kilostar.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
)
async def test_load_models_status_error(mock_client, provider_args):
mock_response = MagicMock()
@@ -88,7 +88,7 @@ async def test_load_models_status_error(mock_client, provider_args):
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
"kilostar.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
)
async def test_load_models_request_error(mock_client, provider_args):
import httpx
@@ -100,7 +100,7 @@ async def test_load_models_request_error(mock_client, provider_args):
mock_client.return_value.__aenter__.return_value = mock_client_instance
import pytest
from pretor.utils.error import RetryableError
from kilostar.utils.error import RetryableError
with pytest.raises(RetryableError):
await OpenAIProvider._load_models(provider_args)
@@ -108,7 +108,7 @@ async def test_load_models_request_error(mock_client, provider_args):
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
"kilostar.core.global_state_machine.model_provider.openai_provider.httpx.AsyncClient"
)
async def test_load_models_generic_error(mock_client, provider_args):
mock_client_instance = AsyncMock()
@@ -121,7 +121,7 @@ async def test_load_models_generic_error(mock_client, provider_args):
@pytest.mark.asyncio
@patch(
"pretor.core.global_state_machine.model_provider.openai_provider.OpenAIProvider._load_models",
"kilostar.core.global_state_machine.model_provider.openai_provider.OpenAIProvider._load_models",
return_value=["gpt-4"],
)
async def test_create_provider(mock_load, provider_args):
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import MagicMock, AsyncMock
from pretor.core.global_state_machine.provider_manager import ProviderManager
from kilostar.core.global_state_machine.provider_manager import ProviderManager
@pytest.mark.asyncio
@@ -1,4 +1,4 @@
from pretor.core.global_state_machine.tool_manager import GlobalToolManager
from kilostar.core.global_state_machine.tool_manager import GlobalToolManager
def test_global_tool_manager_init():
@@ -26,19 +26,19 @@ def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
builtins.__import__ = mock_import
for mod in list(sys.modules.keys()):
if "pretor.core.postgres_database.postgres" in mod or "ray" in mod:
if "kilostar.core.postgres_database.postgres" in mod or "ray" in mod:
del sys.modules[mod]
from pretor.core.postgres_database.postgres import PostgresDatabase # noqa: E402
from kilostar.core.postgres_database.postgres import PostgresDatabase # noqa: E402
builtins.__import__ = real_import
@patch("pretor.core.postgres_database.postgres.create_async_engine")
@patch("pretor.core.postgres_database.postgres.sessionmaker")
@patch("pretor.core.postgres_database.postgres.AuthDatabase")
@patch("pretor.core.postgres_database.postgres.ProviderDatabase")
@patch("pretor.core.postgres_database.postgres.os.environ.get")
@patch("kilostar.core.postgres_database.postgres.create_async_engine")
@patch("kilostar.core.postgres_database.postgres.sessionmaker")
@patch("kilostar.core.postgres_database.postgres.AuthDatabase")
@patch("kilostar.core.postgres_database.postgres.ProviderDatabase")
@patch("kilostar.core.postgres_database.postgres.os.environ.get")
@pytest.mark.asyncio
async def test_postgres_database(
mock_env_get, mock_provider_db, mock_auth_db, mock_sessionmaker, mock_create_engine
@@ -76,7 +76,7 @@ async def test_postgres_database(
mock_auth_db.return_value.get_user_authority = AsyncMock(return_value="test_auth")
with patch(
"pretor.core.postgres_database.postgres.SQLModel.metadata.create_all"
"kilostar.core.postgres_database.postgres.SQLModel.metadata.create_all"
) as mock_create_all:
await db.init_db()
mock_conn.run_sync.assert_called_once_with(mock_create_all)
+13 -13
View File
@@ -1,7 +1,7 @@
import pytest
from pretor.core.workflow.workflow import (
from kilostar.core.workflow_running_engine.workflow import (
WorkStep,
PretorWorkflow,
kilostarWorkflow,
WorkflowStatus,
LogicGate,
)
@@ -23,10 +23,10 @@ def test_work_step():
assert ws.status == "waiting"
def test_pretor_workflow_validation_success():
def test_kilostar_workflow_validation_success():
ws1 = WorkStep(step=1, name="s1", node="control_node", action="a1", desc="d1")
ws2 = WorkStep(step=2, name="s2", node="supervisory_node", action="a2", desc="d2")
wf = PretorWorkflow(
ws2 = WorkStep(step=2, name="s2", node="regulatory_node", action="a2", desc="d2")
wf = kilostarWorkflow(
title="wf1",
work_link=[ws1, ws2],
trace_id="t",
@@ -35,11 +35,11 @@ def test_pretor_workflow_validation_success():
assert wf.title == "wf1"
def test_pretor_workflow_validation_error_step_discontinuous():
def test_kilostar_workflow_validation_error_step_discontinuous():
ws1 = WorkStep(step=1, name="s1", node="control_node", action="a1", desc="d1")
ws2 = WorkStep(step=3, name="s3", node="supervisory_node", action="a2", desc="d2")
ws2 = WorkStep(step=3, name="s3", node="regulatory_node", action="a2", desc="d2")
with pytest.raises(ValueError, match="工作链步数不连续"):
PretorWorkflow(
kilostarWorkflow(
title="wf1",
work_link=[ws1, ws2],
trace_id="t",
@@ -47,14 +47,14 @@ def test_pretor_workflow_validation_error_step_discontinuous():
)
def test_pretor_workflow_validation_error_jump_out_of_bounds():
def test_kilostar_workflow_validation_error_jump_out_of_bounds():
lg = LogicGate(if_fail="jump_to_step_3", if_pass="continue")
ws1 = WorkStep(
step=1, name="s1", node="control_node", action="a1", desc="d1", logic_gate=lg
)
ws2 = WorkStep(step=2, name="s2", node="supervisory_node", action="a2", desc="d2")
ws2 = WorkStep(step=2, name="s2", node="regulatory_node", action="a2", desc="d2")
with pytest.raises(ValueError, match="跳转目标 Step 3 越界了"):
PretorWorkflow(
kilostarWorkflow(
title="wf1",
work_link=[ws1, ws2],
trace_id="t",
@@ -62,13 +62,13 @@ def test_pretor_workflow_validation_error_jump_out_of_bounds():
)
def test_pretor_workflow_validation_error_jump_format_error():
def test_kilostar_workflow_validation_error_jump_format_error():
lg = LogicGate(if_fail="jump_to_step_invalid", if_pass="continue")
ws1 = WorkStep(
step=1, name="s1", node="control_node", action="a1", desc="d1", logic_gate=lg
)
with pytest.raises(ValueError, match="LogicGate 格式错误"):
PretorWorkflow(
kilostarWorkflow(
title="wf1",
work_link=[ws1],
trace_id="t",
@@ -28,9 +28,9 @@ def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
builtins.__import__ = mock_import
for mod in list(sys.modules.keys()):
if "pretor.core.workflow_running_engine.workflow_runner" in mod or "ray" in mod:
if "kilostar.core.workflow_running_engine.workflow_runner" in mod or "ray" in mod:
del sys.modules[mod]
from pretor.core.workflow_running_engine.workflow_runner import ( # noqa: E402
from kilostar.core.workflow_running_engine.workflow_runner import ( # noqa: E402
WorkflowEngine,
WorkflowRunningEngine,
)
@@ -40,7 +40,7 @@ builtins.__import__ = real_import
@pytest.fixture
def mock_ray():
with patch("pretor.core.workflow_running_engine.workflow_runner.ray") as mock_ray:
with patch("kilostar.core.workflow_running_engine.workflow_runner.ray") as mock_ray:
mock_ray.get = lambda x: x
yield mock_ray
@@ -52,14 +52,14 @@ def test_workflow_engine_init():
assert engine.workflow == mock_wf
assert engine.consciousness_node == "conscious"
assert engine.control_node == "control"
assert engine.supervisory_node == "supervisor"
assert engine.regulatory_node == "supervisor"
@pytest.mark.asyncio
async def test_workflow_engine_run():
from pretor.core.workflow.workflow import PretorWorkflow, WorkStep, WorkflowStatus
from kilostar.core.workflow_running_engine.workflow import kilostarWorkflow, WorkStep, WorkflowStatus
mock_wf = MagicMock(spec=PretorWorkflow)
mock_wf = MagicMock(spec=kilostarWorkflow)
step1 = MagicMock(spec=WorkStep)
step1.step = 1
@@ -97,7 +97,7 @@ async def test_workflow_engine_run():
engine = WorkflowEngine(mock_wf, mock_conscious, mock_control, mock_supervisor)
with patch(
"pretor.core.workflow_running_engine.workflow_runner.ray"
"kilostar.core.workflow_running_engine.workflow_runner.ray"
) as mock_ray_patch:
mock_gsm = MagicMock()
mock_ray_patch.get_actor.return_value = mock_gsm
@@ -111,7 +111,7 @@ def test_workflow_running_engine_init():
engine = WorkflowRunningEngine("conscious", "control", "supervisor")
assert engine.consciousness_node == "conscious"
assert engine.control_node == "control"
assert engine.supervisory_node == "supervisor"
assert engine.regulatory_node == "supervisor"
@pytest.mark.asyncio
@@ -128,8 +128,8 @@ async def test_workflow_running_engine_submit():
@pytest.mark.asyncio
async def test_workflow_running_engine_runner():
from pretor.api.platform.event import PretorEvent
from pretor.core.individual.consciousness_node.template import ForWorkflowEngine
from kilostar.api.platform.event import kilostarEvent
from kilostar.core.individual.consciousness_node.template import ForWorkflowEngine
mock_consciousness = MagicMock()
mock_wf = MagicMock()
@@ -143,7 +143,7 @@ async def test_workflow_running_engine_runner():
engine = WorkflowRunningEngine(mock_consciousness, "control", "supervisor")
engine.workflow_queue = asyncio.Queue()
mock_event = PretorEvent(
mock_event = kilostarEvent(
platform="test_platform",
user_id="test_user",
user_name="test_user",
@@ -167,11 +167,11 @@ async def test_workflow_running_engine_runner():
with (
patch(
"pretor.core.workflow_running_engine.workflow_runner.WorkflowEngine"
"kilostar.core.workflow_running_engine.workflow_runner.WorkflowEngine"
) as mock_wf_engine_cls,
patch("builtins.open", new_callable=MagicMock) as mock_open,
patch(
"pretor.core.workflow_running_engine.workflow_runner.ray_actor_hook"
"kilostar.core.workflow_running_engine.workflow_runner.ray_actor_hook"
) as mock_hook,
):
# Instead of patching hook, we inject it directly
+8 -8
View File
@@ -26,11 +26,11 @@ sys.modules["pydantic"] = mock_pydantic
sys.modules["sqlmodel"] = MagicMock()
sys.modules["passlib"] = MagicMock()
sys.modules["passlib.context"] = MagicMock()
sys.modules["pretor.core.database.table.user"] = MagicMock()
sys.modules["kilostar.core.database.table.user"] = MagicMock()
import pytest # noqa: E402
import jwt # noqa: E402
from pretor.utils.access import Accessor # noqa: E402
from kilostar.utils.access import Accessor # noqa: E402
def test_decode_token_success():
@@ -39,7 +39,7 @@ def test_decode_token_success():
payload = {"user_id": "123", "username": "testuser", "exp": 1234567890}
with patch("jwt.decode", return_value=payload) as mock_decode:
with patch("pretor.utils.access.TokenData") as mock_token_data_cls:
with patch("kilostar.utils.access.TokenData") as mock_token_data_cls:
mock_token_data_instance = MagicMock()
mock_token_data_cls.return_value = mock_token_data_instance
@@ -57,7 +57,7 @@ def test_decode_token_expired():
from fastapi import HTTPException
with patch("jwt.decode", side_effect=jwt.ExpiredSignatureError):
with patch("pretor.utils.access.HTTPException", HTTPException):
with patch("kilostar.utils.access.HTTPException", HTTPException):
with pytest.raises(HTTPException) as excinfo:
Accessor._decode_token(token)
@@ -72,7 +72,7 @@ def test_decode_token_invalid():
from fastapi import HTTPException
with patch("jwt.decode", side_effect=jwt.InvalidTokenError):
with patch("pretor.utils.access.HTTPException", HTTPException):
with patch("kilostar.utils.access.HTTPException", HTTPException):
with pytest.raises(HTTPException) as excinfo:
Accessor._decode_token(token)
@@ -88,9 +88,9 @@ def test_decode_token_validation_error():
from fastapi import HTTPException
with patch("jwt.decode", return_value=payload):
with patch("pretor.utils.access.TokenData", side_effect=MockValidationError):
with patch("pretor.utils.access.ValidationError", MockValidationError):
with patch("pretor.utils.access.HTTPException", HTTPException):
with patch("kilostar.utils.access.TokenData", side_effect=MockValidationError):
with patch("kilostar.utils.access.ValidationError", MockValidationError):
with patch("kilostar.utils.access.HTTPException", HTTPException):
with pytest.raises(HTTPException) as excinfo:
Accessor._decode_token(token)