Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion elemental/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, server_ip, user=None, api_key=None):
self.server_ip = server_ip
self.user = user
self.api_key = api_key
self.session = requests.Session()

def generate_headers(self, url=""):
# Generate headers according to how users create ElementalLive class
Expand Down Expand Up @@ -64,7 +65,7 @@ def generate_headers(self, url=""):
def send_request(self, http_method, url, headers, body=""):
# Send request according to different methods
try:
response = requests.request(
response = self.session.request(
method=http_method, url=url, data=body, headers=headers)

except requests.exceptions.RequestException as e:
Expand Down
33 changes: 13 additions & 20 deletions elemental/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,56 +61,49 @@ def test_genterate_header_without_authentication_should_not_contain_user():
assert headers['Content-Type'] == 'application/xml'


@mock.patch('requests.request')
def test_send_request_should_call_request_as_expected(mock_request):
mock_request.return_value = mock_response(status=200)
def test_send_request_should_call_request_as_expected():
client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
client.session.request = mock.MagicMock(
return_value=mock_response(status=200))
client.send_request(
'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY)

request_to_elemental = mock_request.call_args_list[0][1]
request_to_elemental = client.session.request.call_args_list[0][1]
assert request_to_elemental['url'] == f'{ELEMENTAL_ADDRESS}/live_events'
assert request_to_elemental['method'] == 'POST'
assert request_to_elemental['headers']['Accept'] == 'application/xml'
assert request_to_elemental['headers']['Content-Type'] == 'application/xml'


@mock.patch('requests.request')
def test_send_request_should_return_response_on_correct_status_code(
mock_request):
def test_send_request_should_return_response_on_correct_status_code():
response_from_elemental_api = file_fixture('success_response_for_'
'create.xml')
mock_request.return_value = mock_response(
status=201, text=response_from_elemental_api)

client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
client.session.request = mock.MagicMock(return_value=mock_response(
status=201, text=response_from_elemental_api))
response = client.send_request(
'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY)

assert response.text == response_from_elemental_api
assert response.status_code == 201


@mock.patch('requests.request')
def test_send_request_should_raise_InvalidRequest_on_RequestException(
mock_request):
mock_request.side_effect = requests.exceptions.RequestException()

def test_send_request_should_raise_InvalidRequest_on_RequestException():
client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
client.session.request = mock.MagicMock(
side_effect=requests.exceptions.RequestException())

with pytest.raises(InvalidRequest):
client.send_request(
'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY)


@mock.patch('requests.request')
def test_send_request_should_raise_InvalidResponse_on_invalid_status_code(
mock_request):
def test_send_request_should_raise_InvalidResponse_on_invalid_status_code():
response_from_elemental_api = file_fixture('fail_to_create_response.xml')
mock_request.return_value = mock_response(
status=404, text=response_from_elemental_api)

client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
client.session.request = mock.MagicMock(return_value=mock_response(
status=404, text=response_from_elemental_api))

with pytest.raises(InvalidResponse) as exc_info:
client.send_request(
Expand Down