jenkins-bot has submitted this change and it was merged.
Change subject: Add tests for Site.protect(), Page.protect(), fix page locks ......................................................................
Add tests for Site.protect(), Page.protect(), fix page locks
This patch adds unit tests for protecting / unprotecting a page, as well adding tests & fixing Site.lock_page(), Site.unlock_page() Previously, page locking achieved no effect.
A bug, T78522 was encountered, causing the tests to fail for now. The tests pass successfully if the bug is worked around by only protecting a page once per execution.
The failing tests are marked as disabled, however in the meantime there are alternate versions of the tests that works around the bug.
You'll need sysop rights on test.wikipedia.org in order to run the tests.
Bug: T59602 Change-Id: I8c0c91651b1c0083161d0e7c1d4cbf6ad144c669 --- M pywikibot/site.py M tests/page_tests.py M tests/site_tests.py 3 files changed, 151 insertions(+), 2 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 16fe681..8f82568 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -740,9 +740,9 @@ """ self._pagemutex.acquire() try: - while page in self._locked_pages: + while page.title(withSection=False) in self._locked_pages: if not block: - raise PageInUse + raise PageInUse(page.title(withSection=False)) time.sleep(.25) self._locked_pages.append(page.title(withSection=False)) finally: diff --git a/tests/page_tests.py b/tests/page_tests.py index 84338e2..d44825b 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -594,6 +594,62 @@ self.assertTrue(rv)
+class TestPageProtect(TestCase): + + """Test page protect / unprotect actions.""" + + family = 'test' + code = 'test' + + write = True + sysop = True + + def test_applicable_protections(self): + """Test Page.applicable_protections.""" + site = self.get_site() + p1 = pywikibot.Page(site, u'User:Unicodesnowman/NonexistentPage') + p2 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + p3 = pywikibot.Page(site, u'File:Wiki.png') + + self.assertEqual(p1.applicable_protections(), set(['create'])) + self.assertIn('edit', p2.applicable_protections()) + self.assertNotIn('create', p2.applicable_protections()) + self.assertNotIn('upload', p2.applicable_protections()) + self.assertIn('upload', p3.applicable_protections()) + + @unittest.expectedFailure + def test_protect(self): + """Test Page.protect.""" + site = self.get_site() + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + + p1.protect(protections={'edit': 'sysop', 'move': 'autoconfirmed'}, + reason=u'Pywikibot unit test') + self.assertEqual(p1.protection(), + {u'edit': (u'sysop', u'infinity'), + u'move': (u'autoconfirmed', u'infinity')}) + + p1.protect(protections={'edit': '', 'move': ''}, + reason=u'Pywikibot unit test') + self.assertEqual(p1.protection(), {}) + + def test_protect_alt(self): + """Test of Page.protect that works around T78522.""" + site = self.get_site() + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + + p1.protect(protections={'edit': 'sysop', 'move': 'autoconfirmed'}, + reason=u'Pywikibot unit test') + self.assertEqual(p1.protection(), + {u'edit': (u'sysop', u'infinity'), + u'move': (u'autoconfirmed', u'infinity')}) + # workaround + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + p1.protect(protections={'edit': '', 'move': ''}, + reason=u'Pywikibot unit test') + self.assertEqual(p1.protection(), {}) + + if __name__ == '__main__': try: unittest.main() diff --git a/tests/site_tests.py b/tests/site_tests.py index e7438e6..89ac50e 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -651,6 +651,18 @@ self.assertIsInstance(link, pywikibot.Page) self.assertIn(link.namespace(), (2, 3))
+ def test_lock_page(self): + """Test the site.lock_page() and site.unlock_page() method.""" + site = self.get_site() + p1 = pywikibot.Page(site, u'Foo') + + site.lock_page(page=p1, block=True) + self.assertRaises(pywikibot.site.PageInUse, site.lock_page, page=p1, block=False) + site.unlock_page(page=p1) + # verify it's unlocked + site.lock_page(page=p1, block=False) + site.unlock_page(page=p1) +
class TestImageUsage(DefaultSiteTestCase):
@@ -1138,6 +1150,87 @@ total=5)
+class SiteProtectTestCase(DefaultSiteTestCase): + + """Test site protect / unprotect using a sysop account.""" + + family = 'test' + code = 'test' + + write = True + sysop = True + + @unittest.expectedFailure + def test_protect(self): + """Test the site.protect() method.""" + site = self.get_site() + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + + r = site.protect(protections={'edit': 'sysop', 'move': 'autoconfirmed'}, + page=p1, + reason='Pywikibot unit test') + self.assertEqual(r, None) + self.assertEqual(site.page_restrictions(page=p1), + {u'edit': (u'sysop', u'infinity'), + u'move': (u'autoconfirmed', u'infinity')}) + + expiry = pywikibot.Timestamp.fromISOformat('2050-01-01T00:00:00Z') + site.protect(protections={'edit': 'sysop', 'move': 'autoconfirmed'}, + page=p1, + expiry=expiry, + reason='Pywikibot unit test') + + self.assertEqual(site.page_restrictions(page=p1), + {u'edit', (u'sysop', u'2050-01-01T00:00:00Z'), + u'move', (u'autoconfirmed', u'2050-01-01T00:00:00Z')}) + + site.protect(protections={'edit': '', 'move': ''}, + page=p1, + reason='Pywikibot unit test') + self.assertEqual(site.page_restrictions(page=p1), {}) + + def test_protect_alt(self): + """Test the site.protect() method, works around T78522.""" + site = self.get_site() + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + + r = site.protect(protections={'edit': 'sysop', 'move': 'autoconfirmed'}, + page=p1, + reason='Pywikibot unit test') + self.assertEqual(r, None) + self.assertEqual(site.page_restrictions(page=p1), + {u'edit': (u'sysop', u'infinity'), + u'move': (u'autoconfirmed', u'infinity')}) + + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + expiry = pywikibot.Timestamp.fromISOformat('2050-01-01T00:00:00Z') + site.protect(protections={'edit': 'sysop', 'move': 'autoconfirmed'}, + page=p1, + expiry=expiry, + reason='Pywikibot unit test') + + self.assertEqual(site.page_restrictions(page=p1), + {u'edit': (u'sysop', u'2050-01-01T00:00:00Z'), + u'move': (u'autoconfirmed', u'2050-01-01T00:00:00Z')}) + + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + site.protect(protections={'edit': '', 'move': ''}, + page=p1, + reason='Pywikibot unit test') + self.assertEqual(site.page_restrictions(page=p1), {}) + + def test_protect_exception(self): + """Test that site.protect() throws an exception when passed invalid args.""" + site = self.get_site() + p1 = pywikibot.Page(site, u'User:Unicodesnowman/ProtectTest') + self.assertRaises(pywikibot.Error, site.protect, + protections={'anInvalidValue': 'sysop'}, + page=p1, reason='Pywikibot unit test') + self.assertRaises(pywikibot.Error, site.protect, + protections={'edit': 'anInvalidValue'}, + page=p1, reason='Pywikibot unit test') + + class SiteUserTestCase2(DefaultSiteTestCase):
"""More tests that rely on a user account."""
pywikibot-commits@lists.wikimedia.org