-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_wrapper.py
More file actions
215 lines (180 loc) · 8.89 KB
/
test_api_wrapper.py
File metadata and controls
215 lines (180 loc) · 8.89 KB
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
"""Unit tests for the api_wrapper module."""
import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch
from pyflowintel.api_wrapper import PyFlowintel
from pyflowintel.commons.exceptions import PyflowintelConfigurationError
from pyflowintel.settings import FlowintelConfig, TEST_DATA_PATH, DEFAULT_CONFIG_FILE
import pyflowintel.commons.utils as utils
class TestPyFlowintelInit(unittest.TestCase):
"""Test PyFlowintel initialization."""
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_init_with_config(self, mock_http_client):
"""Test initialization with FlowintelConfig."""
config = FlowintelConfig(
base_url="http://localhost:7006/api",
api_key="flowintel-api-key"
)
client = PyFlowintel(config)
self.assertEqual(client.config, config)
mock_http_client.assert_called_once()
self.assertIsNotNone(client.cases)
self.assertIsNotNone(client.tasks)
self.assertIsNotNone(client.admin)
self.assertIsNotNone(client.importers)
self.assertIsNotNone(client.templates)
@unittest.skipUnless(Path(DEFAULT_CONFIG_FILE).exists(), "DEFAULT_CONFIG_FILE does not exist")
@patch("pyflowintel.api_wrapper.HTTPClient")
@patch("pyflowintel.commons.utils.read_yaml")
def test_from_config_default_file(self, mock_read_yaml, mock_http_client):
"""Test initialization with default configuration file
passing a mock HTTPClient to be used in the test
"""
# Mocked config dictionary
default_config_data = {
"base_url": "http://localhost:7006/api",
"api_key": "flowintel-api-key",
"timeout": 30,
"verify_ssl": True
}
mock_read_yaml.return_value = {"flowintel": default_config_data}
client = PyFlowintel.from_config()
# Verify client was created successfully
self.assertIsNotNone(client)
self.assertEqual(client.config.base_url, default_config_data.get("base_url"))
self.assertEqual(client.config.api_key, default_config_data.get("api_key"))
self.assertEqual(client.config.timeout, default_config_data.get("timeout"))
self.assertEqual(client.config.verify_ssl, default_config_data.get("verify_ssl"))
mock_read_yaml.assert_called_once_with(DEFAULT_CONFIG_FILE)
# Verify HTTPClient was initialized with correct parameters
mock_http_client.assert_called_once_with(
base_url=client.config.base_url,
api_key=client.config.api_key,
timeout=client.config.timeout,
verify_ssl=client.config.verify_ssl
)
def test_from_config_custom_file(self):
"""Test initialization with custom configuration file"""
client = PyFlowintel.from_config(f"{TEST_DATA_PATH}/config_sample.yml")
expected_config = FlowintelConfig(
base_url="http://localhost:7006/api",
api_key="test-key",
timeout=10,
verify_ssl=True,
)
self.assertEqual(client.config, expected_config)
def test_from_args(self):
"""Test from_args with custom values."""
client = PyFlowintel.from_args(
base_url="http://example.com/api",
api_key="custom-key",
timeout=60,
verify_ssl=False,
)
self.assertEqual(client.config.base_url, "http://example.com/api")
self.assertEqual(client.config.api_key, "custom-key")
self.assertEqual(client.config.timeout, 60)
self.assertFalse(client.config.verify_ssl)
class TestPyFlowintelContextManager(unittest.TestCase):
"""Test PyFlowintel context manager functionality."""
@classmethod
def setUpClass(cls):
cls.base_url="http://localhost:7006/api"
cls.api_key="test-key"
def test_context_manager_enter(self):
"""Test __enter__ returns self."""
config = FlowintelConfig(base_url=self.base_url, api_key=self.api_key)
client = PyFlowintel(config)
result = client.__enter__()
self.assertIs(result, client)
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_context_manager_exit(self, mock_http_client):
"""Test __exit__ closes the HTTPClient"""
client = PyFlowintel.from_args(base_url=self.base_url, api_key=self.api_key)
client.__exit__(None, None, None)
mock_http_client.return_value.close.assert_called_once()
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_context_manager_with_statement(self, mock_http_client):
"""Test using PyFlowintel as context manager."""
with PyFlowintel.from_args(
base_url=self.base_url, api_key=self.api_key
) as client:
self.assertIsInstance(client, PyFlowintel)
# mock_http_client.return_value replaces the HTTPClient() instance
# created during init. Thus, we can check that the 'close' method
# was called on the HTTPClient
mock_http_client.return_value.close.assert_called_once()
class TestPyFlowintelProperties(unittest.TestCase):
"""Test PyFlowintel properties."""
def setUp(self):
self.config = FlowintelConfig(
base_url="http://localhost:7006/api",
api_key="test-key",
timeout=30
)
self.mock_client = MagicMock()
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_timeout_property_getter(self, mock_http_client):
"""Test timeout property getter."""
self.mock_client.timeout = 30
mock_http_client.return_value = self.mock_client
client = PyFlowintel(self.config)
self.assertEqual(client.timeout, 30)
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_timeout_property_setter_valid(self, mock_http_client):
"""Test timeout property setter with valid value."""
mock_http_client.return_value = self.mock_client
client = PyFlowintel(self.config)
client.timeout = 60
self.assertEqual(self.mock_client.timeout, 60)
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_timeout_property_setter_invalid_non_integer(self, mock_http_client):
"""Test timeout property setter with non-integer value."""
mock_http_client.return_value = self.mock_client
client = PyFlowintel(self.config)
with self.assertRaises(ValueError) as context:
client.timeout = "not-an-integer"
self.assertIn("timeout must be a positive integer", str(context.exception))
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_timeout_property_setter_invalid_negative(self, mock_http_client):
"""Test timeout property setter with negative value."""
mock_http_client.return_value = self.mock_client
client = PyFlowintel(self.config)
with self.assertRaises(ValueError) as context:
client.timeout = -1
self.assertIn("timeout must be a positive integer", str(context.exception))
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_timeout_property_setter_invalid_zero(self, mock_http_client):
"""Test timeout property setter with zero value."""
mock_http_client.return_value = self.mock_client
client = PyFlowintel(self.config)
with self.assertRaises(ValueError) as context:
client.timeout = 0
self.assertIn("timeout must be a positive integer", str(context.exception))
class TestPyFlowintelIntegration(unittest.TestCase):
"""Integration tests for PyFlowintel."""
@patch("pyflowintel.api_wrapper.HTTPClient")
def test_endpoints_receive_same_http_client(self, mock_http_client):
"""Test all endpoints receive the same HTTPClient instance."""
mock_client = MagicMock()
mock_http_client.return_value = mock_client
with patch("pyflowintel.api_wrapper.CaseEndpoint") as mock_case:
with patch("pyflowintel.api_wrapper.TaskEndpoint") as mock_task:
with patch("pyflowintel.api_wrapper.AdminEndpoint") as mock_admin:
with patch(
"pyflowintel.api_wrapper.ImporterEndpoint"
) as mock_importer:
with patch(
"pyflowintel.api_wrapper.TemplateEndpoint"
) as mock_template:
PyFlowintel.from_args(
base_url="http://localhost:7006/api",
api_key="test-key")
# Verify all endpoints received the same client
mock_case.assert_called_once_with(mock_client)
mock_task.assert_called_once_with(mock_client)
mock_admin.assert_called_once_with(mock_client)
mock_importer.assert_called_once_with(mock_client)
mock_template.assert_called_once_with(mock_client)
if __name__ == "__main__":
unittest.main()