コード例 #1
0
    def add_message(self, message):
        """
        Adds a received message from a messaging app to the live page corresponding
        to the channel where the message was posted if such a page exists.

        Args:
            message (dict): A message received from a messaging app.
        """

        channel_id = self.get_channel_id_from_message(message=message)
        try:
            live_page = self.get_live_page_from_channel_id(channel_id=channel_id)
        except self.model.DoesNotExist:
            return

        message_id = self.get_message_id_from_message(message=message)
        live_post = construct_live_post_block(message_id=message_id, created=now())

        message_text = self.get_message_text(message=message)
        self.process_text(live_post=live_post, message_text=message_text)

        files = self.get_message_files(message=message)
        self.process_files(live_post=live_post, files=files)

        live_page.add_live_post(live_post=live_post)
コード例 #2
0
def test_process_files(slack_receiver, slack_image_message, mocker):
    message = slack_image_message["event"]
    files = slack_receiver.get_message_files(message=message)
    image = files[0]

    image_content = get_test_image_file(
        filename=slack_receiver.get_image_name(image=image),
        size=slack_receiver.get_image_dimensions(image=image),
    )
    mocker.patch.object(slack_receiver,
                        "get_image_content",
                        return_value=image_content)
    live_post = blocks.construct_live_post_block(
        message_id="1234",
        created=datetime(1970, 1, 1, 12, 00),
    )
    slack_receiver.process_files(live_post=live_post, files=files)

    post_content = live_post["content"]
    assert post_content[0].block_type == IMAGE

    post_value = post_content[0].value
    assert isinstance(post_value, Image)
    assert post_value.title == image["title"]
    assert (post_value.width,
            post_value.height) == slack_receiver.get_image_dimensions(
                image=image)
コード例 #3
0
ファイル: test_models.py プロジェクト: wagtail/wagtail-live
def test_edit_live_posts_updates_last_updated_at(blog_page_factory):
    """last_updated_at is updated when a live post is added, edited or deleted."""

    page = blog_page_factory(channel_id="some-id")
    last_updated_at = page.last_updated_at
    diff = now() - last_updated_at
    assert diff.total_seconds() == pytest.approx(0.0, abs=1)

    # ADD
    _now = now()
    live_post = construct_live_post_block(message_id="some-id", created=_now)
    page.add_live_post(live_post=live_post)
    assert page.last_updated_at > last_updated_at
    assert page.last_updated_at == _now
    last_updated_at = page.last_updated_at

    # EDIT
    live_post = page.get_live_post_by_index(live_post_index=0)
    page.update_live_post(live_post=live_post)
    assert page.last_updated_at > last_updated_at
    assert page.last_updated_at == live_post.value["modified"]
    last_updated_at = page.last_updated_at

    # DELETE
    page.delete_live_post(message_id="some-id")
    assert page.last_updated_at > last_updated_at
    last_updated_at = page.last_updated_at
    diff = now() - last_updated_at
    assert diff.total_seconds() == pytest.approx(0.0, abs=1)
コード例 #4
0
def test_process_files_bad_dimensions(slack_receiver, slack_image_message,
                                      caplog):
    live_post = blocks.construct_live_post_block(
        message_id="1234",
        created=datetime(1970, 1, 1, 12, 00),
    )
    files = [{"title": "test.png"}]

    assert slack_receiver.process_files(live_post=live_post,
                                        files=files) is None
    assert caplog.messages[
        0] == "Unable to retrieve the dimensions of test.png"
コード例 #5
0
def test_clear_live_post_content_structvalue():
    text_block = construct_text_block(text="Some text")
    live_post = construct_live_post_block(
        message_id="1234",
        created=datetime(1970, 1, 1, 12, 00),
    )

    add_block_to_live_post(TEXT, text_block, live_post)
    clear_live_post_content(live_post)

    assert isinstance(live_post, StructValue)
    assert live_post["content"] == StreamValue(ContentBlock(), [])
コード例 #6
0
def test_add_block_to_live_post_structvalue():
    text_block = construct_text_block(text="Some text")
    live_post = construct_live_post_block(
        message_id="1234",
        created=datetime(1970, 1, 1, 12, 00),
    )

    add_block_to_live_post(TEXT, text_block, live_post)

    assert isinstance(live_post, StructValue)
    setattr(RichText, "__eq__",
            lambda self, other: self.source == other.source)
    assert live_post["content"] == StreamValue(
        ContentBlock(), [("text", RichText("Some text"))])
コード例 #7
0
def test_construct_live_post_block():
    live_post_block = construct_live_post_block(
        message_id="1234",
        created=datetime(1970, 1, 1, 12, 00),
    )
    assert live_post_block == StructValue(
        LivePostBlock(),
        {
            "message_id": "1234",
            "created": datetime(1970, 1, 1, 12, 00),
            "modified": None,
            "show": True,
            "content": StreamValue(ContentBlock(), []),
        },
    )
コード例 #8
0
def test_embed_message(telegram_receiver, telegram_embed_message,
                       blog_page_factory):
    live_post_block = construct_live_post_block(message_id="1234",
                                                created=datetime.now())
    text = telegram_receiver.get_message_text(
        message=telegram_embed_message["message"])
    telegram_receiver.process_text(live_post=live_post_block,
                                   message_text=text)

    content = live_post_block["content"]
    assert content[0].value.source == "This is another test post."
    assert content[1].value.source == "This post contains an embed."
    assert content[2].block_type == EMBED
    assert content[
        2].value.url == "https://www.youtube.com/watch?v=Cq3LOsf2kSY"
