Files
zulip/zerver/webhooks/newrelic/tests.py
PIG208 5ecbfecd77 webhook: Rename FIXTURE_DIR_NAME to WEBHOOK_DIR_NAME.
Since FIXTURE_DIR_NAME is the name of the folder that contains the view
and tests modules of the webhook and another folder called "fixtures" that
store the fixtures, it is more appropriate to call it WEBHOOK_DIR_NAME,
especially when we want to refer to the view module using this variable.
2021-06-29 17:01:54 -07:00

124 lines
4.0 KiB
Python

from zerver.lib.test_classes import WebhookTestCase
class NewRelicHookTests(WebhookTestCase):
STREAM_NAME = "newrelic"
URL_TEMPLATE = "/api/v1/external/newrelic?stream={stream}&api_key={api_key}"
WEBHOOK_DIR_NAME = "newrelic"
def test_open(self) -> None:
expected_topic = "Test policy name (1234)"
expected_message = """
[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>
``` quote
Violation description test.
```
""".strip()
self.check_webhook(
"incident_opened",
expected_topic,
expected_message,
content_type="application/json",
)
def test_closed(self) -> None:
expected_topic = "Test policy name (1234)"
expected_message = """
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **closed** for condition: **Server Down**
""".strip()
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 = """
[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **acknowledged** by **Alice** for condition: **Server Down**
""".strip()
self.check_webhook(
"incident_acknowledged",
expected_topic,
expected_message,
content_type="application/json",
)
def test_not_recognized(self) -> None:
with self.assertRaises(AssertionError) as e:
self.check_webhook(
"incident_state_not_recognized",
"",
"",
content_type="application/json",
)
self.assertIn(
"The newrelic webhook requires current_state be in [open|acknowledged|closed]",
e.exception.args[0],
)
def test_missing_fields(self) -> None:
expected_topic = "Unknown Policy (Unknown ID)"
expected_message = """
[Incident](https://alerts.newrelic.com) **opened** 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",
expected_topic,
expected_message,
content_type="application/json",
)
def test_missing_current_state(self) -> None:
with self.assertRaises(AssertionError) as e:
self.check_webhook(
"incident_missing_current_state",
"",
"",
content_type="application/json",
)
self.assertIn(
"The newrelic webhook requires current_state be in [open|acknowledged|closed]",
e.exception.args[0],
)
def test_missing_timestamp(self) -> None:
with self.assertRaises(AssertionError) as e:
self.check_webhook(
"incident_missing_timestamp",
"",
"",
content_type="application/json",
)
self.assertIn(
"The newrelic webhook requires timestamp in milliseconds", e.exception.args[0]
)
def test_malformatted_time(self) -> None:
with self.assertRaises(AssertionError) as e:
self.check_webhook(
"incident_malformatted_time",
"",
"",
content_type="application/json",
)
self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])
def test_time_too_large(self) -> None:
with self.assertRaises(AssertionError) as e:
self.check_webhook(
"incident_time_too_large",
"",
"",
content_type="application/json",
)
self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])