[python-bugzilla] [PATCH 1/2] Bugzilla.logged_in now returns live state

abn at redhat.com abn at redhat.com
Fri Dec 12 10:00:24 UTC 2014


From: Arun Babu Neelicattu <abn at redhat.com>

- since BugZilla XMLRPC does not currentlly expose a way to query login state
  for a user, we workaround this by calling User.get() with ids set
- extend test suite to cover Bugzilla.logged_in
---
 bugzilla/base.py       | 21 ++++++++++++++++++---
 tests/rw_functional.py |  8 ++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/bugzilla/base.py b/bugzilla/base.py
index 59dff17..fa2b0c2 100644
--- a/bugzilla/base.py
+++ b/bugzilla/base.py
@@ -403,7 +403,6 @@ class BugzillaBase(object):
         self._cookiejar = None
         self._sslverify = bool(sslverify)
 
-        self.logged_in = False
         self.bug_autorefresh = True
 
         # Bugzilla object state info that users shouldn't mess with
@@ -644,7 +643,6 @@ class BugzillaBase(object):
 
         try:
             ret = self._login(self.user, self.password)
-            self.logged_in = True
             self.password = ''
             log.info("login successful for user=%s", self.user)
             return ret
@@ -695,7 +693,24 @@ class BugzillaBase(object):
         self.disconnect()
         self.user = ''
         self.password = ''
-        self.logged_in = False
+
+    @property
+    def logged_in(self):
+        """
+        This is True if this instance is logged in else False.
+
+        We test if this session is authenticated by calling the User.get()
+        XMLRPC method with ids set. Logged-out users cannot pass the 'ids'
+        parameter and will result in a 505 error.
+        """
+        try:
+            self._proxy.User.get({'ids': []})
+            return True
+        except Fault:
+            e = sys.exc_info()[1]
+            if e.faultCode == 505:
+                return False
+            raise e
 
 
     #############################################
diff --git a/tests/rw_functional.py b/tests/rw_functional.py
index 10d4c6d..caab7d5 100644
--- a/tests/rw_functional.py
+++ b/tests/rw_functional.py
@@ -633,6 +633,14 @@ class RHPartnerTest(BaseTest):
         self.assertTrue("Login failed: " in ret)
 
 
+    def test10LoginState(self):
+        bz = self.bzclass(url=self.url, cookiefile=None, tokenfile=None)
+        self.assertFalse(bz.logged_in, "Login state check failed for logged out user.")
+
+        bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
+        self.assertTrue(bz.logged_in, "Login state check failed for logged in user.")
+
+
     def test11UserUpdate(self):
         # This won't work if run by the same user we are using
         bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
-- 
1.9.3



More information about the python-bugzilla mailing list