|
| 1 | +"""Tests for Azure provider creation in team_provider_endpoints.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 5 | + |
| 6 | +from transformerlab.shared.models.models import ProviderType |
| 7 | + |
| 8 | + |
| 9 | +def test_create_azure_provider_injects_resource_group_and_team_id(): |
| 10 | + """create_provider_for_team must inject azure_resource_group and team_id into config for Azure providers.""" |
| 11 | + from transformerlab.services.compute_provider.team_provider_endpoints import create_provider_for_team |
| 12 | + from pydantic import BaseModel |
| 13 | + |
| 14 | + class FakeConfig(BaseModel): |
| 15 | + azure_subscription_id: str = "sub-123" |
| 16 | + azure_tenant_id: str = "tenant-456" |
| 17 | + azure_client_id: str = "client-789" |
| 18 | + azure_client_secret: str = "secret" |
| 19 | + azure_location: str = "eastus" |
| 20 | + |
| 21 | + def model_dump(self, **kwargs): |
| 22 | + return { |
| 23 | + "azure_subscription_id": self.azure_subscription_id, |
| 24 | + "azure_tenant_id": self.azure_tenant_id, |
| 25 | + "azure_client_id": self.azure_client_id, |
| 26 | + "azure_client_secret": self.azure_client_secret, |
| 27 | + "azure_location": self.azure_location, |
| 28 | + } |
| 29 | + |
| 30 | + captured_config = {} |
| 31 | + |
| 32 | + async def fake_create_team_provider(session, team_id, name, provider_type, config, created_by_user_id): |
| 33 | + captured_config.update(config) |
| 34 | + mock_provider = MagicMock() |
| 35 | + mock_provider.type = "azure" |
| 36 | + mock_provider.id = "prov-1" |
| 37 | + mock_provider.name = name |
| 38 | + mock_provider.config = config |
| 39 | + mock_provider.is_default = False |
| 40 | + mock_provider.disabled = False |
| 41 | + mock_provider.supported_accelerators = [] |
| 42 | + mock_provider.team_id = team_id |
| 43 | + mock_provider.created_by_user_id = "user-1" |
| 44 | + mock_provider.created_at = None |
| 45 | + mock_provider.updated_at = None |
| 46 | + return mock_provider |
| 47 | + |
| 48 | + provider_data = MagicMock() |
| 49 | + provider_data.type = ProviderType.AZURE |
| 50 | + provider_data.name = "my-azure" |
| 51 | + provider_data.config = FakeConfig() |
| 52 | + |
| 53 | + with ( |
| 54 | + patch( |
| 55 | + "transformerlab.services.compute_provider.team_provider_endpoints.create_team_provider", |
| 56 | + side_effect=fake_create_team_provider, |
| 57 | + ), |
| 58 | + patch( |
| 59 | + "transformerlab.services.compute_provider.team_provider_endpoints.list_team_providers", |
| 60 | + new_callable=AsyncMock, |
| 61 | + return_value=[], |
| 62 | + ), |
| 63 | + patch( |
| 64 | + "transformerlab.services.compute_provider.team_provider_endpoints.cache.invalidate", |
| 65 | + new_callable=AsyncMock, |
| 66 | + ), |
| 67 | + ): |
| 68 | + asyncio.run( |
| 69 | + create_provider_for_team( |
| 70 | + session=MagicMock(), |
| 71 | + team_id="team-xyz", |
| 72 | + user=MagicMock(id="user-1"), |
| 73 | + provider_data=provider_data, |
| 74 | + force_refresh=False, |
| 75 | + ) |
| 76 | + ) |
| 77 | + |
| 78 | + assert captured_config["azure_resource_group"] == "transformerlab-team-xyz" |
| 79 | + assert captured_config["team_id"] == "team-xyz" |
| 80 | + |
| 81 | + |
| 82 | +def test_azure_is_in_allowed_provider_types(): |
| 83 | + """Azure must be in allowed_provider_types — verifies it no longer raises 400.""" |
| 84 | + from transformerlab.services.compute_provider.team_provider_endpoints import create_provider_for_team |
| 85 | + from fastapi import HTTPException |
| 86 | + from pydantic import BaseModel |
| 87 | + |
| 88 | + class FakeConfig(BaseModel): |
| 89 | + azure_subscription_id: str = "sub-123" |
| 90 | + |
| 91 | + def model_dump(self, **kwargs): |
| 92 | + return {"azure_subscription_id": self.azure_subscription_id} |
| 93 | + |
| 94 | + provider_data = MagicMock() |
| 95 | + provider_data.type = ProviderType.AZURE |
| 96 | + provider_data.name = "my-azure" |
| 97 | + provider_data.config = FakeConfig() |
| 98 | + |
| 99 | + did_raise_type_error = False |
| 100 | + with ( |
| 101 | + patch( |
| 102 | + "transformerlab.services.compute_provider.team_provider_endpoints.list_team_providers", |
| 103 | + new_callable=AsyncMock, |
| 104 | + return_value=[], |
| 105 | + ), |
| 106 | + patch( |
| 107 | + "transformerlab.services.compute_provider.team_provider_endpoints.create_team_provider", |
| 108 | + new_callable=AsyncMock, |
| 109 | + side_effect=Exception("stop here"), |
| 110 | + ), |
| 111 | + patch( |
| 112 | + "transformerlab.services.compute_provider.team_provider_endpoints.cache.invalidate", |
| 113 | + new_callable=AsyncMock, |
| 114 | + ), |
| 115 | + ): |
| 116 | + try: |
| 117 | + asyncio.run( |
| 118 | + create_provider_for_team( |
| 119 | + session=MagicMock(), |
| 120 | + team_id="team-xyz", |
| 121 | + user=MagicMock(id="user-1"), |
| 122 | + provider_data=provider_data, |
| 123 | + force_refresh=False, |
| 124 | + ) |
| 125 | + ) |
| 126 | + except HTTPException as e: |
| 127 | + if e.status_code == 400 and "Invalid provider type" in str(e.detail): |
| 128 | + did_raise_type_error = True |
| 129 | + except Exception: |
| 130 | + pass |
| 131 | + |
| 132 | + assert not did_raise_type_error, "Azure should be in allowed_provider_types" |
0 commit comments