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
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
# The following 9 unit tests are for the old format
|
|
|
|
# corresponding json fixtures were renamed to have the "_old" trailing
|
|
|
|
# These tests and fixtures are to be deleted when old notifications EOLed
|
|
|
|
|
|
|
|
def test_open_old(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Test policy name (1234)"
|
2020-11-19 01:19:08 +01:00
|
|
|
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(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_opened_old",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
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
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_closed_old(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "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(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_closed_old",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-11-19 01:19:08 +01:00
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_acknowledged_old(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Test policy name (1234)"
|
2020-11-19 01:19:08 +01:00
|
|
|
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(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_acknowledged_old",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-11-19 01:19:08 +01:00
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_not_recognized_old(self) -> None:
|
2020-11-19 01:19:08 +01:00
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_state_not_recognized_old",
|
2020-11-19 01:19:08 +01:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
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
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_missing_fields_old(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Unknown Policy (Unknown ID)"
|
2020-11-19 01:19:08 +01:00
|
|
|
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(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_default_fields_old",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
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
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_missing_current_state_old(self) -> None:
|
2020-11-19 01:19:08 +01:00
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_missing_current_state_old",
|
2020-11-19 01:19:08 +01:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
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
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_missing_timestamp_old(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_missing_timestamp_old",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
"The newrelic webhook requires timestamp in milliseconds", e.exception.args[0]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_malformatted_time_old(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_malformatted_time_old",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])
|
|
|
|
|
|
|
|
def test_time_too_large_old(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_time_too_large_old",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])
|
|
|
|
|
|
|
|
# The following 10 unit tests are for the new format
|
|
|
|
# One more test than the old format as we have 4 states instead of 3 in the old
|
|
|
|
# corresponding json fixtures have "_new" trailing in the name
|
|
|
|
|
|
|
|
def test_activated_new(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Test policy name (8ceed342-f305-4bfa-adb8-97ba93f5dd26)"
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message = """
|
|
|
|
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **active** for condition: **Server Down** at <time:2020-11-11 22:32:11.151000+00:00>
|
|
|
|
``` quote
|
|
|
|
Violation description test.
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_active_new",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_created_new(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Test policy name (8114ada3-572e-4550-a310-12375371669e)"
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message = """
|
|
|
|
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **created** for condition: **Server Down**
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_created_new",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_closed_new(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Test policy name (f0d98b28-bf9d-49e7-b9d0-ac7cbb52e73a)"
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message = """
|
|
|
|
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **closed** for condition: **Server Down**
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_closed_new",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_acknowledged_new(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Test policy name (3576f543-dc3c-4d97-9f16-5c81f35195cb)"
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message = """
|
|
|
|
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **acknowledged** by **Alice** for condition: **Server Down**
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_acknowledged_new",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_not_recognized_new(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_state_not_recognized_new",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
"The newrelic webhook requires state be in [created|activated|acknowledged|closed]",
|
|
|
|
e.exception.args[0],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_missing_fields_new(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "Unknown Policy (e04156e4-4cac-4f39-9d27-75d361e40a6d)"
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message = """
|
|
|
|
[Incident](https://alerts.newrelic.com) **active** for condition: **Unknown condition** at <time:2020-11-11 22:32:11.151000+00:00>
|
|
|
|
``` quote
|
|
|
|
No details.
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_default_fields_new",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2022-07-22 11:37:33 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_missing_state_new(self) -> None:
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
|
|
|
"incident_missing_state_new",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
"The newrelic webhook requires state be in [created|activated|acknowledged|closed]",
|
|
|
|
e.exception.args[0],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_missing_timestamp_new(self) -> None:
|
2020-11-19 01:19:08 +01:00
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_missing_timestamp_new",
|
2020-11-19 01:19:08 +01:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
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
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_malformatted_time_new(self) -> None:
|
2020-11-19 01:19:08 +01:00
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_malformatted_time_new",
|
2020-11-19 01:19:08 +01:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
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
|
|
|
|
2022-07-22 11:37:33 +02:00
|
|
|
def test_time_too_large_new(self) -> None:
|
2020-11-19 01:19:08 +01:00
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
self.check_webhook(
|
2022-07-22 11:37:33 +02:00
|
|
|
"incident_time_too_large_new",
|
2020-11-19 01:19:08 +01:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
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])
|