264 lines
9.5 KiB
Python
264 lines
9.5 KiB
Python
"""Unit tests for Jira tools."""
|
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
import pytest
|
|
from extensions.jira.client import JiraClient
|
|
from extensions.jira.tools import (
|
|
jira_get_editable_fields,
|
|
jira_get_issue,
|
|
jira_search,
|
|
jira_search_users,
|
|
jira_update_issue,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_unconfigured():
|
|
with patch("extensions.jira.tools.get_jira_client") as mock_get_client:
|
|
mock_client = MagicMock()
|
|
mock_client.is_configured.return_value = False
|
|
mock_get_client.return_value = mock_client
|
|
|
|
res = await jira_search("assignee = currentUser()")
|
|
assert "Jira не настроена" in res
|
|
|
|
res_update = await jira_update_issue("DEV-1", summary="New")
|
|
assert "Jira не настроена" in res_update
|
|
|
|
res_fields = await jira_get_editable_fields("DEV-1")
|
|
assert "Jira не настроена" in res_fields
|
|
|
|
res_users = await jira_search_users("Ivan")
|
|
assert "Jira не настроена" in res_users
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_search_mocked():
|
|
with patch("extensions.jira.tools.get_jira_client") as mock_get_client:
|
|
mock_client = MagicMock()
|
|
mock_client.is_configured.return_value = True
|
|
mock_client.search_issues = AsyncMock(
|
|
return_value=[
|
|
{
|
|
"key": "DEV-42",
|
|
"fields": {
|
|
"summary": "Fix login bug",
|
|
"status": {"name": "In Progress"},
|
|
"priority": {"name": "High"},
|
|
"assignee": {"displayName": "John Doe"},
|
|
},
|
|
}
|
|
]
|
|
)
|
|
mock_get_client.return_value = mock_client
|
|
|
|
res = await jira_search("assignee = currentUser()")
|
|
assert "DEV-42" in res
|
|
assert "Fix login bug" in res
|
|
assert "In Progress" in res
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_get_editable_fields():
|
|
with patch("extensions.jira.tools.get_jira_client") as mock_get_client:
|
|
mock_client = MagicMock()
|
|
mock_client.is_configured.return_value = True
|
|
mock_client.get_edit_meta = AsyncMock(
|
|
return_value={
|
|
"fields": {
|
|
"summary": {
|
|
"name": "Summary",
|
|
"required": True,
|
|
"schema": {"type": "string"},
|
|
},
|
|
"priority": {
|
|
"name": "Priority",
|
|
"required": False,
|
|
"schema": {"type": "priority"},
|
|
"allowedValues": [{"name": "Highest"}, {"name": "Low"}],
|
|
},
|
|
"customfield_10016": {
|
|
"name": "Story Points",
|
|
"required": False,
|
|
"schema": {"type": "number"},
|
|
},
|
|
}
|
|
}
|
|
)
|
|
mock_get_client.return_value = mock_client
|
|
|
|
res = await jira_get_editable_fields("DEV-42")
|
|
assert "DEV-42" in res
|
|
assert "summary" in res
|
|
assert "[Обязательное]" in res
|
|
assert "Priority" in res
|
|
assert "Highest, Low" in res
|
|
assert "customfield_10016" in res
|
|
assert "Story Points" in res
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_search_users():
|
|
with patch("extensions.jira.tools.get_jira_client") as mock_get_client:
|
|
mock_client = MagicMock()
|
|
mock_client.is_configured.return_value = True
|
|
mock_client.search_users = AsyncMock(
|
|
return_value=[
|
|
{
|
|
"displayName": "Alex Developer",
|
|
"accountId": "acc-12345",
|
|
"emailAddress": "alex@example.com",
|
|
"active": True,
|
|
}
|
|
]
|
|
)
|
|
mock_get_client.return_value = mock_client
|
|
|
|
res = await jira_search_users("Alex")
|
|
assert "Alex Developer" in res
|
|
assert "acc-12345" in res
|
|
assert "alex@example.com" in res
|
|
assert "Активен" in res
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_update_issue_standard_and_custom_fields():
|
|
with patch("extensions.jira.tools.get_jira_client") as mock_get_client:
|
|
mock_client = MagicMock()
|
|
mock_client.is_configured.return_value = True
|
|
mock_client.browse_url.return_value = "https://jira.example.com/browse/DEV-42"
|
|
mock_client.update_issue = AsyncMock(return_value={"status": "success"})
|
|
mock_client.resolve_user = AsyncMock(
|
|
return_value={"accountId": "acc-999", "displayName": "Bob Builder"}
|
|
)
|
|
mock_client.get_edit_meta = AsyncMock(
|
|
return_value={
|
|
"fields": {
|
|
"customfield_10016": {"name": "Story Points", "schema": {"type": "number"}},
|
|
}
|
|
}
|
|
)
|
|
mock_client.get_transitions = AsyncMock(
|
|
return_value=[{"id": "31", "name": "In Progress"}]
|
|
)
|
|
mock_client.transition_issue = AsyncMock()
|
|
mock_get_client.return_value = mock_client
|
|
|
|
res = await jira_update_issue(
|
|
issue_key="dev-42",
|
|
summary="Refactor auth",
|
|
description="New description content",
|
|
status="In Progress",
|
|
assignee="Bob",
|
|
priority="Highest",
|
|
duedate="2026-12-31",
|
|
labels=["backend", "security"],
|
|
components=["Auth Module"],
|
|
issue_type="Bug",
|
|
custom_fields={"Story Points": 8, "customfield_10020": 42},
|
|
)
|
|
|
|
assert "DEV-42" in res
|
|
assert "успешно обновлена" in res
|
|
assert "Refactor auth" in res
|
|
assert "Статус -> 'In Progress'" in res
|
|
assert "Bob Builder" in res
|
|
assert "Highest" in res
|
|
assert "2026-12-31" in res
|
|
assert "Story Points" in res
|
|
|
|
# Verify update_issue payload passed to client
|
|
mock_client.update_issue.assert_awaited_once()
|
|
call_key, call_payload = mock_client.update_issue.call_args[0]
|
|
assert call_key == "DEV-42"
|
|
assert call_payload["summary"] == "Refactor auth"
|
|
assert call_payload["priority"] == {"name": "Highest"}
|
|
assert call_payload["duedate"] == "2026-12-31"
|
|
assert call_payload["labels"] == ["backend", "security"]
|
|
assert call_payload["components"] == [{"name": "Auth Module"}]
|
|
assert call_payload["issuetype"] == {"name": "Bug"}
|
|
assert call_payload["assignee"] == {"accountId": "acc-999"}
|
|
assert call_payload["customfield_10016"] == 8
|
|
assert call_payload["customfield_10020"] == 42
|
|
assert "version" in call_payload["description"]
|
|
|
|
# Verify transition was called
|
|
mock_client.transition_issue.assert_awaited_once_with("DEV-42", "31")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_update_issue_unassign_and_clear_due():
|
|
with patch("extensions.jira.tools.get_jira_client") as mock_get_client:
|
|
mock_client = MagicMock()
|
|
mock_client.is_configured.return_value = True
|
|
mock_client.browse_url.return_value = "https://jira.example.com/browse/DEV-42"
|
|
mock_client.update_issue = AsyncMock(return_value={"status": "success"})
|
|
mock_get_client.return_value = mock_client
|
|
|
|
res = await jira_update_issue(
|
|
issue_key="DEV-42",
|
|
assignee="unassigned",
|
|
duedate="clear",
|
|
)
|
|
|
|
assert "DEV-42" in res
|
|
assert "Исполнитель -> Снят (Unassigned)" in res
|
|
assert "Дедлайн -> Снят" in res
|
|
|
|
mock_client.update_issue.assert_awaited_once()
|
|
call_key, call_payload = mock_client.update_issue.call_args[0]
|
|
assert call_payload["assignee"] is None
|
|
assert call_payload["duedate"] is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_update_issue_no_fields():
|
|
with patch("extensions.jira.tools.get_jira_client") as mock_get_client:
|
|
mock_client = MagicMock()
|
|
mock_client.is_configured.return_value = True
|
|
mock_get_client.return_value = mock_client
|
|
|
|
res = await jira_update_issue(issue_key="DEV-42")
|
|
assert "Не указаны поля" in res
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_client_update_and_meta():
|
|
client = JiraClient(base_url="https://jira.example.com", email="test@example.com", api_token="secret")
|
|
client._request = AsyncMock(return_value={"status": "success", "status_code": 204})
|
|
|
|
# Test update_issue v3
|
|
await client.update_issue("DEV-10", {"summary": "Updated"})
|
|
client._request.assert_awaited_with(
|
|
"PUT",
|
|
"rest/api/3/issue/DEV-10",
|
|
json_data={"fields": {"summary": "Updated"}},
|
|
)
|
|
|
|
# Test get_edit_meta
|
|
client._request.return_value = {"fields": {"summary": {}}}
|
|
meta = await client.get_edit_meta("DEV-10")
|
|
assert "summary" in meta["fields"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_jira_client_resolve_user():
|
|
client = JiraClient(base_url="https://jira.example.com", email="test@example.com", api_token="secret")
|
|
client.search_users = AsyncMock(
|
|
return_value=[
|
|
{"displayName": "Alice Smith", "emailAddress": "alice@company.com", "accountId": "acc-alice"},
|
|
{"displayName": "Bob Builder", "emailAddress": "bob@company.com", "accountId": "acc-bob"},
|
|
]
|
|
)
|
|
|
|
# Test exact email match
|
|
u1 = await client.resolve_user("bob@company.com")
|
|
assert u1 is not None
|
|
assert u1["accountId"] == "acc-bob"
|
|
|
|
# Test unassigned
|
|
u_none = await client.resolve_user("unassigned")
|
|
assert u_none is None
|
|
|
|
|