From ca4c36acf000f54dbdc37c77e7ecb5b085c10e15 Mon Sep 17 00:00:00 2001 From: sidpremkumar Date: Jul 11 2019 15:40:33 +0000 Subject: adding more tests for username and comment functions --- diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py index acf6018..0e7bf1d 100644 --- a/sync2jira/downstream.py +++ b/sync2jira/downstream.py @@ -130,7 +130,8 @@ def _matching_jira_issue_query(client, issue, config, free=False): for result in results_of_query: # If the queried JIRA issue has the id of the upstream issue or the same title if issue.id in result.fields.description or issue.title == result.fields.summary: - search = check_comments_for_duplicate(client, result) + search = check_comments_for_duplicate(client, result, + find_username(issue, config)) if search is True: final_results.append(result) else: diff --git a/tests/test_downstream.py b/tests/test_downstream.py index 8bf18b9..0d50f51 100644 --- a/tests/test_downstream.py +++ b/tests/test_downstream.py @@ -28,8 +28,10 @@ class TestDownstream(unittest.TestCase): # Mock Config dict self.mock_config = { 'sync2jira': { + 'default_jira_instance': 'another_jira_instance', 'jira': { - 'mock_jira_instance': {'mock_jira': 'mock_jira'} + 'mock_jira_instance': {'mock_jira': 'mock_jira'}, + 'another_jira_instance': {'basic_auth': ['mock_user']} }, 'testing': {}, 'legacy_matching': False @@ -1048,3 +1050,85 @@ class TestDownstream(unittest.TestCase): '1234', resolution={'name': 'Duplicate'} ) + + @mock.patch(PATH + 'find_username') + @mock.patch(PATH + 'check_comments_for_duplicate') + @mock.patch('jira.client.JIRA') + def test_matching_jira_issue_query(self, + mock_client, + mock_check_comments_for_duplicates, + mock_find_username): + """ + This tests '_matching_jira_query' function + """ + # Set up return values + mock_downstream_issue = MagicMock() + self.mock_issue.upstream_title = 'mock_upstream_title' + mock_downstream_issue.fields.description = self.mock_issue.id + bad_downstream_issue = MagicMock() + bad_downstream_issue.fields.description = 'bad' + bad_downstream_issue.fields.summary = 'bad' + mock_client.search_issues.return_value = [mock_downstream_issue, bad_downstream_issue] + mock_check_comments_for_duplicates.return_value = True + mock_find_username.return_value = 'mock_username' + + # Call the function + response = d._matching_jira_issue_query( + client=mock_client, + issue=self.mock_issue, + config=self.mock_config + ) + + # Assert everything was called correctly + self.assertEqual(response, [mock_downstream_issue]) + mock_client.search_issues.assert_called_with( + 'issueFunction in linkedIssuesOfRemote("Upstream issue")' + ' and issueFunction in linkedIssuesOfRemote("mock_url")') + mock_check_comments_for_duplicates.assert_called_with( + mock_client, + mock_downstream_issue, + 'mock_username' + ) + mock_find_username.assert_called_with( + self.mock_issue, + self.mock_config + ) + + def test_find_username(self): + """ + Tests 'find_username' function + """ + # Call the function + response = d.find_username( + self.mock_issue, + self.mock_config + ) + + # Assert everything was called correctly + self.assertEqual(response, 'mock_user') + + + @mock.patch('jira.client.JIRA') + def check_comments_for_duplicates(self, + mock_client): + """ + Tests 'check_comments_for_duplicates' function + """ + # Set up return values + mock_comment = MagicMock() + mock_comment.body = 'Marking as duplicate of TEST-1234' + mock_comment.author.name = 'mock_user' + mock_client.comments.return_value = [mock_comment] + mock_client.issue.return_value = 'Successful Call!' + + # Call the function + response = d.check_comments_for_duplicate( + client=self.mock_client, + result=self.mock_downstream, + username='mock_user' + ) + + # Assert everything was called correctly + self.assertEqual(response, 'Successful Call!') + mock_client.comments.assert_called_with(self.mock_downstream) + mock_client.issue.assert_called_with('TEST-1234')