Пример #1
0
def test_deserialize_invalid_type_ws_msg():
    data = json.dumps({
        '_type': 9999,
        'location': {
            'latitude': 0.0,
            'longitude': 1.0
        }
    })
    with pytest.raises(ValueError):
        deserialize_ws_message(data)
Пример #2
0
    async def _on_message(self, message: str) -> None:
        logger.debug("Received message from user_id = '{}' : '{}'".format(
            self.user_id, message))

        try:
            msg = models.deserialize_ws_message(message)
        except (ValueError, TypeError) as e:
            self.write_as_json(
                ErrorMessage(WebSocketErrorType.INVALID_MESSAGE.value))
            logger.error(
                "Invalid message received by user_id : '{}', exception : '{}'".
                format(self.user_id, e))
        else:
            if isinstance(msg, models.LocationUpdateMessage):
                self.handle_location_update(msg)
            elif isinstance(msg, models.ParkingRequestMessage):
                await self.handle_parking_request(msg)
            elif isinstance(msg, models.ParkingAcceptanceMessage):
                await self.handle_parking_acceptance(msg)
            elif isinstance(msg, models.ParkingRejectionMessage):
                self.handle_parking_rejection(msg)
            elif isinstance(msg, models.ParkingCancellationMessage):
                self.handle_parking_cancellation(msg)
            else:
                logger.warning(
                    "Unimplemented message received from user_id = '{}'".
                    format(self.user_id))
                self.write_as_json(
                    ErrorMessage(WebSocketErrorType.NOT_IMPLEMENTED.value))
Пример #3
0
async def test_parking_request_message(ws_url, user_id, mocker, engine):
    user_location = Location(0.0, 1.0)
    lot_location = Location(0.001, 1.0)
    lot_allocation = ParkingLotAllocation(100,
                                          "test_lot",
                                          0,
                                          lot_location,
                                          1,
                                          distance=50.0,
                                          availability=20)
    parking_request = ParkingRequestMessage(user_location)

    mocker.patch.object(engine, 'handle_allocation_request')
    engine.handle_allocation_request.return_value = return_async_value(
        lot_allocation)

    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)
    await conn.write_message(serialize_model(parking_request))
    msg = deserialize_ws_message(await conn.read_message())

    engine.handle_allocation_request.assert_called_once_with(
        user_id, parking_request)
    assert isinstance(msg, ParkingAllocationMessage)
    assert msg.lot == lot_allocation
Пример #4
0
def test_deserialize_valid_ws_msg():
    data = json.dumps({
        '_type': 1,
        'location': {
            'latitude': 0.0,
            'longitude': 1.0
        }
    })
    msg = deserialize_ws_message(data)
    assert isinstance(msg, LocationUpdateMessage)
Пример #5
0
async def test_another_connection_open(ws_url):
    await tornado.websocket.websocket_connect(ws_url)
    conn2: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url)
    msg = deserialize_ws_message(await conn2.read_message())
    assert WebSocketErrorType(
        msg.error) == WebSocketErrorType.ANOTHER_CONNECTION_OPEN

    # confirm that second connection was closed
    assert await conn2.read_message() is None
Пример #6
0
async def test_internal_error(ws_url, user_id, mocker, usessions):
    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)
    session: UserWSHandler = usessions.get_user(user_id).session

    mocker.patch.object(session, '_on_message', side_effect=NameError())
    await conn.write_message("test")

    msg = deserialize_ws_message(await conn.read_message())
    assert WebSocketErrorType(msg.error) == WebSocketErrorType.INTERNAL
Пример #7
0
    def _on_message(self, message: str):
        message = ws_models.deserialize_ws_message(message)
        message_type = message.__class__

        if message_type in self._receive_callbacks:
            self._receive_callbacks[message_type](message)
        elif message_type in self._waiting:
            future_set_result_unless_cancelled(self._waiting[message_type],
                                               message)
            del self._waiting[message_type]
        else:
            self._message_queue[message_type].append(message)
