-
Notifications
You must be signed in to change notification settings - Fork 708
Expand file tree
/
Copy pathtest_rest_client.py
More file actions
459 lines (375 loc) · 17.1 KB
/
test_rest_client.py
File metadata and controls
459 lines (375 loc) · 17.1 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
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
# coding: utf-8
"""
Unit tests for atlassian.rest_client module
"""
from datetime import datetime, timedelta
from types import SimpleNamespace
import pytest
from .mockup import mockup_server
from atlassian.rest_client import AtlassianRestAPI
class TestAtlassianRestAPI:
"""Test cases for AtlassianRestAPI class"""
def setup_method(self):
"""Set up test fixtures"""
self.api = AtlassianRestAPI(
url=f"{mockup_server()}/test",
username="testuser",
password="testpass",
timeout=30,
api_root="rest/api",
api_version="latest",
verify_ssl=True,
cloud=False,
)
def test_init_default_values(self):
"""Test initialization with default values"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test")
assert api.url == f"{mockup_server()}/test"
assert api.username is None
assert api.password is None
assert api.timeout == 75
assert api.api_root == "rest/api"
assert api.api_version == "latest"
assert api.verify_ssl is True
assert api.cloud is False
def test_init_custom_values(self):
"""Test initialization with custom values"""
api = AtlassianRestAPI(
url=f"{mockup_server()}/custom",
username="customuser",
password="custompass",
timeout=60,
api_root="custom/api",
api_version="2",
verify_ssl=False,
cloud=True,
)
assert api.url == f"{mockup_server()}/custom"
assert api.username == "customuser"
assert api.password == "custompass"
assert api.timeout == 60
assert api.api_root == "custom/api"
assert api.api_version == "2"
assert api.verify_ssl is False
assert api.cloud is True
def test_init_with_token(self):
"""Test initialization with API token"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test", token="apitoken123")
# The token should be stored in the session configuration
assert hasattr(api, "session")
def test_init_with_cert(self):
"""Test initialization with certificate"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test", cert=("/path/to/cert.pem", "/path/to/key.pem"))
assert api.cert == ("/path/to/cert.pem", "/path/to/key.pem")
def test_init_with_proxies(self):
"""Test initialization with proxy configuration"""
proxies = {"http": "http://proxy.example.com:8080", "https": "https://proxy.example.com:8080"}
api = AtlassianRestAPI(url=f"{mockup_server()}/test", proxies=proxies)
assert api.proxies == proxies
def test_init_with_backoff_retry(self):
"""Test initialization with backoff and retry configuration"""
api = AtlassianRestAPI(
url=f"{mockup_server()}/test",
backoff_and_retry=True,
retry_status_codes=[500, 502, 503],
max_backoff_seconds=900,
max_backoff_retries=500,
backoff_factor=2.0,
)
assert api.backoff_and_retry is True
assert api.retry_status_codes == [500, 502, 503]
assert api.max_backoff_seconds == 900
assert api.max_backoff_retries == 500
assert api.backoff_factor == 2.0
def test_class_attributes(self):
"""Test that class attributes are properly defined"""
assert hasattr(AtlassianRestAPI, "default_headers")
assert hasattr(AtlassianRestAPI, "experimental_headers")
assert hasattr(AtlassianRestAPI, "form_token_headers")
assert hasattr(AtlassianRestAPI, "no_check_headers")
assert hasattr(AtlassianRestAPI, "safe_mode_headers")
assert hasattr(AtlassianRestAPI, "experimental_headers_general")
def test_default_headers(self):
"""Test default headers configuration"""
headers = AtlassianRestAPI.default_headers
assert "Content-Type" in headers
assert headers["Content-Type"] == "application/json"
assert "Accept" in headers
assert headers["Accept"] == "application/json"
def test_experimental_headers(self):
"""Test experimental headers configuration"""
headers = AtlassianRestAPI.experimental_headers
assert "Content-Type" in headers
assert headers["Content-Type"] == "application/json"
assert "Accept" in headers
assert headers["Accept"] == "application/json"
assert "X-ExperimentalApi" in headers
assert headers["X-ExperimentalApi"] == "opt-in"
def test_form_token_headers(self):
"""Test form token headers configuration"""
headers = AtlassianRestAPI.form_token_headers
assert "Content-Type" in headers
assert headers["Content-Type"] == "application/x-www-form-urlencoded; charset=UTF-8"
assert "X-Atlassian-Token" in headers
assert headers["X-Atlassian-Token"] == "no-check"
def test_no_check_headers(self):
"""Test no check headers configuration"""
headers = AtlassianRestAPI.no_check_headers
assert "X-Atlassian-Token" in headers
assert headers["X-Atlassian-Token"] == "no-check"
def test_safe_mode_headers(self):
"""Test safe mode headers configuration"""
headers = AtlassianRestAPI.safe_mode_headers
assert "X-Atlassian-Token" in headers
assert headers["X-Atlassian-Token"] == "no-check"
assert "Content-Type" in headers
assert headers["Content-Type"] == "application/vnd.atl.plugins.safe.mode.flag+json"
def test_experimental_headers_general(self):
"""Test experimental headers general configuration"""
headers = AtlassianRestAPI.experimental_headers_general
assert "X-Atlassian-Token" in headers
assert headers["X-Atlassian-Token"] == "no-check"
assert "X-ExperimentalApi" in headers
assert headers["X-ExperimentalApi"] == "opt-in"
def test_session_creation(self):
"""Test that session is created during initialization"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test")
assert hasattr(api, "session")
assert api.session is not None
def test_url_construction(self):
"""Test URL construction for API endpoints"""
# The URL construction happens in the request method
# We'll test this indirectly through the request method
assert self.api.url == f"{mockup_server()}/test"
assert self.api.api_root == "rest/api"
assert self.api.api_version == "latest"
def test_cloud_flag(self):
"""Test cloud flag handling"""
cloud_api = AtlassianRestAPI(url=f"{mockup_server()}/test", cloud=True)
assert cloud_api.cloud is True
server_api = AtlassianRestAPI(url=f"{mockup_server()}/test", cloud=False)
assert server_api.cloud is False
def test_advanced_mode(self):
"""Test advanced mode configuration"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test", advanced_mode=True)
assert api.advanced_mode is True
def test_kerberos_configuration(self):
"""Test kerberos configuration"""
# Test that kerberos config is accepted without errors
try:
# This should not crash the test
AtlassianRestAPI(url=f"{mockup_server()}/test", kerberos=True)
except ImportError:
# requests_kerberos is not installed, which is expected
pass
except Exception:
# Other exceptions are also acceptable
pass
def test_cookies_configuration(self):
"""Test cookies configuration"""
cookies = None # Removed unused import
try:
# This should not crash the test
AtlassianRestAPI(url=f"{mockup_server()}/test", cookies=cookies)
except Exception:
# Cookies might not be properly configured, which is acceptable
pass
def test_timeout_configuration(self):
"""Test timeout configuration"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test", timeout=120)
assert api.timeout == 120
def test_verify_ssl_configuration(self):
"""Test SSL verification configuration"""
try:
# This should not crash the test
AtlassianRestAPI(url=f"{mockup_server()}/test", verify_ssl=False)
except Exception:
# SSL verification might not be properly configured, which is acceptable
pass
def test_api_version_types(self):
"""Test different API version types"""
# String version
api1 = AtlassianRestAPI(url=f"{mockup_server()}/test", api_version="2")
assert api1.api_version == "2"
# Integer version
api2 = AtlassianRestAPI(url=f"{mockup_server()}/test", api_version=3)
assert api2.api_version == 3
# Latest version
api3 = AtlassianRestAPI(url=f"{mockup_server()}/test", api_version="latest")
assert api3.api_version == "latest"
def test_api_root_configuration(self):
"""Test API root configuration"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test", api_root="custom/api")
assert api.api_root == "custom/api"
def test_oauth_configuration(self):
"""Test OAuth configuration (without actual OAuth setup)"""
# Test that OAuth config is accepted without errors
oauth_config = {
"access_token": "token123",
"access_token_secret": "secret123",
"consumer_key": "consumer123",
"key_cert": "cert123",
}
# This should not raise an error during initialization
try:
api = AtlassianRestAPI(url=f"{mockup_server()}/test", oauth=oauth_config)
assert hasattr(api, "session")
except Exception:
# OAuth might not be available, which is acceptable
pass
def test_oauth2_configuration(self):
"""Test OAuth2 configuration (without actual OAuth2 setup)"""
# Test that OAuth2 config is accepted without errors
oauth2_config = {"client_id": "client123", "client_secret": "secret123", "access_token": "token123"}
# This should not raise an error during initialization
try:
api = AtlassianRestAPI(url=f"{mockup_server()}/test", oauth2=oauth2_config)
assert hasattr(api, "session")
except Exception:
# OAuth2 might not be available, which is acceptable
pass
def test_methods_exist(self):
"""Test that expected methods exist"""
expected_methods = ["get", "post", "put", "delete", "patch", "request"]
for method_name in expected_methods:
assert hasattr(self.api, method_name), f"Method {method_name} not found"
def test_error_handling_imports(self):
"""Test that error handling classes can be imported"""
try:
from atlassian.errors import (
ApiError,
ApiNotFoundError,
ApiPermissionError,
ApiValueError,
ApiConflictError,
ApiNotAcceptable,
)
# Test that the classes can be instantiated
error = ApiError("Test error")
not_found = ApiNotFoundError("Not found")
permission = ApiPermissionError("Permission denied")
value_error = ApiValueError("Invalid value")
conflict = ApiConflictError("Conflict")
not_acceptable = ApiNotAcceptable("Not acceptable")
assert str(error) == "Test error"
assert str(not_found) == "Not found"
assert str(permission) == "Permission denied"
assert str(value_error) == "Invalid value"
assert str(conflict) == "Conflict"
assert str(not_acceptable) == "Not acceptable"
except ImportError:
pytest.fail("Failed to import error classes")
def test_type_hints_imports(self):
"""Test that type hints can be imported"""
try:
from atlassian.typehints import T_resp_json
assert T_resp_json is not None
except ImportError:
pytest.fail("Failed to import type hints")
def test_request_utils_imports(self):
"""Test that request utilities can be imported"""
try:
from atlassian.request_utils import get_default_logger
assert get_default_logger is not None
except ImportError:
pytest.fail("Failed to import request utilities")
def test_logging_configuration(self):
"""Test that logging is properly configured"""
# The logging is configured at module level, not instance level
assert hasattr(AtlassianRestAPI, "default_headers")
assert AtlassianRestAPI.default_headers is not None
def test_response_attribute(self):
"""Test that response attribute exists"""
assert hasattr(self.api, "response")
assert self.api.response is None
def test_retry_configuration(self):
"""Test retry configuration attributes"""
api = AtlassianRestAPI(
url=f"{mockup_server()}/test",
backoff_and_retry=True,
retry_status_codes=[500, 502, 503],
max_backoff_seconds=900,
max_backoff_retries=500,
backoff_factor=2.0,
)
# Test that retry configuration attributes are set
assert api.retry_status_codes == [500, 502, 503]
assert api.max_backoff_seconds == 900
assert api.max_backoff_retries == 500
assert api.backoff_factor == 2.0
def test_advanced_mode_headers(self):
"""Test that advanced mode affects headers"""
api = AtlassianRestAPI(url=f"{mockup_server()}/test", advanced_mode=True)
# Advanced mode should be set
assert api.advanced_mode is True
def test_cloud_vs_server_behavior(self):
"""Test differences between cloud and server configurations"""
cloud_api = AtlassianRestAPI(url=f"{mockup_server()}/test", cloud=True)
server_api = AtlassianRestAPI(url=f"{mockup_server()}/test", cloud=False)
# Both should have sessions
assert hasattr(cloud_api, "session")
assert hasattr(server_api, "session")
# Cloud flag should be different
assert cloud_api.cloud is True
assert server_api.cloud is False
def test_retry_handler_clamps_retry_after(self, monkeypatch):
"""Ensure large Retry-After headers are clamped to max_backoff_seconds."""
captured = {}
def fake_sleep(delay):
captured["delay"] = delay
monkeypatch.setattr("atlassian.rest_client.time.sleep", fake_sleep)
api = AtlassianRestAPI(
url=f"{mockup_server()}/test",
retry_with_header=True,
max_backoff_seconds=5,
)
handler = api._retry_handler()
response = SimpleNamespace(headers={"Retry-After": "99999999999"}, status_code=429)
assert handler(response) is True
assert captured["delay"] == 5
def test_retry_handler_clamps_non_finite_retry_after(self, monkeypatch):
"""Ensure non-finite Retry-After headers are clamped to max_backoff_seconds."""
captured = {}
def fake_sleep(delay):
captured["delay"] = delay
monkeypatch.setattr("atlassian.rest_client.time.sleep", fake_sleep)
api = AtlassianRestAPI(
url=f"{mockup_server()}/test",
retry_with_header=True,
max_backoff_seconds=7,
)
handler = api._retry_handler()
response = SimpleNamespace(headers={"Retry-After": "1e309"}, status_code=429)
assert handler(response) is True
assert captured["delay"] == 7
def test_retry_handler_parses_http_date(self, monkeypatch):
"""Ensure HTTP-date Retry-After headers are converted to delta seconds."""
captured = {}
def fake_sleep(delay):
captured["delay"] = delay
monkeypatch.setattr("atlassian.rest_client.time.sleep", fake_sleep)
api = AtlassianRestAPI(
url=f"{mockup_server()}/test",
retry_with_header=True,
max_backoff_seconds=60,
)
handler = api._retry_handler()
future_delay = 10
future_date = datetime.utcnow().replace(tzinfo=None) + timedelta(seconds=future_delay)
retry_after_value = future_date.strftime("%a, %d %b %Y %H:%M:%S GMT")
response = SimpleNamespace(headers={"Retry-After": retry_after_value}, status_code=429)
assert handler(response) is True
assert pytest.approx(captured["delay"], rel=0.1) == future_delay
def test_retry_handler_skips_invalid_header(self, monkeypatch):
"""Ensure invalid Retry-After headers fall back to regular logic."""
def fake_sleep(_):
raise AssertionError("sleep should not be called for invalid header")
monkeypatch.setattr("atlassian.rest_client.time.sleep", fake_sleep)
api = AtlassianRestAPI(
url=f"{mockup_server()}/test",
retry_with_header=True,
)
handler = api._retry_handler()
response = SimpleNamespace(headers={"Retry-After": "invalid-value"}, status_code=429)
# Should return False so that other retry mechanisms can take over
assert handler(response) is False