|
| 1 | +import json |
| 2 | +import re |
| 3 | +from datetime import UTC, datetime, timedelta |
| 4 | +from typing import Callable |
| 5 | +from unittest.mock import Mock |
| 6 | + |
| 7 | +import freezegun |
| 8 | +import pytest |
| 9 | +from pytest_mock import MockerFixture |
| 10 | + |
| 11 | +from features.models import Feature, FeatureState |
| 12 | +from integrations.sentry import change_tracking |
| 13 | +from integrations.sentry.models import SentryChangeTrackingConfiguration |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def sentry_configuration(environment: int) -> SentryChangeTrackingConfiguration: |
| 18 | + configuration = SentryChangeTrackingConfiguration( |
| 19 | + environment_id=environment, |
| 20 | + webhook_url="https://sentry.example.com/webhook", |
| 21 | + secret="hush hush!", |
| 22 | + ) |
| 23 | + configuration.save() |
| 24 | + return configuration |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def requests(mocker: MockerFixture) -> Mock: |
| 29 | + return mocker.patch.object(change_tracking, "requests") |
| 30 | + |
| 31 | + |
| 32 | +def test_sentry_change_tracking__flag_created__sends_update_to_sentry( |
| 33 | + mocker: MockerFixture, |
| 34 | + project: int, |
| 35 | + requests: Mock, |
| 36 | + sentry_configuration: SentryChangeTrackingConfiguration, |
| 37 | +) -> None: |
| 38 | + # Given |
| 39 | + feature_obj = Feature( |
| 40 | + name="yet_another_feature", |
| 41 | + project_id=project, |
| 42 | + ) |
| 43 | + |
| 44 | + # When |
| 45 | + with freezegun.freeze_time("2199-01-01T00:00:00.500000+00:00"): |
| 46 | + feature_obj.save() |
| 47 | + |
| 48 | + # Then |
| 49 | + requests.post.assert_called_once_with( |
| 50 | + url=sentry_configuration.webhook_url, |
| 51 | + headers={ |
| 52 | + "Content-Type": "application/json", |
| 53 | + "X-Sentry-Signature": mocker.ANY, |
| 54 | + }, |
| 55 | + data=json.dumps( |
| 56 | + { |
| 57 | + "data": [ |
| 58 | + { |
| 59 | + "action": "created", |
| 60 | + "created_at": "2199-01-01T00:00:00+00:00", |
| 61 | + "created_by": { "id": "[email protected]", "type": "email"}, |
| 62 | + "flag": feature_obj.name, |
| 63 | + }, |
| 64 | + ], |
| 65 | + "meta": {"version": 1}, |
| 66 | + }, |
| 67 | + sort_keys=True, |
| 68 | + ), |
| 69 | + ) |
| 70 | + signature = requests.post.call_args[1]["headers"]["X-Sentry-Signature"] |
| 71 | + assert re.match(r"^[0-9a-f]{64}$", signature) |
| 72 | + |
| 73 | + |
| 74 | +def test_sentry_change_tracking__flag_state_change__sends_update_to_sentry( |
| 75 | + feature_name: str, |
| 76 | + feature_state: int, |
| 77 | + mocker: MockerFixture, |
| 78 | + requests: Mock, |
| 79 | + sentry_configuration: SentryChangeTrackingConfiguration, |
| 80 | +) -> None: |
| 81 | + # Given |
| 82 | + feature_state_obj = FeatureState.objects.get(pk=feature_state) |
| 83 | + |
| 84 | + # When |
| 85 | + with freezegun.freeze_time("2199-01-01T00:00:00.500000+00:00"): |
| 86 | + now = datetime.now(UTC) |
| 87 | + feature_state_obj.enabled = False |
| 88 | + feature_state_obj.live_from = now + timedelta(hours=1) |
| 89 | + feature_state_obj.save() |
| 90 | + |
| 91 | + # Then |
| 92 | + requests.post.assert_called_once_with( |
| 93 | + url=sentry_configuration.webhook_url, |
| 94 | + headers={ |
| 95 | + "Content-Type": "application/json", |
| 96 | + "X-Sentry-Signature": mocker.ANY, |
| 97 | + }, |
| 98 | + data=json.dumps( |
| 99 | + { |
| 100 | + "data": [ |
| 101 | + { |
| 102 | + "action": "updated", |
| 103 | + "created_at": "2199-01-01T01:00:00+00:00", |
| 104 | + "created_by": { "id": "[email protected]", "type": "email"}, |
| 105 | + "change_id": str(feature_state), |
| 106 | + "flag": feature_name, |
| 107 | + }, |
| 108 | + ], |
| 109 | + "meta": {"version": 1}, |
| 110 | + }, |
| 111 | + sort_keys=True, |
| 112 | + ), |
| 113 | + ) |
| 114 | + signature = requests.post.call_args[1]["headers"]["X-Sentry-Signature"] |
| 115 | + assert re.match(r"^[0-9a-f]{64}$", signature) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize( |
| 119 | + "kaboom", |
| 120 | + [ |
| 121 | + lambda feature_state: feature_state.delete(), |
| 122 | + lambda feature_state: feature_state.feature.delete(), |
| 123 | + ], |
| 124 | +) |
| 125 | +def test_sentry_change_tracking__flag_deleted__sends_update_to_sentry( |
| 126 | + feature_name: str, |
| 127 | + feature_state: int, |
| 128 | + kaboom: Callable[[FeatureState], None], |
| 129 | + mocker: MockerFixture, |
| 130 | + requests: Mock, |
| 131 | + sentry_configuration: SentryChangeTrackingConfiguration, |
| 132 | +) -> None: |
| 133 | + # Given |
| 134 | + feature_state_obj = FeatureState.objects.get(pk=feature_state) |
| 135 | + |
| 136 | + # When |
| 137 | + with freezegun.freeze_time("2199-01-01T01:00:00+00:00"): |
| 138 | + kaboom(feature_state_obj) |
| 139 | + |
| 140 | + # Then |
| 141 | + requests.post.assert_called_once_with( |
| 142 | + url=sentry_configuration.webhook_url, |
| 143 | + headers={ |
| 144 | + "Content-Type": "application/json", |
| 145 | + "X-Sentry-Signature": mocker.ANY, |
| 146 | + }, |
| 147 | + data=json.dumps( |
| 148 | + { |
| 149 | + "data": [ |
| 150 | + { |
| 151 | + "action": "deleted", |
| 152 | + "created_at": "2199-01-01T01:00:00+00:00", |
| 153 | + "created_by": { "id": "[email protected]", "type": "email"}, |
| 154 | + "flag": feature_name, |
| 155 | + }, |
| 156 | + ], |
| 157 | + "meta": {"version": 1}, |
| 158 | + }, |
| 159 | + sort_keys=True, |
| 160 | + ), |
| 161 | + ) |
| 162 | + signature = requests.post.call_args[1]["headers"]["X-Sentry-Signature"] |
| 163 | + assert re.match(r"^[0-9a-f]{64}$", signature) |
0 commit comments