|
| 1 | +import json |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from pytest_mock import MockerFixture |
3 | 5 |
|
@@ -79,37 +81,81 @@ def test_data_dog_track_event_not_called_on_audit_log_saved_when_wrong(mocker, p |
79 | 81 | datadog_mock.track_event_async.assert_not_called() |
80 | 82 |
|
81 | 83 |
|
82 | | -def test_data_dog_track_event_called_on_audit_log_saved_when_correct_type( # type: ignore[no-untyped-def] |
83 | | - project, mocker |
84 | | -): |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "use_custom_source, expected_additional_data", |
| 86 | + [(False, {}), (True, {"source_type_name": "flagsmith"})], |
| 87 | +) |
| 88 | +def test_data_dog_track_event_called_on_audit_log_saved_when_correct_type( |
| 89 | + project: Project, |
| 90 | + mocker: MockerFixture, |
| 91 | + use_custom_source: bool, |
| 92 | + expected_additional_data: dict[str, str], |
| 93 | +) -> None: |
85 | 94 | # Given project configured for Datadog integration |
86 | | - datadog_mock = mocker.patch( |
87 | | - "integrations.datadog.datadog.DataDogWrapper.track_event_async" |
88 | | - ) |
| 95 | + requests_session_mock = mocker.patch( |
| 96 | + "integrations.datadog.datadog.requests.Session" |
| 97 | + ).return_value |
89 | 98 |
|
90 | 99 | DataDogConfiguration.objects.create( |
91 | | - project=project, base_url='http"//test.com', api_key="123key" |
| 100 | + project=project, |
| 101 | + base_url="http://test.com", |
| 102 | + api_key="123key", |
| 103 | + use_custom_source=use_custom_source, |
92 | 104 | ) |
93 | 105 |
|
94 | 106 | # When Audit logs created with correct type |
95 | 107 | AuditLog.objects.create( |
96 | 108 | project=project, |
97 | | - log="Some audit log", |
| 109 | + log="Some audit log for feature", |
98 | 110 | related_object_type=RelatedObjectType.FEATURE.name, |
99 | 111 | ) |
100 | 112 | AuditLog.objects.create( |
101 | 113 | project=project, |
102 | | - log="Some audit log", |
| 114 | + log="Some audit log for feature state", |
103 | 115 | related_object_type=RelatedObjectType.FEATURE_STATE.name, |
104 | 116 | ) |
105 | 117 | AuditLog.objects.create( |
106 | 118 | project=project, |
107 | | - log="Some audit log", |
| 119 | + log="Some audit log for segment", |
108 | 120 | related_object_type=RelatedObjectType.SEGMENT.name, |
109 | 121 | ) |
110 | 122 |
|
111 | 123 | # Then datadog track even triggered for each AuditLog |
112 | | - assert 3 == datadog_mock.call_count |
| 124 | + assert requests_session_mock.post.call_args_list == [ |
| 125 | + mocker.call( |
| 126 | + "http://test.com/api/v1/events?api_key=123key", |
| 127 | + data=json.dumps( |
| 128 | + { |
| 129 | + "text": "Some audit log for feature by user system", |
| 130 | + "title": "Flagsmith Feature Flag Event", |
| 131 | + "tags": ["env:unknown"], |
| 132 | + **expected_additional_data, |
| 133 | + } |
| 134 | + ), |
| 135 | + ), |
| 136 | + mocker.call( |
| 137 | + "http://test.com/api/v1/events?api_key=123key", |
| 138 | + data=json.dumps( |
| 139 | + { |
| 140 | + "text": "Some audit log for feature state by user system", |
| 141 | + "title": "Flagsmith Feature Flag Event", |
| 142 | + "tags": ["env:unknown"], |
| 143 | + **expected_additional_data, |
| 144 | + } |
| 145 | + ), |
| 146 | + ), |
| 147 | + mocker.call( |
| 148 | + "http://test.com/api/v1/events?api_key=123key", |
| 149 | + data=json.dumps( |
| 150 | + { |
| 151 | + "text": "Some audit log for segment by user system", |
| 152 | + "title": "Flagsmith Feature Flag Event", |
| 153 | + "tags": ["env:unknown"], |
| 154 | + **expected_additional_data, |
| 155 | + } |
| 156 | + ), |
| 157 | + ), |
| 158 | + ] |
113 | 159 |
|
114 | 160 |
|
115 | 161 | def test_audit_log_get_history_record_model_class(mocker): # type: ignore[no-untyped-def] |
|
0 commit comments