def test_publish_called_on_new_live_page_update_signal(mocker):
    ws_publisher = BaseWebsocketPublisher()
    mocker.patch.object(ws_publisher, "publish", return_value=None)
    live_page_update.connect(ws_publisher)

    try:
        update = {"channel_id": "some-id", "renders": {}, "removals": []}
        live_page_update.send(sender=LivePageMixin, **update)
        ws_publisher.publish.assert_called_once_with(**update)

    finally:
        live_page_update.disconnect(ws_publisher)
Beispiel #2
0
    def ready(self):
        from wagtail_live.publishers.websocket import BaseWebsocketPublisher
        from wagtail_live.utils import get_live_publisher

        live_publisher = get_live_publisher()

        # Connect a listener to the live_page_update signal
        # if the publisher defined uses the websockets technique
        if issubclass(live_publisher, BaseWebsocketPublisher):
            from wagtail_live.signals import live_page_update

            # Set`weak=False` to avoid the publisher being garbage collected.
            # See:
            # https://docs.djangoproject.com/en/3.2/topics/signals/#django.dispatch.Signal.connect
            live_page_update.connect(live_publisher(),
                                     weak=False,
                                     dispatch_uid="live_publisher")
Beispiel #3
0
def test_save_live_page_deleted_post(blog_page_factory):
    # Setup signal callback
    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)

    live_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id": "some-id",
                "created": "2021-01-01T12:00:00",
                "modified": None,
                "show": True,
                "content": [],
            },
        },
    ])
    page = blog_page_factory(channel_id="channel_id", live_posts=live_posts)
    last_updated_at = page.last_updated_at

    new_posts = []
    page.live_posts = new_posts

    try:
        page.save()

        # last_updated_at field is modified
        assert page.last_updated_at > last_updated_at

        # Signal is sent with correct renders
        assert count == 1
        assert _channel_id == "channel_id"
        assert _renders == []
        assert _removals == ["some-id"]

    finally:
        live_page_update.disconnect(callback)
Beispiel #4
0
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)
Beispiel #5
0
def test_publish_new_update_on_live_page_update_signal():
    # Add 'test-channel' to live page group
    channel_layer = channels.layers.get_channel_layer()
    async_to_sync(channel_layer.group_add)("liveblog_some_id", "test-channel")

    # Connect publisher to live_page_update signal
    publisher = DjangoChannelsPublisher()
    live_page_update.connect(publisher)

    try:
        # Send live_page_update signal
        live_page_update.send(sender=LivePageMixin,
                              channel_id="some_id",
                              renders={},
                              removals=[])

        # Ensure that the update is published i.e sent to live page group
        message = async_to_sync(channel_layer.receive)("test-channel")
        assert message == {"type": "update", "renders": {}, "removals": []}

    finally:
        live_page_update.disconnect(publisher)
        async_to_sync(channel_layer.group_discard)("liveblog_some_id",
                                                   "test-channel")
Beispiel #6
0
def test_save_live_page_no_changes(blog_page_factory):
    # Setup signal callback
    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)

    live_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id": "some-id",
                "created": "2021-01-01T12:00:00",
                "modified": None,
                "show": True,
                "content": [
                    {
                        "type": "text",
                        "value": "Some text",
                    },
                ],
            },
        },
    ])
    page = blog_page_factory(channel_id="channel_id", live_posts=live_posts)
    last_updated_at = page.last_updated_at

    new_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id": "some-id",
                "created": "2021-01-01T12:00:00",
                "modified": None,
                "show": True,
                "content": [
                    {
                        "type": "text",
                        "value": "Some text",
                    },
                ],
            },
        },
    ])
    page.live_posts = new_posts

    try:
        page.save()

        # last_updated_at field isn't modified
        assert page.last_updated_at == last_updated_at

        # Signal isn't sent
        assert count == 0
        assert _channel_id is None
        assert _renders is None
        assert _removals is None

    finally:
        live_page_update.disconnect(callback)
