Exemple #1
0
    def prepare_thumbnail(self, post: Post) -> None:
        """
        Prepare the post for the submission with thumbnail.

        Converted the post to the Rich Text JSON format so that Reddit creates
        a thumbnail for the post.
        """
        log.debug("scheduled_post_convert")
        try:
            post.body_rtjson = self.post_converter.convert_to_rtjson(
                post.body_md, )
        except ImageNotFound:
            log.warning("scheduled_post_missing_image", post=str(post))
            post.submit_with_thumbnail = False
            click.echo(
                click.style(
                    "Warning: No image found in the post.\n" +
                    "To submit a post without image set the " +
                    "'submit_with_thumbnail' option to False.",
                    fg="red",
                ))
        except (RedditError, PostConversionError) as exception:
            log.exception("scheduled_post_convert_error")
            exception.hint = (
                "To avoid further issues you can submit a post without " +
                "image by setting the 'submit_with_thumbnail' option to False."
            )
            raise
def test_submit_post_without_thumbnail(
    mock_reddit,
    mock_reddit_helper,
    mock_update_post,
    reddit_cutifier_config,
    post: Post,
):
    """
    Test submitting a post without thumbnail.

    1. Test successful submission:

    Set :attr:`Post.submit_with_thumbnail` to False (to make the test more
    obvious even when the `body_rtjson` is empty).

    Check that :meth:`Reddit.subreddit().submit()` is called with the correct
    arguments.

    Check that :meth:`RedditHelper.submit_post_rtjson()` is not called (used
    only for posts with thumbnail).

    2. Test handling of an exception raised by `PRAW`.
    """
    post.submit_with_thumbnail = False
    reddit_cutifier = RedditCutifier(reddit_cutifier_config)

    subreddit = mock_reddit.return_value.subreddit.return_value
    subreddit.submit.return_value.permalink = "slow_start_link"

    assert reddit_cutifier.submit_post(post).permalink == "slow_start_link"
    assert subreddit.submit.call_args == call(
        title=post.title,
        selftext=post.body_md,
        flair_id=post.flair_id,
    )
    assert not mock_reddit_helper.return_value.submit_post_rtjson.called

    subreddit.submit.side_effect = PrawcoreException
    with pytest.raises(RedditError):
        reddit_cutifier.submit_post(post)

    assert not mock_update_post.called