File size: 27,997 Bytes
cc036ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 | """
Auto Install Routes Test Coverage
Target: api/auto_install_routes.py (100 lines, 3 endpoints)
Coverage Goal: 75%+ line coverage
Tests cover:
- POST /auto-install/install - Single dependency installation
- POST /auto-install/batch - Batch installation for multiple skills
- GET /auto-install/status/{skill_id} - Installation status check
External dependencies mocked:
- AutoInstallerService (async install_dependencies, batch_install)
- Database (get_db dependency override)
Test pattern: Per-file FastAPI app with TestClient (Phase 177/178 pattern)
"""
import pytest
from unittest.mock import AsyncMock, MagicMock, Mock, patch
from fastapi.testclient import TestClient
from fastapi import FastAPI
from sqlalchemy.orm import Session
# ============================================================================
# Fixtures
# ============================================================================
@pytest.fixture(scope="function")
def mock_auto_installer():
"""
Mock AutoInstallerService with async methods.
Provides deterministic mock responses for install operations:
- install_dependencies: AsyncMock returning success/failure results
- batch_install: AsyncMock returning batch installation results
- _get_image_tag: Mock returning Docker image tag
- _image_exists: Mock returning image existence status
Usage:
def test_install_success(mock_auto_installer):
mock_auto_installer.install_dependencies.return_value = {
"success": True,
"image_tag": "skill-123:python"
}
"""
mock = MagicMock()
# Mock async install_dependencies method
mock.install_dependencies = AsyncMock(return_value={
"success": True,
"image_tag": "atom-skill:skill-123-v1",
"installed_packages": ["numpy", "pandas"],
"total_count": 2
})
# Mock async batch_install method
mock.batch_install = AsyncMock(return_value={
"success": True,
"total": 2,
"successes": 2,
"failures": 0,
"results": [
{
"skill_id": "skill-1",
"result": {"success": True, "image_tag": "atom-skill:skill-1-v1"}
},
{
"skill_id": "skill-2",
"result": {"success": True, "image_tag": "atom-skill:skill-2-v1"}
}
]
})
# Mock _get_image_tag (private method)
mock._get_image_tag = Mock(return_value="atom-skill:skill-123-v1")
# Mock _image_exists (private method)
mock._image_exists = Mock(return_value=False)
return mock
@pytest.fixture(scope="function")
def mock_db_for_auto_install():
"""
Mock Session for get_db dependency.
Used to override database dependency in auto install routes.
Returns mock database session for testing.
Usage:
def test_with_mock_db(mock_db_for_auto_install):
# Route handler will use this mock session
pass
"""
mock = MagicMock(spec=Session)
# Mock common session methods
mock.add = MagicMock()
mock.commit = MagicMock()
mock.rollback = MagicMock()
mock.refresh = MagicMock()
mock.query = MagicMock()
mock.flush = MagicMock()
mock.close = MagicMock()
return mock
@pytest.fixture(scope="function")
def auto_install_client(mock_auto_installer, mock_db_for_auto_install):
"""
TestClient with auto install routes and mocked dependencies.
Creates isolated FastAPI app with auto_install_routes router.
Overrides get_db dependency to use mock database.
Patches AutoInstallerService to use mock service.
Usage:
def test_install_endpoint(auto_install_client):
response = auto_install_client.post("/auto-install/install", json={})
assert response.status_code == 200
"""
from api.auto_install_routes import router
from core.database import get_db
app = FastAPI()
app.include_router(router)
# Override get_db dependency
def override_get_db():
yield mock_db_for_auto_install
app.dependency_overrides[get_db] = override_get_db
# Patch AutoInstallerService
with patch('api.auto_install_routes.AutoInstallerService', return_value=mock_auto_installer):
client = TestClient(app)
yield client
# Clean up dependency overrides
app.dependency_overrides.clear()
@pytest.fixture(scope="function")
def sample_install_request():
"""
Factory for valid InstallRequest with default values.
Provides valid installation request data. All fields have
sensible defaults that can be overridden.
Usage:
def test_install(sample_install_request):
data = sample_install_request.copy()
data["skill_id"] = "custom-skill"
response = client.post("/auto-install/install", json=data)
"""
return {
"skill_id": "skill-123",
"packages": ["numpy", "pandas"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
@pytest.fixture(scope="function")
def sample_batch_install_request():
"""
Factory for valid BatchInstallRequest with multiple installations.
Provides valid batch installation request data with default
installations. Can be customized per test.
Usage:
def test_batch_install(sample_batch_install_request):
data = sample_batch_install_request.copy()
response = client.post("/auto-install/batch", json=data)
"""
return {
"installations": [
{
"skill_id": "skill-1",
"packages": ["numpy"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
},
{
"skill_id": "skill-2",
"packages": ["lodash"],
"package_type": "npm",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
],
"agent_id": "agent-001"
}
@pytest.fixture(scope="function")
def install_success_response():
"""
Expected successful install response structure.
Provides reference structure for successful installation responses.
Used to verify API response format.
Usage:
def test_install_response_format(auto_install_client, install_success_response):
response = auto_install_client.post("/auto-install/install", json={})
assert response.json()["success"] == install_success_response["success"]
"""
return {
"success": True,
"image_tag": "atom-skill:skill-123-v1",
"installed_packages": ["numpy", "pandas"],
"total_count": 2
}
@pytest.fixture(scope="function")
def batch_install_response():
"""
Expected batch install response structure.
Provides reference structure for batch installation responses.
Includes total count, successes, failures, and per-skill results.
Usage:
def test_batch_response_format(auto_install_client, batch_install_response):
response = auto_install_client.post("/auto-install/batch", json={})
assert response.json()["total"] == batch_install_response["total"]
"""
return {
"success": True,
"total": 2,
"successes": 2,
"failures": 0,
"results": [
{
"skill_id": "skill-1",
"result": {"success": True, "image_tag": "atom-skill:skill-1-v1"}
},
{
"skill_id": "skill-2",
"result": {"success": True, "image_tag": "atom-npm-skill:skill-2-v1"}
}
]
}
# ============================================================================
# TestAutoInstallSuccess - Single Install Endpoint Tests
# ============================================================================
class TestAutoInstallSuccess:
"""
Happy path tests for POST /auto-install/install endpoint.
Tests successful installation scenarios:
- Python package installation
- NPM package installation
- Vulnerability scanning
- Multiple packages in single request
"""
def test_install_dependencies_python(
self,
auto_install_client,
sample_install_request,
mock_auto_installer
):
"""Test POST /auto-install/install with python packages returns success with image_tag."""
response = auto_install_client.post("/auto-install/install", json=sample_install_request)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert "image_tag" in data
assert "atom-skill:" in data["image_tag"]
assert data["installed_packages"] == ["numpy", "pandas"]
assert data["total_count"] == 2
# Verify service was called
mock_auto_installer.install_dependencies.assert_called_once()
def test_install_dependencies_npm(
self,
auto_install_client,
mock_auto_installer
):
"""Test install with npm package_type works correctly."""
request_data = {
"skill_id": "npm-skill-123",
"packages": ["lodash", "axios"],
"package_type": "npm",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
# Configure mock for npm packages
mock_auto_installer.install_dependencies.return_value = {
"success": True,
"image_tag": "atom-npm-skill:npm-skill-123-v1",
"installed_packages": ["lodash", "axios"],
"total_count": 2
}
response = auto_install_client.post("/auto-install/install", json=request_data)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert "atom-npm-skill:" in data["image_tag"]
assert data["installed_packages"] == ["lodash", "axios"]
def test_install_with_vulnerability_scan(
self,
auto_install_client,
sample_install_request,
mock_auto_installer
):
"""Test scan_for_vulnerabilities=True includes security scan in result."""
sample_install_request["scan_for_vulnerabilities"] = True
response = auto_install_client.post("/auto-install/install", json=sample_install_request)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
# Verify install_dependencies was called with scan_for_vulnerabilities=True
call_args = mock_auto_installer.install_dependencies.call_args
assert call_args.kwargs["scan_for_vulnerabilities"] is True
def test_install_multiple_packages(
self,
auto_install_client,
mock_auto_installer
):
"""Test installing multiple packages in single request succeeds."""
request_data = {
"skill_id": "data-skill-456",
"packages": ["numpy", "pandas", "scikit-learn", "matplotlib"],
"package_type": "python",
"agent_id": "agent-002",
"scan_for_vulnerabilities": False
}
# Configure mock for multiple packages
mock_auto_installer.install_dependencies.return_value = {
"success": True,
"image_tag": "atom-skill:data-skill-456-v1",
"installed_packages": ["numpy", "pandas", "scikit-learn", "matplotlib"],
"total_count": 4
}
response = auto_install_client.post("/auto-install/install", json=request_data)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["total_count"] == 4
assert len(data["installed_packages"]) == 4
def test_install_missing_skill_id(
self,
auto_install_client
):
"""Test missing skill_id returns 422 validation error."""
request_data = {
"packages": ["numpy"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
# Missing skill_id
}
response = auto_install_client.post("/auto-install/install", json=request_data)
assert response.status_code == 422
data = response.json()
assert "detail" in data
def test_install_empty_packages(
self,
auto_install_client
):
"""Test empty packages list returns 422 (min_items=1 constraint)."""
request_data = {
"skill_id": "skill-123",
"packages": [], # Empty list violates min_items=1
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
response = auto_install_client.post("/auto-install/install", json=request_data)
assert response.status_code == 422
data = response.json()
assert "detail" in data
# ============================================================================
# TestAutoInstallBatch - Batch Install Endpoint Tests
# ============================================================================
class TestAutoInstallBatch:
"""
Tests for POST /auto-install/batch endpoint.
Tests batch installation scenarios:
- Multiple skills installation
- Two different skills
- Mixed package types (python and npm)
- Empty installations list validation
- Partial failures
"""
def test_batch_install_success(
self,
auto_install_client,
sample_batch_install_request,
mock_auto_installer
):
"""Test POST /auto-install/batch installs multiple skills successfully."""
response = auto_install_client.post("/auto-install/batch", json=sample_batch_install_request)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["total"] == 2
assert data["successes"] == 2
assert data["failures"] == 0
assert len(data["results"]) == 2
# Verify batch_install was called
mock_auto_installer.batch_install.assert_called_once()
def test_batch_install_two_skills(
self,
auto_install_client,
mock_auto_installer
):
"""Test batch install with 2 different skills."""
# Configure mock for two specific skills
mock_auto_installer.batch_install.return_value = {
"success": True,
"total": 2,
"successes": 2,
"failures": 0,
"results": [
{
"skill_id": "skill-analytics",
"result": {"success": True, "image_tag": "atom-skill:skill-analytics-v1"}
},
{
"skill_id": "skill-frontend",
"result": {"success": True, "image_tag": "atom-npm-skill:skill-frontend-v1"}
}
]
}
request_data = {
"installations": [
{
"skill_id": "skill-analytics",
"packages": ["numpy", "pandas"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
},
{
"skill_id": "skill-frontend",
"packages": ["react", "axios"],
"package_type": "npm",
"agent_id": "agent-001",
"scan_for_vulnerabilities": False
}
],
"agent_id": "agent-001"
}
response = auto_install_client.post("/auto-install/batch", json=request_data)
assert response.status_code == 200
data = response.json()
assert data["total"] == 2
assert data["successes"] == 2
assert len(data["results"]) == 2
# Verify both skills are in results
skill_ids = [r["skill_id"] for r in data["results"]]
assert "skill-analytics" in skill_ids
assert "skill-frontend" in skill_ids
def test_batch_install_mixed_package_types(
self,
auto_install_client,
mock_auto_installer
):
"""Test batch with both python and npm packages."""
request_data = {
"installations": [
{
"skill_id": "python-skill",
"packages": ["numpy"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
},
{
"skill_id": "npm-skill",
"packages": ["lodash"],
"package_type": "npm",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
],
"agent_id": "agent-001"
}
response = auto_install_client.post("/auto-install/batch", json=request_data)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["total"] == 2
def test_batch_install_empty(
self,
auto_install_client
):
"""Test empty installations list returns 422 (min_items=1 constraint)."""
request_data = {
"installations": [], # Empty list violates min_items=1
"agent_id": "agent-001"
}
response = auto_install_client.post("/auto-install/batch", json=request_data)
assert response.status_code == 422
data = response.json()
assert "detail" in data
def test_batch_install_partial_failure(
self,
auto_install_client,
mock_auto_installer
):
"""Test batch with some successes and some failures."""
# Configure batch_install to return partial failure
mock_auto_installer.batch_install.return_value = {
"success": False, # Overall success is False due to failures
"total": 3,
"successes": 2,
"failures": 1,
"results": [
{
"skill_id": "skill-1",
"result": {"success": True, "image_tag": "atom-skill:skill-1-v1"}
},
{
"skill_id": "skill-2",
"result": {"success": True, "image_tag": "atom-skill:skill-2-v1"}
},
{
"skill_id": "skill-3",
"result": {"success": False, "error": "Package not found"}
}
]
}
request_data = {
"installations": [
{
"skill_id": "skill-1",
"packages": ["numpy"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
},
{
"skill_id": "skill-2",
"packages": ["pandas"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
},
{
"skill_id": "skill-3",
"packages": ["nonexistent"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
],
"agent_id": "agent-001"
}
response = auto_install_client.post("/auto-install/batch", json=request_data)
assert response.status_code == 200 # Batch endpoint returns 200 even with partial failures
data = response.json()
assert data["success"] is False # Overall success is False
assert data["total"] == 3
assert data["successes"] == 2
assert data["failures"] == 1
# ============================================================================
# TestAutoInstallStatus - Status Check Endpoint Tests
# ============================================================================
class TestAutoInstallStatus:
"""
Tests for GET /auto-install/status/{skill_id} endpoint.
Tests installation status check scenarios:
- Installed status (image exists)
- Not installed status (image doesn't exist)
- NPM package type
- Python package type (default)
"""
def test_get_status_installed(
self,
auto_install_client,
mock_auto_installer
):
"""Test GET /auto-install/status/{skill_id} returns installed=True when image exists."""
# Configure mock to return image exists
mock_auto_installer._image_exists.return_value = True
mock_auto_installer._get_image_tag.return_value = "atom-skill:skill-123-v1"
response = auto_install_client.get("/auto-install/status/skill-123?package_type=python")
assert response.status_code == 200
data = response.json()
assert data["installed"] is True
assert data["skill_id"] == "skill-123"
assert data["package_type"] == "python"
assert data["image_tag"] == "atom-skill:skill-123-v1"
def test_get_status_not_installed(
self,
auto_install_client,
mock_auto_installer
):
"""Test returns installed=False when image doesn't exist."""
# Configure mock to return image doesn't exist
mock_auto_installer._image_exists.return_value = False
mock_auto_installer._get_image_tag.return_value = "atom-skill:skill-456-v1"
response = auto_install_client.get("/auto-install/status/skill-456?package_type=python")
assert response.status_code == 200
data = response.json()
assert data["installed"] is False
assert data["skill_id"] == "skill-456"
assert data["package_type"] == "python"
assert data["image_tag"] is None # No image_tag when not installed
def test_get_status_npm_package(
self,
auto_install_client,
mock_auto_installer
):
"""Test status check with npm package_type."""
# Configure mock for npm package
mock_auto_installer._image_exists.return_value = True
mock_auto_installer._get_image_tag.return_value = "atom-npm-skill:npm-skill-789-v1"
response = auto_install_client.get("/auto-install/status/npm-skill-789?package_type=npm")
assert response.status_code == 200
data = response.json()
assert data["installed"] is True
assert data["skill_id"] == "npm-skill-789"
assert data["package_type"] == "npm"
assert "atom-npm-skill:" in data["image_tag"]
def test_get_status_python_default(
self,
auto_install_client,
mock_auto_installer
):
"""Test status check defaults to python package_type when not specified."""
mock_auto_installer._image_exists.return_value = True
mock_auto_installer._get_image_tag.return_value = "atom-skill:skill-default-v1"
# Request without package_type query parameter (defaults to python)
response = auto_install_client.get("/auto-install/status/skill-default")
assert response.status_code == 200
data = response.json()
assert data["installed"] is True
assert data["package_type"] == "python" # Default
assert data["skill_id"] == "skill-default"
# ============================================================================
# TestAutoInstallErrorPaths - Error Path Tests
# ============================================================================
class TestAutoInstallErrorPaths:
"""
Error path tests for auto install endpoints.
Tests error scenarios:
- Install failure (400 HTTPException)
- Service errors
- Invalid package type (422 validation)
- Missing agent_id in batch install (422)
- Status check validation
"""
def test_install_failure_response(
self,
auto_install_client,
sample_install_request,
mock_auto_installer
):
"""Test install failure (success=False) returns 400 HTTPException with error details."""
# Configure mock to return failure
mock_auto_installer.install_dependencies.return_value = {
"success": False,
"error": "Package not found: nonexistent-package"
}
response = auto_install_client.post("/auto-install/install", json=sample_install_request)
assert response.status_code == 400
data = response.json()
assert "detail" in data
# Verify error details are passed through
assert "error" in str(data["detail"]).lower() or "success" in str(data["detail"]).lower()
def test_install_service_error(
self,
auto_install_client,
sample_install_request,
mock_auto_installer
):
"""Test AutoInstallerService exception results in error response."""
# Configure mock to return failure (simulating service error)
mock_auto_installer.install_dependencies.return_value = {
"success": False,
"error": "Docker daemon not available"
}
response = auto_install_client.post("/auto-install/install", json=sample_install_request)
# Service failure returns 400 HTTPException from route handler
assert response.status_code == 400
data = response.json()
assert "detail" in data
def test_install_invalid_package_type(
self,
auto_install_client,
mock_auto_installer
):
"""Test invalid package_type (not python/npm) is handled by service."""
# Service accepts any package_type string (no enum validation in Pydantic model)
request_data = {
"skill_id": "skill-123",
"packages": ["some-package"],
"package_type": "golang", # Non-standard package type
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
# Service returns failure for unsupported package types
mock_auto_installer.install_dependencies.return_value = {
"success": False,
"error": "Unsupported package type: golang"
}
response = auto_install_client.post("/auto-install/install", json=request_data)
# Service returns 400 for unsupported package type
assert response.status_code == 400
data = response.json()
assert "detail" in data
def test_batch_install_missing_agent_id(
self,
auto_install_client
):
"""Test batch install missing agent_id returns 422."""
request_data = {
"installations": [
{
"skill_id": "skill-1",
"packages": ["numpy"],
"package_type": "python",
"agent_id": "agent-001",
"scan_for_vulnerabilities": True
}
]
# Missing agent_id at batch level
}
response = auto_install_client.post("/auto-install/batch", json=request_data)
assert response.status_code == 422
data = response.json()
assert "detail" in data
def test_status_skill_id_validation(
self,
auto_install_client
):
"""Test status check with missing skill_id path parameter returns 404."""
# Missing path parameter returns 404 (FastAPI behavior for missing path params)
response = auto_install_client.get("/auto-install/status/")
# FastAPI returns 404 Not Found for routes without required path parameter
assert response.status_code == 404
|