def test_connect_room(httpretty, room_name, service_type, connected, data):
    RavemPlugin.settings.set("api_endpoint", RAVEM_TEST_API_ENDPOINT)
    service_api = get_api(service_type)
    vc_room_id = service_api.get_room_id(data)
    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": None,
                "name": "videoconference",
            }],
        }),
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + service_type + "/connect",
        status=200,
        content_type="application/json",
        body=json.dumps({"result": "OK"}),
    )

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

    connect_room(room_name, vc_room)

    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 + service_type + "/connect")
    assert request.parsed_body == {
        "meetingId": vc_room_id,
        "roomName": room_name
    }

    assert status_request.path.startswith(RAVEM_TEST_PATH + "rooms/details")
    assert status_request.querystring == {
        "where": ["room_name"],
        "value": [room_name]
    }
def test_connect_room_error(caplog, httpretty, room_name, service_type,
                            connected, data):
    RavemPlugin.settings.set("api_endpoint", RAVEM_TEST_API_ENDPOINT)
    error_message = "Some internal error"
    service_api = get_api(service_type)
    vc_room_id = service_api.get_room_id(data)
    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": None,
                "name": "videoconference",
            }],
        }),
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + service_type + "/connect",
        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)

    assert str(excinfo.value) == \
        f"Failed to connect the room {room_name} to the videoconference room {vc_room_id} with error: {error_message}"
    log = extract_logs(caplog, one=True, name="indico.plugin.ravem")
    assert log.message == str(excinfo.value)
def test_connect_room_already_connected(httpretty, room_name, service_type,
                                        connected, data):
    RavemPlugin.settings.set("api_endpoint", RAVEM_TEST_API_ENDPOINT)
    service_api = get_api(service_type)
    vc_room_id = service_api.get_room_id(data)
    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": vc_room_id,
                "name": "videoconference",
            }],
        }),
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + service_type + "/connect",
        status=200,
        content_type="application/json",
        body=json.dumps({"error": "Call already disconnected"}),
    )

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

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

    assert str(
        excinfo.value
    ) == f"The room {room_name} is already connected to the videoconference room {vc_room_id}"
    assert excinfo.value.reason == "already-connected"
def test_get_room_status(httpretty, room_name, service_type, connected, data):
    RavemPlugin.settings.set("api_endpoint", RAVEM_TEST_API_ENDPOINT)
    service_api = get_api(service_type)
    vc_room_id = service_api.get_room_id(data)
    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": data["id"],
                "name": "videoconference",
            }],
        }),
    )

    status = get_room_status(room_name)
    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]
    }

    assert status["room_name"] == room_name
    assert status["connected"] == connected
    assert status["vc_room_id"] == vc_room_id
    assert status["service_type"] == service_type
def test_connect_room_force(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)
    service_api = get_api(service_type)
    vc_room_id = service_api.get_room_id(data)
    different_vc_room = "different_vc_room"
    httpretty.register_uri(
        httpretty.GET,
        RAVEM_TEST_API_ENDPOINT + "rooms/details",
        status=200,
        content_type="application/json",
        responses=[
            httpretty.Response(status=200,
                               content_type='application/json',
                               body=json.dumps({
                                   "roomName":
                                   room_name,
                                   "deviceType":
                                   service_type,
                                   "services": [{
                                       "status": connected,
                                       "eventName": different_vc_room,
                                       "name": "videoconference",
                                   }],
                               }))
        ] * RavemPlugin.settings.get("polling_limit") + [
            httpretty.Response(status=200,
                               content_type='application/json',
                               body=json.dumps({
                                   "roomName":
                                   room_name,
                                   "deviceType":
                                   service_type,
                                   "services": [{
                                       "status": False,
                                       "eventName": None,
                                       "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"}),
    )
    httpretty.register_uri(
        httpretty.POST,
        RAVEM_TEST_API_ENDPOINT + service_type + "/connect",
        status=200,
        content_type="application/json",
        body=json.dumps({"result": "OK"}),
    )

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

    connect_room(room_name, vc_room, force=True)

    # status, disconnect, polling attempts and connect
    number_of_requests = 2 + RavemPlugin.settings.get("polling_limit") + 1
    assert len(httpretty.httpretty.latest_requests) == number_of_requests

    disconnect_request = httpretty.httpretty.latest_requests[1]
    assert disconnect_request.path.startswith(RAVEM_TEST_PATH + service_type +
                                              "/disconnect")
    assert disconnect_request.parsed_body == {"roomName": room_name}

    request = httpretty.last_request()
    assert request.path.startswith(RAVEM_TEST_PATH + service_type + "/connect")
    assert request.parsed_body == {
        "roomName": room_name,
        "meetingId": vc_room_id
    }