-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtest_client_base_exceptions.py
More file actions
162 lines (116 loc) · 4.66 KB
/
test_client_base_exceptions.py
File metadata and controls
162 lines (116 loc) · 4.66 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
from unittest.mock import MagicMock
import pytest
import responses
from requests.exceptions import (
ConnectionError as RequestsConnectionError,
)
from requests.exceptions import (
HTTPError,
SSLError,
Timeout,
)
from cycode.cli.exceptions.custom_exceptions import (
HttpUnauthorizedError,
RequestConnectionError,
RequestHttpError,
RequestSslError,
RequestTimeoutError,
)
from cycode.cyclient import config
from cycode.cyclient.cycode_client_base import CycodeClientBase
def _make_client() -> CycodeClientBase:
return CycodeClientBase(config.cycode_api_url)
# --- _handle_exception mapping ---
def test_handle_exception_timeout() -> None:
client = _make_client()
with pytest.raises(RequestTimeoutError):
client._handle_exception(Timeout('timed out'))
def test_handle_exception_ssl_error() -> None:
client = _make_client()
with pytest.raises(RequestSslError):
client._handle_exception(SSLError('cert verify failed'))
def test_handle_exception_connection_error() -> None:
client = _make_client()
with pytest.raises(RequestConnectionError):
client._handle_exception(RequestsConnectionError('refused'))
def test_handle_exception_http_error_401() -> None:
response = MagicMock()
response.status_code = 401
response.text = 'Unauthorized'
error = HTTPError(response=response)
client = _make_client()
with pytest.raises(HttpUnauthorizedError):
client._handle_exception(error)
def test_handle_exception_http_error_500() -> None:
response = MagicMock()
response.status_code = 500
response.text = 'Internal Server Error'
error = HTTPError(response=response)
client = _make_client()
with pytest.raises(RequestHttpError) as exc_info:
client._handle_exception(error)
assert exc_info.value.status_code == 500
def test_handle_exception_unknown_error_reraises() -> None:
client = _make_client()
with pytest.raises(RuntimeError, match='something unexpected'):
client._handle_exception(RuntimeError('something unexpected'))
# --- HTTP integration via responses mock ---
@responses.activate
def test_get_returns_response_on_success() -> None:
client = _make_client()
url = f'{client.api_url}/test-endpoint'
responses.add(responses.GET, url, json={'ok': True}, status=200)
response = client.get('test-endpoint')
assert response.status_code == 200
assert response.json() == {'ok': True}
@responses.activate
def test_post_returns_response_on_success() -> None:
client = _make_client()
url = f'{client.api_url}/test-endpoint'
responses.add(responses.POST, url, json={'created': True}, status=201)
response = client.post('test-endpoint', body={'data': 'value'})
assert response.status_code == 201
@responses.activate
def test_get_raises_timeout_error() -> None:
client = _make_client()
url = f'{client.api_url}/slow-endpoint'
responses.add(responses.GET, url, body=Timeout('Connection timed out'))
with pytest.raises(RequestTimeoutError):
client.get('slow-endpoint')
@responses.activate
def test_get_raises_ssl_error() -> None:
client = _make_client()
url = f'{client.api_url}/ssl-endpoint'
responses.add(responses.GET, url, body=SSLError('certificate verify failed'))
with pytest.raises(RequestSslError):
client.get('ssl-endpoint')
@responses.activate
def test_get_raises_connection_error() -> None:
client = _make_client()
url = f'{client.api_url}/down-endpoint'
responses.add(responses.GET, url, body=RequestsConnectionError('Connection refused'))
with pytest.raises(RequestConnectionError):
client.get('down-endpoint')
@responses.activate
def test_get_raises_http_unauthorized_error() -> None:
client = _make_client()
url = f'{client.api_url}/auth-endpoint'
responses.add(responses.GET, url, json={'error': 'unauthorized'}, status=401)
with pytest.raises(HttpUnauthorizedError):
client.get('auth-endpoint')
@responses.activate
def test_get_raises_http_error_on_500() -> None:
client = _make_client()
url = f'{client.api_url}/error-endpoint'
responses.add(responses.GET, url, json={'error': 'server error'}, status=500)
with pytest.raises(RequestHttpError) as exc_info:
client.get('error-endpoint')
assert exc_info.value.status_code == 500
@responses.activate
def test_get_raises_http_error_on_403() -> None:
client = _make_client()
url = f'{client.api_url}/forbidden-endpoint'
responses.add(responses.GET, url, json={'error': 'forbidden'}, status=403)
with pytest.raises(RequestHttpError) as exc_info:
client.get('forbidden-endpoint')
assert exc_info.value.status_code == 403