def test_subscribers(self): bob = User() bob.email = "Bob" bob.save() steve = User() steve.email = "Steve" steve.save() feed = Feed() feed.title = "Some Political Bullshit" feed.link = "http://bs.com/rss" feed.site = "http://bs.com" feed.save() feed.add_subscriber(bob) feed.add_subscriber(steve) other_feed = Feed() other_feed.title = "Mom's recipe blog" other_feed.site = "http://yourmom.com" other_feed.link = "http://yourmom.com/rss" other_feed.save() other_feed.add_subscriber(steve) self.assertEqual(feed.subscribers.count(), 2) self.assertEqual(other_feed.subscribers.count(), 1)
def test_resource_put_read(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.save() feed.update() feed.add_subscriber(self.user) response = self.client.get('/api/0.9/item/') self.assertEqual(response.status_code, 200) content = json.loads(response.content) objects = content['objects'] resource = objects[0] response = self.client.get(resource['resource_uri']) self.assertEqual(response.status_code, 200) resource = json.loads(response.content) self.assertEqual(resource['read'], False) resource['read'] = True self.client.put( resource['resource_uri'], data=json.dumps(resource), content_type='application/json') response = self.client.get(resource['resource_uri']) resource = json.loads(response.content) self.assertEqual(resource['read'], True)
def test_dates(self): links = [ "http://theworstthingever.com/index.rdf", "http://news.ycombinator.com/rss", "http://aw.lackof.org/~awilliam/blog/index.rss", "http://adeem.me/blog/feed/rss/", "http://lusars.net/~mhunter/rss.xml", "http://feeds.feedburner.com/Manbabies", "http://www.365portraits.com/rss.php", "http://iphonedevelopmentbits.com/feed/rss", "http://blog.myspace.com/blog/rss.cfm?friendID=4470742", ] for link in links: feed = Feed() feed.link = link feed.save() feed.update() first_update = feed.items.count() # Pause a few seconds, so when we fetch again, utcnow() in the # model will be a different time (and therefore potentially a # different GUID) time.sleep(3) feed.update() second_update = feed.items.count() # Ensure that fetching feeds with missing or malformed dates # do not result in different GUIDs (they should resolve to # the same GUID). self.assertEqual(first_update, second_update)
def test_duplicates(self): user = User() user.email = 'Bob' user.save() tmp = Feed() tmp.title = 'Marginal Revolution' tmp.link = 'http://feeds.feedburner.com/marginalrevolution/feed' tmp.site = 'http://marginalrevolution.com/' last_fetched = datetime.now() - timedelta(minutes=31) tmp.last_fetched = last_fetched # Fetch #1 feed = Feed.create_and_subscribe(tmp.title, tmp.link, tmp.site, user) # Fetch #2 task1 = tasks.UpdateFeedTask() result1 = task1.delay() # Force fetch #3 last_fetched = datetime.now() - timedelta(minutes=31) feed.last_fetched = last_fetched feed.save() task2 = tasks.UpdateFeedTask() result2 = task2.delay() self.assertEqual(feed.feeditems.count(), 15)
def test_resource_read(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.save() feed.update() feed.add_subscriber(self.user) response = self.client.get('/api/0.9/item/') self.assertEqual(response.status_code, 200) content = json.loads(response.content) objects = content['objects'] resource = objects[0] response = self.client.get(resource['resource_uri']) self.assertEqual(response.status_code, 200) content = json.loads(response.content) resource_id = resource['resource_uri'].split('/')[-2] item = FeedItem.objects.get(pk=resource_id) useritem = UserFeedItem.objects.get(user=self.user, item=item) self.assertEqual(resource['read'], False) useritem.read = True useritem.save() response = self.client.get(resource['resource_uri']) resource = json.loads(response.content) self.assertEqual(resource['read'], True)
def test_unauthorized(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.save() uri = '/api/0.9/feed/{0}'.format(feed.pk) response = self.client.get(uri, follow=True) self.assertEqual(response.status_code, 404)
def test_unauthorized(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.save() self.user.subscribe(feed) #Create another feed that the user isn't subscribed to. unused_feed = Feed() unused_feed.link = 'http://xkcd.com/atom.xml' unused_feed.site = 'http://xkcd.com/' unused_feed.save() unused_feed.update() feeditem_id = unused_feed.items.all()[0].pk response = self.client.get( '/api/0.9/item/{0}'.format(feeditem_id), follow=True) self.assertEqual(response.status_code, 404)
def test_single_resource(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.site = 'http://www.paulhummer.org/' feed.save() feed.update() self.user.subscribe(feed) #Create another feed that the user isn't subscribed to. unused_feed = Feed() unused_feed.link = 'http://www.chizang.net/alex/blog/feed/' unused_feed.site = 'http://www.chizang.net/alex/blog/' unused_feed.save() unused_feed.update() response = self.client.get('/api/0.9/item/') self.assertEqual(response.status_code, 200) content = json.loads(response.content) objects = content['objects'] resource = objects[0] response = self.client.get(resource['resource_uri']) self.assertEqual(response.status_code, 200) content = json.loads(response.content) resource_id = resource['resource_uri'].split('/')[-2] item = FeedItem.objects.get(pk=resource_id) useritem = UserFeedItem.objects.get(user=self.user, item=item) self.assertEqual(resource['description'], item.description) self.assertEqual(resource['link'], item.link) self.assertEqual(resource['read'], useritem.read) self.assertEqual(resource['title'], item.title) feed_id = int(resource['feed']['id']) self.assertEqual(feed_id, item.feed.pk) self.assertEqual( sorted(resource.keys()), [u'description', u'feed', u'id', u'link', u'published', u'read', u'resource_uri', u'title'])
def test_feeds_but_no_subscriptions(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.save() response = self.client.get('/api/0.9/feed/') content = json.loads(response.content) objects = content['objects'] self.assertEqual(len(objects), 0)
def test_duplicates(self): """Ensure that we can't create duplicate feeds using create_and_subscribe()""" user = User() user.email = "Bob" user.save() feed = Feed() feed.title = "BoingBoing" feed.link = "http://boingboing.net/atom.xml" feed.site = "http://boingboing.net" f = Feed.create_and_subscribe(feed.title, feed.link, feed.site, user) feed2 = Feed() feed2.title = "BoingBoing" feed2.link = "http://boingboing.net/atom.xml" feed2.site = "http://boingboing.net" # XXX: TODO: we need to add/test duplicate checks save() too :( f2 = Feed.create_and_subscribe(feed2.title, feed2.link, feed2.site, user) self.assertEqual(f.pk, f2.pk)
def test_tagging(self): bob = User() bob.email = "Bob" bob.save() feed = Feed() feed.title = "Some Political Bullshit" feed.link = "http://bs.com/rss" feed.site = "http://bs.com" feed.save() feed.add_subscriber(bob) other_feed = Feed() other_feed.title = "Mom's recipe blog" other_feed.link = "http://yourmom.com/rss" other_feed.site = "http://yourmom.com" other_feed.save() other_feed.add_subscriber(bob) userfeed = UserFeed.objects.get(user=bob, feed=feed) userfeed.tags.add("politics", "mom") userfeed2 = UserFeed.objects.get(user=bob, feed=other_feed) userfeed2.tags.add("mom", "food") self.assertIn("mom", [tag.name for tag in userfeed.tags.all()]) self.assertIn("politics", [tag.name for tag in userfeed.tags.all()]) self.assertNotIn("food", [tag.name for tag in userfeed.tags.all()]) tagged = UserFeed.objects.filter(tags__name__in=["mom"]) self.assertEquals(len(tagged), 2) userfeed.tags.set("test") self.assertEquals(len(userfeed.tags.all()), 1) self.assertNotIn("mom", [tag.name for tag in userfeed.tags.all()]) # API claims we can do this safely without raising an exception userfeed.tags.remove("mom") userfeed.tags.clear() self.assertEquals(len(userfeed.tags.all()), 0)
def test_basics(self): bob = User() bob.email = "Bob" bob.save() steve = User() steve.email = "Steve" steve.save() feed = Feed() feed.title = "Some Political Bullshit" feed.link = "http://bs.com/rss" feed.site = "http://bs.com" feed.save() other_feed = Feed() other_feed.title = "Mom's recipe blog" other_feed.link = "http://yourmom.com/rss" other_feed.site = "http://yourmom.com" other_feed.save() user_feed = UserFeed() user_feed.user = bob user_feed.feed = feed user_feed.save() user_feed2 = UserFeed() user_feed2.user = steve user_feed2.feed = feed user_feed2.save() user_feed3 = UserFeed() user_feed3.user = steve user_feed3.feed = other_feed user_feed3.save() self.assertEqual(feed.subscribers.count(), 2) self.assertEqual(other_feed.subscribers.count(), 1) feeds_for_steve = UserFeed.objects.filter(user=steve) self.assertEqual(len(feeds_for_steve), 2)
def test_tagging(self): user = User() user.email = "Bob" user.save() feed = Feed() feed.title = "BoingBoing" feed.link = "http://boingboing.net" feed.save() feed.add_subscriber(user) item = FeedItem() item.title = "Octopus v. Platypus" item.description = "A fight to the death." item.link = item.guid = "http://www.example.com/rss/post" item.published = datetime.now() item.feed = feed item.save() item2 = FeedItem() item2.title = "Cute bunny rabbit video" item2.description = "They die at the end." item2.link = item.guid = "http://www.example.com/rss/post" item2.published = datetime.now() item2.feed = feed item2.save() userfeeditem = UserFeedItem.objects.get(user=user, item=item) userfeeditem.tags.add("cute", "platypus") userfeeditem2 = UserFeedItem.objects.get(user=user, item=item2) userfeeditem2.tags.add("bunny", "cute") self.assertIn("cute", [tag.name for tag in userfeeditem.tags.all()]) self.assertIn("platypus", [tag.name for tag in userfeeditem.tags.all()]) self.assertNotIn("bunny", [tag.name for tag in userfeeditem.tags.all()]) tagged = UserFeedItem.objects.filter(tags__name__in=["cute"]) self.assertEquals(len(tagged), 2) userfeeditem.tags.set("test") self.assertEquals(len(userfeeditem.tags.all()), 1) self.assertNotIn("cute", [tag.name for tag in userfeeditem.tags.all()]) # API claims we can do this safely without raising an exception userfeeditem.tags.remove("cute") userfeeditem.tags.clear() self.assertEquals(len(userfeeditem.tags.all()), 0)
def test_for_user(self): """Test FeedItemManager.for_user.""" user = User() user.email = "*****@*****.**" user.save() feed = Feed() feed.link = "http://paulhummer.org/rss" feed.site = "http://paulhummer.org/" feed.save() user.subscribe(feed) other_feed = Feed() other_feed.link = "http://www.chizang.net/alex/blog/feed/" other_feed.site = "http://www.chizang.net/alex/blog/" other_feed.save() userfeeditems = FeedItem.objects.for_user(user) self.assertEqual(userfeeditems.count(), feed.items.count()) other_feed.add_subscriber(user) userfeeditems = FeedItem.objects.for_user(user) self.assertEqual(userfeeditems.count(), feed.items.count() + other_feed.items.count())
def test_run(self): feed = Feed() feed.link = 'http://paulhummer.org/rss' last_fetched = datetime.now() - timedelta(minutes=31) feed.last_fetched = last_fetched feed.save() task = tasks.UpdateFeedTask() result = task.delay() self.assertTrue(result.successful()) # Re-fetch the feed feed = Feed.objects.get(link='http://paulhummer.org/rss') self.assertNotEqual(feed.last_fetched, last_fetched) self.assertEqual(feed.items.count(), 20)
def test_update(self): user = User() user.email = "Bob" user.save() feed = Feed() feed.link = "http://paulhummer.org/rss" feed.save() feed.update() # Re-fetch the feed feed = Feed.objects.get(pk=feed.pk) self.assertEqual(feed.items.count(), 20) self.assertEqual(feed.title, "Dapper as...") self.assertTrue(feed.description.startswith("Bike rider"))
def test_unsubscribe(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.save() feed.add_subscriber(self.user) #Ensure the user is subscribed. response = self.api_client.get('/api/0.9/feed/') content = json.loads(response.content) self.assertEqual(len(content['objects']), 1) response = self.api_client.delete( content['objects'][0]['resource_uri']) self.assertEqual(response.status_code, 204) #Ensure the feed is now deleted. response = self.api_client.get('/api/0.9/feed/') content = json.loads(response.content) self.assertEqual(len(content['objects']), 0)
def test_single_resource_list(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.save() feed.add_subscriber(self.user) response = self.client.get('/api/0.9/feed/') content = json.loads(response.content) objects = content['objects'] self.assertEqual(len(objects), 1) feed = Feed.objects.get(pk=feed.pk) resource = objects[0] self.assertEqual(resource['description'], feed.description) self.assertEqual(resource['title'], feed.title) self.assertEqual(resource['link'], feed.link) self.assertEqual( sorted(resource.keys()), [u'description', u'id', u'items', u'link', u'resource_uri', u'title'])
def test_add_subscriber(self): user = User() user.email = "Bob" user.save() feed = Feed() feed.title = "BoingBoing" feed.link = "http://boingboing.net" feed.save() item = FeedItem() item.title = "Octopus v. Platypus" item.description = "A fight to the death." item.link = item.guid = "http://www.example.com/rss/post" item.published = datetime.now() item.feed = feed item.save() # Note carefully... we can safely call add_subscriber at any # point after User and Feed creation and be confident that we'll # never create duplicate UserFeedItem join table entries. # # All existing items *before* add_subscriber are added to user # during add_subscriber time # # All new items *after* subscription are added to user during # FeedItem post_save() signal feed.add_subscriber(user) item2 = FeedItem() item2.title = "Cute bunny rabbit video" item2.description = "They die at the end." item2.link = item.guid = "http://www.example.com/rss/post" item2.published = datetime.now() item2.feed = feed item2.save() self.assertEqual(feed.subscribers.count(), 1) self.assertEqual(user.feeditems.count(), 2)
def test_basics(self): user = User() user.email = "Bob" user.save() feed = Feed() feed.title = "BoingBoing" feed.link = "http://boingboing.net" feed.save() feed.add_subscriber(user) item = FeedItem() item.title = "Octopus v. Platypus" item.description = "A fight to the death." item.link = item.guid = "http://www.example.com/rss/post" item.published = datetime.now() item.feed = feed item.save() # Saving an item in a feed should automatically result in # subscribed users seeing all those new items. self.assertEqual(user.feeditems.count(), 1)
def test_user_subscribe(self): '''Test the syntactic sugar monkeypatch for User.subscribe.''' user = User() user.email = 'Bob' user.save() feed = Feed() feed.title = 'BoingBoing' feed.link = 'http://boingboing.net' feed.save() unused = Feed() unused.title = 'xkcd' unused.save() item = FeedItem() item.title = 'Octopus v. Platypus' item.description = 'A fight to the death.' item.link = item.guid = 'http://www.example.com/rss/post' item.published = datetime.now() item.feed = feed item.save() item2 = FeedItem() item2.title = 'Cute bunny rabbit video' item2.description = 'They die at the end.' item2.link = item.guid = 'http://www.example.com/rss/post' item2.published = datetime.now() item2.feed = feed item2.save() user.subscribe(feed) self.assertEqual(user.feeds.count(), 1) self.assertEqual(user.feeditems.count(), 2) self.assertEqual(user.feeds[0].title, feed.title) # Testing we are using order_by() in User.feeditems() monkeypatch self.assertEqual(user.feeditems[0].title, item.title)
def test_single_resource_list(self): feed = Feed() feed.link = 'http://www.paulhummer.org/rss' feed.site = 'http://www.paulhummer.org/' feed.save() feed.update() self.user.subscribe(feed) response = self.client.get('/api/0.9/item/') self.assertEqual(response.status_code, 200) content = json.loads(response.content) objects = content['objects'] self.assertEqual(len(objects), 20) resource = objects[0] resource_id = resource['resource_uri'].split('/')[-2] item = FeedItem.objects.get(pk=resource_id) self.assertEqual(resource['description'], item.description) self.assertEqual(resource['title'], item.title) self.assertEqual(resource['link'], item.link)