|
| 1 | +from sentry.notifications.types import FallthroughChoiceType |
| 2 | +from sentry.testutils.cases import TestCase |
| 3 | +from sentry.workflow_engine.defaults.detectors import ensure_default_detectors |
| 4 | +from sentry.workflow_engine.defaults.workflows import ( |
| 5 | + connect_workflows_to_issue_stream, |
| 6 | + create_priority_workflow, |
| 7 | + ensure_default_workflows, |
| 8 | +) |
| 9 | +from sentry.workflow_engine.models import ( |
| 10 | + Action, |
| 11 | + DataCondition, |
| 12 | + DataConditionGroup, |
| 13 | + DataConditionGroupAction, |
| 14 | + Detector, |
| 15 | + DetectorWorkflow, |
| 16 | + Workflow, |
| 17 | + WorkflowDataConditionGroup, |
| 18 | +) |
| 19 | +from sentry.workflow_engine.models.data_condition import Condition |
| 20 | +from sentry.workflow_engine.typings.grouptype import IssueStreamGroupType |
| 21 | + |
| 22 | + |
| 23 | +class TestConnectWorkflowsToIssueStream(TestCase): |
| 24 | + def test_creates_detector_workflow_connections(self) -> None: |
| 25 | + project = self.create_project() |
| 26 | + workflow1 = Workflow.objects.create( |
| 27 | + organization=project.organization, |
| 28 | + name="Test Workflow 1", |
| 29 | + ) |
| 30 | + workflow2 = Workflow.objects.create( |
| 31 | + organization=project.organization, |
| 32 | + name="Test Workflow 2", |
| 33 | + ) |
| 34 | + |
| 35 | + connections = connect_workflows_to_issue_stream(project, [workflow1, workflow2]) |
| 36 | + |
| 37 | + assert len(connections) == 2 |
| 38 | + assert DetectorWorkflow.objects.filter(workflow=workflow1).exists() |
| 39 | + assert DetectorWorkflow.objects.filter(workflow=workflow2).exists() |
| 40 | + |
| 41 | + # Verify all workflows are connected to the same issue stream detector |
| 42 | + detector_ids = {c.detector_id for c in connections} |
| 43 | + assert len(detector_ids) == 1 |
| 44 | + detector = Detector.objects.get(id=detector_ids.pop()) |
| 45 | + assert detector.type == IssueStreamGroupType.slug |
| 46 | + |
| 47 | + def test_uses_issue_stream_detector(self) -> None: |
| 48 | + project = self.create_project() |
| 49 | + workflow = Workflow.objects.create( |
| 50 | + organization=project.organization, |
| 51 | + name="Test Workflow", |
| 52 | + ) |
| 53 | + |
| 54 | + connections = connect_workflows_to_issue_stream(project, [workflow]) |
| 55 | + |
| 56 | + connection = connections[0] |
| 57 | + assert connection.detector.type == IssueStreamGroupType.slug |
| 58 | + assert connection.detector.project_id == project.id |
| 59 | + |
| 60 | + # Verify only one issue stream detector exists |
| 61 | + issue_stream_detectors = Detector.objects.filter( |
| 62 | + project=project, type=IssueStreamGroupType.slug |
| 63 | + ) |
| 64 | + assert issue_stream_detectors.count() == 1 |
| 65 | + |
| 66 | + def test_uses_preexisting_issue_stream_detector(self) -> None: |
| 67 | + """Integration test: verifies that if an issue stream detector already exists, it reuses it.""" |
| 68 | + project = self.create_project() |
| 69 | + |
| 70 | + # Create the default detectors first (simulating project setup signal) |
| 71 | + default_detectors = ensure_default_detectors(project) |
| 72 | + existing_detector = default_detectors[IssueStreamGroupType.slug] |
| 73 | + |
| 74 | + # Now connect workflows - should use the existing detector |
| 75 | + workflow = Workflow.objects.create( |
| 76 | + organization=project.organization, |
| 77 | + name="Test Workflow", |
| 78 | + ) |
| 79 | + connections = connect_workflows_to_issue_stream(project, [workflow]) |
| 80 | + |
| 81 | + # Verify it used the pre-existing detector |
| 82 | + assert connections[0].detector_id == existing_detector.id |
| 83 | + |
| 84 | + # Verify still only one issue stream detector exists |
| 85 | + issue_stream_detectors = Detector.objects.filter( |
| 86 | + project=project, type=IssueStreamGroupType.slug |
| 87 | + ) |
| 88 | + assert issue_stream_detectors.count() == 1 |
| 89 | + |
| 90 | + |
| 91 | +class TestCreatePriorityWorkflow(TestCase): |
| 92 | + def test_creates_workflow_with_correct_name(self) -> None: |
| 93 | + org = self.create_organization() |
| 94 | + |
| 95 | + workflow = create_priority_workflow(org) |
| 96 | + |
| 97 | + assert workflow.name == "Send a notification for high priority issues" |
| 98 | + assert workflow.organization_id == org.id |
| 99 | + |
| 100 | + def test_creates_when_condition_group(self) -> None: |
| 101 | + org = self.create_organization() |
| 102 | + |
| 103 | + workflow = create_priority_workflow(org) |
| 104 | + |
| 105 | + assert workflow.when_condition_group is not None |
| 106 | + assert workflow.when_condition_group.logic_type == DataConditionGroup.Type.ANY_SHORT_CIRCUIT |
| 107 | + |
| 108 | + def test_creates_data_conditions(self) -> None: |
| 109 | + org = self.create_organization() |
| 110 | + |
| 111 | + workflow = create_priority_workflow(org) |
| 112 | + |
| 113 | + conditions = DataCondition.objects.filter(condition_group=workflow.when_condition_group) |
| 114 | + assert conditions.count() == 2 |
| 115 | + |
| 116 | + condition_types = {c.type for c in conditions} |
| 117 | + assert Condition.NEW_HIGH_PRIORITY_ISSUE in condition_types |
| 118 | + assert Condition.EXISTING_HIGH_PRIORITY_ISSUE in condition_types |
| 119 | + |
| 120 | + for condition in conditions: |
| 121 | + assert condition.comparison is True |
| 122 | + assert condition.condition_result is True |
| 123 | + |
| 124 | + def test_creates_email_action(self) -> None: |
| 125 | + org = self.create_organization() |
| 126 | + |
| 127 | + create_priority_workflow(org) |
| 128 | + |
| 129 | + action = Action.objects.get(type=Action.Type.EMAIL) |
| 130 | + assert action.config == { |
| 131 | + "target_type": "IssueOwners", |
| 132 | + "target_identifier": None, |
| 133 | + "fallthrough_type": FallthroughChoiceType.ACTIVE_MEMBERS.value, |
| 134 | + } |
| 135 | + |
| 136 | + def test_creates_action_filter_and_links(self) -> None: |
| 137 | + org = self.create_organization() |
| 138 | + |
| 139 | + workflow = create_priority_workflow(org) |
| 140 | + |
| 141 | + # Verify WorkflowDataConditionGroup exists |
| 142 | + workflow_dcg = WorkflowDataConditionGroup.objects.get(workflow=workflow) |
| 143 | + action_filter = workflow_dcg.condition_group |
| 144 | + |
| 145 | + # Verify action is linked to the filter |
| 146 | + action = Action.objects.get(type=Action.Type.EMAIL) |
| 147 | + dcg_action = DataConditionGroupAction.objects.get(action=action) |
| 148 | + assert dcg_action.condition_group == action_filter |
| 149 | + |
| 150 | + # Verify action filter has correct logic type |
| 151 | + assert action_filter.logic_type == DataConditionGroup.Type.ANY_SHORT_CIRCUIT |
| 152 | + |
| 153 | + def test_idempotent_returns_existing_workflow(self) -> None: |
| 154 | + org = self.create_organization() |
| 155 | + |
| 156 | + workflow1 = create_priority_workflow(org) |
| 157 | + workflow2 = create_priority_workflow(org) |
| 158 | + |
| 159 | + assert workflow1.id == workflow2.id |
| 160 | + # Should only have one workflow |
| 161 | + assert ( |
| 162 | + Workflow.objects.filter( |
| 163 | + organization=org, name="Send a notification for high priority issues" |
| 164 | + ).count() |
| 165 | + == 1 |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +class TestEnsureDefaultWorkflows(TestCase): |
| 170 | + def test_creates_and_connects_workflows(self) -> None: |
| 171 | + project = self.create_project() |
| 172 | + |
| 173 | + workflows = ensure_default_workflows(project) |
| 174 | + |
| 175 | + assert len(workflows) == 1 |
| 176 | + workflow = workflows[0] |
| 177 | + assert workflow.name == "Send a notification for high priority issues" |
| 178 | + |
| 179 | + # Verify connection to issue stream detector |
| 180 | + connection = DetectorWorkflow.objects.get(workflow=workflow) |
| 181 | + assert connection.detector.type == IssueStreamGroupType.slug |
| 182 | + assert connection.detector.project_id == project.id |
| 183 | + |
| 184 | + def test_returns_workflows_list(self) -> None: |
| 185 | + project = self.create_project() |
| 186 | + |
| 187 | + workflows = ensure_default_workflows(project) |
| 188 | + |
| 189 | + assert isinstance(workflows, list) |
| 190 | + assert all(isinstance(w, Workflow) for w in workflows) |
0 commit comments