Skip to content

Commit 59dfe61

Browse files
committed
Support bad credentials
1 parent 747c8e1 commit 59dfe61

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/vws/query.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from vws.exceptions import MaxNumResultsOutOfRange
99

1010
from ._authorization import authorization_header, rfc_1123_date
11+
# TODO this cannot be private...
12+
from .vws import _raise_for_result_code
1113

1214

1315
class CloudRecoService:
@@ -74,8 +76,8 @@ def query(
7476
data=content,
7577
)
7678

77-
try:
78-
return response.json()['results']
79-
except json.decoder.JSONDecodeError:
80-
if 'Accepted range is from 1 to 50 (inclusive).' in response.text:
81-
raise MaxNumResultsOutOfRange(response=response)
79+
if 'Accepted range is from 1 to 50 (inclusive).' in response.text:
80+
raise MaxNumResultsOutOfRange(response=response)
81+
82+
_raise_for_result_code(response=response, expected_result_code='Success')
83+
return response.json()['results']

tests/test_query.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import io
66
import uuid
7+
from requests import codes
78

89
import pytest
910

1011
from vws import VWS, CloudRecoService
11-
from vws.exceptions import MaxNumResultsOutOfRange
12+
from vws.exceptions import AuthenticationFailure, MaxNumResultsOutOfRange
1213

1314

1415
class TestQuery:
@@ -125,9 +126,20 @@ def test_too_many(
125126
)
126127
assert str(exc.value) == expected_value
127128

129+
class TestBadCredentials:
130+
131+
def test_bad_credentials(self, high_quality_image: io.BytesIO):
132+
cloud_reco_client = CloudRecoService(
133+
client_access_key='a',
134+
client_secret_key='a',
135+
)
136+
with pytest.raises(AuthenticationFailure) as exc:
137+
cloud_reco_client.query(image=high_quality_image)
138+
139+
assert exc.value.response.status_code == codes.UNAUTHORIZED
140+
128141

129142
# TODO test custom base URL
130-
# TODO test bad credentials
131143
# TODO test options - include_target_data
132144

133145
# TODO do we give an image type? Infer it?

0 commit comments

Comments
 (0)