12
12
from apps .api .permissions import LegacyAccessControlRole
13
13
from apps .grafana_plugin .serializers .sync_data import SyncTeamSerializer
14
14
from apps .grafana_plugin .sync_data import SyncData , SyncSettings , SyncUser
15
- from apps .grafana_plugin .tasks .sync_v2 import start_sync_organizations_v2
15
+ from apps .grafana_plugin .tasks .sync_v2 import start_sync_organizations_v2 , sync_organizations_v2
16
+ from common .constants .plugin_ids import PluginID
16
17
17
18
18
19
@pytest .mark .django_db
@@ -121,6 +122,7 @@ def test_sync_v2_content_encoding(
121
122
incident_enabled = False ,
122
123
incident_backend_url = "" ,
123
124
labels_enabled = False ,
125
+ irm_enabled = False ,
124
126
),
125
127
)
126
128
@@ -140,6 +142,57 @@ def test_sync_v2_content_encoding(
140
142
mock_sync .assert_called ()
141
143
142
144
145
+ @pytest .mark .parametrize (
146
+ "irm_enabled,expected" ,
147
+ [
148
+ (True , True ),
149
+ (False , False ),
150
+ ],
151
+ )
152
+ @pytest .mark .django_db
153
+ def test_sync_v2_irm_enabled (
154
+ make_organization_and_user_with_plugin_token ,
155
+ make_user_auth_headers ,
156
+ settings ,
157
+ irm_enabled ,
158
+ expected ,
159
+ ):
160
+ settings .LICENSE = settings .CLOUD_LICENSE_NAME
161
+ organization , _ , token = make_organization_and_user_with_plugin_token ()
162
+
163
+ assert organization .is_grafana_irm_enabled is False
164
+
165
+ client = APIClient ()
166
+ headers = make_user_auth_headers (None , token , organization = organization )
167
+ url = reverse ("grafana-plugin:sync-v2" )
168
+
169
+ data = SyncData (
170
+ users = [],
171
+ teams = [],
172
+ team_members = {},
173
+ settings = SyncSettings (
174
+ stack_id = organization .stack_id ,
175
+ org_id = organization .org_id ,
176
+ license = settings .CLOUD_LICENSE_NAME ,
177
+ oncall_api_url = "http://localhost" ,
178
+ oncall_token = "" ,
179
+ grafana_url = "http://localhost" ,
180
+ grafana_token = "fake_token" ,
181
+ rbac_enabled = False ,
182
+ incident_enabled = False ,
183
+ incident_backend_url = "" ,
184
+ labels_enabled = False ,
185
+ irm_enabled = irm_enabled ,
186
+ ),
187
+ )
188
+
189
+ response = client .post (url , format = "json" , data = asdict (data ), ** headers )
190
+ assert response .status_code == status .HTTP_200_OK
191
+
192
+ organization .refresh_from_db ()
193
+ assert organization .is_grafana_irm_enabled == expected
194
+
195
+
143
196
@pytest .mark .parametrize (
144
197
"test_team, validation_pass" ,
145
198
[
@@ -190,3 +243,23 @@ def check_call(actual, expected):
190
243
assert check_call (actual_call , expected_call )
191
244
192
245
assert mock_sync .call_count == len (expected_calls )
246
+
247
+
248
+ @patch (
249
+ "apps.grafana_plugin.tasks.sync_v2.GrafanaAPIClient.api_post" ,
250
+ return_value = (None , {"status_code" : status .HTTP_200_OK }),
251
+ )
252
+ @pytest .mark .parametrize (
253
+ "is_grafana_irm_enabled,expected" ,
254
+ [
255
+ (True , PluginID .IRM ),
256
+ (False , PluginID .ONCALL ),
257
+ ],
258
+ )
259
+ @pytest .mark .django_db
260
+ def test_sync_organizations_v2_calls_right_backend_plugin_sync_endpoint (
261
+ mocked_grafana_api_client_api_post , make_organization , is_grafana_irm_enabled , expected
262
+ ):
263
+ org = make_organization (is_grafana_irm_enabled = is_grafana_irm_enabled )
264
+ sync_organizations_v2 (org_ids = [org .pk ])
265
+ mocked_grafana_api_client_api_post .assert_called_once_with (f"api/plugins/{ expected } /resources/plugin/sync" )
0 commit comments