Skip to content

Commit ac853a5

Browse files
committed
session: Move tokencache updating into Bugzilla class
This is wired deep into the backend layer, but it really should just be done after a successful login() call. Move the tokencache.set_value() call to Bugzilla.login() and adjust everything to match. Fill in some more descriptive interactive_login text while we are here Signed-off-by: Cole Robinson <crobinso@redhat.com>
1 parent 50acd2e commit ac853a5

7 files changed

Lines changed: 41 additions & 18 deletions

File tree

bugzilla/_authfiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _get_domain(self, url):
147147

148148
def get_value(self, url):
149149
domain = self._get_domain(url)
150-
if self._cfg.has_option(domain, 'token'):
150+
if domain and self._cfg.has_option(domain, 'token'):
151151
return self._cfg.get(domain, 'token')
152152
return None
153153

bugzilla/_backendxmlrpc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ def _ServerProxy__request(self, methodname, params):
126126
self, methodname, (authparams,))
127127
# pylint: enable=no-member
128128

129-
if isinstance(ret, dict) and 'token' in ret.keys():
130-
self.__bugzillasession.set_token_value(ret.get('token'))
131129
return ret
132130

133131

bugzilla/_session.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ def get_user_agent(self):
6666
return self._user_agent
6767
def get_scheme(self):
6868
return self._scheme
69-
def set_token_value(self, value):
70-
self._tokencache.set_value(self._url, value)
7169

7270
def get_auth_params(self):
7371
# bugzilla.redhat.com will error if there's auth bits in params

bugzilla/base.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,8 @@ def login(self, user=None, password=None, restrict_login=None):
609609
ret = self._backend.user_login(payload)
610610
self.password = ''
611611
log.info("login succeeded for user=%s", self.user)
612+
if "token" in ret:
613+
self._tokencache.set_value(self.url, ret["token"])
612614
return ret
613615
except Exception as e:
614616
log.debug("Login exception: %s", str(e), exc_info=True)
@@ -666,8 +668,13 @@ def interactive_login(self, user=None, password=None, force=False,
666668
log.info('Logging in... ')
667669
out = self.login(user, password, restrict_login)
668670
msg = "Login successful."
669-
if "token" in out and self.tokenfile:
670-
msg += " Token cache saved to %s" % self.tokenfile
671+
if "token" not in out:
672+
msg += " However no token was returned."
673+
else:
674+
if not self.tokenfile:
675+
msg += " Token not saved to disk."
676+
else:
677+
msg += " Token cache saved to %s" % self.tokenfile
671678
if self._get_version() >= 5.0:
672679
msg += "\nToken usage is deprecated. "
673680
msg += "Consider using bugzilla API keys instead. "

tests/data/clioutput/tokenfile.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[example.com]
2+
token = my-fake-token
3+

tests/test_api_authfiles.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import shutil
1414
import tempfile
1515

16-
import requests
17-
1816
import tests
1917
import tests.mockbackend
2018
import tests.utils
@@ -116,13 +114,17 @@ def test_authfiles_saving(monkeypatch):
116114
bzapi.cert = "foo-fake-path"
117115
backend = bzapi._backend # pylint: disable=protected-access
118116
bsession = backend._bugzillasession # pylint: disable=protected-access
117+
btokencache = bzapi._tokencache # pylint: disable=protected-access
119118

120119
# token testing, with repetitions to hit various code paths
121-
bsession.set_token_value(None)
122-
bsession.set_token_value("MY-FAKE-TOKEN")
123-
bsession.set_token_value("MY-FAKE-TOKEN")
124-
bsession.set_token_value(None)
125-
bsession.set_token_value("MY-FAKE-TOKEN")
120+
btokencache.set_value(bzapi.url, None)
121+
assert "Bugzilla_token" not in bsession.get_auth_params()
122+
btokencache.set_value(bzapi.url, "MY-FAKE-TOKEN")
123+
assert bsession.get_auth_params()["Bugzilla_token"] == "MY-FAKE-TOKEN"
124+
btokencache.set_value(bzapi.url, "MY-FAKE-TOKEN")
125+
btokencache.set_value(bzapi.url, None)
126+
assert "Bugzilla_token" not in bsession.get_auth_params()
127+
btokencache.set_value(bzapi.url, "MY-FAKE-TOKEN")
126128

127129
dirname = os.path.dirname(__file__) + "/data/authfiles/"
128130
output_token = dirname + "output-token.txt"
@@ -155,9 +157,8 @@ def test_authfiles_nowrite():
155157
# Setting values tokenfile is None, should be fine
156158
bzapi = tests.mockbackend.make_bz(bz_kwargs={"use_creds": False})
157159
bzapi.connect("https://example.com/foo")
158-
backend = bzapi._backend # pylint: disable=protected-access
159-
bsession = backend._bugzillasession # pylint: disable=protected-access
160+
btokencache = bzapi._tokencache # pylint: disable=protected-access
160161
rcfile = bzapi._rcfile # pylint: disable=protected-access
161162

162-
bsession.set_token_value("NEW-TOKEN-VALUE")
163+
btokencache.set_value(bzapi.url, "NEW-TOKEN-VALUE")
163164
assert rcfile.save_api_key(bzapi.url, "fookey") is None

tests/test_cli_login.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,32 @@ def test_login(run_cli):
6161
# Returns success for logged_in check and hits a tokenfile line
6262
cmd = "bugzilla --ensure-logged-in "
6363
cmd += "login FOO BAR"
64+
tmp = tempfile.NamedTemporaryFile()
6465
fakebz = tests.mockbackend.make_bz(
65-
bz_kwargs={"use_creds": True},
66+
bz_kwargs={"use_creds": True, "tokenfile": tmp.name},
6667
user_login_args="data/mockargs/test_login.txt",
6768
user_login_return={'id': 1234, 'token': 'my-fake-token'},
6869
user_get_args=None,
6970
user_get_return={})
71+
fakebz.connect("https://example.com")
7072
out = run_cli(cmd, fakebz)
7173
assert "Token cache saved" in out
7274
assert fakebz.tokenfile in out
7375
assert "Consider using bugzilla API" in out
76+
tests.utils.diff_compare(open(tmp.name).read(),
77+
"data/clioutput/tokenfile.txt")
78+
79+
# Returns success for logged_in check and hits another tokenfile line
80+
cmd = "bugzilla --ensure-logged-in "
81+
cmd += "login FOO BAR"
82+
fakebz = tests.mockbackend.make_bz(
83+
bz_kwargs={"use_creds": True, "tokenfile": None},
84+
user_login_args="data/mockargs/test_login.txt",
85+
user_login_return={'id': 1234, 'token': 'my-fake-token'},
86+
user_get_args=None,
87+
user_get_return={})
88+
out = run_cli(cmd, fakebz)
89+
assert "Token not saved" in out
7490

7591

7692
def test_interactive_login(monkeypatch, run_cli):

0 commit comments

Comments
 (0)