Beispiel #7
0
def test_save_live_page_different_images(blog_page_factory):
    # Setup signal callback
    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)

    live_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id": "some-id",
                "created": "2021-01-01T12:00:00",
                "modified": None,
                "show": True,
                "content": [
                    {
                        "type": "image",
                        "value": "1",
                    },
                ],
            },
        },
    ])

    page = blog_page_factory(channel_id="channel_id", live_posts=live_posts)
    last_updated_at = page.last_updated_at

    # Replace image
    post = page.get_live_post_by_index(0)
    image1 = get_image_model().objects.create(title="image1",
                                              file=get_test_image_file(
                                                  filename="image1.png",
                                                  size=(100, 100)))
    post.value["content"][0].value = image1

    try:
        page.save()

        # last_updated_at field is modified.
        assert page.last_updated_at > last_updated_at

        post = page.get_live_post_by_message_id(message_id="some-id")

        # The value of the modified field for the first post should change.
        assert post.value["modified"] == page.last_updated_at

        # Signal is sent with correct renders.
        assert count == 1
        assert _channel_id == "channel_id"
        assert _renders == [post]
        assert _removals == []

    finally:
        live_page_update.disconnect(callback)
Beispiel #8
0
def test_save_live_page_different_embed_values(blog_page_factory):
    # Setup signal callback
    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)

    live_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id":
                "some-id",
                "created":
                "2021-01-01T12:00:00",
                "modified":
                None,
                "show":
                True,
                "content": [
                    {
                        "type": "embed",
                        "value": "https://www.youtube.com/watch?v=Wrc_gofwDR8",
                    },
                ],
            },
        },
    ])
    page = blog_page_factory(channel_id="channel_id", live_posts=live_posts)
    last_updated_at = page.last_updated_at

    new_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id":
                "some-id",
                "created":
                "2021-01-01T12:00:00",
                "modified":
                None,
                "show":
                True,
                "content": [
                    {
                        "type":
                        "embed",
                        "value":
                        "https://www.youtube.com/watch?v=CQ7Gx8b7ac4&t=939s",
                    },
                ],
            },
        },
    ])
    page.live_posts = new_posts

    try:
        page.save()

        # last_updated_at field is modified.
        assert page.last_updated_at > last_updated_at

        post = page.get_live_post_by_message_id(message_id="some-id")

        # The value of the modified field for the first post should change.
        assert post.value["modified"] == page.last_updated_at

        # Signal is sent with correct renders.
        assert count == 1
        assert _channel_id == "channel_id"
        assert _renders == [post]
        assert _removals == []

    finally:
        live_page_update.disconnect(callback)
Beispiel #9
0
def test_save_live_page_new_post(blog_page_factory):
    # Setup signal callback
    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)

    live_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id": "some-id",
                "created": "2021-01-01T12:00:00",
                "modified": None,
                "show": True,
                "content": [],
            },
        },
    ])
    page = blog_page_factory(channel_id="channel_id", live_posts=live_posts)
    last_updated_at = page.last_updated_at

    new_posts = json.dumps([
        {
            "type": "live_post",
            "id": "some-id",
            "value": {
                "message_id": "some-id",
                "created": "2021-01-01T12:00:00",
                "modified": None,
                "show": True,
                "content": [],
            },
        },
        {
            "type": "live_post",
            "id": "other-id",
            "value": {
                "message_id": "other-id",
                "created": "2021-01-01T13:00:00",
                "modified": None,
                "show": True,
                "content": [],
            },
        },
    ])
    page.live_posts = new_posts

    try:
        page.save()

        # last_updated_at field is modified
        assert page.last_updated_at > last_updated_at

        # The value of the modified field for the first post shouldn't change
        assert (page.get_live_post_by_message_id(
            message_id="some-id").value["modified"] is None)

        # Signal is sent with correct renders
        assert count == 1
        assert _channel_id == "channel_id"
        assert _renders[0] == page.get_live_post_by_message_id(
            message_id="other-id")
        assert _removals == []

    finally:
        live_page_update.disconnect(callback)