def test_validation_for_pagerduty(self): notification = {"name": "MyPagerduty", "type": "PAGERDUTY", "address": "nzH2LVRdMzun11HNC2oD"} try: schemas_notifications.parse_and_validate(notification, valid_periods) except schemas_exceptions.ValidationException: self.fail("shouldn't happen")
def test_validation_for_webhook_non_zero_period(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "http://somedomain.com", "period": 60} try: schemas_notifications.parse_and_validate(notification, valid_periods) except schemas_exceptions.ValidationException: self.fail("shouldn't happen")
def test_validation_exception_for_invalid_period_non_int(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "//somedomain.com", "period": "zero"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Period zero must be a valid integer", ex.message)
def test_validation_exception_for_invalid_period_for_pagerduty(self): notification = {"name": "MyPagerduty", "type": "PAGERDUTY", "address": "nzH2LVRdMzun11HNC2oD", "period": 60} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Period can only be set with webhooks", ex.message)
def test_validation_exception_for_invalid_period_float(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "//somedomain.com", "period": 1.2} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("expected int for dictionary value @ data['period']", ex.message)
def test_validation_exception_for_webhook_invalid_period(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "//somedomain.com", "period": "10"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("10 is not a valid period", ex.message)
def test_validation_exception_for_invalid_email_address(self): notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Address name@ is not of correct format", ex.message)
def test_validation_exception_for_webhook_invalid_period(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "//somedomain.com", "period": "10"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("10 is not a valid period, not in [0, 60]", ex.message)
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.parse_and_validate(notification, valid_periods) except schemas_exceptions.ValidationException: self.fail("shouldn't happen")
def _parse_and_validate_notification(self, notification, require_all=False): """Validates the notification :param notification: An event object. :raises falcon.HTTPBadRequest """ try: schemas_notifications.parse_and_validate(notification, self.valid_periods, require_all=require_all) except schemas_exceptions.ValidationException as ex: LOG.exception(ex) raise falcon.HTTPBadRequest('Bad Request', ex.message)
def test_validation_for_email(self): notification = { "name": "MyEmail", "type": "EMAIL", "address": "*****@*****.**" } try: schemas_notifications.parse_and_validate(notification, valid_periods) except schemas_exceptions.ValidationException: self.fail("shouldn't happen")
def _parse_and_validate_notification(self, notification, require_all=False): """Validates the notification :param notification: An event object. :raises falcon.HTTPBadRequest """ try: schemas_notifications.parse_and_validate(notification, self.valid_periods, require_all=require_all) except schemas_exceptions.ValidationException as ex: LOG.debug(ex) raise falcon.HTTPBadRequest("Bad Request", ex.message)
def test_validation_exception_for_missing_period(self): notification = { "name": "MyEmail", "type": "EMAIL", "address": "name@domain." } with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods, require_all=True) ex = ve.exception self.assertEqual("Period is required", ex.message)
def test_validation_exception_for_webhook_no_netloc(self): notification = { "name": "MyWebhook", "type": "WEBHOOK", "address": "http://" } with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Address http:// does not have network location", ex.message)
def test_validation_exception_for_webhook_no_scheme(self): notification = { "name": "MyWebhook", "type": "WEBHOOK", "address": "//somedomain.com" } with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Address //somedomain.com does not have URL scheme", ex.message)
def test_validation_exception_for_invalid_period_for_email(self): notification = { "name": "MyEmail", "type": "EMAIL", "address": "*****@*****.**", "period": "60" } with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Period can only be set with webhooks", ex.message)
def test_validation_exception_for_webhook_invalid_scheme(self): notification = { "name": "MyWebhook", "type": "WEBHOOK", "address": "ftp://somedomain.com" } with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual( "Address ftp://somedomain.com scheme is not in ['http', 'https']", ex.message)
def test_validation_exception_for_invalid_period_for_email(self): notification = {"name": "MyEmail", "type": "EMAIL", "address": "*****@*****.**", "period": "60"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Period can only be set with webhooks", ex.message)
def test_validation_exception_for_webhook_no_scheme(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "//somedomain.com"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Address //somedomain.com does not have URL scheme", ex.message)
def test_validation_exception_for_webhook_no_netloc(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "http://"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Address http:// does not have network location", ex.message)
def test_validation_exception_for_webhook_invalid_scheme(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "ftp://somedomain.com"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception self.assertEqual("Address ftp://somedomain.com scheme is not in ['http', 'https']", ex.message)
def test_validation_exception_for_missing_period(self): notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain."} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods, require_all=True) ex = ve.exception self.assertEqual("Period is required", ex.message)
def test_validation_for_email(self): notification = {"name": "MyEmail", "type": "EMAIL", "address": "*****@*****.**"} try: schemas_notifications.parse_and_validate(notification, valid_periods) except schemas_exceptions.ValidationException: self.fail("shouldn't happen")