jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1324314?usp=email )
Change subject: welcome: Fix bad-account queue handling
......................................................................
welcome: Fix bad-account queue handling
Rejecting the first suspected username can access the queue before it
exists. Completed reports also clear a differently named attribute,
leaving processed usernames queued.
Initialize the queue with the bot state, check the reporting threshold
only after accepting a username, and clear the queue after processing.
Add collector-registered regression tests for the queue lifecycle.
Change-Id: I6cf23c2f1c02d29de45df8ce9b5d7bb389a64fab
---
M scripts/welcome.py
M tests/__init__.py
M tests/welcome_tests.py
3 files changed, 66 insertions(+), 8 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/welcome.py b/scripts/welcome.py
index 35e2186..b2b54c7 100755
--- a/scripts/welcome.py
+++ b/scripts/welcome.py
@@ -538,6 +538,7 @@
super().__init__(**kwargs)
self.bname: dict[str, str] = {}
+ self._BAQueue: list[str] = []
self.welcomed_users: list[str] = []
self.log_name = i18n.translate(self.site, LOGBOOK)
@@ -655,13 +656,10 @@
self.show_status()
pywikibot.info(f'{name} is possibly an unwanted username. It will'
' be reported.')
- if hasattr(self, '_BAQueue'):
- self._BAQueue.append(name)
- else:
- self._BAQueue = [name]
+ self._BAQueue.append(name)
- if len(self._BAQueue) >= globalvar.dump_to_log:
- self.report_bad_account()
+ if len(self._BAQueue) >= globalvar.dump_to_log:
+ self.report_bad_account()
def report_bad_account(self) -> None:
"""Report bad account."""
@@ -696,7 +694,7 @@
minor=True)
self.show_status(Msg.DONE)
pywikibot.info('Reported')
- self.BAQueue = []
+ self._BAQueue.clear()
def makelogpage(self) -> None:
"""Make log page."""
@@ -905,7 +903,7 @@
f'Putting the log of the latest {welcomed_count} users...')
self.makelogpage()
- if hasattr(self, '_BAQueue'):
+ if self._BAQueue:
self.show_status()
pywikibot.info('Putting bad name to report page...')
self.report_bad_account()
diff --git a/tests/__init__.py b/tests/__init__.py
index a0e0611..e36e031 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -201,6 +201,7 @@
'template_bot',
'uploadscript',
'weblinkchecker',
+ 'welcome',
}
disabled_test_modules = {
diff --git a/tests/welcome_tests.py b/tests/welcome_tests.py
index 8139ac7..13b8ed4 100755
--- a/tests/welcome_tests.py
+++ b/tests/welcome_tests.py
@@ -21,6 +21,65 @@
net = False
+ def test_rejected_bad_account_not_queued(self) -> None:
+ """Test rejecting the first bad account leaves an empty queue."""
+ site = MagicMock()
+
+ def init(bot, **kwargs) -> None:
+ bot._site = site
+
+ with (
+ patch.object(welcome.SingleSiteBot, '__init__', init),
+ patch.object(welcome.i18n, 'translate', return_value='Log'),
+ patch.object(welcome, 'get_welcome_text'),
+ patch.object(welcome.globalvar, 'random_sign', False),
+ ):
+ bot = welcome.WelcomeBot()
+
+ with (
+ patch.object(welcome.globalvar, 'confirm', True),
+ patch.object(welcome.pywikibot, 'input_choice', return_value='n'),
+ ):
+ bot.collect_bad_accounts('Bad name')
+
+ self.assertIsEmpty(bot._BAQueue)
+
+ def test_report_bad_account_clears_queue(self) -> None:
+ """Test that reported bad accounts are removed from the queue."""
+ report_page = MagicMock()
+ report_page.exists.return_value = False
+ site = MagicMock()
+ site.code = 'en'
+ bot = SimpleNamespace(
+ _BAQueue=['Bad name'], bname={}, show_status=MagicMock(),
+ site=site)
+
+ with (
+ patch.object(welcome.pywikibot, 'Page',
+ return_value=report_page),
+ # T75017: report_bad_account still uses compat's url2link.
+ patch.object(welcome.pywikibot, 'url2link',
+ create=True, return_value='Bad name'),
+ patch.object(welcome.i18n, 'translate',
+ side_effect=['Report page', '* %s']),
+ patch.object(welcome.i18n, 'twtranslate',
+ return_value='Report bad username'),
+ ):
+ welcome.WelcomeBot.report_bad_account(bot)
+
+ self.assertIsEmpty(bot._BAQueue)
+
+ def test_write_log_ignores_empty_bad_account_queue(self) -> None:
+ """Test that an empty bad-account queue is not reported."""
+ bot = SimpleNamespace(
+ _BAQueue=[], report_bad_account=MagicMock(),
+ show_status=MagicMock(), welcomed_users=[])
+
+ with patch.object(welcome.globalvar, 'make_welcome_log', False):
+ welcome.WelcomeBot.write_log(bot)
+
+ bot.report_bad_account.assert_not_called()
+
def test_skip_page_reuses_edit_count(self) -> None:
"""Test that the edit count is retrieved once."""
bot = SimpleNamespace(show_status=MagicMock())
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1324314?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I6cf23c2f1c02d29de45df8ce9b5d7bb389a64fab
Gerrit-Change-Number: 1324314
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]