fix: improve maintenance and synchronize live conversations
This commit is contained in:
1 parent
bb541bd212
commit
ef86a5d3af
297 files changed
+2983
-1274
No files matched your search
@@ -16,14 +16,8 @@ limitations under the License.
|
||||
|
||||
import pytest
|
||||
import json
|
||||
import os
|
||||
import requests
|
||||
from unittest.mock import patch, MagicMock, AsyncMock
|
||||
from unittest.mock import patch, MagicMock
|
||||
from app.tools.jira_toolkit import JiraTools
|
||||
from app.models.jira import AgentJiraConfig, JiraToken
|
||||
from app.models.organization import Organization
|
||||
from app.models.agent import Agent
|
||||
from app.models.session_to_agent import SessionToAgent
|
||||
from uuid import UUID
|
||||
from datetime import timedelta
|
||||
|
||||
@@ -77,7 +71,9 @@ def mock_session_repo():
|
||||
|
||||
@pytest.fixture
|
||||
def mock_jira_service():
|
||||
service = AsyncMock()
|
||||
# JiraTools calls validate_token synchronously; an AsyncMock would hand it
|
||||
# a coroutine object and silently treat that as a truthy validation result.
|
||||
service = MagicMock()
|
||||
service.create_issue.return_value = {
|
||||
"key": "TEST-123",
|
||||
"status": {"name": "Open"},
|
||||
@@ -194,7 +190,7 @@ def test_check_existing_ticket_none(mock_db, mock_jira_service):
|
||||
# Arrange
|
||||
with patch("app.tools.jira_toolkit.JiraService", return_value=mock_jira_service):
|
||||
with patch("app.tools.jira_toolkit.SessionToAgentRepository", return_value=MagicMock()) as mock_session_repo_class:
|
||||
with patch("asyncio.run", side_effect=lambda x: x) as mock_asyncio_run:
|
||||
with patch("asyncio.run", side_effect=lambda x: x):
|
||||
mock_session_repo = mock_session_repo_class.return_value
|
||||
mock_session_repo.get_session.return_value = None
|
||||
|
||||
@@ -205,9 +201,6 @@ def test_check_existing_ticket_none(mock_db, mock_jira_service):
|
||||
)
|
||||
jira_tools.db = mock_db
|
||||
|
||||
# Mock the _check_existing_ticket method
|
||||
jira_tools._check_existing_ticket = AsyncMock(return_value={"exists": False, "message": "No ticket found for this session"})
|
||||
|
||||
# Act
|
||||
result_str = jira_tools.check_existing_ticket()
|
||||
|
||||
@@ -222,7 +215,7 @@ def test_check_existing_ticket_exists(mock_db, mock_jira_service):
|
||||
# Arrange
|
||||
with patch("app.tools.jira_toolkit.JiraService", return_value=mock_jira_service):
|
||||
with patch("app.tools.jira_toolkit.SessionToAgentRepository", return_value=MagicMock()) as mock_session_repo_class:
|
||||
with patch("asyncio.run", side_effect=lambda x: x) as mock_asyncio_run:
|
||||
with patch("asyncio.run", side_effect=lambda x: x):
|
||||
mock_session_repo = mock_session_repo_class.return_value
|
||||
mock_session = MagicMock()
|
||||
mock_session.ticket_id = "TEST-123"
|
||||
@@ -330,7 +323,7 @@ def test_create_jira_ticket_disabled_integration(mock_db, mock_jira_service):
|
||||
# Arrange
|
||||
with patch("app.tools.jira_toolkit.JiraService", return_value=mock_jira_service):
|
||||
with patch("app.tools.jira_toolkit.SessionToAgentRepository", return_value=MagicMock()) as mock_session_repo_class:
|
||||
with patch("asyncio.run", side_effect=lambda x: x) as mock_asyncio_run:
|
||||
with patch("asyncio.run", side_effect=lambda x: x):
|
||||
with patch("app.tools.jira_toolkit.SessionLocal") as mock_session_local:
|
||||
mock_session_repo = mock_session_repo_class.return_value
|
||||
mock_session_repo.get_session.return_value = None
|
||||
@@ -393,7 +386,7 @@ def test_create_jira_ticket_no_token(mock_db, mock_jira_service):
|
||||
# Arrange
|
||||
with patch("app.tools.jira_toolkit.JiraService", return_value=mock_jira_service):
|
||||
with patch("app.tools.jira_toolkit.SessionToAgentRepository", return_value=MagicMock()) as mock_session_repo_class:
|
||||
with patch("asyncio.run", side_effect=lambda x: x) as mock_asyncio_run:
|
||||
with patch("asyncio.run", side_effect=lambda x: x):
|
||||
with patch("app.tools.jira_toolkit.SessionLocal") as mock_session_local:
|
||||
mock_session_repo = mock_session_repo_class.return_value
|
||||
mock_session_repo.get_session.return_value = None
|
||||
@@ -457,7 +450,7 @@ def test_get_ticket_status_api_error(mock_db, mock_jira_service):
|
||||
# Arrange
|
||||
with patch("app.tools.jira_toolkit.JiraService", return_value=mock_jira_service):
|
||||
with patch("app.tools.jira_toolkit.SessionToAgentRepository", return_value=MagicMock()) as mock_session_repo_class:
|
||||
with patch("asyncio.run", side_effect=lambda x: x) as mock_asyncio_run:
|
||||
with patch("asyncio.run", side_effect=lambda x: x):
|
||||
with patch("app.tools.jira_toolkit.SessionLocal") as mock_session_local:
|
||||
mock_session_repo = mock_session_repo_class.return_value
|
||||
mock_session = MagicMock()
|
||||
@@ -620,7 +613,8 @@ async def test_create_jira_ticket_token_refresh_failure(mock_db, mock_jira_servi
|
||||
with patch("app.tools.jira_toolkit.JiraService", return_value=mock_jira_service):
|
||||
with patch("app.tools.jira_toolkit.SessionToAgentRepository", return_value=MagicMock()) as mock_session_repo_class:
|
||||
with patch("requests.post") as mock_post:
|
||||
with patch("os.getenv") as mock_getenv:
|
||||
with patch("app.tools.jira_toolkit.settings.JIRA_CLIENT_ID", "test_client_id"), \
|
||||
patch("app.tools.jira_toolkit.settings.JIRA_CLIENT_SECRET", "test_client_secret"):
|
||||
with patch("datetime.datetime") as mock_datetime:
|
||||
with patch("datetime.timedelta", timedelta):
|
||||
with patch("app.tools.jira_toolkit.SessionLocal") as mock_session_local:
|
||||
@@ -628,12 +622,6 @@ async def test_create_jira_ticket_token_refresh_failure(mock_db, mock_jira_servi
|
||||
mock_now = MagicMock()
|
||||
mock_datetime.utcnow.return_value = mock_now
|
||||
|
||||
# Setup environment variables
|
||||
mock_getenv.side_effect = lambda x: {
|
||||
"JIRA_CLIENT_ID": "test_client_id",
|
||||
"JIRA_CLIENT_SECRET": "test_client_secret"
|
||||
}.get(x)
|
||||
|
||||
# Setup SessionLocal to return our test db when used as context manager
|
||||
mock_session_local.return_value.__enter__.return_value = mock_db
|
||||
mock_session_local.return_value.__exit__.return_value = None
|
||||
@@ -696,7 +684,7 @@ async def test_create_jira_ticket_token_refresh_failure(mock_db, mock_jira_servi
|
||||
|
||||
# Assert
|
||||
assert result["success"] is False
|
||||
assert "Failed to create Jira ticket: Invalid refresh token" in result["message"]
|
||||
assert "Failed to refresh Jira token: Invalid refresh token" in result["message"]
|
||||
assert mock_post.call_count == 1 # Only the failed token refresh attempt
|
||||
assert not mock_db.commit.called # Token should not be updated in database
|
||||
|
||||
@@ -1295,7 +1283,7 @@ async def test_create_jira_ticket_update_description_error(mock_db, mock_jira_se
|
||||
with patch("asyncio.run", side_effect=lambda x: x):
|
||||
with patch("datetime.datetime") as mock_datetime:
|
||||
with patch("datetime.timedelta", timedelta):
|
||||
with patch("requests.post") as mock_post:
|
||||
with patch("requests.post"):
|
||||
with patch("requests.get") as mock_get:
|
||||
with patch("requests.put") as mock_put:
|
||||
with patch("app.tools.jira_toolkit.SessionLocal") as mock_session_local:
|
||||
@@ -1401,4 +1389,4 @@ async def test_create_jira_ticket_update_description_error(mock_db, mock_jira_se
|
||||
# Assert
|
||||
assert result["success"] is False
|
||||
assert "Error updating Jira ticket" in result["message"]
|
||||
assert mock_put.call_count == 0 # No update should be attempted
|
||||
assert mock_put.call_count == 0 # No update should be attempted
|
||||
Reference in new issue
Block a user