57 lines
2.3 KiB
Python
57 lines
2.3 KiB
Python
import pytest
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
# Mock the firestore client before it's imported by the server
|
|
@patch("google.cloud.firestore.Client")
|
|
def test_create_a2h2a_ticket_dry_run(mock_firestore_client):
|
|
from server import create_a2h2a_ticket
|
|
import asyncio
|
|
|
|
mock_db = MagicMock()
|
|
mock_firestore_client.return_value = mock_db
|
|
|
|
dry_run_payload = {
|
|
"dry_run": True,
|
|
"source": {"reporter": "test", "trigger": "test", "affected_service": "test", "project_id": "test"},
|
|
"proposed_action": {"execution_tool": "test", "tool_parameters": {}},
|
|
"context": {"summary": "test", "user_justification": "test", "conversation_history": []},
|
|
"severity": "LOW",
|
|
"governance": {"approval_status": "PENDING", "authorized_approver": "test", "requires_mfa": True, "timeout_minutes": 15}
|
|
}
|
|
|
|
result = asyncio.run(create_a2h2a_ticket(dry_run_payload))
|
|
|
|
assert result["dry_run"] is True
|
|
assert result["message"] == "Ticket preview only; no ticket was created."
|
|
assert "proposed_ticket" in result
|
|
mock_db.collection.assert_not_called()
|
|
|
|
@patch("google.cloud.firestore.Client")
|
|
def test_create_a2h2a_ticket_live_run(mock_firestore_client):
|
|
from server import create_a2h2a_ticket
|
|
import asyncio
|
|
|
|
mock_db = MagicMock()
|
|
mock_firestore_client.return_value = mock_db
|
|
mock_collection = MagicMock()
|
|
mock_db.collection.return_value = mock_collection
|
|
mock_document = MagicMock()
|
|
mock_collection.document.return_value = mock_document
|
|
|
|
live_run_payload = {
|
|
"dry_run": False,
|
|
"source": {"reporter": "test", "trigger": "test", "affected_service": "test", "project_id": "test"},
|
|
"proposed_action": {"execution_tool": "test", "tool_parameters": {}},
|
|
"context": {"summary": "test", "user_justification": "test", "conversation_history": []},
|
|
"severity": "LOW",
|
|
"governance": {"approval_status": "PENDING", "authorized_approver": "test", "requires_mfa": True, "timeout_minutes": 15}
|
|
}
|
|
|
|
result = asyncio.run(create_a2h2a_ticket(live_run_payload))
|
|
|
|
assert result["status"] == "created"
|
|
assert "ticket_id" in result
|
|
assert "review_url" in result
|
|
mock_db.collection.assert_called_once_with("a2h2a_tickets")
|
|
mock_collection.document.assert_called_once()
|
|
mock_document.set.assert_called_once() |