@@ -1190,6 +1190,206 @@ def test_slack_uses_list_of_custom_slack_channel():
1190
1190
assert expected_data2 == json .loads (mock_post_request .call_args_list [1 ][1 ]['data' ])
1191
1191
1192
1192
1193
+ def test_slack_attach_kibana_discover_url_when_generated ():
1194
+ rule = {
1195
+ 'name' : 'Test Rule' ,
1196
+ 'type' : 'any' ,
1197
+ 'slack_attach_kibana_discover_url' : True ,
1198
+ 'slack_webhook_url' : 'http://please.dontgohere.slack' ,
1199
+ 'alert' : []
1200
+ }
1201
+ rules_loader = FileRulesLoader ({})
1202
+ rules_loader .load_modules (rule )
1203
+ alert = SlackAlerter (rule )
1204
+ match = {
1205
+ '@timestamp' : '2016-01-01T00:00:00' ,
1206
+ 'kibana_discover_url' : 'http://kibana#discover'
1207
+ }
1208
+ with mock .patch ('requests.post' ) as mock_post_request :
1209
+ alert .alert ([match ])
1210
+
1211
+ expected_data = {
1212
+ 'username' : 'elastalert' ,
1213
+ 'parse' : 'none' ,
1214
+ 'text' : '' ,
1215
+ 'attachments' : [
1216
+ {
1217
+ 'color' : 'danger' ,
1218
+ 'title' : 'Test Rule' ,
1219
+ 'text' : BasicMatchString (rule , match ).__str__ (),
1220
+ 'mrkdwn_in' : ['text' , 'pretext' ],
1221
+ 'fields' : []
1222
+ },
1223
+ {
1224
+ 'color' : '#ec4b98' ,
1225
+ 'title' : 'Discover in Kibana' ,
1226
+ 'title_link' : 'http://kibana#discover'
1227
+ }
1228
+ ],
1229
+ 'icon_emoji' : ':ghost:' ,
1230
+ 'channel' : ''
1231
+ }
1232
+ mock_post_request .assert_called_once_with (
1233
+ rule ['slack_webhook_url' ],
1234
+ data = mock .ANY ,
1235
+ headers = {'content-type' : 'application/json' },
1236
+ proxies = None ,
1237
+ verify = False ,
1238
+ timeout = 10
1239
+ )
1240
+ actual_data = json .loads (mock_post_request .call_args_list [0 ][1 ]['data' ])
1241
+ assert expected_data == actual_data
1242
+
1243
+
1244
+ def test_slack_attach_kibana_discover_url_when_not_generated ():
1245
+ rule = {
1246
+ 'name' : 'Test Rule' ,
1247
+ 'type' : 'any' ,
1248
+ 'slack_attach_kibana_discover_url' : True ,
1249
+ 'slack_webhook_url' : 'http://please.dontgohere.slack' ,
1250
+ 'alert' : []
1251
+ }
1252
+ rules_loader = FileRulesLoader ({})
1253
+ rules_loader .load_modules (rule )
1254
+ alert = SlackAlerter (rule )
1255
+ match = {
1256
+ '@timestamp' : '2016-01-01T00:00:00'
1257
+ }
1258
+ with mock .patch ('requests.post' ) as mock_post_request :
1259
+ alert .alert ([match ])
1260
+
1261
+ expected_data = {
1262
+ 'username' : 'elastalert' ,
1263
+ 'parse' : 'none' ,
1264
+ 'text' : '' ,
1265
+ 'attachments' : [
1266
+ {
1267
+ 'color' : 'danger' ,
1268
+ 'title' : 'Test Rule' ,
1269
+ 'text' : BasicMatchString (rule , match ).__str__ (),
1270
+ 'mrkdwn_in' : ['text' , 'pretext' ],
1271
+ 'fields' : []
1272
+ }
1273
+ ],
1274
+ 'icon_emoji' : ':ghost:' ,
1275
+ 'channel' : ''
1276
+ }
1277
+ mock_post_request .assert_called_once_with (
1278
+ rule ['slack_webhook_url' ],
1279
+ data = mock .ANY ,
1280
+ headers = {'content-type' : 'application/json' },
1281
+ proxies = None ,
1282
+ verify = False ,
1283
+ timeout = 10
1284
+ )
1285
+ actual_data = json .loads (mock_post_request .call_args_list [0 ][1 ]['data' ])
1286
+ assert expected_data == actual_data
1287
+
1288
+
1289
+ def test_slack_kibana_discover_title ():
1290
+ rule = {
1291
+ 'name' : 'Test Rule' ,
1292
+ 'type' : 'any' ,
1293
+ 'slack_attach_kibana_discover_url' : True ,
1294
+ 'slack_kibana_discover_title' : 'Click to discover in Kibana' ,
1295
+ 'slack_webhook_url' : 'http://please.dontgohere.slack' ,
1296
+ 'alert' : []
1297
+ }
1298
+ rules_loader = FileRulesLoader ({})
1299
+ rules_loader .load_modules (rule )
1300
+ alert = SlackAlerter (rule )
1301
+ match = {
1302
+ '@timestamp' : '2016-01-01T00:00:00' ,
1303
+ 'kibana_discover_url' : 'http://kibana#discover'
1304
+ }
1305
+ with mock .patch ('requests.post' ) as mock_post_request :
1306
+ alert .alert ([match ])
1307
+
1308
+ expected_data = {
1309
+ 'username' : 'elastalert' ,
1310
+ 'parse' : 'none' ,
1311
+ 'text' : '' ,
1312
+ 'attachments' : [
1313
+ {
1314
+ 'color' : 'danger' ,
1315
+ 'title' : 'Test Rule' ,
1316
+ 'text' : BasicMatchString (rule , match ).__str__ (),
1317
+ 'mrkdwn_in' : ['text' , 'pretext' ],
1318
+ 'fields' : []
1319
+ },
1320
+ {
1321
+ 'color' : '#ec4b98' ,
1322
+ 'title' : 'Click to discover in Kibana' ,
1323
+ 'title_link' : 'http://kibana#discover'
1324
+ }
1325
+ ],
1326
+ 'icon_emoji' : ':ghost:' ,
1327
+ 'channel' : ''
1328
+ }
1329
+ mock_post_request .assert_called_once_with (
1330
+ rule ['slack_webhook_url' ],
1331
+ data = mock .ANY ,
1332
+ headers = {'content-type' : 'application/json' },
1333
+ proxies = None ,
1334
+ verify = False ,
1335
+ timeout = 10
1336
+ )
1337
+ actual_data = json .loads (mock_post_request .call_args_list [0 ][1 ]['data' ])
1338
+ assert expected_data == actual_data
1339
+
1340
+
1341
+ def test_slack_kibana_discover_color ():
1342
+ rule = {
1343
+ 'name' : 'Test Rule' ,
1344
+ 'type' : 'any' ,
1345
+ 'slack_attach_kibana_discover_url' : True ,
1346
+ 'slack_kibana_discover_color' : 'blue' ,
1347
+ 'slack_webhook_url' : 'http://please.dontgohere.slack' ,
1348
+ 'alert' : []
1349
+ }
1350
+ rules_loader = FileRulesLoader ({})
1351
+ rules_loader .load_modules (rule )
1352
+ alert = SlackAlerter (rule )
1353
+ match = {
1354
+ '@timestamp' : '2016-01-01T00:00:00' ,
1355
+ 'kibana_discover_url' : 'http://kibana#discover'
1356
+ }
1357
+ with mock .patch ('requests.post' ) as mock_post_request :
1358
+ alert .alert ([match ])
1359
+
1360
+ expected_data = {
1361
+ 'username' : 'elastalert' ,
1362
+ 'parse' : 'none' ,
1363
+ 'text' : '' ,
1364
+ 'attachments' : [
1365
+ {
1366
+ 'color' : 'danger' ,
1367
+ 'title' : 'Test Rule' ,
1368
+ 'text' : BasicMatchString (rule , match ).__str__ (),
1369
+ 'mrkdwn_in' : ['text' , 'pretext' ],
1370
+ 'fields' : []
1371
+ },
1372
+ {
1373
+ 'color' : 'blue' ,
1374
+ 'title' : 'Discover in Kibana' ,
1375
+ 'title_link' : 'http://kibana#discover'
1376
+ }
1377
+ ],
1378
+ 'icon_emoji' : ':ghost:' ,
1379
+ 'channel' : ''
1380
+ }
1381
+ mock_post_request .assert_called_once_with (
1382
+ rule ['slack_webhook_url' ],
1383
+ data = mock .ANY ,
1384
+ headers = {'content-type' : 'application/json' },
1385
+ proxies = None ,
1386
+ verify = False ,
1387
+ timeout = 10
1388
+ )
1389
+ actual_data = json .loads (mock_post_request .call_args_list [0 ][1 ]['data' ])
1390
+ assert expected_data == actual_data
1391
+
1392
+
1193
1393
def test_http_alerter_with_payload ():
1194
1394
rule = {
1195
1395
'name' : 'Test HTTP Post Alerter With Payload' ,
0 commit comments