def test_invalid_json_respons_is_handled(mocker, caplog, method): request = mocker.patch('indico_ravem.util.requests.request') request.method = method.upper() request.url = RavemPlugin.settings.get('api_endpoint') + 'test_endpoint' response = MagicMock() response.request = request response.url = response.request.url response.json.return_value = {'bad': 'json'} response.raise_for_status.return_value = False request.return_value = response with pytest.raises(RavemAPIException) as excinfo: ravem_api_call('test_endpoint', method=method) err_msg = '{0} {1} returned json without a result or error: {2}'.format( method.upper(), RavemPlugin.settings.get('api_endpoint') + 'test_endpoint', {'bad': 'json'}) assert excinfo.value.message == err_msg assert excinfo.value.endpoint == 'test_endpoint' assert excinfo.value.response == response log = extract_logs(caplog, one=True, name='indico.plugin.ravem') assert log.message == err_msg assert request.call_count == 1
def test_raises_timeout(mocker): request = mocker.patch('indico_ravem.util.requests.request') request.side_effect = Timeout('Timeout test error message', request=request) with pytest.raises(Timeout) as excinfo: ravem_api_call('test_endpoint') assert excinfo.value.message == "Timeout while contacting the room." assert request.call_count == 1
def test_accepts_json(mocker): request = mocker.patch('indico_ravem.util.requests.request') response = MagicMock() response.json.return_value = {'result': 'test'} response.raise_for_status.return_value = False request.return_value = response ravem_api_call('test_endpoint', param1='test1', param2='test2') assert request.call_count == 1 assert request.call_args[1]['headers']['Accept'] == 'application/json'
def test_correct_auth_method(mocker): request = mocker.patch('indico_ravem.util.requests.request') response = MagicMock() response.json.return_value = {'result': 'test'} response.raise_for_status.return_value = False request.return_value = response ravem_api_call('test_endpoint', param1='test1', param2='test2') assert request.call_count == 1 assert isinstance(request.call_args[1]['auth'], HTTPDigestAuth)
def test_correct_http_method(mocker, method): request = mocker.patch('indico_ravem.util.requests.request') response = MagicMock() response.json.return_value = {'result': 'test'} response.raise_for_status.return_value = False request.return_value = response ravem_api_call('test_endpoint', method=method, param1='test1', param2='test2') assert request.call_count == 1 assert request.call_args[0][0] == method
def test_params_generated(mocker, params): request = mocker.patch('indico_ravem.util.requests.request') response = MagicMock() response.json.return_value = {'result': 'test'} response.raise_for_status.return_value = False request.return_value = response ravem_api_call('test_endpoint', **params) assert request.call_count == 1 assert request.call_args[1]['params'] == params
def test_unexpected_exception_is_logged(mocker, caplog, method, params): request = mocker.patch('indico_ravem.util.requests.request') request.side_effect = IndexError('this is unexpected') with pytest.raises(IndexError) as excinfo: ravem_api_call('test_endpoint', method=method, **params) assert excinfo.value.message == 'this is unexpected' log = extract_logs(caplog, one=True, name='indico.plugin.ravem') assert log.message == "failed call: {0} {1} with {2}: {3}".format(method.upper(), 'test_endpoint', params, 'this is unexpected') assert request.call_count == 1
def test_correct_api_endpoint(mocker, root_endpoint, endpoint, expected_url): request = mocker.patch('indico_ravem.util.requests.request') response = MagicMock() response.json.return_value = {'result': 'test'} response.raise_for_status.return_value = False request.return_value = response RavemPlugin.settings.set('api_endpoint', root_endpoint) ravem_api_call(endpoint, param1='test1', param2='test2') assert request.call_count == 1 assert request.call_args[0][1] == expected_url
def test_correct_auth_credentials(mocker, username, password): request = mocker.patch('indico_ravem.util.requests.request') response = MagicMock() response.json.return_value = {'result': 'test'} response.raise_for_status.return_value = False request.return_value = response RavemPlugin.settings.set_multi({'username': username, 'password': password}) ravem_api_call('test_endpoint', param1='test1', param2='test2') assert request.call_count == 1 auth = request.call_args[1]['auth'] assert auth.username == username assert auth.password == password
def test_correct_auth_method(mocker): request = mocker.patch('indico_ravem.util.requests.request') response = MagicMock() response.json.return_value = {'result': 'test'} response.raise_for_status.return_value = False request.return_value = response token = 'foo' RavemPlugin.settings.set('access_token', token) ravem_api_call('test_endpoint', param1='test1', param2='test2') assert request.call_count == 1 assert 'Authorization' in request.call_args[1]['headers'] assert request.call_args[1]['headers'][ 'Authorization'] == 'Bearer %s' % token
def connect_endpoint(self, room_name, vc_room_id): return ravem_api_call('%s/connect' % self.SERVICE_TYPE, method='POST', json={ 'meetingId': vc_room_id, 'roomName': room_name })
def disconnect_endpoint(room_name, vc_room_name, service_type): """Disconnects a physical room from a vidyo room using the RAVEM API. This call will return "OK" as a result immediately if RAVEM can (or has started to) perform the operation. This does not mean the operation has actually succeeded. One should poll for the status of the room afterwards using the `get_endpoint_status` method after some delay to allow the operation to be performed. :param room_name: str -- the name of the physical room :param vc_room_name: str -- The Vidyo room name (which is recommended to disconnect the physical room from the Vidyo room in case the service type is Vidyo.) :param service_type: str -- The endpoint type (usually `vidyo` or `other`) :returns: dict -- {'result': 'OK'} if the operation "succeeds", raises a RavemAPIException otherwise. """ params = { 'method': 'POST', 'where': 'room_name', 'value': room_name, 'type': service_type } if service_type == 'vidyo': params['vidyo_room_name'] = vc_room_name return ravem_api_call('videoconference/disconnect', **params)
def test_http_error_is_logged(mocker, caplog, method, params): request = mocker.patch('indico_ravem.util.requests.request') request.method = method.upper() request.url = RavemPlugin.settings.get('api_endpoint') + 'test_endpoint' response = MagicMock() response.raise_for_status.side_effect = HTTPError('Well this is embarrassing') response.request = request response.url = response.request.url request.return_value = response with pytest.raises(HTTPError) as excinfo: ravem_api_call('test_endpoint', method=method, **params) assert excinfo.value.message == 'Well this is embarrassing' log = extract_logs(caplog, one=True, name='indico.plugin.ravem') assert log.message == '{0} {1} failed with {2}'.format( method.upper(), RavemPlugin.settings.get('api_endpoint') + 'test_endpoint', 'Well this is embarrassing') assert request.call_count == 1
def get_endpoint_status(room_name): """Returns the status of an physical room. This call returns the status of a room equipped with videoconference capable device :param room_name: str -- the name of the physical room """ room_name = room_name.replace('/', '-', 1) return ravem_api_call('rooms/details', method='GET', params={ 'where': 'room_name', 'value': room_name })
def get_endpoint_status(room_name): """Returns the status of an physical room. This call returns the status of a room equipped with Vidyo capable device :param room_name: str -- the name of the physical room :returns: dict -- the status of the room as a JSON response according to the RAVEM API. """ return ravem_api_call('getstatus', method='GET', service_name='videoconference', where='room_name', value=room_name)
def connect_endpoint(vidyo_room_id, query): """Connects a physical room to a Vidyo room using the RAVEM API. This call will return "OK" as a result immediately if RAVEM can (or has started to) perform the operation. This does not mean the operation has actually succeeded. One should poll for the status of the room afterwards using the `get_endpoint_status` method after some delay to allow the operation to be performed.. :param vidyo_room_id: str -- target Vidyo room ID :param query: str -- search query passed to RAVEM to allow it to find the physical room from Vidyo. Usually, simply the room's endpoint as specified in the room's status data. :returns: dict -- {'result': 'OK'} if the operation "succeeds", raises a RavemAPIException otherwise. """ return ravem_api_call('videoconference/connect', method='POST', vidyo_room_id=vidyo_room_id, query=query)