From 9970b6275b3e22d41f732d438aa10bbcd377447e Mon Sep 17 00:00:00 2001 From: Shinya Kawabata Date: Tue, 5 Jan 2016 16:45:32 +0900 Subject: [PATCH] Fix edit notification page field is too small Added unit tests to java monasca-api. Fixed python monasca-api and added unit tests. See also a monasca-ui patch. Change-Id: Ida354b4b29bc3a9305551d8e82f7e61fb5834ca1 Closes-Bug: #1527660 --- .../command/CreateNotificationMethodTest.java | 58 ++++++++++++++- monasca_api/tests/test_validation.py | 70 +++++++++++++++++++ .../notifications_request_body_schema.py | 2 +- monasca_tempest_tests/tests/api/constants.py | 2 +- 4 files changed, 129 insertions(+), 3 deletions(-) diff --git a/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java b/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java index 188f31dea..d45a65216 100644 --- a/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java +++ b/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java @@ -17,8 +17,16 @@ package monasca.api.app.command; import static monasca.common.dropwizard.JsonHelpers.jsonFixture; import static org.testng.Assert.assertEquals; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; import javax.ws.rs.WebApplicationException; +import org.apache.commons.lang3.StringUtils; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.fasterxml.jackson.databind.JsonMappingException; @@ -29,6 +37,15 @@ import monasca.api.domain.model.notificationmethod.NotificationMethodType; @Test public class CreateNotificationMethodTest extends AbstractModelTest { + + private static Validator validator; + + @BeforeClass + public static void setUp() { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + validator = factory.getValidator(); + } + public void shouldDeserializeFromJson() throws Exception { CreateNotificationMethodCommand newNotificationMethod = new CreateNotificationMethodCommand("MyEmail", NotificationMethodType.EMAIL, "a@b"); @@ -73,7 +90,7 @@ public class CreateNotificationMethodTest extends AbstractModelTest { public void testValidationForWebhook() { CreateNotificationMethodCommand newNotificationMethod = - new CreateNotificationMethodCommand("MyEmail", NotificationMethodType.WEBHOOK, "http://somedomain.com"); + new CreateNotificationMethodCommand("MyWebhook", NotificationMethodType.WEBHOOK, "http://somedomain.com"); newNotificationMethod.validate(); } @@ -90,4 +107,43 @@ public class CreateNotificationMethodTest extends AbstractModelTest { new CreateNotificationMethodCommand("MyPagerduty", NotificationMethodType.PAGERDUTY, "nzH2LVRdMzun11HNC2oD"); newNotificationMethod.validate(); } + + public void testValidationForMaxNameAddress() { + String name = StringUtils.repeat("A", 250); + assertEquals(name.length(), 250); + String address = "http://" + StringUtils.repeat("A", 502) + ".io"; + assertEquals(address.length(), 512); + CreateNotificationMethodCommand newNotificationMethod = + new CreateNotificationMethodCommand(name, NotificationMethodType.WEBHOOK, address); + Set> constraintViolations = + validator.validate(newNotificationMethod); + + assertEquals(constraintViolations.size(), 0); + } + + public void testValidationExceptionForExceededNameLength() { + String name = StringUtils.repeat("A", 251); + assertEquals(name.length(), 251); + CreateNotificationMethodCommand newNotificationMethod = + new CreateNotificationMethodCommand(name, NotificationMethodType.WEBHOOK, "http://somedomain.com"); + Set> constraintViolations = + validator.validate(newNotificationMethod); + + assertEquals(constraintViolations.size(), 1); + assertEquals(constraintViolations.iterator().next().getMessage(), + "size must be between 1 and 250"); + } + + public void testValidationExceptionForExceededAddressLength() { + String address = "http://" + StringUtils.repeat("A", 503) + ".io"; + assertEquals(address.length(), 513); + CreateNotificationMethodCommand newNotificationMethod = + new CreateNotificationMethodCommand("MyWebhook", NotificationMethodType.WEBHOOK, address); + Set> constraintViolations = + validator.validate(newNotificationMethod); + + assertEquals(constraintViolations.size(), 1); + assertEquals(constraintViolations.iterator().next().getMessage(), + "size must be between 1 and 512"); + } } diff --git a/monasca_api/tests/test_validation.py b/monasca_api/tests/test_validation.py index c9d3a605e..308a7d1c8 100644 --- a/monasca_api/tests/test_validation.py +++ b/monasca_api/tests/test_validation.py @@ -14,6 +14,8 @@ # under the License. import falcon +import monasca_api.v2.common.schemas.exceptions as schemas_exceptions +import monasca_api.v2.common.schemas.notifications_request_body_schema as schemas_notifications import monasca_api.v2.common.validation as validation import monasca_api.v2.reference.helpers as helpers @@ -199,3 +201,71 @@ class TestConvertTimeString(unittest.TestCase): self.assertRaises( ValueError, helpers._convert_time_string, date_time_string) + + +class TestNotificationValidation(unittest.TestCase): + + def test_validation_for_email(self): + notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain.com"} + try: + schemas_notifications.validate(notification) + except schemas_exceptions.ValidationException: + self.fail("shouldn't happen") + + def test_validation_exception_for_email(self): + # ToDo: this function is not implemented yet. + # notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain."} + # self.assertRaises( + # schemas_exceptions.ValidationException, + # schemas_notifications.validate, notification) + pass + + def test_validation_for_webhook(self): + notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "http://somedomain.com"} + try: + schemas_notifications.validate(notification) + except schemas_exceptions.ValidationException: + self.fail("shouldn't happen") + + def test_validation_exception_for_webhook(self): + # ToDo: this function is not implemented yet. + # notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "ftp://localhost"} + # self.assertRaises( + # schemas_exceptions.ValidationException, + # schemas_notifications.validate, notification) + pass + + def test_validation_for_pagerduty(self): + notification = {"name": "MyPagerduty", "type": "PAGERDUTY", + "address": "nzH2LVRdMzun11HNC2oD"} + try: + schemas_notifications.validate(notification) + except schemas_exceptions.ValidationException: + self.fail("shouldn't happen") + + def test_validation_for_max_name_address(self): + name = "A" * 250 + self.assertEqual(250, len(name)) + address = "http://" + "A" * 502 + ".io" + self.assertEqual(512, len(address)) + notification = {"name": name, "type": "WEBHOOK", "address": address} + try: + schemas_notifications.validate(notification) + except schemas_exceptions.ValidationException: + self.fail("shouldn't happen") + + def test_validation_exception_for_exceeded_name_length(self): + name = "A" * 251 + self.assertEqual(251, len(name)) + notification = {"name": name, "type": "WEBHOOK", "address": "http://somedomain.com"} + self.assertRaises( + schemas_exceptions.ValidationException, + schemas_notifications.validate, notification) + + def test_validation_exception_for_exceeded_address_length(self): + address = "http://" + "A" * 503 + ".io" + self.assertEqual(513, len(address)) + notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": address} + self.assertRaises( + schemas_exceptions.ValidationException, + schemas_notifications.validate, notification) diff --git a/monasca_api/v2/common/schemas/notifications_request_body_schema.py b/monasca_api/v2/common/schemas/notifications_request_body_schema.py index 7a33c7886..f675a70ec 100644 --- a/monasca_api/v2/common/schemas/notifications_request_body_schema.py +++ b/monasca_api/v2/common/schemas/notifications_request_body_schema.py @@ -28,7 +28,7 @@ notification_schema = { "PAGERDUTY", "pagerduty")), voluptuous.Required('address'): voluptuous.Schema( voluptuous.All(voluptuous.Any(str, unicode), - voluptuous.Length(max=100)))} + voluptuous.Length(max=512)))} request_body_schema = voluptuous.Schema(voluptuous.Any(notification_schema)) diff --git a/monasca_tempest_tests/tests/api/constants.py b/monasca_tempest_tests/tests/api/constants.py index 6da7ea201..baa582ec1 100644 --- a/monasca_tempest_tests/tests/api/constants.py +++ b/monasca_tempest_tests/tests/api/constants.py @@ -27,7 +27,7 @@ MAX_ALARM_DEFINITION_ACTIONS_LENGTH = 50 MAX_NOTIFICATION_METHOD_NAME_LENGTH = 250 MAX_NOTIFICATION_METHOD_TYPE_LENGTH = 100 -MAX_NOTIFICATION_METHOD_ADDRESS_LENGTH = 100 +MAX_NOTIFICATION_METHOD_ADDRESS_LENGTH = 512 INVALID_CHARS_NOTIFICATION = "<>={}(),\"\;&" MAX_LIST_MEASUREMENTS_NAME_LENGTH = 255