def test_run(self): with open(SECURE_FILE) as f: secure = f.read() owner = User() owner.email = 'Alex' owner.save() other_owner = User() other_owner.email = 'Mike' other_owner.save() other_feed = Feed() other_feed.save() other_owner.subscribe(other_feed) task = tasks.SyncFromReaderAPITask() result = task.delay(owner, 10, '*****@*****.**', secure) self.assertTrue(result.successful()) feeds = Feed.objects.all() # Tricky. We are subscribed to 122 feeds # We create another feed above, to get to 123 # But 3 feeds in the import were "shared-with-you" so the total # number of feeds should be 126 self.assertEqual(feeds.count(), 126) total_feeds = Feed.objects.all().count() owner = User.objects.get(pk=owner.pk) # Verify that we do not get subscribed to feeds when items are # 'shared-with-you'. self.assertEqual(owner.feeds.count(), 122) # Ensure create_raw() won't create a duplicate feed title = u'A Softer World' link = u'http://www.rsspect.com/rss/asw.xml' site = u'http://www.asofterworld.com' feed = Feed.objects.get(link=link) duplicate = Feed.create_raw(title, link, site) duplicate.add_subscriber(owner) self.assertEqual(feed.pk, duplicate.pk) # Testing that subscribing a second time doesn't blow up. duplicate2 = Feed.create_and_subscribe(title, link, site, owner) self.assertEqual(feed.pk, duplicate2.pk) tagged = UserFeedItem.objects.filter(tags__name__in=['shared-with-you']) self.assertEqual(len(tagged), 10)
def _import_json_items(self, import_file): data = None for f in self.z.namelist(): if import_file in f: data = json.loads(self.z.open(f).read(), strict=False) break if data is None: return False try: # This is like, weak sauce verification, hoping that we're # not about to get bogus data. Still, a carefully crafted # attack file could make it past this check. id = data['id'] if not (id.endswith('starred') or id.endswith('broadcast-friends') or id.endswith('broadcast') or id.endswith('post') or id.endswith('like')): return False except KeyError: return False for i in data['items']: title = i['origin']['title'] site = i['origin']['htmlUrl'] link = i['origin']['streamId'] if link.startswith('feed/'): link = link.replace('feed/', '', 1) # These are some weird bullshit links created by google # reader. Try and discover a real link instead. elif link.startswith('user/'): maybe = Feed.autodiscover(site) if maybe: link = maybe feed = Feed.create_raw(title, link, site) item = {} item['id'] = i['id'] item['title'] = i['title'] item['url'] = i.get('canonical', i.get('alternate', ''))[0]['href'] try: item['content'] = i['content']['content'] except KeyError: try: item['content'] = i['summary']['content'] except KeyError: # No idea if this is even possible, we should squawk item['content'] = '' item['time'] = i['published'] entry = FakeEntry(item) user_item = _new_user_item(self.user, feed, entry) for c in i.get('categories', []): if c.startswith('user/') and c.endswith('/like'): user_item.tags.add('liked') elif c.startswith('user/') and c.endswith('/post'): user_item.tags.add('notes') elif c.startswith('user/') and c.endswith('/created'): user_item.tags.add('notes') # Annoyingly, if something is shared-with-you, it also # gets the /broadcast tag. So if we are processing the # shared-with-you.json file, don't mark those items as # things that *you've* shared elif c.startswith('user/') and c.endswith('/broadcast') \ and not id.endswith('broadcast-friends'): user_item.tags.add('shared') elif c.startswith('user/') and c.endswith('/broadcast-friends'): user_item.tags.add('shared-with-you') elif c.startswith('user/') and c.endswith('/starred'): user_item.starred = True elif c.startswith('user/') and c.endswith('/read'): user_item.read = True elif c.startswith('user/') and ('label' in c): tag = c.split('/')[-1] user_item.tags.append(tag) user_item.tags.add('imported') user_item.save() try: # This comes from the 'shared-with-you' category. friend_userid = i['via'][0]['href'].split('/')[-4] # Not sure how to save/model friend_userid yet except KeyError: pass # XXX: should we do something about i['comments'] too? for a in i['annotations']: # Following attributes are interesting, but not sure # how to model them yet. # - a['content'] # - a['userId'] # - a['profileId'] pass
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)