import unittest from unittest.mock import patch import web_view def hit(title: str) -> web_view.Hit: return { "ns": 0, "title": title, "pageid": 1, "size": 1, "wordcount": 1, "snippet": "", "timestamp": "", } class ArticlePageSkipTests(unittest.TestCase): def setUp(self) -> None: web_view.app.config["TESTING"] = True self.client = web_view.app.test_client() def test_session_skips_are_left_out_of_candidates(self) -> None: hits = [ hit("Mass racial violence in the United States"), hit("National Register of Historic Places listings in Brookhaven (town), New York"), hit("Useful candidate"), ] with self.client.session_transaction() as session: session["skipped"] = { "African-American neighborhood": [ "Mass racial violence in the United States", "National Register of Historic Places listings in Brookhaven (town), New York", ] } with ( patch("web_view.api.get_wiki_info", return_value=None), patch("web_view.search_count", return_value=3), patch("web_view.search_count_with_link", return_value=0), patch("web_view.search_no_link", return_value=(3, hits)), ): response = self.client.get("/link/African-American_neighborhood") body = response.get_data(as_text=True) self.assertEqual(response.status_code, 200) self.assertNotIn( 'data-title="Mass racial violence in the United States"', body ) self.assertNotIn( 'data-title="National Register of Historic Places listings in Brookhaven (town), New York"', body, ) self.assertIn("Useful candidate", body) def test_redirect_target_is_left_out_of_candidates(self) -> None: hits = [ hit("Documentary film"), hit("Useful candidate"), ] with ( patch("web_view.api.get_wiki_info", return_value="Documentary film"), patch("web_view.search_count", return_value=2), patch("web_view.search_count_with_link", return_value=0), patch("web_view.search_no_link", return_value=(2, hits)), ): response = self.client.get("/link/DVD_documentary") body = response.get_data(as_text=True) self.assertEqual(response.status_code, 200) self.assertNotIn('data-title="Documentary film"', body) self.assertIn("Useful candidate", body) def test_redirect_target_case_variant_is_left_out_of_candidates(self) -> None: hits = [hit("documentary film")] with ( patch("web_view.api.get_wiki_info", return_value="Documentary film"), patch("web_view.search_count", return_value=1), patch("web_view.search_count_with_link", return_value=0), patch("web_view.search_no_link", return_value=(1, hits)), ): response = self.client.get("/link/DVD_documentary") body = response.get_data(as_text=True) self.assertEqual(response.status_code, 200) self.assertIn("No more candidates found for this article.", body) class ValidHitTests(unittest.TestCase): def setUp(self) -> None: web_view.app.config["TESTING"] = True self.client = web_view.app.test_client() def test_redirect_target_is_not_valid_hit(self) -> None: with patch("web_view.get_diff") as get_diff: response = self.client.get( "/api/1/valid_hit", query_string={ "link_to": "DVD documentary", "link_from": "Documentary film", "redirect_to": "Documentary film", }, ) self.assertEqual(response.status_code, 200) self.assertEqual(response.get_json(), {"valid": False}) get_diff.assert_not_called() class ProtectedCandidateTests(unittest.TestCase): def test_edit_protected_hits_are_filtered_out(self) -> None: hits = [hit("Editable"), hit("Protected")] hits[0]["pageid"] = 10 hits[1]["pageid"] = 20 with patch( "web_view.get_page_info_by_id", return_value={ 10: {"pageid": 10, "title": "Editable", "protection": []}, 20: { "pageid": 20, "title": "Protected", "protection": [ {"type": "edit", "level": "sysop", "expiry": "infinity"} ], }, }, ): filtered = web_view.filter_edit_protected_hits(hits) self.assertEqual([item["title"] for item in filtered], ["Editable"]) def test_search_no_link_filters_edit_protected_results(self) -> None: hits = [hit("Editable"), hit("Protected")] hits[0]["pageid"] = 10 hits[1]["pageid"] = 20 with ( patch( "web_view.run_search", return_value={"searchinfo": {"totalhits": 2}, "search": hits}, ), patch( "web_view.get_page_info_by_id", return_value={ 10: {"pageid": 10, "title": "Editable", "protection": []}, 20: { "pageid": 20, "title": "Protected", "protection": [ {"type": "edit", "level": "sysop", "expiry": "infinity"} ], }, }, ), ): totalhits, filtered = web_view.search_no_link("Target") self.assertEqual(totalhits, 2) self.assertEqual([item["title"] for item in filtered], ["Editable"]) if __name__ == "__main__": unittest.main()