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
Example #2
0
def test_create_next_double_payment(caplog, creation_params):
    creation_params['registration'].transaction = MagicMock(
        status=TransactionStatus.successful)
    _, double_payment = PaymentTransaction.create_next(**creation_params)
    log = extract_logs(caplog, one=True, name='indico.payment').message
    assert 'already paid' in log
    assert double_payment
def test_get_room_status_error(caplog, httpretty, room_name, error):
    RavemPlugin.settings.set("api_endpoint", RAVEM_TEST_API_ENDPOINT)
    httpretty.register_uri(
        httpretty.GET,
        RAVEM_TEST_API_ENDPOINT + "rooms/details",
        status=200,
        content_type="application/json",
        body=json.dumps({"error": error}),
    )

    room_verbose_name = "room_verbose_name"
    with pytest.raises(RavemException) as excinfo:
        get_room_status(room_name, room_verbose_name)

    assert str(
        excinfo.value
    ) == f"Failed to get status of room {room_verbose_name} with error: {error}"
    log = extract_logs(caplog, one=True, name="indico.plugin.ravem")
    assert log.message == f"Failed to get status of room {room_verbose_name} with error: {error}"

    assert len(httpretty.httpretty.latest_requests) == 1
    request = httpretty.last_request()

    assert request.path.startswith(RAVEM_TEST_PATH + "rooms/details")
    assert request.querystring == {
        "where": ["room_name"],
        "value": [room_name]
    }
Example #4
0
def test_create_next_with_exception(caplog, mocker, creation_params, exception):
    mocker.patch.object(TransactionStatusTransition, 'next')
    TransactionStatusTransition.next.side_effect = exception('TEST_EXCEPTION')
    transaction = PaymentTransaction.create_next(**creation_params)
    log = extract_logs(caplog, one=True, name='indico.payment')
    assert transaction is None
    assert 'TEST_EXCEPTION' in log.message
    if log.exc_info:
        assert log.exc_info[0] == exception
Example #5
0
def test_evaluate_notice(caplog, criteria, expected):
    versions = {
        'python_version': Version('3.9.6'),
        'postgres_version': Version('12.7'),
        'indico_version': Version('3.0.1')
    }
    notice = SystemNotice(id='test', message='testing', when=criteria, severity=NoticeSeverity.highlight)
    assert notice.evaluate(versions) == expected
    assert not extract_logs(caplog, required=False, name='indico.notices')
Example #6
0
def test_create_next_with_exception(caplog, mocker, creation_params, exception):
    mocker.patch.object(TransactionStatusTransition, 'next')
    TransactionStatusTransition.next.side_effect = exception('TEST_EXCEPTION')
    transaction = PaymentTransaction.create_next(**creation_params)
    log = extract_logs(caplog, one=True, name='indico.payment')
    assert transaction is None
    assert 'TEST_EXCEPTION' in log.message
    if log.exc_info:
        assert log.exc_info[0] == exception
Example #7
0
def test_evaluate_notice_invalid(caplog, criterion, message):
    versions = {
        'python_version': Version('3.14.15'),
        'postgres_version': Version('12.7'),
        'indico_version': Version('3.0.1')
    }
    criteria = SystemNoticeCriteria(python_version=criterion)
    notice = SystemNotice(id='test', message='testing', when=criteria, severity=NoticeSeverity.highlight)
    assert not notice.evaluate(versions)
    assert extract_logs(caplog, one=True, name='indico.notices').message == message
Example #8
0
def test_evaluate_notice_all_ops(caplog, op, space, expected):
    versions = {
        'python_version': Version('3.14.15'),
        'postgres_version': Version('12.7'),
        'indico_version': Version('3.0.1')
    }
    criteria = SystemNoticeCriteria(python_version=f'{op}{space}3.14.15')
    notice = SystemNotice(id='test', message='testing', when=criteria, severity=NoticeSeverity.highlight)
    assert notice.evaluate(versions) == expected
    assert not extract_logs(caplog, required=False, name='indico.notices')
