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_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_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_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_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_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_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_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_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())