Skip to content
Snippets Groups Projects
Commit e7c51daa authored by Adam Williamson's avatar Adam Williamson Committed by Adam Williamson
Browse files

Make logged_in more accurate


I found this while writing integration tests. If you try to login
with an incorrect username and password, mwclient correctly raises
mwclient.errors.LoginError...but if you then check logged_in, it
is True. Clearly that's wrong.

The logic for setting logged_in will basically consider us logged
in any time we process an API call and there is no 'anon' key in
the userinfo dict *or there is no userinfo dict at all*. The last
API call processed on a failed login attempt has no userinfo
dict.

This tweaks it so we only changed the logged_in value (in either
direction) if the API call we're handling has a userinfo dict.

We also fix the type of the empty userinfo we create if there
isn't one in the call (no idea why it was a tuple, it doesn't
really matter but it's weird) and explicitly set self.logged_in
to False in Site initialization. I don't think this is strictly
necessary as we can expect the init process to result in at least
one API call with a userinfo dict, but it feels more correct.

Signed-off-by: default avatarAdam Williamson <awilliam@redhat.com>
parent 3c7244ed
No related branches found
No related tags found
No related merge requests found
...@@ -102,6 +102,7 @@ class Site: ...@@ -102,6 +102,7 @@ class Site:
self.compress = compress self.compress = compress
self.max_lag = str(max_lag) self.max_lag = str(max_lag)
self.force_login = force_login self.force_login = force_login
self.logged_in = False
if reqs and connection_options: if reqs and connection_options:
raise ValueError( raise ValueError(
"reqs is a deprecated alias of connection_options. Do not specify both." "reqs is a deprecated alias of connection_options. Do not specify both."
...@@ -381,13 +382,14 @@ class Site: ...@@ -381,13 +382,14 @@ class Site:
try: try:
userinfo = info['query']['userinfo'] userinfo = info['query']['userinfo']
except KeyError: except KeyError:
userinfo = () userinfo = {}
if 'blockedby' in userinfo: if 'blockedby' in userinfo:
self.blocked = (userinfo['blockedby'], userinfo.get('blockreason', '')) self.blocked = (userinfo['blockedby'], userinfo.get('blockreason', ''))
else: else:
self.blocked = False self.blocked = False
self.hasmsg = 'messages' in userinfo self.hasmsg = 'messages' in userinfo
self.logged_in = 'anon' not in userinfo if userinfo:
self.logged_in = 'anon' not in userinfo
if 'warnings' in info: if 'warnings' in info:
for module, warning in info['warnings'].items(): for module, warning in info['warnings'].items():
if '*' in warning: if '*' in warning:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment