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', {
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        }, REQUEST_BODY)
def test_send_request_should_raise_NotFound_on_404():
    response_from_elemental_api = file_fixture('fail_to_create_response.xml')

    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(NotFound) as exc_info:
        client.send_request('POST', f'{ELEMENTAL_ADDRESS}/live_events',
                            HEADERS, REQUEST_BODY)

    assert str(exc_info.value).endswith(
        f"Response: 404\n{response_from_elemental_api}")
def test_send_request_should_raise_InvalidResponse_on_invalid_status_code():
    response_from_elemental_api = file_fixture('fail_to_create_response.xml')

    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('POST', f'{ELEMENTAL_ADDRESS}/live_events', {
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        }, REQUEST_BODY)

    assert str(exc_info.value).endswith(
        f"Response: 404\n{response_from_elemental_api}")
def test_update_event_with_restart():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }
    client.send_request = mock.Mock()
    client.send_request.return_value = mock_response(status=200)

    client.update_event('53', '<updated-event />', restart=True)

    client.send_request.assert_called_once_with(
        http_method='PUT',
        url='FAKE_ADDRESS.com/live_events/53?unlocked=1',
        headers={
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        },
        body='<updated-event />',
        timeout=None)
    send_mock_call = client.send_request.call_args_list[0][1]
    assert send_mock_call['http_method'] == 'PUT'
    assert send_mock_call[
        'url'] == f'{ELEMENTAL_ADDRESS}/live_events/53?unlocked=1'
    assert send_mock_call['headers'] == {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }
def test_create_event():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    elemental_response = file_fixture('success_response_for_create.xml')

    client.send_request.return_value = mock_response(
        status=201, content=elemental_response)

    event_id = client.create_event('<new-event />')

    client.send_request.assert_called_once_with(
        http_method='POST',
        url='FAKE_ADDRESS.com/live_events',
        headers={
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        },
        body='<new-event />',
        timeout=None)

    send_mock_call = client.send_request.call_args_list[0][1]
    assert send_mock_call['http_method'] == 'POST'
    assert send_mock_call['url'] == f'{ELEMENTAL_ADDRESS}/live_events'
    assert send_mock_call['headers'] == {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }
    assert event_id == {'id': '53'}
def test_find_devices_in_use_will_call_send_request_as_expect():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    client.send_request.return_value = \
        mock_response(status=200,
                      text=file_fixture('sample_event_list.xml'))

    client.find_devices_in_use()

    client.send_request.assert_called_with(http_method="GET",
                                           url=f'{ELEMENTAL_ADDRESS}'
                                           f'/live_events?'
                                           f'filter=active',
                                           headers={
                                               'Accept': 'application/xml',
                                               'Content-Type':
                                               'application/xml'
                                           },
                                           timeout=None)
def test_event_stop_output():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }
    client.send_request = mock.Mock()
    client.send_request.return_value = mock_response(status=200)

    client.event_stop_output(event_id='53', output_id='13')

    client.send_request.assert_called_once_with(
        http_method='POST',
        url='FAKE_ADDRESS.com/live_events/53/stop_output',
        headers={
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        },
        body='<output_id>13</output_id>',
        timeout=None)
    send_mock_call = client.send_request.call_args_list[0][1]
    assert send_mock_call['http_method'] == 'POST'
    assert send_mock_call[
        'url'] == f'{ELEMENTAL_ADDRESS}/live_events/53/stop_output'
    assert send_mock_call['headers'] == {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }
def test_get_preview_will_raise_ElementalException_if_preview_unavailable():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    client.send_request.return_value = mock_response(
        status=200,
        text=json.dumps({
            "type":
            "error",
            "message":
            "Input is invalid. "
            "Device already in use."
        }))

    with pytest.raises(ElementalException) as exc_info:
        client.generate_preview('1')

    respond_text = json.dumps({
        'type':
        'error',
        'message':
        'Input is invalid. '
        'Device already in use.'
    })
    assert str(exc_info.value).endswith(f"Response: 200\n" f"{respond_text}")
def test_get_event_status_missing_status_in_elemental_response():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    response_from_elemental_api = """<?xml version="1.0" encoding="UTF-8"?>
    <live_event href="/live_events/18">
    </live_event>
    """
    client.send_request.return_value = mock_response(
        status=200, text=response_from_elemental_api)
    event_id = '999'

    status = client.get_event_status(event_id)

    assert status == 'unknown'
    client.send_request.assert_called_once_with(
        http_method='GET',
        url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}/status',
        headers={
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        },
        timeout=None)
def test_get_input_device_by_id_will_get_right_devices_info():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    client.find_devices_in_use = mock.Mock()
    client.find_devices_in_use.return_value = ("HD-SDI 1", )
    client.send_request.return_value = \
        mock_response(status=200,
                      text=file_fixture('sample_single_device.xml'))

    res = client.get_input_device_by_id('2')
    assert res == {
        "id": "2",
        "name": None,
        "device_name": "HD-SDI 2",
        "device_number": "0",
        "device_type": "AJA",
        "description": "AJA Capture Card",
        "channel": "2",
        "channel_type": "HD-SDI",
        "quad": "false",
        'availability': True
    }
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', {
                            'Accept': 'application/xml',
                            'Content-Type': 'application/xml'
                        },
                        REQUEST_BODY,
                        timeout=TIMEOUT)

    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'
    assert request_to_elemental['timeout'] == TIMEOUT
