Skip to content

Commit 2b1281f

Browse files
Use proper REST API route for getting a single bug (fixes #174) (#183)
This avoids an `IndexError` in Bugzilla._getbug` and ensures that a `BugzillaError` gets raised e.g. if the bug ID does not exist or the client is not authorized. closes #174
1 parent 343f15e commit 2b1281f

3 files changed

Lines changed: 85 additions & 2 deletions

File tree

bugzilla/_backendrest.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,22 @@ def bug_create(self, paramdict):
107107
def bug_fields(self, paramdict):
108108
return self._get("/field/bug", paramdict)
109109
def bug_get(self, bug_ids, aliases, paramdict):
110+
bug_list = listify(bug_ids)
111+
alias_list = listify(aliases)
110112
data = paramdict.copy()
111-
data["id"] = listify(bug_ids)
112-
data["alias"] = listify(aliases)
113+
114+
# FYI: The high-level API expects the backends to raise an exception
115+
# when retrieval of a single bug fails (default behavior of the XMLRPC
116+
# API), but the REST API simply returns an empty search result set.
117+
# To ensure compliant behavior, the REST backend needs to use the
118+
# explicit URL to get a single bug.
119+
if len(bug_list or []) + len(alias_list or []) == 1:
120+
for id_list in (bug_list, alias_list):
121+
if id_list:
122+
return self._get("/bug/%s" % id_list[0], data)
123+
124+
data["id"] = bug_list
125+
data["alias"] = alias_list
113126
ret = self._get("/bug", data)
114127
return ret
115128

tests/test_backend_rest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from types import MethodType
2+
3+
from bugzilla._backendrest import _BackendREST
4+
from bugzilla._session import _BugzillaSession
5+
6+
7+
def test_getbug():
8+
session = _BugzillaSession(url="http://example.com",
9+
user_agent="py-bugzilla-test",
10+
sslverify=False,
11+
cert=None,
12+
tokencache={},
13+
api_key="",
14+
is_redhat_bugzilla=False)
15+
backend = _BackendREST(url="http://example.com",
16+
bugzillasession=session)
17+
18+
def _assertion(self, *args):
19+
self.assertion_called = True
20+
assert args and args[0] == url
21+
22+
setattr(backend, "_get", MethodType(_assertion, backend))
23+
24+
for _ids, aliases, url in (
25+
(1, None, "/bug/1"),
26+
([1], [], "/bug/1"),
27+
(None, "CVE-1999-0001", "/bug/CVE-1999-0001"),
28+
([], ["CVE-1999-0001"], "/bug/CVE-1999-0001"),
29+
(1, "CVE-1999-0001", "/bug"),
30+
):
31+
backend.assertion_called = False
32+
33+
backend.bug_get(_ids, aliases, {})
34+
35+
assert backend.assertion_called is True

tests/test_ro_functional.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
"""
99
Unit tests that do readonly functional tests against real bugzilla instances.
1010
"""
11+
from xmlrpc.client import Fault
12+
1113
import pytest
1214

1315
import bugzilla
16+
from bugzilla.exceptions import BugzillaError
1417
import tests
1518

1619

@@ -295,6 +298,38 @@ def testGetBugAlias(backends):
295298
assert bug.bug_id == 720773
296299

297300

301+
def testGetBug404(backends):
302+
"""
303+
getbug() is expected to raise an error, if a bug ID or alias does not exist
304+
"""
305+
bz = _open_bz(REDHAT_URL, **backends)
306+
307+
try:
308+
bz.getbug(100000000)
309+
except Fault as error: # XMLRPC API
310+
assert error.faultCode == 101
311+
except BugzillaError as error: # REST API
312+
assert error.code == 101
313+
else:
314+
raise AssertionError("No exception raised")
315+
316+
317+
def testGetBugAlias404(backends):
318+
"""
319+
getbug() is expected to raise an error, if a bug ID or alias does not exist
320+
"""
321+
bz = _open_bz(REDHAT_URL, **backends)
322+
323+
try:
324+
bz.getbug("CVE-1234-4321")
325+
except Fault as error: # XMLRPC API
326+
assert error.faultCode == 100
327+
except BugzillaError as error: # REST API
328+
assert error.code == 100
329+
else:
330+
raise AssertionError("No exception raised")
331+
332+
298333
def testQuerySubComponent(run_cli, backends):
299334
bz = _open_bz(REDHAT_URL, **backends)
300335

0 commit comments

Comments
 (0)