示例#1
0
    def test_read_template_path_error(self, error):
        """Reading from invalid template name should log appropriate error."""
        name = "invalid_template_name"
        location = config.config.get('mail.templates_basepath')
        directory = get_absolute_path(location)
        file_name = "%s.tpl" % (name)
        template_path = os.path.join(directory, file_name)

        mail.read_template(name)

        # Assert error is logged correctly
        self.assertEqual(error.call_count, 1)
        self.assertEqual("Path does not exist: %s" % (template_path), error.mock_calls[0][1][0])
示例#2
0
文件: test_mail.py 项目: jsoref/bodhi
    def test_read_template_io_error(self, error):
        """IOError while opening template file should be logged appropriately."""
        name = "fedora_errata_template"
        location = config.config.get('mail.templates_basepath')
        directory = get_absolute_path(location)
        file_name = "%s.tpl" % (name)
        template_path = os.path.join(directory, file_name)

        with mock.patch('bodhi.server.mail.open', side_effect=IOError()):
            mail.read_template(name)

        # Assert error is logged correctly.
        self.assertEqual(error.call_count, 2)
        self.assertEqual("Unable to read template file: %s" % (template_path),
                         error.mock_calls[0][1][0])
        self.assertEqual("IO Error[None]: None", error.mock_calls[1][1][0])
示例#3
0
    def test_edit_mail_template_with_invalid_value(self):
        """Test appropriate error is returned when provided `mail_template` doesn't exist."""
        name = "F22"
        location = config.get('mail.templates_basepath')
        directory = get_absolute_path(location)
        template_list = [os.path.splitext(file)[0] for file in os.listdir(directory)]
        template_vals = ", ".join(template_list)

        res = self.app.get('/releases/%s' % name, status=200)
        r = res.json_body

        r["edited"] = name
        r["mail_template"] = "invalid_template_name"
        r["csrf_token"] = self.get_csrf_token()

        res = self.app.post("/releases/", r, status=400)

        self.assertEqual(res.json_body['errors'][0]['name'], 'mail_template')
        self.assertEqual(res.json_body['errors'][0]['description'],
                         '"invalid_template_name" is not one of {}'.format(template_vals))
示例#4
0
文件: errors.py 项目: sitedata/bodhi
    def __init__(self, request):
        """
        Initialize the HTML error handler to render an error message for human readers.

        This method sets the Response body to rendered HTML version of the given errors, and the
        status code to the code specified by errors.

        Args:
            request (pyramid.request.Request): The current Request.
        """
        location = config.get('mako.directories')
        directory = get_absolute_path(location)

        lookup = mako.lookup.TemplateLookup(
            directories=[directory],
            output_encoding='utf-8',
            input_encoding='utf-8',
        )
        template = lookup.get_template('errors.html')
        errors = request.errors

        try:
            body = template.render(
                errors=errors,
                status=errors.status,
                request=request,
                summary=status2summary(errors.status),
            )
        except Exception:
            log.error(mako.exceptions.text_error_template().render())
            raise

        # This thing inherits from both Exception *and* Response, so.. take the
        # Response path in the diamond inheritance chain and ignore the
        # exception side.
        # That turns this thing into a "real boy" like pinnochio.
        pyramid.response.Response.__init__(self, body)

        self.status = errors.status
        self.content_type = 'text/html'
示例#5
0
文件: mail.py 项目: sedrubal/bodhi
def read_template(name):
    """
    Read template text from file.

    Args:
        name (basestring): The name of the email template stored in 'release' table in database.
    Returns:
        basestring: The text read from the file.
    """
    location = config.get('mail.templates_basepath')
    directory = get_absolute_path(location)
    file_name = "%s.tpl" % (name)
    template_path = os.path.join(directory, file_name)

    if os.path.exists(template_path):
        try:
            with open(template_path) as template_file:
                return to_unicode(template_file.read())
        except IOError as e:
            log.error("Unable to read template file: %s" % (template_path))
            log.error("IO Error[%s]: %s" % (e.errno, e.strerror))
    else:
        log.error("Path does not exist: %s" % (template_path))
示例#6
0
文件: schemas.py 项目: sitedata/bodhi
from bodhi.server.models import (
    ContentType,
    PackageManager,
    ReleaseState,
    UpdateRequest,
    UpdateSeverity,
    UpdateStatus,
    UpdateSuggestion,
    UpdateType,
    TestGatingStatus,
)
from bodhi.server.validators import validate_csrf_token


# Retrieving list of templates from filesystem for `mail_template` validation in SaveReleaseSchema
template_directory = util.get_absolute_path(config.get('mail.templates_basepath'))
MAIL_TEMPLATES = [os.path.splitext(file)[0] for file in os.listdir(template_directory)]


class CSRFProtectedSchema(colander.MappingSchema):
    """A mixin class to validate csrf tokens."""

    csrf_token = colander.SchemaNode(
        colander.String(),
        name="csrf_token",
        validator=validate_csrf_token,
    )


class Bugs(colander.SequenceSchema):
    """A SequenceSchema to validate a list of Bug objects."""