def test_send_request_should_return_response_on_correct_status_code():
    response_from_elemental_api = file_fixture(
        'success_response_for_create.xml')
    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',
                                   {
                                       'Accept': 'application/xml',
                                       'Content-Type': 'application/xml'
                                   }, REQUEST_BODY)

    assert response.text == response_from_elemental_api
    assert response.status_code == 201
def test_find_devices_in_use_will_return_in_used_devices():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    client.send_request.return_value = \
        mock_response(status=200,
                      text=file_fixture('sample_event_list.xml'))

    devices = client.find_devices_in_use()
    assert devices == {'HD-SDI 1'}
def test_describe_event_will_return_event_info_with_empty_origin_url_if_destination_is_missing_in_response(
):
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }
    client.send_request = mock.Mock()
    client.send_request.return_value = mock_response(
        status=200, text='<live_event></live_event>')

    event_id = '139'
    event_info = client.describe_event(event_id)
    assert event_info == {
        'origin_url': '',
        'backup_url': None,
        'status': 'unknown'
    }
def test_get_event_status():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    response_from_elemental_api = """<?xml version="1.0" encoding="UTF-8"?>
    <live_event href="/live_events/18">
      <node>ctcsdprdel5</node>
      <user_data/>
      <submitted>2020-11-02 18:38:27 -0500</submitted>
      <priority>50</priority>
      <restart_on_failure>false</restart_on_failure>
      <status>pending</status>
      <average_fps>0</average_fps>
      <start_time/>
      <elapsed>0</elapsed>
      <elapsed_time_in_words>00:00:00</elapsed_time_in_words>
      <dropped_frames/>
      <buffer_avg/>
      <buffer_max/>
    </live_event>
    """
    client.send_request.return_value = mock_response(
        status=200, text=response_from_elemental_api)
    event_id = '999'

    status = client.get_event_status(event_id)

    assert status == 'pending'
    client.send_request.assert_called_once_with(
        http_method='GET',
        url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}/status',
        headers={
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        },
        timeout=None)
def test_get_preview_will_parse_response_json_as_expect():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    client.send_request.return_value = mock_response(
        status=200,
        text=file_fixture('success_response_for_generate_preview.json'))

    response = client.generate_preview('2')

    assert response == {
        'preview_url':
        f'{ELEMENTAL_ADDRESS}/'
        f'images/thumbs/p_1563568669_job_0.jpg'
    }
def test_describe_event_will_return_event_info_as_expect():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }
    client.send_request = mock.Mock()
    response_from_elemental_api = file_fixture('sample_event.xml')
    client.send_request.return_value = mock_response(
        status=200, text=response_from_elemental_api)

    event_id = '139'
    event_info = client.describe_event(event_id)
    assert event_info == {
        'origin_url': 'https://vmjhch43nfkghi.data.mediastore.us-east-1.'
        'amazonaws.com/mortyg3b4/master/mortyg3b4.m3u8',
        'backup_url': 'https://vmjhch43nfkghi.data.mediastore.us-east-1.'
        'amazonaws.com/mortyg3b4/backup/mortyg3b4.m3u8',
        'status': 'complete'
    }
def test_cancel_event_should_call_send_request_as_expected():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    client.send_request.return_value = mock_response(status=200)

    event_id = '999'
    client.cancel_event(event_id)
    client.send_request.assert_called_once_with(
        http_method='POST',
        url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}/cancel',
        headers={
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        },
        timeout=None)
def test_get_input_device_by_id_will_call_send_request_as_expect():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    client.find_devices_in_use = mock.Mock()
    client.find_devices_in_use.return_value = ("HD-SDI 1", )
    client.send_request.return_value = \
        mock_response(status=200,
                      text=file_fixture('sample_single_device.xml'))

    client.get_input_device_by_id('2')

    client.send_request.\
        assert_called_with(http_method="GET",
                           url=f'{ELEMENTAL_ADDRESS}/devices/2',
                           headers={'Accept': 'application/xml', 'Content-Type': 'application/xml'}, timeout=None)
def test_describe_event_will_call_send_request_as_expect():
    client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)

    client.generate_headers = mock.Mock()
    client.generate_headers.return_value = {
        'Accept': 'application/xml',
        'Content-Type': 'application/xml'
    }

    client.send_request = mock.Mock()
    response_from_elemental_api = file_fixture('sample_event.xml')
    client.send_request.return_value = mock_response(
        status=200, text=response_from_elemental_api)

    event_id = '999'
    client.describe_event(event_id)
    client.send_request.assert_called_once_with(
        http_method='GET',
        url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}',
        headers={
            'Accept': 'application/xml',
            'Content-Type': 'application/xml'
        },
        timeout=None)