Пример #8
0
async def test_parking_rejection_message(ws_url, user_id, mocker, usessions):
    lot_id = 7
    mocker.spy(usessions, "add_user_rejection")

    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)

    await conn.write_message(serialize_model(ParkingRejectionMessage(lot_id)))
    msg = deserialize_ws_message(await conn.read_message())

    usessions.add_user_rejection.assert_called_once_with(user_id, lot_id)
    assert isinstance(msg, ConfirmationMessage)
Пример #9
0
async def test_db_error(ws_url, user_id, mocker, usessions):
    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)
    session: UserWSHandler = usessions.get_user(user_id).session

    from asyncpg import PostgresError
    mocker.patch.object(session,
                        '_on_message',
                        side_effect=PostgresError.new({}))
    await conn.write_message("test")

    msg = deserialize_ws_message(await conn.read_message())
    assert WebSocketErrorType(msg.error) == WebSocketErrorType.DATABASE
Пример #10
0
async def test_parking_acceptance_message_happy_case(ws_url, user_id, mocker,
                                                     dbaccess, engine):
    lot_id = 7

    mocker.patch.object(dbaccess, "allocate_parking_lot")
    dbaccess.allocate_parking_lot.return_value = return_async_value(True)
    mocker.spy(engine, "commit_allocation")

    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)

    await conn.write_message(serialize_model(ParkingAcceptanceMessage(lot_id)))
    msg = deserialize_ws_message(await conn.read_message())

    engine.commit_allocation.assert_called_once_with(user_id, lot_id)
    assert isinstance(msg, ConfirmationMessage)
Пример #11
0
async def test_parking_acceptance_message_failed_commit(
        ws_url, user_id, mocker, dbaccess, engine):
    lot_id = 7

    mocker.patch.object(dbaccess, "allocate_parking_lot")
    dbaccess.allocate_parking_lot.return_value = return_async_value(False)
    mocker.spy(engine, "commit_allocation")

    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)

    await conn.write_message(serialize_model(ParkingAcceptanceMessage(lot_id)))
    msg = deserialize_ws_message(await conn.read_message())

    engine.commit_allocation.assert_called_once_with(user_id, lot_id)
    assert WebSocketErrorType(
        msg.error) == WebSocketErrorType.ALLOCATION_COMMIT_FAIL
Пример #12
0
async def test_parking_request_message_no_parking_lot(ws_url, user_id, mocker,
                                                      engine):
    user_location = Location(0.0, 1.0)
    parking_request = ParkingRequestMessage(user_location)

    mocker.patch.object(engine, 'handle_allocation_request')
    engine.handle_allocation_request.return_value = return_async_value(None)

    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)
    await conn.write_message(serialize_model(parking_request))
    msg = deserialize_ws_message(await conn.read_message())

    engine.handle_allocation_request.assert_called_once_with(
        user_id, parking_request)
    assert WebSocketErrorType(
        msg.error) == WebSocketErrorType.NO_AVAILABLE_PARKING_LOT
Пример #13
0
    def on_message(self, message: str) -> None:
        logger.debug("Received message from user_id = '{}' : '{}'".format(self.user_id, message))
        msg = models.deserialize_ws_message(message)

        if isinstance(msg, models.LocationUpdateMessage):
            logger.debug("Received location update from user_id = '{}'".format(self.user_id))
            self.usessions.update_user_location(self.user_id, msg.location)
        elif isinstance(msg, models.ParkingRequestMessage):
            logger.debug("Received parking request from user_id = '{}'".format(self.user_id))
        elif isinstance(msg, models.ParkingAcceptanceMessage):
            logger.debug("Received parking acceptance from user_id = '{}'".format(self.user_id))
        elif isinstance(msg, models.ParkingRejectionMessage):
            logger.debug("Received parking rejection from user_id = '{}'".format(self.user_id))
            self.usessions.add_user_rejection(self.user_id, msg.id)
        elif isinstance(msg, models.ParkingCancellationMessage):
            logger.info("Parking cancelled for user_id = '{}'".format(self.user_id))
            self.close()
Пример #14
0
async def test_invalid_message(ws_url, user_id):
    conn: WebSocketClientConnection = await tornado.websocket.websocket_connect(
        ws_url + user_id)
    await conn.write_message("test")
    msg = deserialize_ws_message(await conn.read_message())
    assert WebSocketErrorType(msg.error) == WebSocketErrorType.INVALID_MESSAGE