http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10279
Revision: 10279 Author: valhallasw Date: 2012-06-03 10:42:58 +0000 (Sun, 03 Jun 2012) Log Message: ----------- Site.logged_in now no longer calls the site to check the username + added tests to define behaviour.
Modified Paths: -------------- branches/rewrite/pywikibot/site.py
Added Paths: ----------- branches/rewrite/tests/dry_site_tests.py
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2012-06-03 10:25:52 UTC (rev 10278) +++ branches/rewrite/pywikibot/site.py 2012-06-03 10:42:58 UTC (rev 10279) @@ -678,12 +678,16 @@ @param sysop: if True, require sysop privileges.
""" - if self.userinfo['name'] is None or\ - self._username[sysop] is None or\ - pywikibot.Page(self, ns=2, title=self.userinfo['name']) != \ - pywikibot.Page(self, ns=2, title=self._username[sysop]): + if sysop and 'sysop' not in self.userinfo['groups']: return False - return (not sysop) or 'sysop' in self.userinfo['groups'] + + if not self.userinfo['name']: + return False + + if self.userinfo['name'] != self._username[sysop]: + pywikibot.warning("Logged in as %s instead of %s" % (self.userinfo['name'], self._username[sysop])) + + return True
@deprecated("Site.user()") def loggedInAs(self, sysop = False):
Added: branches/rewrite/tests/dry_site_tests.py =================================================================== --- branches/rewrite/tests/dry_site_tests.py (rev 0) +++ branches/rewrite/tests/dry_site_tests.py 2012-06-03 10:42:58 UTC (rev 10279) @@ -0,0 +1,24 @@ +import pywikibot + +class DrySite(pywikibot.site.APISite): + @property + def userinfo(self): + return self._userinfo + +def test_logged_in(): + x = DrySite('en') + + x._userinfo = {'name': None, 'groups': []} + x._username = ['normal_user', 'sysop_user'] + + assert not x.logged_in(True) + assert not x.logged_in(False) + + x._userinfo['name'] = 'normal_user' + assert not x.logged_in(True) + assert x.logged_in(False) + + x._userinfo['name'] = 'sysop_user' + x._userinfo['groups'] = ['sysop'] + assert x.logged_in(True) + assert x.logged_in(False) \ No newline at end of file
pywikipedia-svn@lists.wikimedia.org