예제 #1
0
async def test_patch_message_endpoint(request_factory, patched_context,
                                      request_header_authorization, mocker):
    new_message = DBMessage(
        id=1,
        sender_id=1,
        recipient_id=2,
        message="message",
    )

    patched_query = mocker.patch('db.queries.message.patch_message')
    patched_query.return_value = new_message
    patched_query = mocker.patch('db.queries.file.get_file_ids_by_msd_id')
    patched_query.return_value = []

    json_data = {'message': 'Hello, lex123.'}

    request = request_factory(method='patch',
                              content_type='application/json',
                              headers=request_header_authorization,
                              json=json_data)

    endpoint = MessageEndpoint(None,
                               patched_context,
                               '/msg/1', ['PATCH'],
                               auth_required=True)
    response = await endpoint(request, message_id=1)

    assert response.status == 200
async def test_post_messages_endpoint(request_factory, patched_context,
                                      request_header_authorization, mocker):
    new_message = DBMessage(
        id=1,
        sender_id=1,
        recipient_id=2,
        message="message",
    )

    patched_query = mocker.patch('db.queries.message.create_message')
    patched_query.return_value = new_message

    json_data = {'recipient': 'lex123', 'message': 'Hello, lex123.'}

    request = request_factory(method='post',
                              content_type='application/json',
                              headers=request_header_authorization,
                              json=json_data)

    endpoint = MessagesEndpoint(None,
                                patched_context,
                                '/msg', ['POST'],
                                auth_required=True)
    response = await endpoint(request)

    assert response.status == 201
예제 #3
0
def create_message(session: DBSession, message: RequestCreateMessageDto,
                   converse_id: dict) -> DBMessage:
    new_message = DBMessage(message=message.message,
                            sender_id=converse_id.get('sender_id'),
                            recipient_id=converse_id.get('recipient_id'))

    session.add_model(new_message)

    return new_message
예제 #4
0
def create_message(session: DBSession, *, message: str, sender_id: str,
                   recipient_id: str) -> DBMessage:
    new_message = DBMessage(
        message=message,
        sender_id=sender_id,
        recipient_id=recipient_id,
    )
    session.add_model(new_message)

    return new_message
예제 #5
0
def create_message(session: DBSession, message: RequestCreateMessageDto, sender_id: int) -> DBMessage:

    new_message = DBMessage(
        message=message.message,
        recipient_id=message.recipient_id,
        sender_id=sender_id
    )
    session.add_model(new_message)

    return new_message
예제 #6
0
def create_message(session: DBSession, message: RequestCreateMessageDto,
                   token: dict, recipient_id: int) -> DBMessage:
    # создание модели DBMessage
    new_message = DBMessage(
        message=message.message,
        sender_id=token['id'],
        recipient_id=recipient_id,
    )

    # добавляем модель в БД
    session.add_model(new_message)

    return new_message
예제 #7
0
def create_message(session: DBSession, message: RequestCreateMessageDto,
                   sender: int) -> DBMessage:
    if session.get_user_by_id(uid=message.recipient_id) is None:
        raise DBUserNotExistsException

    new_message = DBMessage(
        recipient_id=message.recipient_id,
        message=message.message,
        sender_id=sender,
    )
    session.add_model(new_message)

    return new_message
예제 #8
0
def create_message(session: DBSession, message: RequestCreateMessageDto,
                   sender_id: int) -> DBMessage:
    # Поиск по login в users
    recipient = session.get_user_by_login(message.recipient)
    if recipient is None:
        raise DBUserNotExistsException

    new_message = DBMessage(sender_id=sender_id,
                            recipient_id=recipient.id,
                            message=message.message)

    session.add_model(new_message)

    return new_message
예제 #9
0
def create_message(session: DBSession, message: RequestCreateMessageDto, uid: int) -> DBMessage:

    recipient_id = get_uid_by_login(session=session, login=message.recipient)

    new_message = DBMessage(

        sender_id=uid,
        recipient_id=recipient_id,
        message=message.message

    )

    session.add_model(new_message)

    return new_message
예제 #10
0
def create_message(session: DBSession, message: RequestCreateMessageDto, sender_id: int) -> DBMessage:
    recipient = session.get_employee_by_login(login=message.recipient)

    if recipient is None:
        raise DBEmployeeNotExistsException

    new_message = DBMessage(
        sender_id=sender_id,
        recipient_id=recipient.id,
        message=message.message
    )

    session.add_model(new_message)

    return new_message
예제 #11
0
def create_message(session: DBSession, message: RequestCreateMessageDto, uid: int) -> DBMessage:
    recipient = session.get_user_by_login(login=message.recipient, add_filter='users.is_delete = False')

    if recipient is None:
        raise DBUserNotExistsException

    new_message = DBMessage(
        sender_id=uid,
        message=message.message,
        recipient_id=recipient.id,
    )

    session.add_model(new_message)

    return new_message
예제 #12
0
def create_message(session: DBSession, message: RequestCreateMessageDto,
                   user_id: int) -> DBMessage:
    try:
        recipient_id = session.get_user_id_by_login(message.recipient)
    except Exception:
        raise DBUserNotExistsException

    new_message = DBMessage(
        message=message.message,
        sender_id=user_id,
        recipient_id=recipient_id,
    )

    session.add_model(new_message)

    return new_message
예제 #13
0
def create_message(session: DBSession, message: RequestCreateMessageDto, sid: int) -> DBMessage:

    rid = get_employee_id_by_login(session, message.recipient)

    try:
        session.get_employee_by_id(rid)
    except DBEmployeeNotExistsException:
        raise SanicEmployeeNotFound('Employee not found')

    new_message = DBMessage(
        sender_id=sid,
        recipient_id=rid,
        message=message.message,
    )

    session.add_model(new_message)

    return new_message
예제 #14
0
async def test_get_message_endpoint(request_factory, patched_context,
                                    request_header_authorization, mocker):
    new_message = DBMessage(
        id=1,
        sender_id=2,
        recipient_id=1,
        message="message",
    )
    patched_query = mocker.patch('db.queries.message.get_message')
    patched_query.return_value = new_message
    patched_query = mocker.patch('db.queries.file.get_file_ids_by_msd_id')
    patched_query.return_value = []

    request = request_factory(method='get',
                              headers=request_header_authorization)
    endpoint = MessageEndpoint(None,
                               patched_context,
                               '/msg/1', ['GET'],
                               auth_required=True)
    response = await endpoint(request, message_id=1)

    assert response.status == 200
예제 #15
0
def delete_message(db_message: DBMessage) -> DBMessage:
    db_message.is_delete = True
    return db_message
예제 #16
0
def patch_message(db_message: DBMessage, edit_message: str) -> DBMessage:
    db_message.message = edit_message
    return db_message
예제 #17
0
def delete_message(db_message: DBMessage) -> DBMessage:
    try:
        db_message.is_delete = True
    except AttributeError:
        raise DBMessageNotExistsException
    return db_message