From 81e618b40bc6f1e80293e406569481137b1a76be Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Tue, 14 Jun 2016 00:59:18 -0700 Subject: [PATCH 1/2] Check for empty subreddit name. --- rtv/content.py | 6 ++++++ tests/test_content.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/rtv/content.py b/rtv/content.py index cac7358..8ca0324 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -6,6 +6,7 @@ from datetime import datetime import six import praw +from praw.errors import InvalidSubreddit from kitchen.text.display import wrap from . import exceptions @@ -398,6 +399,11 @@ class SubredditContent(Content): submissions = reddit.search(query, subreddit=name, sort=order) else: + if name == '': + # Praw does not correctly handle empty strings + # https://github.com/praw-dev/praw/issues/615 + raise InvalidSubreddit() + if name == 'front': dispatch = { None: reddit.get_front_page, diff --git a/tests/test_content.py b/tests/test_content.py index f111910..5860a3f 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -259,6 +259,13 @@ def test_content_subreddit_from_name(reddit, terminal): SubredditContent.from_name(reddit, name, terminal.loader) assert isinstance(terminal.loader.exception, exceptions.SubredditError) + # A couple of edge cases + names = ['', '/', '//', '/////////////////'] + for name in names: + with terminal.loader(): + SubredditContent.from_name(reddit, name, terminal.loader) + assert isinstance(terminal.loader.exception, exceptions.SubredditError) + # Front page alias name = '/r/front/rising' content = SubredditContent.from_name(reddit, name, terminal.loader) From ab86b5d8758ed263314ac1e92598c25c8220fc41 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Tue, 14 Jun 2016 01:05:20 -0700 Subject: [PATCH 2/2] Fixed test. --- tests/test_content.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_content.py b/tests/test_content.py index 5860a3f..41a0e74 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -264,7 +264,8 @@ def test_content_subreddit_from_name(reddit, terminal): for name in names: with terminal.loader(): SubredditContent.from_name(reddit, name, terminal.loader) - assert isinstance(terminal.loader.exception, exceptions.SubredditError) + assert isinstance(terminal.loader.exception, + praw.errors.InvalidSubreddit) # Front page alias name = '/r/front/rising' @@ -351,4 +352,4 @@ def test_content_subscription_empty(terminal): with terminal.loader(): SubscriptionContent(subscriptions, terminal.loader) - assert isinstance(terminal.loader.exception, exceptions.SubscriptionError) \ No newline at end of file + assert isinstance(terminal.loader.exception, exceptions.SubscriptionError)