diff --git a/rtv/content.py b/rtv/content.py index 8de20e3..2ae45d6 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -123,6 +123,24 @@ class Content(object): data['hidden'] = False data['saved'] = comment.saved + + return data + + @classmethod + def coerce_saved_comment(cls, comment, data): + """ + Parse through a submission comment saved and return a dict with data ready to + be displayed through the terminal. + """ + + data['title'] = data['body'] + data['comments'] = '' + data['url_full'] = data['permalink'] + data['url'] = data['permalink'] + data['nsfw'] = comment.over_18 + data['subreddit'] = six.text_type(comment.subreddit) + data['url_type'] = 'selfpost' + return data @classmethod @@ -409,7 +427,7 @@ class SubredditContent(Content): if not reddit.is_oauth_session(): raise exceptions.AccountError('Not logged in') elif order: - submissions = reddit.user.get_submitted(sort=order) + submissions = reddit.user.get_saved(sort=order) else: submissions = reddit.user.get_saved() @@ -469,12 +487,12 @@ class SubredditContent(Content): except StopIteration: raise IndexError else: - # TODO: In order to display saved comment, we need to - # coerce the comment into a submission - try: + if hasattr(submission,'title'): data = self.strip_praw_submission(submission) - except: - continue + # when submission is a saved commment + else: + data = self.strip_praw_comment(submission) + data = self.coerce_saved_comment(submission, data) data['index'] = len(self._submission_data) + 1 # Add the post number to the beginning of the title diff --git a/tests/test_content.py b/tests/test_content.py index 41a0e74..0a2a8f9 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -277,6 +277,49 @@ def test_content_subreddit_from_name(reddit, terminal): SubredditContent.from_name(reddit, 'front', terminal.loader, query='pea') SubredditContent.from_name(reddit, 'python', terminal.loader, query='pea') +def test_content_subreddit_from_saved(reddit, terminal, oauth, refresh_token): + + # Not logged in + with terminal.loader(): + SubredditContent.from_name(reddit, '/r/saved', terminal.loader) + assert isinstance(terminal.loader.exception, exceptions.AccountError) + + # Logged in + name = 'saved' + oauth.config.refresh_token = refresh_token + oauth.authorize() + with terminal.loader(): + content = SubredditContent.from_name(reddit, name, terminal.loader) + assert content.name == '/r/saved' + assert content.order is None + + # Can submit without the /r/ and with the order in the name + name = 'saved/top/' + oauth.config.refresh_token = refresh_token + oauth.authorize() + with terminal.loader(): + content = SubredditContent.from_name(reddit, name, terminal.loader) + assert content.name == '/r/saved' + assert content.order == 'top' + + # Explicit order trumps implicit + name = '/r/saved' + oauth.config.refresh_token = refresh_token + oauth.authorize() + with terminal.loader(): + content = SubredditContent.from_name( + reddit, name, terminal.loader, order='new') + assert content.name == '/r/saved' + assert content.order == 'new' + + # Invalid order raises an exception + name = '/r/saved/fake' + oauth.config.refresh_token = refresh_token + oauth.authorize() + with terminal.loader(): + SubredditContent.from_name(reddit, name, terminal.loader) + assert isinstance(terminal.loader.exception, exceptions.SubredditError) + def test_content_subreddit_multireddit(reddit, terminal): diff --git a/tests/test_submission.py b/tests/test_submission.py index eb1e513..180fc52 100644 --- a/tests/test_submission.py +++ b/tests/test_submission.py @@ -79,6 +79,7 @@ def test_submission_unauthenticated(submission_page, terminal): 'c', # Comment 'e', # Edit 'd', # Delete + 'w', # Save ] for ch in methods: submission_page.controller.trigger(ch) @@ -168,6 +169,29 @@ def test_submission_vote(submission_page, refresh_token): assert data['likes'] is None +def test_submission_save(submission_page, refresh_token): + + # Log in + submission_page.config.refresh_token = refresh_token + submission_page.oauth.authorize() + + # Test voting on the submission + with mock.patch('praw.objects.Submission.save') as save, \ + mock.patch('praw.objects.Submission.unsave') as unsave: + + data = submission_page.content.get(submission_page.nav.absolute_index) + + # Save + submission_page.controller.trigger('w') + assert save.called + assert data['saved'] is True + + # Unsave + submission_page.controller.trigger('w') + assert unsave.called + assert data['saved'] is False + + def test_submission_comment(submission_page, terminal, refresh_token): # Log in