forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_msteamswebhook.py
93 lines (75 loc) · 3.52 KB
/
test_msteamswebhook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import unittest
from alerta.app import create_app, custom_webhooks
from uuid import uuid4
import alerta_msteamswebhook
class MsteamsWebhookTestCase(unittest.TestCase):
def setUp(self):
test_config = {
'TESTING': True,
'AUTH_REQUIRED': False
}
self.app = create_app(test_config)
self.client = self.app.test_client()
custom_webhooks.webhooks['msteams'] = alerta_msteamswebhook.MsteamsWebhook()
self.headers = {
'Content-Type': 'application/json'
}
self.alert_id = 'f0c55228-c61d-462a-9aeb-f6048d37fdf6'
def test_msteamswebhook(self):
payload_cmd = """
{
"action": "%s",
"alert_id": "%s"
}
"""
# Missing alert_id
payload_invalidcmd = """
{
"action": "%s"
}
"""
payload_blackout = r"""
{
"action": "blackout",
"environment": "Production",
"resource": "webhooktest1",
"event": "DiskUtilHigh"
}
"""
# ack
# TODO: needs to mock Alert.find_by_id / alert.set_status
#response = self.client.post('/webhooks/msteams', data=payload_cmd % ('ack', self.alert_id), content_type='application/json', headers=self.headers)
#self.assertEqual(response.status_code, 200)
#data = json.loads(response.data.decode('utf-8'))
#self.assertEqual(data['status'], 'ok')
#self.assertEqual(data['message'], 'status changed')
#self.assertTrue(bool(response.headers.get('CARD-ACTION-STATUS', False)))
# ack with missing alert_id
response = self.client.post('/webhooks/msteams', data=payload_invalidcmd % 'ack', content_type='application/json', headers=self.headers)
self.assertEqual(response.status_code, 400)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['status'], 'error')
self.assertEqual(data['message'], 'Missing/invalid alert_id')
# ack with bogus alert_id
response = self.client.post('/webhooks/msteams', data=payload_cmd % ('ack', '7a0e3ee1-fbaa-45th-isis-bogus'), content_type='application/json', headers=self.headers)
self.assertEqual(response.status_code, 400)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['status'], 'error')
self.assertEqual(data['message'], 'Missing/invalid alert_id')
# close alert
# TODO: needs to mock Alert.find_by_id / alert.set_status
#response = self.client.post('/webhooks/msteams', data=payload_cmd % ('close', self.alert_id), content_type='application/json', headers=self.headers)
#self.assertEqual(response.status_code, 200)
#data = json.loads(response.data.decode('utf-8'))
#self.assertEqual(data['status'], 'ok')
#self.assertEqual(data['message'], 'status changed')
#self.assertTrue(bool(response.headers.get('CARD-ACTION-STATUS', False)))
# create blackout
# TODO: needs to mock: blackout.create()
#response = self.client.post('/webhooks/msteams', data=payload_blackout, content_type='application/json', headers=self.headers)
#self.assertEqual(response.status_code, 201)
#data = json.loads(response.data.decode('utf-8'))
#self.assertEqual(data['status'], 'ok')
#self.assertEqual(data['message'], 'blackout created')
#self.assertTrue(bool(response.headers.get('CARD-ACTION-STATUS', False)))