Exemple #1
0
def test_json_template(template_string, data, expected):
    if expected == JSONTemplateParseException:
        with pytest.raises(JSONTemplateParseException):
            JSONTemplate(template_string)
    else:
        jt = JSONTemplate(template_string)
        assert jt.apply(data) == expected
Exemple #2
0
    def perform(self, notification_obj, event_handler, notification_data):
        config_data = notification_obj.method_config_dict
        url = config_data.get("url", "")
        if not url:
            return

        payload = notification_data["event_data"]
        template = config_data.get("template")
        if template:
            try:
                jt = JSONTemplate(template)
                payload = jt.apply(payload)
            except JSONTemplateParseException as jtpe:
                logger.exception("Got exception when trying to process template `%s`", template)
                raise NotificationMethodPerformException(str(jtpe))

        headers = {"Content-type": "application/json"}

        try:
            resp = requests.post(
                url,
                data=json.dumps(payload),
                headers=headers,
                cert=_ssl_cert(),
                timeout=METHOD_TIMEOUT,
            )
            if resp.status_code / 100 != 2:
                error_message = "%s response for webhook to url: %s" % (resp.status_code, url)
                logger.error(error_message)
                logger.error(resp.content)
                raise NotificationMethodPerformException(error_message)

        except requests.exceptions.RequestException as ex:
            logger.exception("Webhook was unable to be sent")
            raise NotificationMethodPerformException(ex.message)
Exemple #3
0
    def validate(self, namespace_name, repo_name, config_data):
        # Validate the URL.
        url = config_data.get("url", "")
        if not url:
            raise CannotValidateNotificationMethodException("Missing webhook URL")

        # If a template was specified, ensure it is a valid template.
        template = config_data.get("template")
        if template:
            # Validate template.
            try:
                JSONTemplate(template)
            except JSONTemplateParseException as jtpe:
                raise CannotValidateNotificationMethodException(str(jtpe))
Exemple #4
0
    def validate(self, namespace_name, repo_name, config_data):
        # Validate the URL.
        url = config_data.get("url", "")
        if not url:
            raise CannotValidateNotificationMethodException("Missing webhook URL")

        parsed = urlparse(url)
        if parsed.scheme != "https" and parsed.scheme != "http":
            raise CannotValidateNotificationMethodException("Invalid webhook URL")

        if parsed.hostname.lower() in HOSTNAME_BLACKLIST:
            raise CannotValidateNotificationMethodException("Invalid webhook URL")

        # If a template was specified, ensure it is a valid template.
        template = config_data.get("template")
        if template:
            # Validate template.
            try:
                JSONTemplate(template)
            except JSONTemplateParseException as jtpe:
                raise CannotValidateNotificationMethodException(str(jtpe))