Skip to content

Commit 1f81b02

Browse files
authored
Pass Bugzilla API key in request header instead of query param (#396)
See https://bmo.readthedocs.io/en/latest/api/core/v1/general.html?highlight=x-bugzilla-api-key#authentication While trying the new Webhooks endpoint, I realized that passing the API key in query params didn't work. The endpoint was expecting the `"x-bugzilla-api-key"` header.
1 parent 4fa5cec commit 1f81b02

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

jbi/services/bugzilla.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ def __init__(self, base_url, api_key):
3333

3434
def _call(self, verb, url, *args, **kwargs):
3535
"""Send HTTP requests with API key in querystring parameters."""
36-
# Send API key as querystring parameter.
37-
kwargs.setdefault("params", {}).setdefault("api_key", self.api_key)
36+
# Send API key in headers.
37+
# https://bmo.readthedocs.io/en/latest/api/core/v1/general.html?highlight=x-bugzilla-api-key#authentication
38+
headers = kwargs.setdefault("headers", {})
39+
headers.setdefault("x-bugzilla-api-key", self.api_key)
3840
resp = self._client.request(verb, url, *args, **kwargs)
3941
resp.raise_for_status()
4042
parsed = resp.json()

tests/unit/services/test_bugzilla.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
import responses
5+
from responses import matchers
56

67
from jbi.environment import get_settings
78
from jbi.services.bugzilla import BugzillaClientError, get_client
@@ -35,14 +36,23 @@ def test_bugzilla_methods_are_retried_if_raising(mocked_responses):
3536

3637

3738
@pytest.mark.no_mocked_bugzilla
38-
def test_bugzilla_key_is_passed_in_querystring(mocked_responses):
39-
url = (
40-
f"{get_settings().bugzilla_base_url}/rest/whoami?api_key=fake_bugzilla_api_key"
39+
def test_bugzilla_key_is_passed_in_header(mocked_responses):
40+
url = f"{get_settings().bugzilla_base_url}/rest/whoami"
41+
mocked_responses.add(
42+
responses.GET,
43+
url,
44+
json={"id": "you"},
45+
match=[
46+
matchers.header_matcher({"x-bugzilla-api-key": "fake_bugzilla_api_key"})
47+
],
4148
)
42-
mocked_responses.add(responses.GET, url, json={"id": "you"})
4349

4450
assert get_client().logged_in
4551

52+
assert len(mocked_responses.calls) == 1
53+
# The following assertion is redundant with matchers but also more explicit.
54+
assert "x-bugzilla-api-key" in mocked_responses.calls[0].request.headers
55+
4656

4757
@pytest.mark.no_mocked_bugzilla
4858
def test_bugzilla_raises_if_response_has_error(mocked_responses):

0 commit comments

Comments
 (0)