Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from mamba import description, context, it, before
from expects import expect, equal
from doublex import Stub, Spy
from doublex_expects import have_been_called_with, have_been_called
from amadeus import NetworkError, Response, Client
with description('ResponseError') as self:
with context('str(error)'):
with it('should be undefine if no response is present'):
error = NetworkError(None)
expect(str(error)).to(equal('[---]'))
with it('should just return the code if no data is present'):
response = Stub(Response)
response.parsed = True
response.result = {}
response.status_code = 400
error = NetworkError(response)
expect(str(error)).to(equal('[400]'))
with it('should be rich if errors are present'):
response = Stub(Response)
response.parsed = True
response.result = {
'errors': [
{
'detail': 'This field must be filled.',
'source': {'parameter': 'departureDate'}
},
try:
from urllib.error import URLError
except ImportError: # pragma: no cover
from urllib2 import Request as URLError # pragma: no cover
from amadeus import Client, Response, ResponseError
with description('HTTP') as self:
with before.each:
self.client = Client(
client_id='123',
client_secret='234',
log_level='silent'
)
self.response = Stub(Response)
self.request_method = method_returning(self.response)
with context('Client.get'):
with it('should pass all details to the request method'):
self.client.request = self.request_method
response = self.client.get('/foo', foo='bar')
expect(response).to(equal(self.response))
expect(self.client.request).to(
have_been_called_with('GET', '/foo', {'foo': 'bar'})
)
with context('Client.post'):
with it('should pass all details to the request method'):
self.client.request = self.request_method
response = self.client.post('/foo', {'foo': 'bar'})
expect(response).to(equal(self.response))
with description('Pagination') as self:
with before.all:
# Initialize the client and pagination object
self.next_response = Stub(Response)
with Spy(Client) as client:
client.request(ANY_ARG).returns(self.next_response)
self.client = client
self.pagination = Pagination()
self.pagination.request = self.client.request
# Initialize the previous request made and response received
request = Stub(Request)
request.verb = self.verb = 'GET'
request.path = self.path = '/a'
request.params = self.params = {'a': 'b'}
self.response = Response(Stub('http_response'), request)
with context('Client.previous'):
with it('should create a new request with the page and call it'):
self.response.result = {
'meta': {'links': {'previous': 'http://f.co?page=1'}}
}
next_response = self.pagination.previous(self.response)
expect(self.client.request).to(
have_been_called_with('GET', '/a',
{'a': 'b', 'page': {'offset': '1'}})
)
expect(next_response).to(equal(self.next_response))
with it('should return nil if the page was not found'):
self.response.result = {'meta': {'links': {}}}
)
http_response.read().returns(u'Hello World')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
with it('should parse no body for a missing content type'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns([])
http_response.read().returns('Hello World')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
with it('should raise a ParseError if it could not be parsed'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{')
response = Response(http_response, self.request)
expect(lambda: response._parse(self.client)).to(
raise_error(ParserError)
http_response.read().returns('{ "data" : { "a" : 1 } }')
response = Response(http_response, self.request)
response = response._parse(self.client)
expect(response.data).to(equal({'a': 1}))
expect(response).to(be_a(Response))
with it('should parse the body for content type vnd.amadeus+json'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/vnd.amadeus+json')]
)
http_response.read().returns('{ "data" : { "a" : 1 } }')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.data).to(equal({'a': 1}))
with it('should parse no body for other content types'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'plain/text')]
)
http_response.read().returns(u'Hello World')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
with it('should raise a ParseError if it could not be parsed'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{')
response = Response(http_response, self.request)
expect(lambda: response._parse(self.client)).to(
raise_error(ParserError)
)
with context('Response._detect_error'):
with before.all:
http_response = Stub()
request = Stub(Request)
self.client = Stub(Client)
self.client.log_level = 'silent'
self.response = Response(http_response, request)
with it('should raise a network error if no status code was found'):
self.response.status_code = None
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{')
response = Response(http_response, self.request)
expect(lambda: response._parse(self.client)).to(
raise_error(ParserError)
)
with context('Response._detect_error'):
with before.all:
http_response = Stub()
request = Stub(Request)
self.client = Stub(Client)
self.client.log_level = 'silent'
self.response = Response(http_response, request)
with it('should raise a network error if no status code was found'):
self.response.status_code = None
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
raise_error(NetworkError)
)
with it('should raise a server error if a 500 occured'):
self.response.status_code = 500
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
raise_error(ServerError)
)
with it('should raise a authentication error if a 401 happened'):
from mamba import description, context, it
from expects import expect, equal
from doublex import Stub
from amadeus import Response, Request
with description('Response') as self:
with context('.__init__'):
with it('should take a http response object and request'):
http_response = Stub('http_response')
request = Stub(Request)
response = Response(http_response, request)
expect(response.http_response).to(equal(http_response))
expect(response.request).to(equal(request))
with Stub(Client) as client:
client.log_level = 'silent'
self.client = client
with it('should parse the body'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{ "data" : { "a" : 1 } }')
response = Response(http_response, self.request)
response = response._parse(self.client)
expect(response.data).to(equal({'a': 1}))
expect(response).to(be_a(Response))
with it('should parse the body for content type vnd.amadeus+json'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/vnd.amadeus+json')]
)
http_response.read().returns('{ "data" : { "a" : 1 } }')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.data).to(equal({'a': 1}))
with it('should parse no body for other content types'):
with Stub() as http_response:
http_response.status_code = '200'
'source': {'parameter': 'destination'}
}
]
}
response.status_code = 401
error = NetworkError(response)
expect(str(error)).to(equal(
('''[401]
[departureDate] This field must be filled.
[origin] This field must be filled.
[destination] This field must be filled.''')
))
with it('should be rich if an error_description is present'):
response = Stub(Response)
response.parsed = True
response.result = {'error_description': 'error'}
response.status_code = 401
error = NetworkError(response)
expect(str(error)).to(equal('[401]\nerror'))
with context('.code'):
with it('should determine the code off the class name'):
error = NetworkError(None)
expect(error.code).to(equal('NetworkError'))
with context('.log'):
with before.all:
self.client = Stub(Client)
self.error = NetworkError(None)