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