コード例 #9
0
def test_process_files_bad_mimetype(slack_receiver, slack_image_message,
                                    caplog):
    live_post = blocks.construct_live_post_block(
        message_id="1234",
        created=datetime(1970, 1, 1, 12, 00),
    )
    message = slack_image_message["event"]
    files = slack_receiver.get_message_files(message=message)
    image = files[0]
    image["mimetype"] = "image/bad_mimetype"
    assert slack_receiver.process_files(live_post=live_post,
                                        files=files) is None
    assert (
        caplog.messages[0] ==
        "Couldn't upload test_image.png. Images of type bad_mimetype aren't supported yet."
    )
コード例 #10
0
def test_embed_message_2(telegram_receiver, telegram_embed_message_2,
                         blog_page_factory):
    live_post_block = construct_live_post_block(message_id="1234",
                                                created=datetime.now())
    text = telegram_receiver.get_message_text(
        message=telegram_embed_message_2["message"])
    telegram_receiver.process_text(live_post=live_post_block,
                                   message_text=text)

    content = live_post_block["content"]
    assert content[0].value.source == "This is another test post."
    assert content[1].value.source == (
        '<a href="https://www.youtube.com/watch?v=Cq3LOsf2kSY">'
        "https://www.youtube.com/watch?v=Cq3LOsf2kSY</a>. "
        "Some content here.")
    assert content[2].value.source == (
        "Some content here. "
        '<a href="https://www.youtube.com/watch?v=Cq3LOsf2kSY">'
        "https://www.youtube.com/watch?v=Cq3LOsf2kSY</a>")
コード例 #11
0
ファイル: test_models.py プロジェクト: wagtail/wagtail-live
def test_edit_live_posts_sends_signal(blog_page_factory):
    count = 0
    _channel_id = _renders = _removals = None

    def callback(sender, channel_id, renders, removals, **kwargs):
        nonlocal count, _channel_id, _renders, _removals
        _channel_id, _renders, _removals = channel_id, renders, removals
        count += 1

    live_page_update.connect(callback)
    page = blog_page_factory(channel_id="some-id")

    try:
        # ADD
        live_post = construct_live_post_block(message_id="some-id",
                                              created=now())
        page.add_live_post(live_post=live_post)
        live_post = page.get_live_post_by_index(live_post_index=0)
        live_post_id = live_post.id

        assert count == 1
        assert _channel_id == "some-id"
        assert _renders == [live_post]
        assert _removals == []

        # EDIT
        page.update_live_post(live_post=live_post)
        assert count == 2
        assert _channel_id == "some-id"
        assert _renders == [live_post]
        assert _removals == []

        # DELETE
        page.delete_live_post(message_id="some-id")
        assert count == 3
        assert _channel_id == "some-id"
        assert _renders == {}
        assert _removals == [live_post_id]

    finally:
        live_page_update.disconnect(callback)
コード例 #12
0
ファイル: test_models.py プロジェクト: wagtail/wagtail-live
def test_live_page_mixin_add_live_post(blog_page_factory):
    """Adds live post and keeps live posts sorted by creation time."""

    message_id_0 = "some_message_id"
    message_id_1 = "other_message_id"
    live_posts = json.dumps([
        {
            "type": "live_post",
            "id": "f6d17667-65f8-4202-9051-48f45d71bd2e",
            "value": {
                "message_id": message_id_1,
                "created": "9999-01-01T12:00:00",
                "modified": "9999-01-01T12:00:00",
                "show": True,
                "content": [],
            },
        },
        {
            "type": "live_post",
            "id": "906f6590-225f-4204-9e8a-de283f1d173c",
            "value": {
                "message_id": message_id_0,
                "created": "2020-01-01T12:00:00",
                "modified": "2020-01-01T12:00:00",
                "show": True,
                "content": [],
            },
        },
    ])
    page = blog_page_factory(channel_id="some-id", live_posts=live_posts)

    live_post = construct_live_post_block(message_id="some-id", created=now())
    page.add_live_post(live_post=live_post)
    assert len(page.live_posts) == 3

    live_post = page.get_live_post_by_message_id(message_id="some-id")
    assert isinstance(live_post, StreamValue.StreamChild)
    assert live_post.value["message_id"] == "some-id"

    # It should be inserted between the existing live posts
    assert page.get_live_post_index(message_id="some-id") == 1
コード例 #13
0
def test_process_text(telegram_receiver, telegram_message_with_entities):
    live_post_block = construct_live_post_block(message_id="1234",
                                                created=datetime.now())
    text = telegram_receiver.get_message_text(
        message=telegram_message_with_entities["message"])
    telegram_receiver.process_text(live_post=live_post_block,
                                   message_text=text)

    content = live_post_block["content"]
    assert content[
        0].value.source == "This post contains different type of entities."
    assert (
        content[1].value.source ==
        'This is a regular link <a href="https://github.com/">https://github.com/</a>.'
    )
    assert content[
        2].value.source == "This is a hashtag entity #WagtailLiveBot"
    assert (
        content[3].value.source ==
        'This is a link_text <a href="https://github.com/wagtail">wagtail</a>.'
    )