From b142dfbfb2d547e7d4646a8ab6634d396c937e7b Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Wed, 23 Oct 2019 17:34:56 +0200 Subject: [PATCH] Assess alert_type in event creation (#467) --- datadog/api/events.py | 5 +++++ tests/integration/api/test_api.py | 1 + tests/unit/api/test_api.py | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/datadog/api/events.py b/datadog/api/events.py index 2cd52a6a2..253df33a0 100644 --- a/datadog/api/events.py +++ b/datadog/api/events.py @@ -1,3 +1,4 @@ +from datadog.api.exceptions import ApiError from datadog.api.resources import GetableAPIResource, CreateableAPIResource, \ SearchableAPIResource from datadog.util.compat import iteritems @@ -58,6 +59,10 @@ def create(cls, attach_host_name=True, **params): >>> api.Event.create(title=title, text=text, tags=tags) """ + if params.get("alert_type"): + if params["alert_type"] not in ["error", "warning", "info", "success"]: + raise ApiError("Parameter alert_type must be either error, warning, info or success") + return super(Event, cls).create(attach_host_name=attach_host_name, **params) @classmethod diff --git a/tests/integration/api/test_api.py b/tests/integration/api/test_api.py index 212842294..71a01a66c 100644 --- a/tests/integration/api/test_api.py +++ b/tests/integration/api/test_api.py @@ -11,6 +11,7 @@ from datadog import api as dog from datadog import initialize + TEST_USER = os.environ.get("DD_TEST_CLIENT_USER") API_KEY = os.environ.get("DD_TEST_CLIENT_API_KEY", "a" * 32) APP_KEY = os.environ.get("DD_TEST_CLIENT_APP_KEY", "a" * 40) diff --git a/tests/unit/api/test_api.py b/tests/unit/api/test_api.py index 0ce77dc4c..820840794 100644 --- a/tests/unit/api/test_api.py +++ b/tests/unit/api/test_api.py @@ -6,12 +6,13 @@ from time import time # 3p -import mock +import mock, pytest # datadog from datadog import initialize, api, util from datadog.api import ( Distribution, + Event, Metric, ServiceCheck, User @@ -484,6 +485,19 @@ def test_actionable(self): self.assertIsNone(kwargs["data"]) +class TestEventResource(DatadogAPIWithInitialization): + + def test_submit_event_wrong_alert_type(self): + """ + Assess that an event submitted with a wrong alert_type raises the correct Exception + """ + with pytest.raises(ApiError) as excinfo: + Event.create( + title="test no hostname", text="test no hostname", attach_host_name=False, alert_type="wrong_type" + ) + assert "Parameter alert_type must be either error, warning, info or success" in str(excinfo.value) + + class TestMetricResource(DatadogAPIWithInitialization): def submit_and_assess_metric_payload(self, serie, attach_host_name=True):