Skip to content

Commit

Permalink
Mock using same response object from correct framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Vebop committed Aug 23, 2024
1 parent 4951cf8 commit 00a639d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions manager/api/tests/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from unittest.mock import patch

import requests
import rest_framework
from django.test import TestCase, override_settings

from manager.utils import (
Expand Down Expand Up @@ -345,6 +346,7 @@ def test_update_time_lost(self):

jira_response = update_time_lost(12, 97.01)
assert jira_response.status_code == 400
assert jira_response.data["ack"] == "Jira time_lost field could not be updated"

def test_add_comment(self):
"""Test call to jira_comment function with all needed parameters"""
Expand All @@ -360,13 +362,20 @@ def test_add_comment(self):

mock_time_lost_patcher = patch("manager.utils.update_time_lost")
mock_time_lost_client = mock_time_lost_patcher.start()
mock_time_lost_client.return_value = response
time_lost_response = rest_framework.response.Response()
time_lost_response.status_code = 200
time_lost_response.data = {
"ack": "Jira time_lost field updated",
}
mock_time_lost_client.return_value = time_lost_response

jira_response = jira_comment(self.jira_request_narrative_full_jira_comment.data)
assert jira_response.status_code == 200
assert jira_response.data["ack"] == "Jira comment created"

def test_add_comment_fail(self):
"""Test jira_comment() return value when update_time_lost()
fails during jira_comment()"""
mock_jira_patcher = patch("requests.post")
mock_jira_client = mock_jira_patcher.start()
response = requests.Response()
Expand All @@ -375,12 +384,16 @@ def test_add_comment_fail(self):

mock_time_lost_patcher = patch("manager.utils.update_time_lost")
mock_time_lost_client = mock_time_lost_patcher.start()
time_response = requests.Response()
time_response.status_code = 400
mock_time_lost_client.return_value = time_response
time_lost_response = rest_framework.response.Response()
time_lost_response.status_code = 400
time_lost_response.data = {
"ack": "Jira time_lost field could not be updated",
}
mock_time_lost_client.return_value = time_lost_response

resp = jira_comment(self.jira_request_narrative_full_jira_comment.data)
assert resp.status_code == 400
assert resp.data["ack"] == "Jira time_lost field could not be updated"

@patch.dict(os.environ, {"JIRA_API_HOSTNAME": "jira.lsstcorp.org"})
def test_handle_narrative_jira_payload(self):
Expand Down
2 changes: 1 addition & 1 deletion manager/manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def jira_comment(request_data):
timelost_response = update_time_lost(
jira_id=jira_id, add_time_lost=request_data.get("time_lost", 0.0)
)
if timelost_response.status_code == 400:
if timelost_response.status_code != 200:
return timelost_response

if response.status_code == 201:
Expand Down

0 comments on commit 00a639d

Please sign in to comment.