def test_bad_username(self): with patch('r2.models.account.Account') as MockAccount: MockAccount._by_name.side_effect = NotFound self.assertEqual( utils.url_to_thing('http://reddit.local/user/missing_user'), None, )
def GET_oembed(self, url, parent, live): """Get the oEmbed response for a URL, if any exists. Spec: http://www.oembed.com/ Optional parameters (parent, live) are passed through as embed options to oEmbed renderers. """ response.content_type = "application/json" thing = url_to_thing(url) if not thing: abort(404) embed_options = { "parent": parent, "live": live, } try: return scriptsafe_dumps(_oembed_for(thing, **embed_options)) except ForbiddenError: abort(403) except NotImplementedError: abort(404)
def test_short_username(self): with patch('r2.models.account.Account') as MockAccount: MockAccount._by_name.return_value = s.Account self.assertEqual( utils.url_to_thing('https://reddit.local/u/wting'), s.Account, )
def test_subreddit(self): with patch('r2.models.Subreddit') as MockSubreddit: MockSubreddit._by_name.return_value = s.Subreddit self.assertEqual( utils.url_to_thing('http://reddit.local/r/pics/'), s.Subreddit, )
def test_username_subcategories(self): user_categories = [ 'comments', 'submitted', 'gilded', 'upvoted', 'downvoted', 'hidden', 'saved' ] with patch('r2.models.account.Account') as MockAccount: MockAccount._by_name.return_value = s.Account for category in user_categories: self.assertEqual( utils.url_to_thing('https://reddit.local/u/wting/%s/' % category), s.Account, )
def test_username_subcategories(self): user_categories = [ 'comments', 'submitted', 'gilded', 'upvoted', 'downvoted', 'hidden', 'saved' ] with patch('r2.models.account.Account') as MockAccount: MockAccount._by_name.return_value = s.Account for category in user_categories: self.assertEqual( utils.url_to_thing( 'https://reddit.local/u/wting/%s/' % category ), s.Account, )
def get_content_id(): from r2.lib import utils thing = utils.url_to_thing(request.fullurl) if not thing: return None, None content_id = None type_name = getattr(thing, '_type_name', None) if type_name == 'comment': # We use the parent link for comment permalink pages, since # they share a canonical URL link = getattr(thing, 'link', thing.link_slow) content_id = link._fullname elif type_name == 'link': content_id = thing._fullname elif type_name == 'subreddit': content_id = thing._fullname return content_id, type_name
def test_frontpage(self): self.assertEqual( utils.url_to_thing('http://reddit.local/'), None, )