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_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_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_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_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_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 handle(self, *args, **options): Feed.update_feeds(100)