2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2016-09-20 22:51:11 +02:00
|
|
|
class NewRelicHookTests(WebhookTestCase):
|
2021-02-12 08:20:45 +01:00
|
|
|
STREAM_NAME = "newrelic"
|
2020-04-09 21:51:58 +02:00
|
|
|
URL_TEMPLATE = "/api/v1/external/newrelic?stream={stream}&api_key={api_key}"
|
2021-06-26 09:18:33 +02:00
|
|
|
WEBHOOK_DIR_NAME = "newrelic"
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
def test_open(self) -> None:
|
|
|
|
expected_topic = "Test policy name (1234)"
|
|
|
|
expected_message = """
|
2020-12-13 21:52:38 +01:00
|
|
|
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **opened** for condition: **Server Down** at <time:2020-11-11 22:32:11.151000+00:00>
|
2020-11-19 01:19:08 +01:00
|
|
|
``` quote
|
|
|
|
Violation description test.
|
|
|
|
```
|
|
|
|
""".strip()
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
2020-11-19 01:19:08 +01:00
|
|
|
"incident_opened",
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
2020-11-19 01:19:08 +01:00
|
|
|
content_type="application/json",
|
2020-08-23 15:49:24 +02:00
|
|
|
)
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
def test_closed(self) -> None:
|
|
|
|
expected_topic = "Test policy name (1234)"
|
2019-04-17 23:26:31 +02:00
|
|
|
expected_message = """
|
2020-11-19 01:19:08 +01:00
|
|
|
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **closed** for condition: **Server Down**
|
|
|
|
""".strip()
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
self.check_webhook(
|
|
|
|
"incident_closed",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_acknowledged(self) -> None:
|
|
|
|
expected_topic = "Test policy name (1234)"
|
|
|
|
expected_message = """
|
2020-12-13 21:52:38 +01:00
|
|
|
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **acknowledged** by **Alice** for condition: **Server Down**
|
2020-11-19 01:19:08 +01:00
|
|
|
""".strip()
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_acknowledged",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
def test_not_recognized(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_state_not_recognized",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertIn(
|
|
|
|
"The newrelic webhook requires current_state be in [open|acknowledged|closed]",
|
|
|
|
e.exception.args[0],
|
|
|
|
)
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
def test_missing_fields(self) -> None:
|
|
|
|
expected_topic = "Unknown Policy (Unknown ID)"
|
|
|
|
expected_message = """
|
2020-12-13 21:52:38 +01:00
|
|
|
[Incident](https://alerts.newrelic.com) **opened** for condition: **Unknown condition** at <time:2020-11-11 22:32:11.151000+00:00>
|
2019-04-17 23:26:31 +02:00
|
|
|
``` quote
|
2020-11-19 01:19:08 +01:00
|
|
|
No details.
|
2019-04-17 23:26:31 +02:00
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
2020-11-19 01:19:08 +01:00
|
|
|
"incident_default_fields",
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
2020-11-19 01:19:08 +01:00
|
|
|
content_type="application/json",
|
2020-08-23 15:49:24 +02:00
|
|
|
)
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
def test_missing_current_state(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_missing_current_state",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertIn(
|
|
|
|
"The newrelic webhook requires current_state be in [open|acknowledged|closed]",
|
|
|
|
e.exception.args[0],
|
|
|
|
)
|
2020-11-19 01:19:08 +01:00
|
|
|
|
|
|
|
def test_missing_timestamp(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_missing_timestamp",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertIn(
|
|
|
|
"The newrelic webhook requires timestamp in milliseconds", e.exception.args[0]
|
|
|
|
)
|
2020-11-19 01:19:08 +01:00
|
|
|
|
|
|
|
def test_malformatted_time(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_malformatted_time",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2021-03-05 21:22:50 +01:00
|
|
|
self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])
|
2020-11-19 01:19:08 +01:00
|
|
|
|
|
|
|
def test_time_too_large(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_time_too_large",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2021-03-05 21:22:50 +01:00
|
|
|
self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])
|