Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
res = expect(c, 'GET', '/payments')
res.status_code = 401
with pytest.raises(errors.AuthenticationError):
c.get('/payments')
res = expect(c, 'GET', '/payments')
res.status_code = 404
with pytest.raises(errors.NotFoundError):
c.get('/payments')
res = expect(c, 'GET', '/payments')
res.status_code = 500
with pytest.raises(errors.Error):
c.get('/payments')
def test_preapprovals(c):
c.force_authenticate()
res = expect(c, 'GET', '/preapproval/search', params={'id': '1234'})
res.data = {'paging': {}, 'results': [{'id': '1234'}]}
c.preapprovals.get('1234')
# test not found for .get explicitly since this is a convenience method
res = expect(c, 'GET', '/preapproval/search', params={'id': '1234'})
res.data = {'paging': {}, 'results': []}
with pytest.raises(errors.NotFoundError):
c.preapprovals.get('1234')
expect(c, 'POST', '/preapproval', json={'foo': 'bar'})
c.preapprovals.create(foo='bar')
expect(c, 'PUT', '/preapproval/1234', json={'foo': 'bar'})
c.preapprovals.update(id='1234', foo='bar')
expect(c, 'PUT', '/preapproval/1234', json={'status': 'cancelled'})
c.preapprovals.cancel('1234')
expect(c, 'PUT', '/preapproval/1234', json={'status': 'paused'})
c.preapprovals.pause('1234')
expect(c, 'GET', '/preapproval/search', params={'foo': 'bar'})
c.preapprovals.search(foo='bar')
def test_error_handling(c):
c.force_authenticate()
res = expect(c, 'GET', '/payments')
res.status_code = 400
with pytest.raises(errors.BadRequestError):
c.get('/payments')
res = expect(c, 'GET', '/payments')
res.status_code = 401
with pytest.raises(errors.AuthenticationError):
c.get('/payments')
res = expect(c, 'GET', '/payments')
res.status_code = 404
with pytest.raises(errors.NotFoundError):
c.get('/payments')
res = expect(c, 'GET', '/payments')
res.status_code = 500
def test_authentication_error(c):
res = expect(c, 'POST', '/oauth/token', data={
'client_id': 'XXX',
'client_secret': 'XXX',
'grant_type': 'client_credentials'
})
res.status_code = 400
with pytest.raises(errors.BadRequestError):
c.authenticate()
def _handle_request_error(self, error):
if isinstance(error, requests.HTTPError):
status = error.response.status_code
if status == 400:
raise errors.BadRequestError(error)
elif status == 401:
raise errors.AuthenticationError(error)
elif status == 404:
raise errors.NotFoundError(error)
raise errors.Error(error)
def get(self, id):
# NOTE: this is actually performing a search with ID and mangling
# the response data to conform to a single object format.
# There is no method to retrieve a preapproval by ID at the moment.
res = self.search(id=id)
if not res.data['results']:
raise errors.NotFoundError('could not find preapproval with ID = %s' % id)
res = Response(self._client.client, res._response) # pylint: disable=protected-access
res.data = res.data['results'][0]
return res
def _handle_request_error(self, error):
if isinstance(error, requests.HTTPError):
status = error.response.status_code
if status == 400:
raise errors.BadRequestError(error)
elif status == 401:
raise errors.AuthenticationError(error)
elif status == 404:
raise errors.NotFoundError(error)
raise errors.Error(error)