|
| 1 | +from dataclasses import dataclass |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from sentry.testutils.cases import APITestCase |
| 5 | +from sentry.testutils.silo import region_silo_test |
| 6 | +from sentry.utils.registry import Registry |
| 7 | +from sentry.workflow_engine.models.data_condition import Condition |
| 8 | +from sentry.workflow_engine.types import DataConditionHandler |
| 9 | + |
| 10 | + |
| 11 | +class OrganizationDataConditionAPITestCase(APITestCase): |
| 12 | + endpoint = "sentry-api-0-organization-data-condition-index" |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + super().setUp() |
| 16 | + self.login_as(user=self.user) |
| 17 | + self.registry = Registry[DataConditionHandler](enable_reverse_lookup=False) |
| 18 | + self.registry_patcher = patch( |
| 19 | + "sentry.workflow_engine.registry.condition_handler_registry", |
| 20 | + new=self.registry, |
| 21 | + ) |
| 22 | + self.registry_patcher.__enter__() |
| 23 | + |
| 24 | + @self.registry.register(Condition.REAPPEARED_EVENT) |
| 25 | + @dataclass(frozen=True) |
| 26 | + class TestWorkflowTrigger(DataConditionHandler): |
| 27 | + type = DataConditionHandler.Type.WORKFLOW_TRIGGER |
| 28 | + comparison_json_schema = {"type": "boolean"} |
| 29 | + |
| 30 | + @self.registry.register(Condition.AGE_COMPARISON) |
| 31 | + @dataclass(frozen=True) |
| 32 | + class TestActionFilter(DataConditionHandler): |
| 33 | + type = DataConditionHandler.Type.ACTION_FILTER |
| 34 | + filter_group = DataConditionHandler.FilterGroup.ISSUE_ATTRIBUTES |
| 35 | + comparison_json_schema = { |
| 36 | + "type": "object", |
| 37 | + "properties": { |
| 38 | + "value": {"type": "integer", "minimum": 0}, |
| 39 | + }, |
| 40 | + "required": ["value"], |
| 41 | + "additionalProperties": False, |
| 42 | + } |
| 43 | + |
| 44 | + def tearDown(self) -> None: |
| 45 | + super().tearDown() |
| 46 | + self.registry_patcher.__exit__(None, None, None) |
| 47 | + |
| 48 | + |
| 49 | +@region_silo_test |
| 50 | +class OrganizationDataCondiitonIndexBaseTest(OrganizationDataConditionAPITestCase): |
| 51 | + def test_simple(self): |
| 52 | + response = self.get_success_response(self.organization.slug) |
| 53 | + assert len(response.data) == 2 |
| 54 | + |
| 55 | + def test_type_filter(self): |
| 56 | + response = self.get_success_response( |
| 57 | + self.organization.slug, type=DataConditionHandler.Type.WORKFLOW_TRIGGER |
| 58 | + ) |
| 59 | + assert len(response.data) == 1 |
| 60 | + assert response.data[0] == { |
| 61 | + "condition_id": Condition.REAPPEARED_EVENT.value, |
| 62 | + "type": DataConditionHandler.Type.WORKFLOW_TRIGGER.value, |
| 63 | + "comparison_json_schema": {"type": "boolean"}, |
| 64 | + } |
| 65 | + |
| 66 | + response = self.get_success_response( |
| 67 | + self.organization.slug, type=DataConditionHandler.Type.ACTION_FILTER |
| 68 | + ) |
| 69 | + assert len(response.data) == 1 |
| 70 | + assert response.data[0] == { |
| 71 | + "condition_id": Condition.AGE_COMPARISON.value, |
| 72 | + "type": DataConditionHandler.Type.ACTION_FILTER.value, |
| 73 | + "filter_group": DataConditionHandler.FilterGroup.ISSUE_ATTRIBUTES.value, |
| 74 | + "comparison_json_schema": { |
| 75 | + "type": "object", |
| 76 | + "properties": { |
| 77 | + "value": {"type": "integer", "minimum": 0}, |
| 78 | + }, |
| 79 | + "required": ["value"], |
| 80 | + "additionalProperties": False, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + def test_invalid_type(self): |
| 85 | + response = self.get_error_response(self.organization.slug, type="invalid") |
| 86 | + assert response.status_code == 400 |
0 commit comments