Skip to content

Commit 35cdc65

Browse files
authored
Fix #240: log responses objects (#276)
* Fix #240: log responses objects * Fix usage of markers and remove warnings * Update secrets baseline
1 parent 18cac73 commit 35cdc65

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@
122122
"filename": "README.md",
123123
"hashed_secret": "04e78d6e804f2b59e6cb282cb9ed2c7bfd8a9737",
124124
"is_verified": false,
125-
"line_number": 242
125+
"line_number": 245
126126
}
127127
]
128128
},
129-
"generated_at": "2022-09-28T11:04:17Z"
129+
"generated_at": "2022-10-07T13:12:35Z"
130130
}

jbi/actions/default.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ def __call__(self, context: ActionContext) -> ActionResult:
9696

9797
for step in self.steps[context.operation]:
9898
context, step_responses = step(context=context, **self.parameters)
99+
for response in step_responses:
100+
logger.debug(
101+
"Received %s",
102+
response,
103+
extra={
104+
"response": response,
105+
**context.dict(),
106+
},
107+
)
99108
responses += step_responses
100109

101110
return True, {"responses": responses}

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ env_override_existing_values = true
4747
env_files = [
4848
".env.example"
4949
]
50+
markers = [
51+
"no_mocked_bugzilla",
52+
"no_mocked_jira",
53+
]
5054

5155
[tool.pylint]
5256
[tool.pylint.'MESSAGES CONTROL']

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def settings():
4646
def mocked_bugzilla(request):
4747
if "no_mocked_bugzilla" in request.keywords:
4848
yield None
49+
bugzilla.get_client.cache_clear()
4950
else:
5051
with mock.patch("jbi.services.bugzilla.BugzillaClient") as mocked_bz:
5152
yield mocked_bz()
@@ -56,6 +57,7 @@ def mocked_bugzilla(request):
5657
def mocked_jira(request):
5758
if "no_mocked_jira" in request.keywords:
5859
yield None
60+
jira.get_client.cache_clear()
5961
else:
6062
with mock.patch("jbi.services.jira.Jira") as mocked_jira:
6163
yield mocked_jira()

tests/unit/actions/test_default.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import logging
12
from unittest import mock
23

34
import pytest
5+
import responses
46

57
from jbi.actions import default
8+
from jbi.environment import get_settings
69
from jbi.models import ActionContext
710

811

@@ -36,13 +39,55 @@ def test_default_returns_callable_without_data():
3639
assert "missing 1 required positional argument: 'context'" in str(exc_info.value)
3740

3841

42+
@pytest.mark.no_mocked_bugzilla
43+
@pytest.mark.no_mocked_jira
44+
def test_default_logs_all_received_responses(
45+
mocked_responses, caplog, context_comment_example: ActionContext
46+
):
47+
# In this test, we don't mock the Jira and Bugzilla clients
48+
# because we want to make sure that actual responses objects are logged
49+
# successfully.
50+
settings = get_settings()
51+
url = f"{settings.jira_base_url}rest/api/2/issue/JBI-234/comment"
52+
mocked_responses.add(
53+
responses.POST,
54+
url,
55+
json={
56+
"id": "10000",
57+
"key": "ED-24",
58+
},
59+
)
60+
61+
action = default.init(
62+
jira_project_key="",
63+
steps={"new": [], "existing": [], "comment": ["create_comment"]},
64+
)
65+
66+
with caplog.at_level(logging.DEBUG):
67+
action(context=context_comment_example)
68+
69+
captured_log_msgs = [
70+
(r.msg % r.args, r.response)
71+
for r in caplog.records
72+
if r.name == "jbi.actions.default"
73+
]
74+
75+
assert captured_log_msgs == [
76+
(
77+
"Received {'id': '10000', 'key': 'ED-24'}",
78+
{"id": "10000", "key": "ED-24"},
79+
)
80+
]
81+
82+
3983
def test_default_returns_callable_with_data(
4084
context_create_example: ActionContext, mocked_jira, mocked_bugzilla
4185
):
4286
sentinel = mock.sentinel
4387
mocked_jira.create_issue.return_value = {"key": "k"}
4488
mocked_jira.create_or_update_issue_remote_links.return_value = sentinel
4589
mocked_bugzilla.get_bug.return_value = context_create_example.bug
90+
mocked_bugzilla.get_comments.return_value = []
4691
callable_object = default.init(jira_project_key=context_create_example.jira.project)
4792

4893
handled, details = callable_object(context=context_create_example)

0 commit comments

Comments
 (0)