def test_connect_room_force_fail(caplog, httpretty, room_name, service_type,
                                 connected, data):
    RavemPlugin.settings.set("api_endpoint", RAVEM_TEST_API_ENDPOINT)
    RavemPlugin.settings.set("polling_limit", 3)
    RavemPlugin.settings.set("polling_interval", 100)
    different_vc_room = "different_vc_room"
    httpretty.register_uri(
        httpretty.GET,
        RAVEM_TEST_API_ENDPOINT + "rooms/details",
        status=200,
        content_type="application/json",
        body=json.dumps({
            "roomName":
            room_name,
            "deviceType":
            service_type,
            "services": [{
                "status": connected,
                "eventName": different_vc_room,
                "name": "videoconference",
            }],
        }),
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + service_type + "/disconnect",
        status=200,
        content_type="application/json",
        body=json.dumps({"result": "OK"}),
    )

    vc_room = MagicMock()
    vc_room.type = service_type
    vc_room.data = data

    with pytest.raises(RavemException) as excinfo:
        connect_room(room_name, vc_room, force=True)

    assert (
        str(excinfo.value) ==
        f"Failed to disconnect the room {room_name} from the videoconference room {different_vc_room} "
        "with an unknown error")
    log = extract_logs(caplog, one=True, name="indico.plugin.ravem")
    assert (
        log.message ==
        f"Failed to disconnect the room {room_name} from the videoconference room {different_vc_room} "
        "with an unknown error")

    assert len(httpretty.httpretty.latest_requests
               ) == 2 + RavemPlugin.settings.get("polling_limit")
    request = httpretty.httpretty.latest_requests[1]
    assert request.path.startswith(RAVEM_TEST_PATH + service_type +
                                   "/disconnect")
    assert request.parsed_body == {"roomName": room_name}
Example #10
0
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
Example #11
0
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 test_connect_room_force_error(caplog, httpretty, room_name, service_type,
                                  connected, data):
    RavemPlugin.settings.set("api_endpoint", RAVEM_TEST_API_ENDPOINT)
    error_message = "Some internal error"
    different_vc_room = "different_vc_room"
    httpretty.register_uri(
        httpretty.GET,
        RAVEM_TEST_API_ENDPOINT + "rooms/details",
        status=200,
        content_type="application/json",
        body=json.dumps({
            "roomName":
            room_name,
            "deviceType":
            service_type,
            "services": [{
                "status": connected,
                "eventName": different_vc_room,
                "name": "videoconference",
            }],
        }),
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + service_type + "/disconnect",
        status=200,
        content_type="application/json",
        body=json.dumps({"error": error_message}),
    )

    vc_room = MagicMock()
    vc_room.type = service_type
    vc_room.data = data

    with pytest.raises(RavemException) as excinfo:
        connect_room(room_name, vc_room, force=True)

    assert (
        str(excinfo.value) ==
        f"Failed to disconnect the room {room_name} from the videoconference room "
        f"{different_vc_room} with error: {error_message}")
    log = extract_logs(caplog, one=True, name="indico.plugin.ravem")
    assert log.message == str(excinfo.value)
Example #13
0
def test_get_room_status_service_not_found(caplog, httpretty, room_name):
    RavemPlugin.settings.set('api_endpoint', RAVEM_TEST_API_ENDPOINT)
    httpretty.register_uri(
        httpretty.GET,
        RAVEM_TEST_API_ENDPOINT + 'getstatus',
        status=200,
        content_type='application/json',
        body=json.dumps({'result': 'Service not found'})
    )

    with pytest.raises(RavemException) as excinfo:
        get_room_status(room_name)

    assert excinfo.value.message == "Vidyo is not supported in the room {0}".format(room_name)
    log = extract_logs(caplog, one=True, name='indico.plugin.ravem')
    assert log.message == "Vidyo is not supported in the room {0}".format(room_name)

    assert len(httpretty.httpretty.latest_requests) == 1
    request = httpretty.last_request()

    assert request.path.startswith(RAVEM_TEST_PATH + 'getstatus')
    assert request.querystring == {'service_name': ['videoconference'], 'where': ['room_name'], 'value': [room_name]}
Example #14
0
def test_create_next_double_payment(caplog, creation_params):
    creation_params['registration'].transaction = MagicMock(status=TransactionStatus.successful)
    _, double_payment = PaymentTransaction.create_next(**creation_params)
    log = extract_logs(caplog, one=True, name='indico.payment').message
    assert 'already paid' in log
    assert double_payment
