def test_simple_xml(self, mock): self._populate_mock(mock, status=200, test_file="rss_xhtml_body.xml", content_type="application/rss+xml") ls = timezone.now() src = Source(name="test1", feed_url=BASE_URL, interval=0, last_success=ls, last_change=ls) src.save() # Read the feed once to get the 1 post and the etag read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) self.assertEqual(src.posts.count(), 1) # got the one post self.assertEqual(src.interval, 60) self.assertEqual(src.etag, "an-etag") self.assertNotEqual(src.last_success, ls) self.assertNotEqual(src.last_change, ls)
def test_perm_redirect(self, mock): new_url = "http://new.feed.com/" self._populate_mock(mock, status=301, test_file="empty_file.txt", content_type="text/plain", headers={"Location": new_url}) self._populate_mock(mock, status=200, test_file="rss_xhtml_body.xml", content_type="application/xml+rss", url=new_url) src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 301) self.assertEqual(src.interval, 60) self.assertEqual(src.feed_url, new_url) read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) self.assertEqual(src.posts.count(), 1) self.assertEqual(src.interval, 60) self.assertTrue(src.live)
def test_etags(self, mock): self._populate_mock(mock, status=200, test_file="rss_xhtml_body.xml", content_type="application/xml+rss") self._populate_mock(mock, status=304, test_file="empty_file.txt", content_type="application/xml+rss", etag="an-etag") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() # Read the feed once to get the 1 post and the etag read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) self.assertEqual(src.posts.count(), 1) # got the one post self.assertEqual(src.interval, 60) self.assertEqual(src.etag, "an-etag") # Read the feed again to get a 304 and a small increment to the interval read_feed(src) src.refresh_from_db() self.assertEqual(src.posts.count(), 1) # should have no more self.assertEqual(src.status_code, 304) self.assertEqual(src.interval, 70) self.assertTrue(src.live)
def test_fucking_cloudflare(self, mock): self._populate_mock(mock, status=200, test_file="json_simple_two_entry.json", content_type="application/json") self._populate_mock(mock, status=403, test_file="json_simple_two_entry.json", content_type="application/json", is_cloudflare=True) src = Source(name="test1", feed_url=BASE_URL, interval=0, is_cloudflare=False) src.save() # Read the feed once to get the 1 post and the etag read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 403) src = Source(name="test1", feed_url=BASE_URL, interval=0, is_cloudflare=True) src.save() # Read the feed once to get the 1 post and the etag read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200)
def test_server_error_2(self, mock): self._populate_mock(mock, status=503, test_file="empty_file.txt", content_type="text/plain") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 503) # error! self.assertEqual(src.posts.count(), 0) # can't have got any self.assertTrue(src.live) self.assertEqual(src.interval, 120)
def test_feed_gone(self, mock): self._populate_mock(mock, status=410, test_file="empty_file.txt", content_type="text/plain") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 410) # it returned a page, but not a feed self.assertEqual(src.posts.count(), 0) # can't have got any self.assertFalse(src.live)
def test_sanitize_1(self, mock): self._populate_mock(mock, status=200, test_file="json_simple_two_entry.json", content_type="application/json") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() # Read the feed once to get the 1 post and the etag read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) p = src.posts.all()[0] self.assertFalse("<script>" in p.body)
def test_podcast(self, mock): self._populate_mock(mock, status=200, test_file="podcast.json", content_type="application/json") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() # read the feed to update the name read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) post = src.posts.all()[0] self.assertEqual(post.enclosures.count(), 1)
def test_sanitize_2(self, mock): """ Another test that the sanitization is going on. This time we have stolen a test case from the feedparser libarary """ self._populate_mock(mock, status=200, test_file="sanitizer_bad_comment.json", content_type="application/json") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() # read the feed to update the name read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) self.assertEqual(src.name, "safe")
def test_podcast(self, mock): self._populate_mock(mock, status=200, test_file="podcast.xml", content_type="application/rss+xml") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() # Read the feed once to get the 1 post and the etag read_feed(src) src.refresh_from_db() self.assertEqual( src.description, 'SU: Three nerds discussing tech, Apple, programming, and loosely related matters.' ) self.assertEqual(src.posts.all()[0].enclosures.count(), 1)
def test_sanitize_attrs(self, mock): self._populate_mock(mock, status=200, test_file="sanitizer_img_attrs.xml", content_type="application/rss+xml") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() # read the feed to update the name read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) body = src.posts.all()[0].body self.assertTrue("<img" in body) self.assertFalse("align=" in body) self.assertFalse("hspace=" in body)
def test_sanitize_1(self, mock): """ Make sure feedparser's sanitization is running """ self._populate_mock(mock, status=200, test_file="rss_xhtml_body.xml", content_type="application/rss+xml") src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() # Read the feed once to get the 1 post and the etag read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) p = src.posts.all()[0] self.assertFalse("<script>" in p.body)
def test_permission_denied(self, mock): self._populate_mock(mock, status=403, test_file="empty_file.txt", content_type="text/plain") ls = timezone.now() src = Source(name="test1", feed_url=BASE_URL, interval=0, last_success=ls) src.save() read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 403) # it returned a page, but not a feed self.assertEqual(src.posts.count(), 0) # can't have got any self.assertFalse(src.live)
def test_temp_redirect(self, mock): new_url = "http://new.feed.com/" self._populate_mock(mock, status=302, test_file="empty_file.txt", content_type="text/plain", headers={"Location": new_url}) self._populate_mock(mock, status=200, test_file="rss_xhtml_body.xml", content_type="application/xml+rss", url=new_url) src = Source(name="test1", feed_url=BASE_URL, interval=0) src.save() self.assertIsNone(src.last_302_start) read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) self.assertEqual(src.last_302_url, new_url) # this is where went self.assertIsNotNone(src.last_302_start) self.assertEqual(src.posts.count(), 1) # after following redirect will have 1 post self.assertEqual(src.interval, 60) self.assertTrue(src.live) # do it all again - shouldn't change read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) # it returned a page, but not a feed self.assertEqual(src.last_302_url, new_url) # this is where went self.assertIsNotNone(src.last_302_start) self.assertEqual(src.posts.count(), 1) # after following redirect will have 1 post self.assertEqual(src.interval, 80) self.assertTrue(src.live) # now we test making it permaent src.last_302_start = timezone.now() - timedelta(days=365) src.save() read_feed(src) src.refresh_from_db() self.assertEqual(src.status_code, 200) self.assertEqual(src.last_302_url, ' ') self.assertIsNone(src.last_302_start) self.assertEqual(src.posts.count(), 1) self.assertEqual(src.interval, 100) self.assertEqual(src.feed_url, new_url) self.assertTrue(src.live)