jenkins-bot has submitted this change and it was merged.
Change subject: Do not use ParamInfo during action=login
......................................................................
Do not use ParamInfo during action=login
ParamInfo is not available on private wikis, so action=login
must explicitly use POST.
Split the lazy private wiki login tests into two classes, as
only the first login behaves as expected; the second login
using the same site will cause an APIError instead of NoUsername.
Bug: T100961
Change-Id: I84f7822186df0def7d2b0fb92fffe665b3c65c5c
---
M pywikibot/data/api.py
M pywikibot/login.py
M tests/api_tests.py
M tests/utils.py
4 files changed, 78 insertions(+), 10 deletions(-)
Approvals:
John Vandenberg: Looks good to me, but someone else must approve
XZise: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index f008fca..1538e85 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -2533,6 +2533,7 @@
% diff.seconds)
time.sleep(diff.seconds)
login_request = Request(site=self.site,
+ use_get=False,
action="login",
lgname=self.username,
lgpassword=self.password)
diff --git a/pywikibot/login.py b/pywikibot/login.py
index 70f3f31..2855357 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -45,7 +45,7 @@
}
-class LoginManager:
+class LoginManager(object):
"""Site login manager."""
@@ -58,11 +58,13 @@
@param site: Site object to log into
@type site: BaseSite
- @param user: username to use
+ @param user: username to use.
+ If user is None, the username is loaded from config.usernames.
@type user: basestring
@param password: password to use
@type password: basestring
- @param sysop: login as sysop account
+ @param sysop: login as sysop account.
+ The sysop username is loaded from config.sysopnames.
@type sysop: bool
@raises NoUsername: No username is configured for the requested site.
diff --git a/tests/api_tests.py b/tests/api_tests.py
index b0f88af..46319f1 100644
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""API test module."""
#
-# (C) Pywikibot team, 2007-2014
+# (C) Pywikibot team, 2007-2015
#
# Distributed under the terms of the MIT license.
#
@@ -14,6 +14,7 @@
import pywikibot.data.api as api
import pywikibot.family
+import pywikibot.login
import pywikibot.site
from pywikibot.tools import MediaWikiVersion
@@ -23,7 +24,7 @@
DefaultSiteTestCase,
DefaultDrySiteTestCase,
)
-from tests.utils import allowed_failure
+from tests.utils import allowed_failure, FakeLoginManager
class TestApiFunctions(DefaultSiteTestCase):
@@ -664,25 +665,74 @@
self.assertEqual(req._data, data)
-class TestLazyLogin(TestCase):
+class TestLazyLoginBase(TestCase):
"""
Test that it tries to login when read API access is denied.
Because there is no such family configured it creates an AutoFamily and
BaseSite on it's own. It's testing against steward.wikimedia.org.
+
+ These tests are split into two subclasses as only the first failed login
+ behaves as expected. All subsequent logins will raise an APIError, making
+ it impossible to test two scenarios with the same APISite object.
"""
net = True
hostname = 'steward.wikimedia.org'
- def test_access_denied(self):
- """Test the query."""
+ @classmethod
+ def setUpClass(cls):
+ """Set up steward Family."""
+ super(TestLazyLoginBase, cls).setUpClass()
fam = pywikibot.family.AutoFamily(
'steward', 'https://steward.wikimedia.org/w/api.php')
- site = pywikibot.site.APISite('steward', fam)
- req = api.Request(site=site, action='query')
+ cls.site = pywikibot.site.APISite('steward', fam)
+
+
+class TestLazyLoginNotExistUsername(TestLazyLoginBase):
+
+ """Test missing username."""
+
+ # FIXME: due to limitations of LoginManager, it will ask the user
+ # for a password even if the username does not exist, and even if
+ # pywikibot is not connected to a tty. T100964
+
+ def setUp(self):
+ self.orig_login_manager = pywikibot.data.api.LoginManager
+ pywikibot.data.api.LoginManager = FakeLoginManager
+
+ def tearDown(self):
+ pywikibot.data.api.LoginManager = self.orig_login_manager
+
+ def test_access_denied_notexist_username(self):
+ """Test the query with a username which does not exist."""
+ self.site._username = ['Not registered username', None]
+ req = api.Request(site=self.site, action='query')
self.assertRaises(pywikibot.NoUsername, req.submit)
+ # FIXME: T100965
+ self.assertRaises(api.APIError, req.submit)
+
+
+class TestLazyLoginNoUsername(TestLazyLoginBase):
+
+ """Test no username."""
+
+ def test_access_denied_no_username(self):
+ """Test the query without a username."""
+ self.site._username = [None, None]
+
+ # FIXME: The following prevents LoginManager
+ # from loading the username from the config when the site
+ # username is None. i.e. site.login(user=None) means load
+ # username from the configuration.
+ if 'steward' in pywikibot.config.usernames:
+ del pywikibot.config.usernames['steward']
+
+ req = api.Request(site=self.site, action='query')
+ self.assertRaises(pywikibot.NoUsername, req.submit)
+ # FIXME: T100965
+ self.assertRaises(api.APIError, req.submit)
class TestBadTokenRecovery(TestCase):
diff --git a/tests/utils.py b/tests/utils.py
index 093c6b5..c763aed 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -266,6 +266,21 @@
return self._disambig
+class FakeLoginManager(pywikibot.data.api.LoginManager):
+
+ """Loads a fake password."""
+
+ @property
+ def password(self):
+ """Get the fake password."""
+ return 'foo'
+
+ @password.setter
+ def password(self, value):
+ """Ignore password changes."""
+ pass
+
+
def execute(command, data_in=None, timeout=0, error=None):
"""
Execute a command and capture outputs.
--
To view, visit https://gerrit.wikimedia.org/r/214990
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I84f7822186df0def7d2b0fb92fffe665b3c65c5c
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>