|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from tfe import client, config |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def test_config(): |
| 10 | + return config.Config(address="https://app.terraform.io", token="test-token") |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_response(): |
| 15 | + response = Mock() |
| 16 | + response.headers = { |
| 17 | + "TFP-API-Version": "2.5.0", |
| 18 | + "X-TFE-Version": "v202308-1", |
| 19 | + "TFP-AppName": "HCP Terraform", |
| 20 | + } |
| 21 | + response.raise_for_status.return_value = None |
| 22 | + return response |
| 23 | + |
| 24 | + |
| 25 | +class TestClient: |
| 26 | + @patch("requests.Session.get") |
| 27 | + def test_client_initialization(self, mock_get, test_config, mock_response): |
| 28 | + """Test basic client setup works.""" |
| 29 | + mock_get.return_value = mock_response |
| 30 | + |
| 31 | + client_instance = client.Client(config=test_config) |
| 32 | + |
| 33 | + assert client_instance.config.address == "https://app.terraform.io" |
| 34 | + assert client_instance.config.token == "test-token" |
| 35 | + assert client_instance.base_url == "https://app.terraform.io/api/v2/" |
| 36 | + assert ( |
| 37 | + client_instance.registry_base_url |
| 38 | + == "https://app.terraform.io/api/registry/" |
| 39 | + ) |
| 40 | + |
| 41 | + @patch("requests.Session.get") |
| 42 | + def test_url_normalization(self, mock_get, mock_response): |
| 43 | + """Test that paths get normalized with trailing slashes.""" |
| 44 | + mock_get.return_value = mock_response |
| 45 | + |
| 46 | + cfg = config.Config( |
| 47 | + address="https://example.com", |
| 48 | + token="test", |
| 49 | + base_path="/custom/api", # no trailing slash |
| 50 | + registry_base_path="/registry", # no trailing slash |
| 51 | + ) |
| 52 | + |
| 53 | + client_instance = client.Client(config=cfg) |
| 54 | + |
| 55 | + assert client_instance.base_url == "https://example.com/custom/api/" |
| 56 | + assert client_instance.registry_base_url == "https://example.com/registry/" |
| 57 | + |
| 58 | + @patch("requests.Session.get") |
| 59 | + def test_api_metadata_extraction(self, mock_get, test_config, mock_response): |
| 60 | + """Test that API metadata gets extracted from response headers.""" |
| 61 | + mock_get.return_value = mock_response |
| 62 | + |
| 63 | + client_instance = client.Client(config=test_config) |
| 64 | + |
| 65 | + assert client_instance.remote_api_version == "2.5.0" |
| 66 | + assert client_instance.remote_tfe_version == "v202308-1" |
| 67 | + assert client_instance.app_name == "HCP Terraform" |
| 68 | + |
| 69 | + @patch("requests.Session.get") |
| 70 | + def test_cloud_vs_enterprise_detection(self, mock_get, test_config): |
| 71 | + """Test detection between cloud and enterprise instances.""" |
| 72 | + # Test HCP Terraform (cloud) |
| 73 | + cloud_response = Mock() |
| 74 | + cloud_response.headers = {"TFP-AppName": "HCP Terraform"} |
| 75 | + cloud_response.raise_for_status.return_value = None |
| 76 | + mock_get.return_value = cloud_response |
| 77 | + |
| 78 | + cloud_client = client.Client(config=test_config) |
| 79 | + assert cloud_client.is_cloud() is True |
| 80 | + assert cloud_client.is_enterprise() is False |
| 81 | + |
| 82 | + # Test Terraform Enterprise |
| 83 | + enterprise_response = Mock() |
| 84 | + enterprise_response.headers = {"TFP-AppName": "Terraform Enterprise"} |
| 85 | + enterprise_response.raise_for_status.return_value = None |
| 86 | + mock_get.return_value = enterprise_response |
| 87 | + |
| 88 | + enterprise_client = client.Client(config=test_config) |
| 89 | + assert enterprise_client.is_cloud() is False |
| 90 | + assert enterprise_client.is_enterprise() is True |
| 91 | + |
| 92 | + @patch("requests.Session.get") |
| 93 | + def test_fake_api_version_for_testing(self, mock_get, test_config, mock_response): |
| 94 | + """Test the fake API version setter for testing scenarios.""" |
| 95 | + mock_get.return_value = mock_response |
| 96 | + |
| 97 | + client_instance = client.Client(config=test_config) |
| 98 | + |
| 99 | + # Original version from mock |
| 100 | + assert client_instance.remote_api_version == "2.5.0" |
| 101 | + |
| 102 | + # Set fake version |
| 103 | + client_instance.set_fake_remote_api_version("3.0.0") |
| 104 | + assert client_instance.remote_api_version == "3.0.0" |
0 commit comments