|
| 1 | +import logging |
| 2 | +from unittest.mock import Mock |
| 3 | + |
| 4 | +from dojo.jira_link import helper as jira_helper |
| 5 | +from unittests.dojo_test_case import DojoTestCase |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class JIRAHelperTest(DojoTestCase): |
| 11 | + |
| 12 | + """Unit tests for JIRA helper functions""" |
| 13 | + |
| 14 | + def create_mock_jira_issue(self, status_category_key=None, resolution=None): |
| 15 | + """ |
| 16 | + Helper to create a mock JIRA issue with configurable status category and resolution. |
| 17 | +
|
| 18 | + Args: |
| 19 | + status_category_key: The key for statusCategory (e.g., "new", "indeterminate", "done") |
| 20 | + resolution: Resolution value (None, "None", or a dict with resolution details) |
| 21 | +
|
| 22 | + """ |
| 23 | + issue = Mock() |
| 24 | + issue.fields = Mock() |
| 25 | + |
| 26 | + if status_category_key is not None: |
| 27 | + issue.fields.status = Mock() |
| 28 | + issue.fields.status.statusCategory = Mock() |
| 29 | + issue.fields.status.statusCategory.key = status_category_key |
| 30 | + else: |
| 31 | + # Simulate missing status or statusCategory |
| 32 | + del issue.fields.status |
| 33 | + |
| 34 | + issue.fields.resolution = resolution |
| 35 | + |
| 36 | + return issue |
| 37 | + |
| 38 | + def test_issue_from_jira_is_active_with_new_status(self): |
| 39 | + """Test that issues with 'new' status category are treated as active""" |
| 40 | + issue = self.create_mock_jira_issue(status_category_key="new") |
| 41 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 42 | + self.assertTrue(result, "Issue with 'new' status category should be active") |
| 43 | + |
| 44 | + def test_issue_from_jira_is_active_with_indeterminate_status(self): |
| 45 | + """Test that issues with 'indeterminate' status category are treated as active""" |
| 46 | + issue = self.create_mock_jira_issue(status_category_key="indeterminate") |
| 47 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 48 | + self.assertTrue(result, "Issue with 'indeterminate' status category should be active") |
| 49 | + |
| 50 | + def test_issue_from_jira_is_active_with_done_status(self): |
| 51 | + """Test that issues with 'done' status category are treated as inactive""" |
| 52 | + issue = self.create_mock_jira_issue(status_category_key="done") |
| 53 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 54 | + self.assertFalse(result, "Issue with 'done' status category should be inactive") |
| 55 | + |
| 56 | + def test_issue_from_jira_is_active_with_unknown_status_and_no_resolution(self): |
| 57 | + """Test that issues with unknown status category fall back to resolution check""" |
| 58 | + issue = self.create_mock_jira_issue(status_category_key="custom_status", resolution=None) |
| 59 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 60 | + self.assertTrue(result, "Issue with unknown status and no resolution should be active") |
| 61 | + |
| 62 | + def test_issue_from_jira_is_active_with_unknown_status_and_resolution(self): |
| 63 | + """Test that issues with unknown status category and resolution are treated as inactive""" |
| 64 | + resolution = {"id": "11", "name": "Fixed"} |
| 65 | + issue = self.create_mock_jira_issue(status_category_key="custom_status", resolution=resolution) |
| 66 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 67 | + self.assertFalse(result, "Issue with unknown status and resolution should be inactive") |
| 68 | + |
| 69 | + def test_issue_from_jira_is_active_with_unknown_status_and_none_resolution(self): |
| 70 | + """Test that issues with unknown status category and 'None' resolution are treated as active""" |
| 71 | + issue = self.create_mock_jira_issue(status_category_key="custom_status", resolution="None") |
| 72 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 73 | + self.assertTrue(result, "Issue with unknown status and 'None' resolution should be active") |
| 74 | + |
| 75 | + def test_issue_from_jira_is_active_without_status_category_and_no_resolution(self): |
| 76 | + """Test fallback to resolution check when status category is not available""" |
| 77 | + issue = Mock() |
| 78 | + issue.fields = Mock() |
| 79 | + issue.fields.resolution = None |
| 80 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 81 | + self.assertTrue(result, "Issue without status category and no resolution should be active") |
| 82 | + |
| 83 | + def test_issue_from_jira_is_active_without_status_category_with_resolution(self): |
| 84 | + """Test fallback to resolution check when status category is not available""" |
| 85 | + issue = Mock() |
| 86 | + issue.fields = Mock() |
| 87 | + issue.fields.resolution = {"id": "11", "name": "Fixed"} |
| 88 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 89 | + self.assertFalse(result, "Issue without status category but with resolution should be inactive") |
| 90 | + |
| 91 | + def test_issue_from_jira_is_active_without_status_category_with_none_string_resolution(self): |
| 92 | + """Test that 'None' string resolution is treated as active""" |
| 93 | + issue = Mock() |
| 94 | + issue.fields = Mock() |
| 95 | + issue.fields.resolution = "None" |
| 96 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 97 | + self.assertTrue(result, "Issue with 'None' string resolution should be active") |
| 98 | + |
| 99 | + def test_issue_from_jira_is_active_without_fields(self): |
| 100 | + """Test that issues without fields attribute fall back gracefully""" |
| 101 | + issue = Mock(spec=[]) # Mock with no attributes |
| 102 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 103 | + self.assertTrue(result, "Issue without fields should default to active") |
| 104 | + |
| 105 | + def test_issue_from_jira_is_active_with_missing_status_attribute(self): |
| 106 | + """Test AttributeError handling when status is missing""" |
| 107 | + issue = Mock() |
| 108 | + issue.fields = Mock(spec=["resolution"]) # Has fields but no status |
| 109 | + issue.fields.resolution = None |
| 110 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 111 | + self.assertTrue(result, "Issue with missing status attribute should fall back to resolution check") |
| 112 | + |
| 113 | + def test_issue_from_jira_is_active_with_missing_status_category(self): |
| 114 | + """Test AttributeError handling when statusCategory is missing""" |
| 115 | + issue = Mock() |
| 116 | + issue.fields = Mock() |
| 117 | + issue.fields.status = Mock(spec=[]) # Has status but no statusCategory |
| 118 | + issue.fields.resolution = None |
| 119 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 120 | + self.assertTrue(result, "Issue with missing statusCategory should fall back to resolution check") |
| 121 | + |
| 122 | + def test_issue_from_jira_is_active_with_missing_status_category_key(self): |
| 123 | + """Test AttributeError handling when statusCategory.key is missing""" |
| 124 | + issue = Mock() |
| 125 | + issue.fields = Mock() |
| 126 | + issue.fields.status = Mock() |
| 127 | + issue.fields.status.statusCategory = Mock(spec=[]) # Has statusCategory but no key |
| 128 | + issue.fields.resolution = None |
| 129 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 130 | + self.assertTrue(result, "Issue with missing statusCategory.key should fall back to resolution check") |
| 131 | + |
| 132 | + def test_issue_from_jira_is_active_status_category_takes_precedence(self): |
| 133 | + """Test that status category takes precedence over resolution""" |
| 134 | + # Create an issue with "done" status but no resolution |
| 135 | + issue = self.create_mock_jira_issue(status_category_key="done", resolution=None) |
| 136 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 137 | + self.assertFalse(result, "Status category should take precedence over resolution") |
| 138 | + |
| 139 | + # Create an issue with "new" status but has a resolution |
| 140 | + resolution = {"id": "11", "name": "Fixed"} |
| 141 | + issue = self.create_mock_jira_issue(status_category_key="new", resolution=resolution) |
| 142 | + result = jira_helper.issue_from_jira_is_active(issue) |
| 143 | + self.assertTrue(result, "Status category should take precedence over resolution") |
0 commit comments