Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_fail_create():
responses.add(
responses.POST,
'https://api.sparkpost.com/api/v1/templates',
status=500,
content_type='application/json',
body="""
{"errors": [{"message": "You failed", "description": "More Info"}]}
"""
)
with pytest.raises(SparkPostAPIException):
sp = SparkPost('fake-key')
sp.templates.create()
def test_fail_get():
responses.add(
responses.GET,
'https://api.sparkpost.com/api/v1/transmissions/foobar',
status=404,
content_type='application/json',
body="""
{"errors": [{"message": "cant find", "description": "where you go"}]}
"""
)
with pytest.raises(SparkPostAPIException):
sp = SparkPost('fake-key')
sp.transmission.get('foobar')
def test_fail_request():
responses.add(
responses.GET,
fake_uri,
status=500,
content_type='application/json',
body='{"errors": [{"message": "failure", "description": "desc"}]}'
)
resource = create_resource()
with pytest.raises(SparkPostAPIException):
resource.request('GET', resource.uri)
def test_fail_get():
responses.add(
responses.GET,
'https://api.sparkpost.com/api/v1/suppression-list/foobar',
status=404,
content_type='application/json',
body="""
{"errors": [{"message": "cant find", "description": "where you go"}]}
"""
)
with pytest.raises(SparkPostAPIException):
sp = SparkPost('fake-key')
sp.suppression_list.get('foobar')
def test_fail_preview():
responses.add(
responses.POST,
'https://api.sparkpost.com/api/v1/templates/foobar/preview',
status=404,
content_type='application/json',
body="""
{"errors": [{"message": "cant find", "description": "where you go"}]}
"""
)
with pytest.raises(SparkPostAPIException):
sp = SparkPost('fake-key')
sp.templates.preview('foobar', {})
def set_mock_failure(self, status_code=400, raw=b'{"errors":[{"message":"test error"}]}', encoding='utf-8'):
from sparkpost.exceptions import SparkPostAPIException
# Need to build a real(-ish) requests.Response for SparkPostAPIException
response = requests.Response()
response.status_code = status_code
response.encoding = encoding
response.raw = six.BytesIO(raw)
response.url = "/mock/send"
self.mock_send.side_effect = SparkPostAPIException(response)
def test_fail_get():
responses.add(
responses.GET,
'%s/fake.com' % base_uri,
status=404,
content_type='application/json',
body='{"errors": [{"message": "cannot find"}]}'
)
with pytest.raises(SparkPostAPIException):
sp = SparkPost('fake-key')
sp.sending_domains.get('fake.com')
def test_fail_domains():
responses.add(
responses.GET,
'https://api.sparkpost.com/api/v1/metrics/domains',
status=500,
content_type='application/json',
body="""
{"errors": [{"message": "You failed", "description": "More Info"}]}
"""
)
with pytest.raises(SparkPostAPIException):
sp = SparkPost('fake-key')
sp.metrics.domains.list()
# TODO: select exception to catch here
except: # noqa: E722
errors = [response.text or ""]
self.status = response.status_code
self.response = response
self.errors = errors
message = """Call to {uri} returned {status_code}, errors:
{errors}
""".format(
uri=response.url,
status_code=response.status_code,
errors='\n'.join(errors)
)
super(SparkPostAPIException, self).__init__(message, *args, **kwargs)
def request(self, method, uri, headers, **kwargs):
response = self.sess.request(method, uri, headers=headers, **kwargs)
if response.status_code == 204:
return True
if not response.ok:
raise SparkPostAPIException(response)
if 'results' in response.json():
return response.json()['results']
return response.json()