jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/768195 )
Change subject: [tests] Add ability to require special rights for tests ......................................................................
[tests] Add ability to require special rights for tests
- Add NeedRightsMixin to skip if a needed user right is missing. the 'rights' attribute sets 'login' - fix 'login' setting if 'write' attribute is used - add rights attribute to several tests and remove sysop instead - update doc
Bug: T71283 Change-Id: I2dd835f143ce11bc190c4102de29b2084ddcbfce --- M tests/README.rst M tests/aspects.py M tests/edit_tests.py M tests/flow_edit_tests.py M tests/page_tests.py M tests/protectbot_tests.py M tests/site_tests.py M tests/token_tests.py 8 files changed, 42 insertions(+), 10 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/README.rst b/tests/README.rst index 934cf86..f4ec38e 100644 --- a/tests/README.rst +++ b/tests/README.rst @@ -258,5 +258,5 @@ - ``dry = True`` : test class can use a fake site object - ``cached = True``: test class may aggressively cache API responses - ``login = True`` : test class needs to login to site -- ``sysop = True`` : test class needs to login to site as a sysop +- ``rights = '<rights>'`` : test class needs specific rights. Multiple rights must be delimited with `,`. - ``write = True`` : test class needs to write to a site diff --git a/tests/aspects.py b/tests/aspects.py index c4e4ca1..59a7748 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -635,6 +635,32 @@ return userpage
+class NeedRightsMixin(TestCaseBase): + + """Require specific rights.""" + + @classmethod + def setUpClass(cls): + """Set up the test class. + + Skip the test class if the user does not have required rights. + """ + super().setUpClass() + for site_dict in cls.sites.values(): + site = site_dict['site'] + + if site.siteinfo['readonly'] or site.obsolete: + raise unittest.SkipTest( + 'Site {} has readonly state: {}'.format( + site, site.siteinfo.get('readonlyreason', ''))) + + for right in cls.rights.split(','): + if not site.has_right(right): + raise unittest.SkipTest('User "{}" does not have required ' + 'user right "{}"' + .format(site.user(), right)) + + class MetaTestCaseClass(type):
"""Test meta class.""" @@ -686,7 +712,7 @@ for base in bases: for key in ('cached', 'code', 'dry', 'family', 'hostname', 'hostnames', 'net', 'oauth', 'pwb', 'site', 'sites', - 'sysop', 'user', 'wikibase', 'write'): + 'rights', 'sysop', 'user', 'wikibase', 'write'): if hasattr(base, key) and key not in dct: dct[key] = getattr(base, key)
@@ -779,9 +805,13 @@ assert not hostnames, 'net must be True with hostnames defined'
if dct.get('write'): - dct.setdefault('user', True) + dct.setdefault('login', True) bases = cls.add_base(bases, SiteWriteMixin)
+ if dct.get('rights'): + bases = cls.add_base(bases, NeedRightsMixin) + dct.setdefault('login', True) + if dct.get('login') or dct.get('sysop'): bases = cls.add_base(bases, RequireLoginMixin)
diff --git a/tests/edit_tests.py b/tests/edit_tests.py index 32c1a4e..05303ac 100644 --- a/tests/edit_tests.py +++ b/tests/edit_tests.py @@ -77,7 +77,7 @@ code = 'test'
write = True - sysop = True + rights = 'mergehistory'
def setup_test_pages(self): """Helper function to set up pages that we will use in these tests.""" diff --git a/tests/flow_edit_tests.py b/tests/flow_edit_tests.py index f09a40b..e8b0644 100644 --- a/tests/flow_edit_tests.py +++ b/tests/flow_edit_tests.py @@ -256,7 +256,7 @@
"""Deleting and Suppressing topics and posts."""
- sysop = True + rights = 'flow-delete,flow-suppress'
def test_delete(self): """Delete and restore a test topic and post.""" diff --git a/tests/page_tests.py b/tests/page_tests.py index f42f319..3ed030b 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -1099,7 +1099,7 @@ code = 'test'
write = True - sysop = True + rights = 'protect'
def test_protect(self): """Test Page.protect.""" diff --git a/tests/protectbot_tests.py b/tests/protectbot_tests.py index 6b3dda9..42d4c97 100644 --- a/tests/protectbot_tests.py +++ b/tests/protectbot_tests.py @@ -21,7 +21,7 @@ family = 'wikipedia' code = 'test'
- sysop = True + rights = 'protect' write = True
def test_protect(self): diff --git a/tests/site_tests.py b/tests/site_tests.py index 11f52a1..9813d81 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -1890,7 +1890,7 @@
"""Test site method using a sysop account."""
- sysop = True + rights = 'delete'
def test_methods(self): """Test sysop related methods.""" @@ -1900,6 +1900,7 @@ self.assertFalse(mysite.has_right('nonexistent_right')) self.assertIsInstance(mysite.has_group('bots'), bool) self.assertFalse(mysite.has_group('nonexistent_group')) + self.assertTrue(mysite.has_right(self.rights))
def test_deletedrevs(self): """Test the site.deletedrevs() method.""" @@ -2067,13 +2068,13 @@
class TestSiteSysopWrite(TestCase):
- """Test site sysop methods that require writing.""" + """Test site methods that require writing rights."""
family = 'wikipedia' code = 'test'
write = True - sysop = True + rights = 'delete,deleterevision,protect'
def test_protect(self): """Test the site.protect() method.""" diff --git a/tests/token_tests.py b/tests/token_tests.py index f9ffadb..f94bdaa 100644 --- a/tests/token_tests.py +++ b/tests/token_tests.py @@ -127,6 +127,7 @@
login = True write = True + rights = 'patrol'
def test_patrol(self): """Test the site.patrol() method."""
pywikibot-commits@lists.wikimedia.org