186 lines
6.2 KiB
Python
186 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
from io import StringIO
|
|
import sys
|
|
|
|
from kumacli.cmd.maintenance import MaintenanceCommands, handle_maintenance_command
|
|
|
|
|
|
class TestMaintenanceCommands:
|
|
def test_list_maintenances_success(self, mock_client, mock_maintenances, capsys):
|
|
"""Test successful maintenance listing"""
|
|
# Setup
|
|
mock_client.api.get_maintenances.return_value = mock_maintenances
|
|
|
|
maintenance_commands = MaintenanceCommands(mock_client)
|
|
|
|
# Execute
|
|
maintenance_commands.list_maintenances()
|
|
|
|
# Verify
|
|
mock_client.api.get_maintenances.assert_called_once()
|
|
captured = capsys.readouterr()
|
|
assert "Test Maintenance" in captured.out
|
|
assert "Inactive Maintenance" in captured.out
|
|
assert "Active" in captured.out
|
|
assert "Inactive" in captured.out
|
|
|
|
def test_list_maintenances_empty(self, mock_client, capsys):
|
|
"""Test maintenance listing with no maintenances"""
|
|
# Setup
|
|
mock_client.api.get_maintenances.return_value = []
|
|
|
|
maintenance_commands = MaintenanceCommands(mock_client)
|
|
|
|
# Execute
|
|
maintenance_commands.list_maintenances()
|
|
|
|
# Verify
|
|
captured = capsys.readouterr()
|
|
assert "No maintenances found" in captured.out
|
|
|
|
def test_list_maintenances_api_error(self, mock_client, capsys):
|
|
"""Test maintenance listing with API error"""
|
|
from uptime_kuma_api import UptimeKumaException
|
|
|
|
# Setup
|
|
mock_client.api.get_maintenances.side_effect = UptimeKumaException("API Error")
|
|
|
|
maintenance_commands = MaintenanceCommands(mock_client)
|
|
|
|
# Execute
|
|
maintenance_commands.list_maintenances()
|
|
|
|
# Verify
|
|
captured = capsys.readouterr()
|
|
assert "Error listing maintenances: API Error" in captured.out
|
|
|
|
def test_delete_maintenance_by_id(self, mock_client, capsys):
|
|
"""Test deleting maintenance by ID"""
|
|
# Setup
|
|
mock_maintenance = {"id": 1, "title": "Test Maintenance"}
|
|
mock_client.api.get_maintenance.return_value = mock_maintenance
|
|
mock_client.api.delete_maintenance.return_value = {
|
|
"msg": "Deleted Successfully"
|
|
}
|
|
|
|
maintenance_commands = MaintenanceCommands(mock_client)
|
|
|
|
# Execute
|
|
maintenance_commands.delete_maintenance(maintenance_id=1)
|
|
|
|
# Verify
|
|
mock_client.api.get_maintenance.assert_called_once_with(1)
|
|
mock_client.api.delete_maintenance.assert_called_once_with(1)
|
|
captured = capsys.readouterr()
|
|
assert (
|
|
"Successfully deleted maintenance 'Test Maintenance' (ID: 1)"
|
|
in captured.out
|
|
)
|
|
|
|
def test_delete_all_maintenances(self, mock_client, mock_maintenances, capsys):
|
|
"""Test deleting all maintenances"""
|
|
# Setup
|
|
mock_client.api.get_maintenances.return_value = mock_maintenances
|
|
mock_client.api.delete_maintenance.return_value = {
|
|
"msg": "Deleted Successfully"
|
|
}
|
|
|
|
maintenance_commands = MaintenanceCommands(mock_client)
|
|
|
|
# Execute
|
|
maintenance_commands.delete_maintenance(delete_all=True)
|
|
|
|
# Verify
|
|
assert mock_client.api.delete_maintenance.call_count == 2
|
|
captured = capsys.readouterr()
|
|
assert "Found 2 maintenances to delete:" in captured.out
|
|
assert "Successfully deleted 2 out of 2 maintenances" in captured.out
|
|
|
|
def test_delete_maintenance_no_params(self, mock_client, capsys):
|
|
"""Test deleting maintenance without parameters"""
|
|
maintenance_commands = MaintenanceCommands(mock_client)
|
|
|
|
# Execute
|
|
maintenance_commands.delete_maintenance()
|
|
|
|
# Verify
|
|
captured = capsys.readouterr()
|
|
assert (
|
|
"Error: Either --id or --all flag is required for delete operation"
|
|
in captured.out
|
|
)
|
|
|
|
|
|
class TestMaintenanceCommandHandler:
|
|
def test_handle_maintenance_command_no_action(self, mock_client, capsys):
|
|
"""Test maintenance command handler with no action"""
|
|
# Setup
|
|
mock_args = Mock()
|
|
mock_args.maintenance_action = None
|
|
|
|
# Mock the parser setup
|
|
with patch("kumacli.cmd.maintenance.setup_maintenance_parser") as mock_setup:
|
|
mock_parser = Mock()
|
|
mock_setup._parser = mock_parser
|
|
|
|
# Execute
|
|
result = handle_maintenance_command(mock_args, mock_client)
|
|
|
|
# Verify
|
|
assert result is False
|
|
|
|
def test_handle_maintenance_command_list(self, mock_client, mock_maintenances):
|
|
"""Test maintenance command handler for list action"""
|
|
# Setup
|
|
mock_args = Mock()
|
|
mock_args.maintenance_action = "list"
|
|
mock_client.api.get_maintenances.return_value = mock_maintenances
|
|
|
|
# Execute
|
|
result = handle_maintenance_command(mock_args, mock_client)
|
|
|
|
# Verify
|
|
assert result is True
|
|
mock_client.api.get_maintenances.assert_called_once()
|
|
|
|
def test_handle_maintenance_command_delete(self, mock_client):
|
|
"""Test maintenance command handler for delete action"""
|
|
# Setup
|
|
mock_args = Mock()
|
|
mock_args.maintenance_action = "delete"
|
|
mock_args.id = 1
|
|
mock_args.all = False
|
|
|
|
mock_maintenance = {"id": 1, "title": "Test Maintenance"}
|
|
mock_client.api.get_maintenance.return_value = mock_maintenance
|
|
mock_client.api.delete_maintenance.return_value = {
|
|
"msg": "Deleted Successfully"
|
|
}
|
|
|
|
# Execute
|
|
result = handle_maintenance_command(mock_args, mock_client)
|
|
|
|
# Verify
|
|
assert result is True
|
|
mock_client.api.delete_maintenance.assert_called_once_with(1)
|
|
|
|
def test_handle_maintenance_command_unknown_action(self, mock_client, capsys):
|
|
"""Test maintenance command handler with unknown action"""
|
|
# Setup
|
|
mock_args = Mock()
|
|
mock_args.maintenance_action = "unknown"
|
|
|
|
# Execute
|
|
result = handle_maintenance_command(mock_args, mock_client)
|
|
|
|
# Verify
|
|
assert result is False
|
|
captured = capsys.readouterr()
|
|
assert (
|
|
"Unknown maintenance action. Use --help for usage information."
|
|
in captured.out
|
|
)
|