def test_opengraph_ignored_if_not_enough_attributes(self, og): og.return_value = {} result = fetch_og_preview(self.content, self.urls) self.assertFalse(result) og.return_value = {"foo": "bar"} result = fetch_og_preview(self.content, self.urls) self.assertFalse(result)
def test_if_cached_already_dont_fetch(self): with freeze_time(datetime.date.today() - datetime.timedelta(days=3)): opengraph = OpenGraphCacheFactory(url=self.urls[0]) result = fetch_og_preview(self.content, self.urls) self.assertTrue(result) self.content.refresh_from_db() self.assertEqual(self.content.opengraph, opengraph)
def test_opengraph_cache_created_without_image_for_nsfw_content(self, og): og.return_value = MockOpenGraph({"title": "foo", "image": "https://domain.tld/pic.png"}) opengraph = fetch_og_preview(self.nsfw_content, self.urls) self.assertEqual(opengraph.title, "foo") self.assertEqual(opengraph.description, "") self.assertEqual(opengraph.image, "") self.assertEqual(opengraph.url, self.urls[0])
def test_opengraph_cache_created(self, og): og.return_value = MockOpenGraph({"title": "foo"}) opengraph = fetch_og_preview(self.content, self.urls) self.assertEqual(opengraph.title, "foo") self.assertEqual(opengraph.description, "") self.assertEqual(opengraph.image, "") self.assertEqual(opengraph.url, self.urls[0])
def test_opengraph_fetch_called(self, og): fetch_og_preview(self.content, self.urls) og.assert_called_once_with(url=self.urls[0], parser="lxml")
def test_if_cached_already_but_older_than_7_days_then_fetch(self, og): with freeze_time(datetime.date.today() - datetime.timedelta(days=8)): OpenGraphCacheFactory(url=self.urls[0]) fetch_og_preview(self.content, self.urls) og.assert_called_once_with(url=self.urls[0], parser="lxml")
def test_if_cached_already_dont_fetch(self): opengraph = OpenGraphCacheFactory(url=self.urls[0]) result = fetch_og_preview(self.content, self.urls) self.assertTrue(result) self.content.refresh_from_db() self.assertEqual(self.content.opengraph, opengraph)
def test_opengraph_connection_error_is_passed(self, og): result = fetch_og_preview(self.content, self.urls) self.assertFalse(result)
def test_opengraph_data_error_is_passed(self, og, create): og.return_value = MockOpenGraph({"title": "foo"}) result = fetch_og_preview(self.content, self.urls) self.assertFalse(result)
def test_opengraph_integrity_error_updates_with_existing_object(self, og, filter): opengraph = OpenGraphCacheFactory(url=self.urls[0]) og.return_value = MockOpenGraph({"title": "foo"}) result = fetch_og_preview(self.content, self.urls) self.assertEqual(opengraph, result)
def test_opengraph_integrity_error_updates_with_existing_object( self, og, filter): opengraph = OpenGraphCacheFactory(url=self.urls[0]) og.return_value = MockOpenGraph({"title": "foo"}) result = fetch_og_preview(self.content, self.urls) self.assertEqual(opengraph, result)
def test_opengraph_data_cleared_before_fetching(self, og_parse): OpenGraph.__data__ = {"foo": "bar"} fetch_og_preview(self.content, self.urls) self.assertEqual(OpenGraph.__data__, {})