def test_is_valid_token(self, mocked_session): mediawiki = MediaWikiService() mocked_session.return_value.post.return_value.json.return_value = { "checktoken": { "result": "valid" } } token = "token example" post_json_data = {"token": token} post_page_params = { "action": "checktoken", "maxlag": "5", "type": "csrf", "format": "json", } is_valid_token = mediawiki.is_valid_token(token) mocked_session.return_value.post.assert_called_with( url="https://your-wiki.org/api.php", params=post_page_params, data=post_json_data, ) mocked_session.return_value.post.return_value.json.assert_called_with() self.assertTrue(is_valid_token)
def test_edit_page(self, mocked_session): mocked_session.return_value.post.return_value.json.return_value = { "edit": { "result": "Success" } } page_title = "page title example" post_page_params = { "action": "edit", "maxlag": "5", "title": page_title, "nocreate": "true", "contentmodel": "wikitext", "bot": "true", "format": "json", } token = "token example" page_text = "page text example" post_json_data = {"token": token, "text": page_text} mediawiki = MediaWikiService() mediawiki.edit_page(token, page_title, page_text) mocked_session.return_value.post.assert_called_with( url="https://your-wiki.org/api.php", params=post_page_params, data=post_json_data, ) mocked_session.return_value.post.return_value.json.assert_called_with()
def wikitext_to_dict(self, page_title: str): mediawiki = MediaWikiService() text = mediawiki.get_page_text(page_title) redirect_page = MediaWikiService().is_redirect_page(text) if redirect_page: raise ValueError( f"Error getting text from the page '{page_title}'." f" Page was moved from '{page_title}' to '{redirect_page}'") else: sections = WikiSectionService().get_sections(text) page_sections_dict = self.generate_sections_dict(sections) if not page_sections_dict: raise ValueError(f"Error parsing page '{page_title}' to dict") else: return page_sections_dict
def test_move_page(self, mocked_session): mocked_session.return_value.post.return_value.json.return_value = { "move": { "from": "Organised Editing/Activities/TM3", "to": "Organised Editing/Activities/TM4", } } old_page_title = "old page" new_page_title = "new page" post_page_params = { "action": "move", "maxlag": "5", "from": old_page_title, "to": new_page_title, "movetalk": "true", "format": "json", } token = "token example" post_json_data = {"token": token} MediaWikiService().move_page(token, old_page_title, new_page_title) mocked_session.return_value.post.assert_called_with( url="https://your-wiki.org/api.php", params=post_page_params, data=post_json_data, ) mocked_session.return_value.post.return_value.json.assert_called_with()
def edit_page(self, document_data: dict, update_fields: dict, overview_page_data: dict): mediawiki = MediaWikiService() token = mediawiki.get_token() updated_text = self.edit_page_text(update_fields, overview_page_data, document_data) mediawiki.edit_page(token, self.templates.oeg_page, updated_text)
def test_get_page_text_fails_without_existing_page(self, mocked_session): mediawiki = MediaWikiService() page_title = "non existing page" mocked_session.return_value.get.return_value.json.return_value = { "error": { "code": "missingtitle" } } with self.assertRaises(MediaWikiServiceError): mediawiki.get_page_text(page_title)
def create_page(self, document_data: dict) -> None: """ Creates a wiki page Keyword arguments: document_data -- All required data for a project using Organised Editing Guidelines """ mediawiki = MediaWikiService() wikitext = WikiTextService() token = mediawiki.get_token() page_title = self.templates.oeg_page overview_page_sections = self.document_to_page_sections(document_data) sections_text = wikitext.generate_text_from_dict( self.templates.page_template, f"=={self.templates.page_initial_section}==", overview_page_sections, ) updated_text = WikiTableService().add_table_row( page_text=sections_text, new_row=self.generate_activities_list_table_row(document_data), table_section_title=self.templates.activities_list_section_title, table_template=self.templates.table_template, ) if mediawiki.is_existing_page(page_title): page_text = MediaWikiService().get_page_text( self.templates.oeg_page) overview_page_table = (WikiSectionService().get_section_table( page_text, self.templates.activities_list_section_title).string) updated_text = WikiTableService().add_table_row( page_text=page_text, new_row=self.generate_activities_list_table_row(document_data), table_section_title=self.templates. activities_list_section_title, table_template=overview_page_table, ) mediawiki.edit_page(token, self.templates.oeg_page, updated_text) else: mediawiki.create_page(token, page_title, updated_text)
def test_is_valid_token_fails_with_invalid_token(self, mocked_session): mediawiki = MediaWikiService() mocked_session.return_value.post.return_value.json.return_value = { "checktoken": { "result": "invalid" } } token = "invalid token example" is_valid_token = mediawiki.is_valid_token(token) self.assertFalse(is_valid_token)
def test_edit_page_fails_with_non_existing_page(self, mocked_session): mediawiki = MediaWikiService() mocked_session.return_value.post.return_value.json.return_value = { "error": { "code": "missingtitle" } } with self.assertRaises(MediaWikiServiceError): token = "token example" page_title = "page title example" page_text = "page text example" mediawiki.edit_page(token, page_title, page_text)
def test_move_page_fails_with_same_page_name(self, mocked_session): mediawiki = MediaWikiService() token = "token example" old_page_title = "old page" new_page_title = "new page" mocked_session.return_value.post.return_value.json.return_value = { "error": { "code": "selfmove" } } with self.assertRaises(MediaWikiServiceError): mediawiki.move_page(token, old_page_title, new_page_title)
def create_page(self, document_data: dict): """ Creates a wiki page Keyword arguments: document_data -- All required data for a project using Organised Editing Guidelines """ mediawiki = MediaWikiService() organisation_page_sections = self.document_to_page_sections( document_data) sections_text = WikiTextService().generate_text_from_dict( self.templates.page_template, self.templates.page_initial_section, organisation_page_sections, ) updated_text = WikiTableService().add_table_row( page_text=sections_text, new_row=self.generate_projects_list_table_row(document_data), table_section_title=self.templates.projects_section, table_template=self.templates.table_template, ) page_title = f"{self.templates.oeg_page}/{document_data['organisation']['name'].capitalize()}" token = mediawiki.get_token() if mediawiki.is_existing_page(page_title): page_text = MediaWikiService().get_page_text(page_title) organisation_page_table = (WikiSectionService().get_section_table( page_text, self.templates.projects_list_section).string) updated_text = WikiTableService().add_table_row( page_text=page_text, new_row=self.generate_projects_list_table_row(document_data), table_section_title=self.templates.projects_list_section, table_template=organisation_page_table, ) mediawiki.edit_page(token, page_title, updated_text) else: mediawiki.create_page(token, page_title, updated_text)
def test_generate_login_token(self, mocked_session): mediawiki = MediaWikiService() get_params = { "action": "query", "maxlag": "5", "format": "json", "meta": "tokens", "type": "login", } mediawiki.generate_login_token() mocked_session.return_value.get.assert_called_with( url="https://your-wiki.org/api.php", params=get_params) mocked_session.return_value.get.return_value.json.assert_called_with()
def test_login_fails_with_invalid_credentials(self, mocked_login_token, mocked_session): mediawiki = MediaWikiService() mocked_session.return_value.post.return_value.json.return_value = { "login": { "result": "Failed" } } with self.assertRaises(MediaWikiServiceError): mocked_session.return_value.get.return_value.json.return_value = { "query": { "tokens": { "csrftoken": "invalid token" } } } mediawiki.login()
def edit_page_text(self, update_fields: dict, overview_page_data: dict, document_data: dict): page_text = MediaWikiService().get_page_text(self.templates.oeg_page) updated_table_fields = self.get_update_table_fields( update_fields, overview_page_data) if updated_table_fields: overview_page_table = WikiSectionService().get_section_table( page_text, self.templates.activities_list_section_title) project_list_section_title = ( f"\n=={self.templates.page_initial_section}==\n" f"==={self.templates.activities_list_section_title}===\n") updated_text = WikiTableService().edit_table( overview_page_table.string, project_list_section_title, updated_table_fields, ) return updated_text else: return page_text
def test_login(self, mocked_login_token, mocked_session): mediawiki = MediaWikiService() post_page_params = { "action": "login", "maxlag": "5", "lgname": "bot name", "format": "json", } post_json_data = { "lgpassword": "******", "lgtoken": "login token" } mediawiki.login() mocked_session.return_value.post.assert_called_with( url="https://your-wiki.org/api.php", params=post_page_params, data=post_json_data, ) mocked_session.return_value.post.return_value.json.assert_called_with()
def test_non_existing_page(self, mocked_session): mediawiki = MediaWikiService() page_title = "existing page" get_page_params = { "action": "parse", "maxlag": "5", "page": page_title, "prop": "wikitext", "format": "json", } mocked_session.return_value.get.return_value.json.return_value = { "error": { "code": "missingtitle" } } existing_page = mediawiki.is_existing_page(page_title) mocked_session.return_value.get.assert_called_with( url="https://your-wiki.org/api.php", params=get_page_params) mocked_session.return_value.get.return_value.json.assert_called_with() self.assertFalse(existing_page)
def test_get_page_text(self, mocked_session): mediawiki = MediaWikiService() page_title = "example page" get_page_params = { "action": "parse", "maxlag": "5", "page": page_title, "prop": "wikitext", "format": "json", } mocked_session.return_value.get.return_value.json.return_value = { "parse": { "wikitext": { "*": "page text" } } } mediawiki.get_page_text(page_title) mocked_session.return_value.get.assert_called_with( url="https://your-wiki.org/api.php", params=get_page_params) mocked_session.return_value.get.return_value.json.assert_called_with()
def edit_page( self, update_organisation_page: dict, update_fields: dict, current_organisation_page: dict, ) -> None: """ Edits a organisation wiki page Keyword arguments: update_organisation_page -- All required data for a project using Organised Editing Guidelines """ # Get the text for the updated organisation page mediawiki = MediaWikiService() token = mediawiki.get_token() page_title = ( f"{self.templates.oeg_page}/" f"{current_organisation_page['organisation']['name'].capitalize()}" ) updated_text = self.get_edit_page_text(update_fields, current_organisation_page, update_organisation_page) # Update the organisation page and update the page name, if necessary if ("organisation" in update_fields.keys() and "name" in update_fields["organisation"].keys() and update_fields["organisation"]["name"].capitalize() != current_organisation_page["organisation"]["name"].capitalize() ): new_page = ( f"{self.templates.oeg_page}/" f'{update_organisation_page["organisation"]["name"].capitalize()}' ) mediawiki.move_page(token=token, old_page=page_title, new_page=new_page) mediawiki.edit_page(token, new_page, updated_text) else: mediawiki.edit_page(token, page_title, updated_text)
def edit_page(self, document_data: dict, update_fields: dict, project_page_data: dict): """ Edits a wiki page Keyword arguments: document_data -- All required data for a project using Organised Editing Guidelines """ mediawiki = MediaWikiService() token = mediawiki.get_token() page_title = (f"{self.templates.oeg_page}/Projects/" f'{project_page_data["project"]["name"].capitalize()}') project_page_sections = self.document_to_page_sections(document_data) sections_text = WikiTextService().generate_text_from_dict( self.templates.page_template, f"=={self.templates.page_initial_section}==", project_page_sections, ) updated_text = WikiTableService().add_table_row( page_text=sections_text, new_row=self.get_project_users_table_rows(document_data), table_section_title=self.templates.team_user_section, table_template=self.templates.table_template, ) if ("project" in update_fields.keys() and "name" in update_fields["project"].keys() and update_fields["project"]["name"].capitalize() != project_page_data["project"]["name"].capitalize()): new_page = (f"{self.templates.oeg_page}/Projects/" f'{document_data["project"]["name"].capitalize()}') mediawiki.move_page(token=token, old_page=page_title, new_page=new_page) mediawiki.edit_page(token, new_page, updated_text) else: mediawiki.edit_page(token, page_title, updated_text)
def create_page(self, document_data: dict) -> None: """ Creates a wiki page Keyword arguments: document_data -- All required data for a project using Organised Editing Guidelines """ mediawiki = MediaWikiService() token = mediawiki.get_token() project_page_sections = self.document_to_page_sections(document_data) sections_text = WikiTextService().generate_text_from_dict( self.templates.page_template, f"=={self.templates.page_initial_section}==", project_page_sections, ) updated_text = WikiTableService().add_table_row( page_text=sections_text, new_row=self.get_project_users_table_rows(document_data), table_section_title=self.templates.team_user_section, table_template=self.templates.table_template, ) page_title = (f"{self.templates.oeg_page}/Projects/" f'{document_data["project"]["name"].capitalize()}') if mediawiki.is_existing_page(page_title): raise ValueError( "Error reporting project " f"'{document_data['project']['name'].capitalize()}'." " Project already was reported.") else: mediawiki.create_page( token=token, page_title=page_title, page_text=updated_text, )
def test_get_token(self, mocked_valid_token, mocked_session): mediawiki = MediaWikiService() get_page_params = { "action": "query", "maxlag": "5", "meta": "tokens", "format": "json", } expected_token = "supersecrettoken" mocked_session.return_value.get.return_value.json.return_value = { "query": { "tokens": { "csrftoken": expected_token } } } token = mediawiki.get_token() mocked_session.return_value.get.assert_called_with( url="https://your-wiki.org/api.php", params=get_page_params) mocked_session.return_value.get.return_value.json.assert_called_with() self.assertEqual(expected_token, token)
def test_get_token_fails_with_invalid_token(self, mocked_valid_token, mocked_session): mediawiki = MediaWikiService() get_page_params = { "action": "query", "maxlag": "5", "meta": "tokens", "format": "json", } with self.assertRaises(MediaWikiServiceError): mocked_session.return_value.get.return_value.json.return_value = { "query": { "tokens": { "csrftoken": "invalid token" } } } mediawiki.get_token() mocked_session.return_value.get.assert_called_with( url="https://your-wiki.org/api.php", params=get_page_params) mocked_session.return_value.get.return_value.json.assert_called_with()
def enabled_to_report(self, document_data: dict): if MediaWikiService().is_existing_page(self.templates.oeg_page): overview_dictionary = self.wikitext_to_dict( self.templates.oeg_page) serialized_overview_page = self.parse_page_to_serializer( overview_dictionary) organisation_names = [ organisation_data["name"] for organisation_data in serialized_overview_page["organisation"] ] platform_names = [ platform_data["name"] for platform_data in serialized_overview_page["platform"] ] if (document_data["organisation"]["name"].capitalize() in organisation_names and document_data["platform"]["name"] in platform_names): return False else: return True else: return True
def enabled_to_report(self, document_data): page_title = f"{self.templates.oeg_page}/{document_data['organisation']['name'].capitalize()}" if MediaWikiService().is_existing_page(page_title): organisation_dictionary = self.wikitext_to_dict(page_title) serialized_organisation_page = self.parse_page_to_serializer( organisation_dictionary) project_names = [ project_data["name"] for project_data in serialized_organisation_page["project"] ] platform_names = [ platform_data["name"] for platform_data in serialized_organisation_page["platform"] ] if (document_data["project"]["name"].capitalize() in project_names and document_data["platform"]["name"] in platform_names): return False else: return True else: return True
def get_edit_page_text( self, update_fields: dict, current_organisation_page: dict, update_organisation_page: dict, ) -> str: """ Get the text for a updated organisation page Keyword Arguments: update_fields -- Fields that are being updated current_organisation_page -- Dict with the current organisation page content that is being updated update_organisation_page -- Dict with the organisation page updated content Returns: updated_text -- Text for the updated organisation page """ # Get text of a organisation page page_title = ( f"{self.templates.oeg_page}/" f"{current_organisation_page['organisation']['name'].capitalize()}" ) page_text = MediaWikiService().get_page_text(page_title) # Generate updated text for organisation page update_current_organisation_page = self.document_to_page_sections( update_organisation_page) sections_text = WikiTextService().generate_text_from_dict( page_text, self.templates.page_initial_section, update_current_organisation_page, ) updated_table_fields = self.get_update_table_fields( update_fields, current_organisation_page) # Update organisation page text if none table field needs updated project_list_table = WikiSectionService().get_section_table( page_text, self.templates.projects_list_section) project_list_section_title = ( f"\n=={self.templates.projects_section}==\n" f"==={self.templates.projects_list_section}===\n") updated_text = (sections_text + project_list_section_title + project_list_table.string) if updated_table_fields: # Update organisation page text if any table field needs updated forbidden_fields = [ self.templates.projects_list_project_author_column, self.templates.projects_list_project_status_column, ] project_wiki_page = WikiTextService().hyperlink_wiki_page( wiki_page= (f"{self.templates.oeg_page}/Projects/" f'{current_organisation_page["project"]["name"].capitalize()}' ), text=current_organisation_page["project"]["name"].capitalize(), ) updated_project_list_table = WikiTableService().edit_table( project_list_table.string, project_list_section_title, updated_table_fields, project_wiki_page, forbidden_fields, ) updated_text = sections_text + updated_project_list_table return updated_text
def test_is_not_redirect_page(self, mocked_session): # page_text = f"page text" redirect_page = MediaWikiService().is_redirect_page("page text") self.assertFalse(redirect_page)
def test_is_redirect_page(self, mocked_session): redirected_page = "page name" page_text = f"#REDIRECT [[{redirected_page}]]" redirect_page = MediaWikiService().is_redirect_page(page_text) self.assertEqual(redirected_page, redirect_page)