3
3
from rest_framework import status
4
4
from rest_framework .test import APIClient
5
5
6
- from apps .alerts .models import CustomButton
6
+ from apps .webhooks .models import Webhook
7
7
8
8
9
9
@pytest .mark .django_db
10
- def test_get_custom_actions (
11
- make_organization_and_user_with_token ,
12
- make_custom_action ,
13
- ):
10
+ def test_get_custom_actions (make_organization_and_user_with_token , make_custom_webhook ):
14
11
organization , user , token = make_organization_and_user_with_token ()
15
12
client = APIClient ()
16
13
17
- custom_action = make_custom_action (organization = organization )
14
+ custom_action = make_custom_webhook (organization = organization )
18
15
19
16
url = reverse ("api-public:actions-list" )
20
17
@@ -29,12 +26,18 @@ def test_get_custom_actions(
29
26
"id" : custom_action .public_primary_key ,
30
27
"name" : custom_action .name ,
31
28
"team_id" : None ,
32
- "url" : custom_action .webhook ,
29
+ "url" : custom_action .url ,
33
30
"data" : custom_action .data ,
34
31
"user" : custom_action .user ,
35
32
"password" : custom_action .password ,
36
33
"authorization_header" : custom_action .authorization_header ,
37
- "forward_whole_payload" : custom_action .forward_whole_payload ,
34
+ "forward_whole_payload" : custom_action .forward_all ,
35
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
36
+ "trigger_template" : custom_action .trigger_template ,
37
+ "headers" : custom_action .headers ,
38
+ "http_method" : custom_action .http_method ,
39
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
40
+ "integration_filter" : custom_action .integration_filter ,
38
41
}
39
42
],
40
43
"current_page_number" : 1 ,
@@ -49,13 +52,13 @@ def test_get_custom_actions(
49
52
@pytest .mark .django_db
50
53
def test_get_custom_actions_filter_by_name (
51
54
make_organization_and_user_with_token ,
52
- make_custom_action ,
55
+ make_custom_webhook ,
53
56
):
54
57
organization , user , token = make_organization_and_user_with_token ()
55
58
client = APIClient ()
56
59
57
- custom_action = make_custom_action (organization = organization )
58
- make_custom_action (organization = organization )
60
+ custom_action = make_custom_webhook (organization = organization )
61
+ make_custom_webhook (organization = organization )
59
62
url = reverse ("api-public:actions-list" )
60
63
61
64
response = client .get (f"{ url } ?name={ custom_action .name } " , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
@@ -69,12 +72,18 @@ def test_get_custom_actions_filter_by_name(
69
72
"id" : custom_action .public_primary_key ,
70
73
"name" : custom_action .name ,
71
74
"team_id" : None ,
72
- "url" : custom_action .webhook ,
75
+ "url" : custom_action .url ,
73
76
"data" : custom_action .data ,
74
- "user" : custom_action .user ,
77
+ "user" : custom_action .username ,
75
78
"password" : custom_action .password ,
76
79
"authorization_header" : custom_action .authorization_header ,
77
- "forward_whole_payload" : custom_action .forward_whole_payload ,
80
+ "forward_whole_payload" : custom_action .forward_all ,
81
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
82
+ "trigger_template" : custom_action .trigger_template ,
83
+ "headers" : custom_action .headers ,
84
+ "http_method" : custom_action .http_method ,
85
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
86
+ "integration_filter" : custom_action .integration_filter ,
78
87
}
79
88
],
80
89
"current_page_number" : 1 ,
@@ -89,12 +98,12 @@ def test_get_custom_actions_filter_by_name(
89
98
@pytest .mark .django_db
90
99
def test_get_custom_actions_filter_by_name_empty_result (
91
100
make_organization_and_user_with_token ,
92
- make_custom_action ,
101
+ make_custom_webhook ,
93
102
):
94
103
organization , user , token = make_organization_and_user_with_token ()
95
104
client = APIClient ()
96
105
97
- make_custom_action (organization = organization )
106
+ make_custom_webhook (organization = organization )
98
107
99
108
url = reverse ("api-public:actions-list" )
100
109
@@ -117,12 +126,12 @@ def test_get_custom_actions_filter_by_name_empty_result(
117
126
@pytest .mark .django_db
118
127
def test_get_custom_action (
119
128
make_organization_and_user_with_token ,
120
- make_custom_action ,
129
+ make_custom_webhook ,
121
130
):
122
131
organization , user , token = make_organization_and_user_with_token ()
123
132
client = APIClient ()
124
133
125
- custom_action = make_custom_action (organization = organization )
134
+ custom_action = make_custom_webhook (organization = organization )
126
135
127
136
url = reverse ("api-public:actions-detail" , kwargs = {"pk" : custom_action .public_primary_key })
128
137
@@ -132,12 +141,18 @@ def test_get_custom_action(
132
141
"id" : custom_action .public_primary_key ,
133
142
"name" : custom_action .name ,
134
143
"team_id" : None ,
135
- "url" : custom_action .webhook ,
144
+ "url" : custom_action .url ,
136
145
"data" : custom_action .data ,
137
- "user" : custom_action .user ,
146
+ "user" : custom_action .username ,
138
147
"password" : custom_action .password ,
139
148
"authorization_header" : custom_action .authorization_header ,
140
- "forward_whole_payload" : custom_action .forward_whole_payload ,
149
+ "forward_whole_payload" : custom_action .forward_all ,
150
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
151
+ "trigger_template" : custom_action .trigger_template ,
152
+ "headers" : custom_action .headers ,
153
+ "http_method" : custom_action .http_method ,
154
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
155
+ "integration_filter" : custom_action .integration_filter ,
141
156
}
142
157
143
158
assert response .status_code == status .HTTP_200_OK
@@ -158,18 +173,24 @@ def test_create_custom_action(make_organization_and_user_with_token):
158
173
159
174
response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
160
175
161
- custom_action = CustomButton .objects .get (public_primary_key = response .data ["id" ])
176
+ custom_action = Webhook .objects .get (public_primary_key = response .data ["id" ])
162
177
163
178
expected_result = {
164
179
"id" : custom_action .public_primary_key ,
165
180
"name" : custom_action .name ,
166
181
"team_id" : None ,
167
- "url" : custom_action .webhook ,
182
+ "url" : custom_action .url ,
168
183
"data" : custom_action .data ,
169
- "user" : custom_action .user ,
184
+ "user" : custom_action .username ,
170
185
"password" : custom_action .password ,
171
186
"authorization_header" : custom_action .authorization_header ,
172
- "forward_whole_payload" : custom_action .forward_whole_payload ,
187
+ "forward_whole_payload" : custom_action .forward_all ,
188
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
189
+ "trigger_template" : custom_action .trigger_template ,
190
+ "headers" : custom_action .headers ,
191
+ "http_method" : custom_action .http_method ,
192
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
193
+ "integration_filter" : custom_action .integration_filter ,
173
194
}
174
195
175
196
assert response .status_code == status .HTTP_201_CREATED
@@ -195,18 +216,24 @@ def test_create_custom_action_nested_data(make_organization_and_user_with_token)
195
216
196
217
response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
197
218
198
- custom_action = CustomButton .objects .get (public_primary_key = response .data ["id" ])
219
+ custom_action = Webhook .objects .get (public_primary_key = response .data ["id" ])
199
220
200
221
expected_result = {
201
222
"id" : custom_action .public_primary_key ,
202
223
"name" : custom_action .name ,
203
224
"team_id" : None ,
204
- "url" : custom_action .webhook ,
225
+ "url" : custom_action .url ,
205
226
"data" : custom_action .data ,
206
- "user" : custom_action .user ,
227
+ "user" : custom_action .username ,
207
228
"password" : custom_action .password ,
208
229
"authorization_header" : custom_action .authorization_header ,
209
- "forward_whole_payload" : custom_action .forward_whole_payload ,
230
+ "forward_whole_payload" : custom_action .forward_all ,
231
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
232
+ "trigger_template" : custom_action .trigger_template ,
233
+ "headers" : custom_action .headers ,
234
+ "http_method" : custom_action .http_method ,
235
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
236
+ "integration_filter" : custom_action .integration_filter ,
210
237
}
211
238
212
239
assert response .status_code == status .HTTP_201_CREATED
@@ -232,18 +259,24 @@ def test_create_custom_action_valid_after_render(make_organization_and_user_with
232
259
233
260
response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
234
261
235
- custom_action = CustomButton .objects .get (public_primary_key = response .data ["id" ])
262
+ custom_action = Webhook .objects .get (public_primary_key = response .data ["id" ])
236
263
237
264
expected_result = {
238
265
"id" : custom_action .public_primary_key ,
239
266
"name" : custom_action .name ,
240
267
"team_id" : None ,
241
- "url" : custom_action .webhook ,
268
+ "url" : custom_action .url ,
242
269
"data" : custom_action .data ,
243
- "user" : custom_action .user ,
270
+ "user" : custom_action .username ,
244
271
"password" : custom_action .password ,
245
272
"authorization_header" : custom_action .authorization_header ,
246
- "forward_whole_payload" : custom_action .forward_whole_payload ,
273
+ "forward_whole_payload" : custom_action .forward_all ,
274
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
275
+ "trigger_template" : custom_action .trigger_template ,
276
+ "headers" : custom_action .headers ,
277
+ "http_method" : custom_action .http_method ,
278
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
279
+ "integration_filter" : custom_action .integration_filter ,
247
280
}
248
281
249
282
assert response .status_code == status .HTTP_201_CREATED
@@ -269,94 +302,39 @@ def test_create_custom_action_valid_after_render_use_all_data(make_organization_
269
302
270
303
response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
271
304
272
- custom_action = CustomButton .objects .get (public_primary_key = response .data ["id" ])
305
+ custom_action = Webhook .objects .get (public_primary_key = response .data ["id" ])
273
306
274
307
expected_result = {
275
308
"id" : custom_action .public_primary_key ,
276
309
"name" : custom_action .name ,
277
310
"team_id" : None ,
278
- "url" : custom_action .webhook ,
311
+ "url" : custom_action .url ,
279
312
"data" : custom_action .data ,
280
- "user" : custom_action .user ,
313
+ "user" : custom_action .username ,
281
314
"password" : custom_action .password ,
282
315
"authorization_header" : custom_action .authorization_header ,
283
- "forward_whole_payload" : custom_action .forward_whole_payload ,
316
+ "forward_whole_payload" : custom_action .forward_all ,
317
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
318
+ "trigger_template" : custom_action .trigger_template ,
319
+ "headers" : custom_action .headers ,
320
+ "http_method" : custom_action .http_method ,
321
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
322
+ "integration_filter" : custom_action .integration_filter ,
284
323
}
285
324
286
325
assert response .status_code == status .HTTP_201_CREATED
287
326
assert response .json () == expected_result
288
327
289
328
290
- @pytest .mark .django_db
291
- def test_create_custom_action_invalid_data (
292
- make_organization_and_user_with_token ,
293
- ):
294
- organization , user , token = make_organization_and_user_with_token ()
295
- client = APIClient ()
296
-
297
- url = reverse ("api-public:actions-list" )
298
-
299
- data = {
300
- "name" : "Test outgoing webhook" ,
301
- "url" : "invalid_url" ,
302
- }
303
-
304
- response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
305
-
306
- assert response .status_code == status .HTTP_400_BAD_REQUEST
307
- assert response .data ["url" ][0 ] == "URL is incorrect"
308
-
309
- data = {
310
- "name" : "Test outgoing webhook" ,
311
- }
312
-
313
- response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
314
-
315
- assert response .status_code == status .HTTP_400_BAD_REQUEST
316
- assert response .data ["url" ][0 ] == "This field is required."
317
-
318
- data = {
319
- "url" : "https://example.com" ,
320
- }
321
-
322
- response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
323
-
324
- assert response .status_code == status .HTTP_400_BAD_REQUEST
325
- assert response .data ["name" ][0 ] == "This field is required."
326
-
327
- data = {
328
- "name" : "Test outgoing webhook" ,
329
- "url" : "https://example.com" ,
330
- "data" : "invalid_json" ,
331
- }
332
-
333
- response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
334
-
335
- assert response .status_code == status .HTTP_400_BAD_REQUEST
336
- assert response .data ["data" ][0 ] == "Data has incorrect format"
337
-
338
- data = {
339
- "name" : "Test outgoing webhook" ,
340
- "url" : "https://example.com" ,
341
- # This would need a `| tojson` or some double quotes around it to pass validation.
342
- "data" : "{{ alert_payload.name }}" ,
343
- }
344
-
345
- response = client .post (url , data = data , format = "json" , HTTP_AUTHORIZATION = f"{ token } " )
346
-
347
- assert response .status_code == status .HTTP_400_BAD_REQUEST
348
- assert response .data ["data" ][0 ] == "Data has incorrect format"
349
-
350
-
351
329
@pytest .mark .django_db
352
330
def test_update_custom_action (
353
331
make_organization_and_user_with_token ,
354
- make_custom_action ,
332
+ make_custom_webhook ,
355
333
):
356
334
organization , user , token = make_organization_and_user_with_token ()
357
335
client = APIClient ()
358
336
359
- custom_action = make_custom_action (organization = organization )
337
+ custom_action = make_custom_webhook (organization = organization )
360
338
361
339
url = reverse ("api-public:actions-detail" , kwargs = {"pk" : custom_action .public_primary_key })
362
340
@@ -372,12 +350,18 @@ def test_update_custom_action(
372
350
"id" : custom_action .public_primary_key ,
373
351
"name" : data ["name" ],
374
352
"team_id" : None ,
375
- "url" : custom_action .webhook ,
353
+ "url" : custom_action .url ,
376
354
"data" : custom_action .data ,
377
- "user" : custom_action .user ,
355
+ "user" : custom_action .username ,
378
356
"password" : custom_action .password ,
379
357
"authorization_header" : custom_action .authorization_header ,
380
- "forward_whole_payload" : custom_action .forward_whole_payload ,
358
+ "forward_whole_payload" : custom_action .forward_all ,
359
+ "is_webhook_enabled" : custom_action .is_webhook_enabled ,
360
+ "trigger_template" : custom_action .trigger_template ,
361
+ "headers" : custom_action .headers ,
362
+ "http_method" : custom_action .http_method ,
363
+ "trigger_type" : Webhook .PUBLIC_TRIGGER_TYPES_MAP [custom_action .trigger_type ],
364
+ "integration_filter" : custom_action .integration_filter ,
381
365
}
382
366
383
367
assert response .status_code == status .HTTP_200_OK
@@ -389,12 +373,12 @@ def test_update_custom_action(
389
373
@pytest .mark .django_db
390
374
def test_delete_custom_action (
391
375
make_organization_and_user_with_token ,
392
- make_custom_action ,
376
+ make_custom_webhook ,
393
377
):
394
378
organization , user , token = make_organization_and_user_with_token ()
395
379
client = APIClient ()
396
380
397
- custom_action = make_custom_action (organization = organization )
381
+ custom_action = make_custom_webhook (organization = organization )
398
382
url = reverse ("api-public:actions-detail" , kwargs = {"pk" : custom_action .public_primary_key })
399
383
400
384
assert custom_action .deleted_at is None
0 commit comments