-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathexception_test.py
More file actions
237 lines (189 loc) · 8.53 KB
/
exception_test.py
File metadata and controls
237 lines (189 loc) · 8.53 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
import pytest
from unittest.mock import Mock
from checkout_sdk.exception import (
CheckoutException,
CheckoutArgumentException,
CheckoutAuthorizationException,
CheckoutApiException
)
from checkout_sdk.authorization_type import AuthorizationType
from checkout_sdk.utils import map_to_http_metadata
def test_checkout_exception():
with pytest.raises(CheckoutException) as exc_info:
raise CheckoutException("Test message")
exception = exc_info.value
assert str(exception) == "Test message"
def test_checkout_argument_exception():
with pytest.raises(CheckoutArgumentException) as exc_info:
raise CheckoutArgumentException("Argument error occurred")
exception = exc_info.value
assert str(exception) == "Argument error occurred"
def test_checkout_argument_exception_no_message():
with pytest.raises(CheckoutArgumentException) as exc_info:
raise CheckoutArgumentException()
exception = exc_info.value
assert str(exception) == ""
def test_checkout_authorization_exception():
with pytest.raises(CheckoutAuthorizationException) as exc_info:
raise CheckoutAuthorizationException("Authorization error occurred")
exception = exc_info.value
assert str(exception) == "Authorization error occurred"
def test_invalid_authorization():
auth_type = Mock(spec=AuthorizationType)
auth_type.name = "SECRET_KEY"
with pytest.raises(CheckoutAuthorizationException) as exc_info:
CheckoutAuthorizationException.invalid_authorization(auth_type)
assert "SECRET_KEY authorization type" in str(exc_info.value)
def test_invalid_key():
key_type = Mock(spec=AuthorizationType)
key_type.name = "PUBLIC_KEY"
with pytest.raises(CheckoutAuthorizationException) as exc_info:
CheckoutAuthorizationException.invalid_key(key_type)
assert "PUBLIC_KEY is required for this operation" in str(exc_info.value)
@pytest.mark.parametrize("auth_type_name", ["SECRET_KEY", "PUBLIC_KEY", "CUSTOM_KEY"])
def test_invalid_authorization_various_types(auth_type_name):
auth_type = Mock(spec=AuthorizationType)
auth_type.name = auth_type_name
with pytest.raises(CheckoutAuthorizationException) as exc_info:
CheckoutAuthorizationException.invalid_authorization(auth_type)
assert f"{auth_type_name} authorization type" in str(exc_info.value)
def test_checkout_api_exception():
response = Mock()
response.status_code = 400
response.text = '{"error_type": "request_invalid", "error_codes": ["invalid_field"], "request_id": "req_123456"}'
response.json.return_value = {
"error_type": "request_invalid",
"error_codes": ["invalid_field"],
"request_id": "req_123456"
}
response.headers = {}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.http_metadata.status_code == 400
assert exception.error_type == "request_invalid"
assert exception.error_details == ["invalid_field"]
assert exception.request_id == "req_123456"
def test_checkout_api_exception_without_error_details():
response = Mock()
response.status_code = 500
response.text = '{"message": "Internal Server Error", "request_id": "req_789012"}'
response.json.return_value = {
"message": "Internal Server Error",
"request_id": "req_789012"
}
response.headers = {}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.http_metadata.status_code == 500
assert exception.error_type is None
assert exception.error_details is None
assert exception.request_id == "req_789012"
def test_checkout_api_exception_empty_response():
response = Mock()
response.status_code = 404
response.text = ''
response.headers = {'Cko-Request-Id': 'header_req_345678'}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.http_metadata.status_code == 404
assert exception.error_type is None
assert exception.error_details is None
assert exception.request_id == "header_req_345678"
def test_checkout_api_exception_non_json_response():
response = Mock()
response.status_code = 502
response.text = 'Bad Gateway'
response.json.side_effect = ValueError("No JSON object could be decoded")
response.headers = {'Cko-Request-Id': 'header_req_502502'}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.http_metadata.status_code == 502
assert exception.error_type is None
assert exception.error_details is None
assert exception.request_id == "header_req_502502"
def test_checkout_api_exception_request_id_from_header_fallback():
response = Mock()
response.status_code = 400
response.text = '{"error_type": "request_invalid", "error_codes": ["invalid_field"]}'
response.json.return_value = {
"error_type": "request_invalid",
"error_codes": ["invalid_field"]
}
response.headers = {'Cko-Request-Id': '0120e756-6d00-453c-a398-ff1643f9a873'}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.request_id == "0120e756-6d00-453c-a398-ff1643f9a873"
assert exception.error_type == "request_invalid"
assert exception.error_details == ["invalid_field"]
def test_checkout_api_exception_no_request_id_anywhere():
response = Mock()
response.status_code = 400
response.text = '{"error_type": "request_invalid"}'
response.json.return_value = {"error_type": "request_invalid"}
response.headers = {} # Sin Cko-Request-Id
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.request_id is None
assert exception.error_type == "request_invalid"
@pytest.mark.parametrize("status_code", [400, 401, 403, 404, 500])
def test_checkout_api_exception_various_status_codes(status_code):
response = Mock()
response.status_code = status_code
response.text = ''
response.headers = {'Cko-Request-Id': f'req_{status_code}'}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.http_metadata.status_code == status_code
assert exception.request_id == f'req_{status_code}'
def test_map_to_http_metadata():
response = Mock()
response.status_code = 200
response.headers = {'Content-Type': 'application/json'}
metadata = map_to_http_metadata(response)
assert metadata.status_code == 200
assert metadata.headers == {'Content-Type': 'application/json'}
def test_checkout_api_exception_message():
response = Mock()
response.status_code = 400
response.text = '{"error_type": "invalid_request", "error_codes": ["bad_request"], "request_id": "msg_req_400"}'
response.json.return_value = {
"error_type": "invalid_request",
"error_codes": ["bad_request"],
"request_id": "msg_req_400"
}
response.headers = {}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
expected_message = "The API response status code (400) does not indicate success."
assert str(exception) == expected_message
assert exception.request_id == "msg_req_400"
def test_checkout_api_exception_no_response_text():
response = Mock()
response.status_code = 400
response.text = None
response.headers = {'Cko-Request-Id': 'no_text_req_id'}
with pytest.raises(CheckoutApiException) as exc_info:
raise CheckoutApiException(response)
exception = exc_info.value
assert exception.http_metadata.status_code == 400
assert exception.error_type is None
assert exception.error_details is None
assert exception.request_id == "no_text_req_id"
def test_checkout_api_exception_logs_on_json_parse_error(caplog):
response = Mock()
response.status_code = 502
response.text = 'Bad Gateway'
response.json.side_effect = ValueError("No JSON object could be decoded")
response.headers = {'Cko-Request-Id': 'header_req_logging'}
with caplog.at_level("ERROR"):
with pytest.raises(CheckoutApiException):
raise CheckoutApiException(response)
assert any("Failed to parse response JSON payload" in m for m in caplog.messages)