def test_lookup_by_starred(self): '''Only return the unread items.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() item2 = FeedItem() item2.feed = feed item2.title = 'Feed title 2' item2.link = 'http://www.paulhummer.org/rss/2' item2.guid = 'http://www.paulhummer.org/rss/2' item2.published = datetime.now() item2.save() item3 = FeedItem() item3.feed = feed item3.title = 'Feed title 3' item3.link = 'http://www.paulhummer.org/rss/3' item3.guid = 'http://www.paulhummer.org/rss/3' item3.published = datetime.now() item3.save() userfeeditem = UserFeedItem.objects.get( user=self.user, feed=feed, item=item) userfeeditem.starred = True userfeeditem.save() userfeeditem2 = UserFeedItem.objects.get( user=self.user, feed=feed, item=item2) userfeeditem2.starred = True userfeeditem2.save() # Actual test result = self.api_client.get( '/api/0.9.5/item/?starred=True') self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(len(content['objects']), 2) result = self.api_client.get( '/api/0.9.5/item/?starred=False') self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(len(content['objects']), 1)
def test_lookup_by_unread_and_feed(self): '''Only return the unread items by feed.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() item2 = FeedItem() item2.feed = feed item2.title = 'Feed title 2' item2.link = 'http://www.paulhummer.org/rss/2' item2.guid = 'http://www.paulhummer.org/rss/2' item2.published = datetime.now() item2.save() userfeed = UserFeed.objects.get(user=self.user, feed=feed) userfeeditem = UserFeedItem.objects.get( user=self.user, feed=feed, item=item) userfeeditem.read = True userfeeditem.save() mike = User.objects.create_user( 'mike', '*****@*****.**', password='******') mike.save() feed2 = Feed.create_and_subscribe( 'Gothamist', 'http://feeds.gothamistllc.com/gothamist05-', None, mike) item4 = FeedItem() item4.feed = feed2 item4.title = 'Feed title' item4.link = 'http://www.gothamist.com/rss/1' item4.guid = 'http://www.gothamist.com/rss/1' item4.published = datetime.now() item4.save() # Actual test result = self.api_client.get( '/api/0.9.5/item/?read=False&feed={0}'.format(userfeed.pk)) self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(len(content['objects']), 1)
def test_malformed(self): """Test nasty feeds that we've discovered in the wild""" owner = User() owner.email = "Bob" owner.save() # Really long titles. title = u"minimal linux" link = u"http://minimallinux.com/rss" site = u"http://minimallinux.com/" feed = Feed.create_and_subscribe(title, link, site, owner) # This feed is dead, so we don't expect any items downloaded. userfeeditems = FeedItem.objects.for_user(owner) self.assertEqual(userfeeditems.count(), 0) # Discovered while trying to import from google reader. # >>> len(e.title) # 857 item = FeedItem() item.feed = feed item.title = u"This isn't a question but more of a submission for your \"enough\" poll-type thing.<br>\r\n<br>\r\nI would go with Arch as my main distro. It\u2019s the simplest and easiest to setup of any distro I\u2019ve used.<br>\r\n<br>\r\nMy window manager of choice would be dwm. Amazingly simple and very customizable.<br>\r\n<br>\r\nI would use vim for any text editing I would need to do (which mostly involves programming)<br>\r\n<br>\r\nI would be able to get away with using feh as my image viewer, since I never really need to do any image editing.<br>\r\n<br>\r\nChromium, of course.<br>\r\n<br>\r\nI would use dmenu as my app launcher. Another suckless creation, very simple and very fast.<br>\r\n<br>\r\nDropbox for file syncing.<br>\r\n<br>\r\nPidgin (haven't looked that much for something simpler) for AIM.<br>\r\n<br>\r\nI would also need a few programming related things such as ruby, gcc, make, etc." item.link = u"http://minimallinux.com/post/7031884799" item.description = u"<p>Nice\u2014good stuff here. I really dig dmenu.</p>" item.published = datetime.utcfromtimestamp(1309319839) item.atom_id = "" item.reader_guid = u"tag:google.com,2005:reader/item/022fe5bb1abdb67a" item.guid = item.calculate_guid() item.save() userfeeditems = FeedItem.objects.for_user(owner) self.assertEqual(userfeeditems.count(), 1)
def test_endpoint_paging(self): '''Page out more than 20 items.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) for i in xrange(0, 50): item = FeedItem() item.feed = feed item.title = 'Feed title {0}'.format(i) item.link = 'http://www.paulhummer.org/rss/{0}'.format(i) item.guid = 'http://www.paulhummer.org/rss/{0}'.format(i) item.published = datetime.now() item.save() # Actual test result = self.api_client.get('/api/0.9.5/item/') self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(content.keys(), ['meta', 'objects']) self.assertEqual( content['meta'].keys(), ['previous', 'total_count', 'offset', 'limit', 'next']) self.assertEqual(content['meta']['previous'], None) self.assertEqual(content['meta']['total_count'], 50) self.assertEqual(content['meta']['offset'], 0) self.assertEqual(content['meta']['limit'], 20) self.assertEqual( content['meta']['next'].split('?')[1], 'limit=20&offset=20') self.assertEqual(len(content['objects']), 20)
def test_endpoint_by_id(self): '''Get a specific feed item.''' feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() userfeeditem = UserFeedItem.objects.get(user=self.user, item=item) userfeeditem.tags.add('hipster', 'douche') userfeeditem.save() result = self.api_client.get('/api/0.9.5/item/') content = json.loads(result.content) result = self.api_client.get(content['objects'][0]['resource_uri']) self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual( sorted(content.keys()), ['description', 'feed', 'feed_id', 'feed_tags', 'feed_title', 'id', 'link', 'published', 'read', 'resource_uri', 'starred', 'tags', 'title']) self.assertEqual(content['description'], item.description) self.assertEqual(content['link'], item.link) self.assertEqual( dateutil.parser.parse(content['published']), item.published) self.assertEqual(content['title'], item.title) self.assertEqual(content['feed'], '/api/0.9.5/feed/1/') self.assertEqual(content['tags'], ['hipster', 'douche'])
def test_endpoint_single_item(self): '''The root feed endpoint returns a list of items.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() # Actual test result = self.api_client.get('/api/0.9.5/item/') self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(content.keys(), ['meta', 'objects']) self.assertEqual( content['meta'].keys(), ['previous', 'total_count', 'offset', 'limit', 'next']) self.assertEqual(content['meta']['previous'], None) self.assertEqual(content['meta']['total_count'], 1) self.assertEqual(content['meta']['offset'], 0) self.assertEqual(content['meta']['limit'], 20) self.assertEqual(content['meta']['next'], None)
def test_sort_published(self): '''We can sort by 'published'.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed 1' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() item = FeedItem() item.feed = feed item.title = 'Feed 2' item.link = 'http://www.paulhummer.org/rss/2' item.guid = 'http://www.paulhummer.org/rss/2' item.published = datetime.now() - timedelta(days=-1) item.save() item = FeedItem() item.feed = feed item.title = 'Feed 3' item.link = 'http://www.paulhummer.org/rss/3' item.guid = 'http://www.paulhummer.org/rss/3' item.published = datetime.now() - timedelta(days=-2) item.save() # Actual test result = self.api_client.get('/api/0.9.5/item/?order_by=-published') self.assertEqual(result.status_code, 200) content = json.loads(result.content) self.assertEqual( [item['title'] for item in content['objects']], ['Feed 3', 'Feed 2', 'Feed 1']) result = self.api_client.get('/api/0.9.5/item/?order_by=published') self.assertEqual(result.status_code, 200) content = json.loads(result.content) self.assertEqual( [item['title'] for item in content['objects']], ['Feed 1', 'Feed 2', 'Feed 3'])
def test_lookup_by_feed_id(self): '''Filter items by feed.id.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() item = FeedItem() item.feed = feed item.title = 'Feed title 2' item.link = 'http://www.paulhummer.org/rss/2' item.guid = 'http://www.paulhummer.org/rss/2' item.published = datetime.now() item.save() feed2 = Feed.create_and_subscribe( 'Gothamist', 'http://feeds.gothamistllc.com/gothamist05', None, self.user) item2 = FeedItem() item2.feed = feed2 item2.title = 'Feed title' item2.link = 'http://www.gothamist.com/rss/1' item2.guid = 'http://www.gothamist.com/rss/1' item2.published = datetime.now() item2.save() userfeed = UserFeed.objects.get(user=self.user, feed=feed) # Actual test result = self.api_client.get( '/api/0.9.5/item/?feed={0}'.format(userfeed.pk)) self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(len(content['objects']), 2)
def test_endpoint_only_owned(self): '''Don't return items where UserFeedItem.user is not the user.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() mike = User.objects.create_user( 'mike', '*****@*****.**', password='******') mike.save() feed = Feed.create_and_subscribe( 'Gothamist', 'http://feeds.gothamistllc.com/gothamist05', None, mike) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.gothamist.com/rss/1' item.guid = 'http://www.gothamist.com/rss/1' item.published = datetime.now() item.save() # Actual test result = self.api_client.get('/api/0.9.5/item/') self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(content.keys(), ['meta', 'objects']) self.assertEqual( content['meta'].keys(), ['previous', 'total_count', 'offset', 'limit', 'next']) self.assertEqual(content['meta']['previous'], None) self.assertEqual(content['meta']['total_count'], 1) self.assertEqual(content['meta']['offset'], 0) self.assertEqual(content['meta']['limit'], 20) self.assertEqual(content['meta']['next'], None)
def _new_user_item(user, feed, entry): try: item = FeedItem.objects.get(reader_guid=entry.id) except ObjectDoesNotExist: tmp = FeedItem() tmp.feed = feed tmp.title = entry.title tmp.link = entry.url tmp.atom_id = '' tmp.description = entry.content tmp.reader_guid = entry.id tmp.published = datetime.utcfromtimestamp(entry.time) tmp.guid = tmp.calculate_guid() item = FeedItem._get_or_create(tmp) try: user_item = item.userfeeditem(user) except ObjectDoesNotExist: # Nasty. The above only works if a user is actually # subscribed to a feed. However, it can be the case # where we're trying to import Google Reader, and we're # processing items that have been shared with us. In # this case, we probably won't be subscribed to the # feed, and more, we probably don't want to subscribe to # the feed. So manually create a UserFeedItem so the # Item can be accessed by the User. We can pull it out # of the db later by searching for the 'shared-with-you' # tag. user_item = UserFeedItem() user_item.item = item user_item.user = user user_item.feed = feed user_item.save() user_item.read = entry.read user_item.starred = entry.starred for t in entry.tags: user_item.tags.add(t) user_item.save() return user_item
def test_put_starred(self): '''We can set 'starred' via the API.''' # Test data feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) item = FeedItem() item.feed = feed item.title = 'Feed title -_-' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() useritem = UserFeedItem.objects.get(user=self.user, item=item, feed=feed) endpoint = '/api/0.9.5/item/{0}/'.format(useritem.pk) resource = json.loads(self.api_client.get(endpoint).content) resource['starred'] = True # Actual test result = self.api_client.put( endpoint, format='json', data=resource) self.assertEqual(result.status_code, 204) new_resource = json.loads(self.api_client.get(endpoint).content) self.assertEqual(new_resource['starred'], True)
def test_lookup_by_feed_id_withoffset(self): '''Filter items by feed.id. A bug existed where a lookup by id would return items for a different feed. This occurs if Feed.pk and UserFeed.pk were not the same, which isn't normally the case in test data. ''' # Test data deadfeed = Feed.create_raw( 'Dead Feed 1', 'http://example.com/dead1.rss', 'http://example.com/dead1.html') deadfeed2 = Feed.create_raw( 'Dead Feed 2', 'http://example.com/dead2.rss', 'http://example.com/dead2.html') feed = Feed.create_and_subscribe( 'Paul Hummer', 'http://www.paulhummer.org/rss', None, self.user) # Let's make sure they don't share ids userfeed = UserFeed.objects.get(user=self.user, feed=feed) self.assertNotEqual(feed.pk, userfeed.pk) item = FeedItem() item.feed = feed item.title = 'Feed title' item.link = 'http://www.paulhummer.org/rss/1' item.guid = 'http://www.paulhummer.org/rss/1' item.published = datetime.now() item.save() item = FeedItem() item.feed = feed item.title = 'Feed title 2' item.link = 'http://www.paulhummer.org/rss/2' item.guid = 'http://www.paulhummer.org/rss/2' item.published = datetime.now() item.save() deadfeed3 = Feed.create_raw( 'Dead Feed 3', 'http://example.com/dead3.rss', 'http://example.com/dead3.html') feed2 = Feed.create_and_subscribe( 'Gothamist', 'http://feeds.gothamistllc.com/gothamist05', None, self.user) item2 = FeedItem() item2.feed = feed2 item2.title = 'Feed title' item2.link = 'http://www.gothamist.com/rss/1' item2.guid = 'http://www.gothamist.com/rss/1' item2.published = datetime.now() item2.save() userfeed = UserFeed.objects.get(user=self.user, feed=feed) # Actual test result = self.api_client.get( '/api/0.9.5/item/?feed={0}'.format(userfeed.pk)) self.assertEqual(200, result.status_code) content = json.loads(result.content) self.assertEqual(len(content['objects']), 2)