def test_updates_feeds_no_summary_no_content(self): # test update_feeds where entry has no "summary" field and uses "content" instead # test update_feeds, ensure feed's entries are added correctly f, u = self._test_subscribe_setup() f.subscribe(u) time_hack_formatted = http_date(now().timestamp()) with requests_mock.Mocker() as mock: mock.get(f.feed_url, text=""" <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title> <link href="http://example.org/"/> <updated>{0}</updated> <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link href="http://example.org/2003/12/13/atom03"/> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>{0}</updated> <author> <name>John Doe</name> <email>[email protected]</email </author> </entry> </feed> """.format(time_hack_formatted), status_code=200, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds() e = f.entry_set.first() self.assertEqual('No summary.', e.content)
def test_get_feed_not_owned_by_user_fails(self): user2 = User(username="******", email="*****@*****.**", password="******") user2.save() new_feed = Feed(title="another feed", url="http://anotherfeed.com", user=user2) new_feed.save() response = self.client.get("/api/feeds/{0}/".format(new_feed.id), format='json') self.assertEqual(response.status_code, 403)
def test_refreshing_others_feed(self): user2 = User(username="******", password="******") user2.save() new_feed = Feed(title="another feed", url="http://anotherfeed.com", user=user2) new_feed.save() response = self.client.post("/api/feeds/{0}/refresh/".format(new_feed.id),format="json") self.assertEqual(response.status_code, 403)
def test_view_ajax_updateFeed(self): """ Update partial feed """ httpd = FeedParserTestServer(1) httpd.start() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_2.rss' tempRss = Feed(name="Temp", url=rssUrl) tempRss.save() response = self.client.get('/updateFeed/1/', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(len(response.context['FeedList']), 1)
def test_clean_fields_raises_validation_error_with_invalid_feed_url(self): feed = Feed(title="google", url="http://google.com") validation_error = False try: feed.clean_fields() except ValidationError: validation_error = True self.assertTrue(validation_error)
def test_read_functions(self): """ check read functions for an entry """ httpd = FeedParserTestServer(1) httpd.start() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_2.rss' tempRss = Feed(name="Temp", url=rssUrl) tempRss.fetch() self.assertEqual(tempRss.unread(), 1) entry = FeedEntry.objects.all()[0] entry.markRead() self.assertEqual(tempRss.unread(), 0)
def test_view_ajax_updateFeed(self): """ Update partial feed """ httpd = FeedParserTestServer(1) httpd.start() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_2.rss' tempRss = Feed(name="Temp", url=rssUrl) tempRss.save() response = self.client.get('/updateFeed/1/', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(len(response.context['FeedList']), 1) #print response.content
def test_update_feeds_num_feeds(self): # test update feeds with only 1 feed requested, should update the last feed added to the DB only f, u = self._test_subscribe_setup() f = Feed.objects.create(feed_url='http://example.com/feed/') f.subscribe(u) with requests_mock.Mocker() as mock: mock.get(f.feed_url, text="", status_code=304, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds(1) self.assertEqual(1, Feed.objects.exclude(last_checked=None).count())
def test_update_feeds_HTTP_error(self): # test update_feeds, ensure HTTP error is logged, increments # feed error count f, u = self._test_subscribe_setup() f.subscribe(u) with requests_mock.Mocker() as mock: mock.get(f.feed_url, text="", status_code=500, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds() f = Feed.objects.get(pk=1) self.assertEqual(1, f.error_count)
def test_view_ajax_mark_read(self): """ Test view for mark server """ httpd = FeedParserTestServer(1) httpd.start() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_2.rss' tempRss = Feed(name="Temp", url=rssUrl) tempRss.fetch() response = self.client.get('/markRead/1/', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) self.assertEqual(tempRss.unread(), 0) # bad request response = self.client.get('/markRead/10/', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 500)
def setUp(self): self.url = '/api/feeds/' # create user self.user = User(username="******", email="*****@*****.**", password="******") self.user.save() # Sample feed self.feed = Feed(title="Ars Technica", url="http://arstechnica.com/rss", user=self.user) self.feed.save() # setup request client self.client = APIClient() self.client.force_authenticate(user=self.user, token=self.user.auth_token) self.list_view = FeedViewSet.as_view({ 'get': 'list', 'post': 'create' }) self.detail_view = FeedViewSet.as_view({ 'put': 'update', 'delete': 'destroy', 'get': 'retrieve' })
def test_double_update(self): """Check for no changes to model when rss is not changed""" httpd = FeedParserTestServer(2) httpd.start() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_1.rss' tempRss = Feed(name="Craig's List Free", url=rssUrl) newEntry = tempRss.fetch() tempFeedList = FeedEntry.objects.all() self.assertEqual(len(tempFeedList), 100) self.assertEqual(len(newEntry), 100) tempFeedList = FeedEntry.objects.get(link='http://columbus.craigslist.org/zip/1326047327.html') self.assertEqual(tempFeedList.title, 'at the curb: dresser and small table (Royal Dornoch Cir. Delaware 43015)') newEntry = tempRss.fetch() tempFeedList = FeedEntry.objects.all() self.assertEqual(len(tempFeedList), 100) tempFeedList = FeedEntry.objects.get(link='http://columbus.craigslist.org/zip/1326047327.html') self.assertEqual(len(newEntry), 0)
def test_update_feeds_no_new_entries(self): f, u = self._test_subscribe_setup() f.subscribe(u) time_hack = now() time_hack_formatted = http_date(time_hack.timestamp()) Entry.objects.create( feed=f, entry_id='urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', link='http://example.org/2003/12/13/atom03', title='Atom-Powered Robots Run Amok', content='Some text.', updated=time_hack, published=time_hack) prev_date = now() - timedelta(days=1) time_hack_formatted2 = http_date(prev_date.timestamp()) with requests_mock.Mocker() as mock: mock.get(f.feed_url, text=""" <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title> <link href="http://example.org/"/> <updated>{0}</updated> <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link href="http://example.org/2003/12/13/atom03"/> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>{1}</updated> <summary>Some text.</summary> <author> <name>John Doe</name> <email>[email protected]</email </author> </entry> </feed> """.format(time_hack_formatted, time_hack_formatted2), status_code=200, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds() self.assertEqual(1, f.entry_set.count())
def test_changed_update(self): """Update and check for new entries""" httpd = FeedParserTestServer(2) httpd.start() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_1.rss' tempRss = Feed(name="Craig's List Free", url=rssUrl) tempRss.fetch() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_1_update.rss' tempRss = Feed(url=rssUrl) tempRss.save() newEntry = tempRss.fetch() tempFeedList = FeedEntry.objects.all() self.assertEqual(len(tempFeedList), 105) self.assertEqual(len(newEntry), 5)
def test_get_feeds_from_url_existing(self): # test get_feeds_from_url from existing feed, should return feed w/out # requesting feed = Feed.objects.create(site_url='http://example.com', feed_url='http://example.com/feed/') self.assertListEqual( Feed.get_feeds_from_url('http://example.com/feed/'), [ feed, ])
def test_simpleBufferObject_purge(self): buffer = SimpleBufferObject(Feed, 2) f = Feed(title='feed1', feed_url='http://example.com/feed1') buffer.add(f) before_count = Feed.objects.count() buffer.purge() self.assertEqual(Feed.objects.count(), before_count + 1) self.assertEqual(buffer.count, 0) self.assertEqual(buffer.buffer, list())
def test_update_feeds_conditional_get(self): # test update_feeds, ensure conditional GET is used when available time_hack = now() f, u = self._test_subscribe_setup() f.subscribe(u) f.etag = 'test' f.last_modified = time_hack f.save() with requests_mock.Mocker() as mock: mock.get(f.feed_url, text='', status_code=304, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds() history = mock.request_history[0] self.assertEqual('test', history.headers.get('If-None-Match', None)) self.assertEqual(http_date(time_hack.timestamp()), history.headers.get('If-Modified-Since', None))
def test_update_feeds_changed_url_new(self): # test update_feeds, ensures redirect to URL of new feed causes updating feed's URL to be updated f, u = self._test_subscribe_setup() f.subscribe(u) with requests_mock.Mocker() as mock: mock.get('http://example.com/feed22/', text='', status_code=200, headers={'content_type': 'application/atom+xml'}) mock.get(f.feed_url, text='', status_code=301, headers={ 'location': 'http://example.com/feed22/', 'content-type': 'text/plain' }) Feed.update_feeds() f = Feed.objects.get(pk=1) self.assertEqual('http://example.com/feed22/', f.feed_url)
def test_update_feeds_changed_url(self): # test update_feeds, ensure redirect to URL of existing feed causes updating feed to be disabled test_url = 'http://example.com/feed22/' f, u = self._test_subscribe_setup() f.subscribe(u) with requests_mock.Mocker() as mock: mock.get(f.feed_url, text='', status_code=301, headers={ 'location': test_url, 'content-type': 'text/plain' }) mock.get(test_url, text='', status_code=200, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds() f = Feed.objects.get(id=f.id) self.assertEqual(test_url, f.feed_url)
def test_get_feeds_from_url_from_HTML_page_hit_max(self): # test get_feeds_from_url from HTML page, number of feeds in page should hit default max start = Feed.objects.count() feed_urls = '' for x in range(MAX_FEEDS + 1): feed_urls += '<link rel="alternate" type="application/atom+xml" href="http://example.com/feed%s">' % x with requests_mock.Mocker() as mock: mock.get('http://example.com/', text=""" <!DOCTYPE html> <html> <head> %s </head> <body> </body> </html> """ % feed_urls, status_code=200, headers={'content-type': 'text/html'}) Feed.get_feeds_from_url('http://example.com/') self.assertEqual(Feed.objects.count() - start, MAX_FEEDS)
def test_double_update(self): """Check for no changes to model when rss is not changed""" httpd = FeedParserTestServer(2) httpd.start() rssUrl = 'http://localhost:8097/src/reader/test_rss/test_1.rss' tempRss = Feed(name="Craig's List Free", url=rssUrl) newEntry = tempRss.fetch() tempFeedList = FeedEntry.objects.all() self.assertEqual(len(tempFeedList), 100) self.assertEqual(len(newEntry), 100) tempFeedList = FeedEntry.objects.get( link='http://columbus.craigslist.org/zip/1326047327.html') self.assertEqual( tempFeedList.title, 'at the curb: dresser and small table (Royal Dornoch Cir. Delaware 43015)' ) newEntry = tempRss.fetch() tempFeedList = FeedEntry.objects.all() self.assertEqual(len(tempFeedList), 100) tempFeedList = FeedEntry.objects.get( link='http://columbus.craigslist.org/zip/1326047327.html') self.assertEqual(len(newEntry), 0)
def test_update_feeds_last_checked(self): # test update_feeds, ensure last_checked and next_checked are updated time_hack_high = now() + timedelta(seconds=1) time_hack_low = time_hack_high + timedelta(seconds=-2) f, u = self._test_subscribe_setup() f.subscribe(u) with requests_mock.Mocker() as mock: mock.get('http://example.com/feedtest/', text="", status_code=304, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds() f = Feed.objects.get(pk=1) self.assertLess(time_hack_low, f.last_checked) self.assertGreater(time_hack_high, f.last_checked) self.assertLess(time_hack_low + timedelta(hours=f.check_frequency), f.next_checked) self.assertGreater( time_hack_high + timedelta(hours=f.check_frequency), f.next_checked)
def test_update_feeds_feed_meta(self): # test update_feeds, ensure feed meta data is updated this includes reseting error count and updating feed # title, description f, u = self._test_subscribe_setup() f.subscribe(u) f.increment_error_count() f.save() with requests_mock.Mocker() as mock: mock.get(f.feed_url, text=""" <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title> <link href="http://example.org/"/> <logo>http://example.com/icon.jpg</logo> <subtitle>this is a feed</subtitle> <updated>2003-12-13T18:30:02Z</updated> <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link href="http://example.org/2003/12/13/atom03"/> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text.</summary> </entry> </feed> """, status_code=200, headers={'content_type': 'application/atom+xml'}) Feed.update_feeds() f = Feed.objects.get(pk=1) self.assertEqual(0, f.error_count) self.assertEqual('Example Feed', f.title) self.assertEqual('this is a feed', f.description)
def test_get_feeds_from_url_from_feed(self): # test get_feeds_from_url from feed, should return input URL url = 'http://example.com/feed/' self.assertEqual(Feed.objects.filter(feed_url=url).count(), 0) with requests_mock.Mocker() as mock: mock.get(url, text=""" <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title> <link href="http://example.org/feed/" rel="self" /> <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> <updated>2003-12-13T18:30:02Z</updated> <entry> <title>Atom-Powered Robots Run Amok</title> <link href="http://example.org/2003/12/13/atom03" /> <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/> <link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text.</summary> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> <p>This is the entry content.</p> </div> </content> <author> <name>John Doe</name> <email>[email protected]</email> </author> </entry> </feed> """, status_code=200, headers={'content-type': 'application/atom+xml;weird'}) self.assertListEqual( Feed.get_feeds_from_url('http://example.com/feed/'), [ Feed.objects.get(feed_url=url), ])
def test_get_feeds_from_url_from_HTML_page(self): # test get_feeds_from_url from HTML page, should return rel link for feed url = 'http://example.com/feed/' self.assertEqual(Feed.objects.filter(feed_url=url).count(), 0) with requests_mock.Mocker() as mock: mock.get('http://example.com/', text=""" <!DOCTYPE html> <html> <head> <link rel="alternate" type="application/atom+xml" href="%s"> </head> <body> </body> </html> """ % url, status_code=200, headers={'content-type': 'text/html'}) self.assertListEqual( Feed.get_feeds_from_url('http://example.com/'), [ Feed.objects.get(feed_url=url), ], )
def add_feed(request): user = request.user if not request.POST: ##todo fixme respond correctly return None feed_url = request.POST['url'] try: feed = Feed.objects.get(url=feed_url) except Feed.DoesNotExist: feed = Feed() feed.url = feed_url feed.save() try: category_slug=request.POST['category'] base_category = Category.objects.get(category_slug=category_slug) category = UserCategory.objects.get(category=base_category,user=user) except: default_category = Category.objects.get(category_slug=DEFAULT_CATEGORY_SLUG) category = UserCategory.objects.get(category=default_category,user=user) category.feeds.add(feed) category.save() feed.fetch() return HttpResponseRedirect(reverse('my_entries'))
class FeedViewSetTest(TestCase): # TODO: use local feeds not online def setUp(self): self.url = '/api/feeds/' # create user self.user = User(username="******", email="*****@*****.**", password="******") self.user.save() # Sample feed self.feed = Feed(title="Ars Technica", url="http://arstechnica.com/rss", user=self.user) self.feed.save() # setup request client self.client = APIClient() self.client.force_authenticate(user=self.user, token=self.user.auth_token) self.list_view = FeedViewSet.as_view({ 'get': 'list', 'post': 'create' }) self.detail_view = FeedViewSet.as_view({ 'put': 'update', 'delete': 'destroy', 'get': 'retrieve' }) def test_post_feed_when_authenticated_succeeds(self): response = self.client.post( self.url, { "url": "http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml" }, format='json') self.assertEqual(response.status_code, 201) def test_get_feeds_of_another_user_fails(self): response = self.client.get(self.url, {'user_id': 2}, format='json') self.assertEqual(response.status_code, 403) def test_get_feed_owned_by_user_succeeds(self): response = self.client.get('/api/feeds/1/', format='json') self.assertEqual(response.status_code, 200) def test_get_feed_not_owned_by_user_fails(self): user2 = User(username="******", email="*****@*****.**", password="******") user2.save() new_feed = Feed(title="another feed", url="http://anotherfeed.com", user=user2) new_feed.save() response = self.client.get("/api/feeds/{0}/".format(new_feed.id), format='json') self.assertEqual(response.status_code, 403) def test_refreshing_own_feed(self): #TODO Add better test response = self.client.post("/api/feeds/1/refresh/", format="json") self.assertEqual(response.status_code, 201) def test_refreshing_others_feed(self): user2 = User(username="******", password="******") user2.save() new_feed = Feed(title="another feed", url="http://anotherfeed.com", user=user2) new_feed.save() response = self.client.post("/api/feeds/{0}/refresh/".format(new_feed.id),format="json") self.assertEqual(response.status_code, 403) def test_marking_own_feed_as_read(self): response = self.client.post("/api/feeds/{0}/mark_as_read/".format(self.feed.id), format="json") self.assertEqual(response.status_code, 200)
def test_simpleBufferObject_context(self): before_count = Feed.objects.count() with SimpleBufferObject(Feed, 1) as buffer: f = Feed(title='feed1', feed_url='http://example.com/feed1') buffer.add(f) self.assertEqual(Feed.objects.count(), before_count + 1)
def handle(self, *args, **options): Feed.update_feeds(100)
def test_simpleBufferObject_auto_purge(self): buffer = SimpleBufferObject(Feed, 1) f = Feed(title='feed1', feed_url='http://example.com/feed1') buffer.add(f) self.assertEqual(buffer.count, 0)
def test_get_feeds_from_url_HTTP_error(self): # testing get_feeds_from_url with HTTP error, should return None with requests_mock.Mocker() as mock: mock.get('http://example.com/feed/', text='', status_code=400) self.assertEqual( [], Feed.get_feeds_from_url('http://example.com/feed/'))