Exemple #1
0
    def submit_post(self, post: Post) -> Submission:
        """
        Submit the post to Reddit.

        If the post is set to be submitted with thumbnail, submit its body in
        the Rich Text JSON format. Otherwise submit the Markdown content using
        regular method of `PRAW`.
        """
        log.info("post_submit", post=str(post))
        try:
            if post.submit_with_thumbnail and post.body_rtjson:
                submission = self.reddit_helper.submit_post_rtjson(
                    subreddit=post.subreddit,
                    title=post.title,
                    body_rtjson=post.body_rtjson,
                    flair_id=post.flair_id,
                )
            else:
                submission = self.reddit.subreddit(
                    post.subreddit,
                ).submit(
                    title=post.title,
                    selftext=post.body_md,
                    flair_id=post.flair_id,
                )
        except PrawcoreException as exception:
            log.exception("post_submit_error")
            raise RedditError(
                "Failed to submit the post.",
            ) from exception

        post.submission_id = submission.id

        log.debug("post_submit_result", permalink=submission.permalink)

        if not post.submit_with_thumbnail:
            return submission

        delay = self.post_update_delay
        log.debug("post_update_delay", delay=delay)
        click.echo(
            click.style(
                "Waiting {0}s before updating the post.".format(delay / 1000),
                fg="cyan",
            ),
        )
        time.sleep(delay / 1000)

        return self.update_post(post, submission)
def test_update_post(
    mock_reddit,
    reddit_cutifier_config,
    post: Post,
    submission,
    capsys,
):
    """
    Test updating a post.

    1. Test a successful edit.

    2. Test updating without providing both `submission` parameter and
       the :attr:`Post.submission_id`.

    3. Previous test with the `submission_id` provided.

    4. Test handling of an exception raised by `PRAW`.
    """
    reddit_cutifier = RedditCutifier(reddit_cutifier_config)

    reddit_cutifier.update_post(post, submission)

    assert submission.edit.call_args == call(post.body_md)

    with pytest.raises(RuntimeError):
        reddit_cutifier.update_post(post)

    post.submission_id = "cute_id"
    reddit_cutifier.update_post(post)

    assert mock_reddit.return_value.submission.call_args == call("cute_id")

    submission.edit.side_effect = PrawcoreException
    reddit_cutifier.update_post(post, submission)

    captured = capsys.readouterr()
    assert "Failed to update the post" in captured.err