Example #15
0
def test_connect_room_force_error(caplog, httpretty, room_name, status, data):
    RavemPlugin.settings.set('api_endpoint', RAVEM_TEST_API_ENDPOINT)
    error_message = 'Some internal error'
    different_vc_room = 'different_vc_room'
    httpretty.register_uri(
        httpretty.GET,
        RAVEM_TEST_API_ENDPOINT + 'getstatus',
        status=200,
        content_type='application/json',
        body=json.dumps({
            'result': {
                'name': room_name,
                'vc_endpoint_legacy_hostname': data.get('legacy_hostname'),
                'vc_endpoint_vidyo_username': data.get('vidyo_username'),
                'vc_endpoint_legacy_ip': data.get('legacy_ip'),
                'vc_endpoint_vidyo_extension': data.get('vidyo_extension'),
                'services': [{
                    'status': status,
                    'event_name': different_vc_room,
                    'name': 'videoconference',
                    'event_type': data.get('event_type')
                }],
                'common_name': data.get('common_name')
            }})
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + 'videoconference/disconnect',
        status=200,
        content_type='application/json',
        body=json.dumps({'error': error_message})
    )

    vc_room = MagicMock()
    vc_room.name = data.get('event_name')

    with pytest.raises(RavemException) as excinfo:
        connect_room(room_name, vc_room, force=True)

    assert excinfo.value.message == "Failed to disconnect the room {0} from the Vidyo room {1} with error: {2}" \
                                    .format(room_name, different_vc_room, error_message)
    log = extract_logs(caplog, one=True, name='indico.plugin.ravem')
    assert log.message == "Failed to disconnect the room {0} from the Vidyo room {1} with error: {2}" \
                          .format(room_name, different_vc_room, error_message)

    assert len(httpretty.httpretty.latest_requests) == 2
    status_request = httpretty.httpretty.latest_requests[0]
    request = httpretty.last_request()

    assert request.path.startswith(RAVEM_TEST_PATH + 'videoconference/disconnect')
    query = {
        'type': [data.get('event_type')],
        'where': ['room_name'],
        'value': [room_name],
    }
    if data.get('event_type') == 'vidyo':
        query['vidyo_room_name'] = [different_vc_room]
    assert request.querystring == query

    assert status_request.path.startswith(RAVEM_TEST_PATH + 'getstatus')
    assert status_request.querystring == {
        'service_name': ['videoconference'],
        'where': ['room_name'],
        'value': [room_name]
    }
Example #16
0
def test_disconnect_room_force(caplog, httpretty, room_name, status, data):
    caplog.set_level(logging.INFO)
    RavemPlugin.settings.set('api_endpoint', RAVEM_TEST_API_ENDPOINT)
    different_vc_room = 'different_vc_room'
    httpretty.register_uri(
        httpretty.GET,
        RAVEM_TEST_API_ENDPOINT + 'getstatus',
        status=200,
        content_type='application/json',
        body=json.dumps({
            'result': {
                'name': room_name,
                'vc_endpoint_legacy_hostname': data.get('legacy_hostname'),
                'vc_endpoint_vidyo_username': data.get('vidyo_username'),
                'vc_endpoint_legacy_ip': data.get('legacy_ip'),
                'vc_endpoint_vidyo_extension': data.get('vidyo_extension'),
                'services': [{
                    'status': status,
                    'event_name': different_vc_room,
                    'name': 'videoconference',
                    'event_type': data.get('event_type')
                }],
                'common_name': data.get('common_name')
            }})
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + 'videoconference/disconnect',
        status=200,
        content_type='application/json',
        body=json.dumps({'result': 'OK'})
    )

    vc_room = MagicMock()
    vc_room.name = data.get('event_name')

    disconnect_room(room_name, vc_room, force=True)

    log = extract_logs(caplog, one=True, name='indico.plugin.ravem')
    assert log.message == "Force disconnect of room {0} from vc_room {1} (expected to disconnect from vc_room {2})" \
                          .format(room_name, different_vc_room, data.get('event_name'))

    assert len(httpretty.httpretty.latest_requests) == 2
    status_request = httpretty.httpretty.latest_requests[0]
    request = httpretty.last_request()

    assert request.path.startswith(RAVEM_TEST_PATH + 'videoconference/disconnect')
    query = {
        'type': [data.get('event_type')],
        'where': ['room_name'],
        'value': [room_name],
    }
    if data.get('event_type') == 'vidyo':
        query['vidyo_room_name'] = [different_vc_room]
    assert request.querystring == query

    assert status_request.path.startswith(RAVEM_TEST_PATH + 'getstatus')
    assert status_request.querystring == {
        'service_name': ['videoconference'],
        'where': ['room_name'],
        'value': [room_name]
    }