Skip to content

Commit cf00877

Browse files
author
João Silva
committed
!50 bug(targets): [#101] Probely CLI - target delete not working Closes #101
1 parent 0cbe1c7 commit cf00877

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

probely/sdk/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _call_probely_api(cls, request):
119119

120120
status_code = resp.status_code
121121
try:
122-
content = json.loads(resp.content)
122+
content = {} if resp.content == b"" else json.loads(resp.content)
123123
except json.JSONDecodeError: # todo: needs testing
124124
logger.debug(
125125
"Something wrong with the API. Response content is not valid JSON."

probely/sdk/targets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def delete_targets(targets_ids: List[str]):
7070
:type targets_ids: List[str].
7171
7272
"""
73+
validate_resource_ids(PROBELY_API_TARGETS_URL, targets_ids)
74+
7375
url = PROBELY_API_TARGETS_BULK_DELETE_URL
7476

7577
logger.debug("Delete targets : %s", targets_ids)

tests/sdk/test_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,22 @@ def test_probely_api_client__content_when_content_invalid(
222222
assert str(raised_ex.value) == "API is unavailable. Contact support."
223223

224224

225+
@patch("probely.sdk.client.requests.Session.send")
226+
def test_probely_api_client__content_is_empty_string(
227+
requests_session_send_mock: Mock,
228+
set_default_api_key,
229+
):
230+
requests_session_send_mock.return_value = Mock(
231+
status_code=204,
232+
content=b"",
233+
)
234+
235+
status_code, content = ProbelyAPIClient.delete("https://irrelevant_url.com")
236+
237+
assert content == {}
238+
assert status_code == 204
239+
240+
225241
@patch("probely.sdk.client.requests.Session.send")
226242
def test_probely_api_client__return_is_correct(
227243
requests_session_send_mock: Mock,

tests/sdk/test_targets.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_retrieve_target__success_api_call(api_client_mock: Mock):
9090
def test_delete_target__success_api_call(api_client_mock: Mock):
9191
resp_code = 204
9292
testable_id = "2DZkoZH8WMEM"
93-
resp_content = {"id": testable_id}
93+
resp_content = b""
9494

9595
api_client_mock.return_value = (resp_code, resp_content)
9696

@@ -127,14 +127,20 @@ def test_delete_target__unsuccessful_api_call(api_client_mock: Mock):
127127

128128

129129
@patch("probely.sdk.client.ProbelyAPIClient.post")
130-
def test_delete_targets__unsuccessful_api_call(api_client_mock: Mock):
130+
@patch("probely.sdk.targets.validate_resource_ids")
131+
def test_delete_targets__unsuccessful_api_call(
132+
validate_resource_ids_mock: MagicMock, api_client_mock: MagicMock
133+
):
134+
validate_resource_ids_mock.return_value = None
131135
resp_code = 400
132136
testable_id = "2DZkoZH8WMEM"
133137

134-
api_client_mock.return_value = (resp_code, {})
138+
api_client_mock.return_value = (resp_code, b"")
135139
with pytest.raises(BaseException) as exc_info:
136140
delete_targets([testable_id])
137141

142+
validate_resource_ids_mock.assert_called_once()
143+
138144
expected_call_url = PROBELY_API_TARGETS_BULK_DELETE_URL
139145
api_client_mock.assert_called_with(
140146
url=expected_call_url, payload={"ids": [testable_id]}

0 commit comments

Comments
 (0)