Merge "Fix edit notification page field is too small"

This commit is contained in:
Jenkins
2016-01-24 17:43:54 +00:00
committed by Gerrit Code Review
4 changed files with 129 additions and 3 deletions

View File

@@ -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<ConstraintViolation<CreateNotificationMethodCommand>> 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<ConstraintViolation<CreateNotificationMethodCommand>> 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<ConstraintViolation<CreateNotificationMethodCommand>> constraintViolations =
validator.validate(newNotificationMethod);
assertEquals(constraintViolations.size(), 1);
assertEquals(constraintViolations.iterator().next().getMessage(),
"size must be between 1 and 512");
}
}

View File

@@ -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)

View File

@@ -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))

View File

@@ -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