Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default validation level to strict and honor validation_level #72

Merged
merged 4 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,20 @@ If there are configurations you'd like change:
- render errors: defaults to `true` (errors raised)
- minify: defaults to `false` (not minified)
- beautify: defaults to `true` (beautified)
- validation_level: defaults to `soft` (MJML syntax validation)
- validation_level: defaults to `strict` (abort on any template error, see [MJML validation documentation](https://github.com/mjmlio/mjml/tree/master/packages/mjml-validator#validating-mjml) for possible values)

```ruby
# config/initializers/mjml.rb
Mjml.setup do |config|
# set to `false` to ignore errors silently
config.raise_render_exception = true
# ignore errors silently
config.raise_render_exception = false

# optimize the size of your email
config.beautify = false
config.minify = true
config.validation_level = "strict"

# render illformed MJML templates, not recommended
config.validation_level = "soft"
end
```

Expand Down
2 changes: 1 addition & 1 deletion lib/mjml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Mjml
@@mjml_binary_error_string = "Couldn't find the MJML #{Mjml.mjml_binary_version_supported} binary.. have you run $ npm install mjml?"
@@beautify = true
@@minify = false
@@validation_level = "soft"
@@validation_level = "strict"

def self.check_version(bin)
stdout, _, status = run_mjml('--version', mjml_bin: bin)
Expand Down
7 changes: 4 additions & 3 deletions lib/mjml/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ def render
# Exec mjml command
#
# @return [String] The result as string
def run(in_tmp_file, beautify=true, minify=false, validation_level="soft")
def run(in_tmp_file, beautify=true, minify=false, validation_level="strict")
Tempfile.create(["out", ".html"]) do |out_tmp_file|
command = "-r #{in_tmp_file} -o #{out_tmp_file.path} --config.beautify #{beautify} --config.minify #{minify} --config.validationLevel #{validation_level}"
_, stderr, _ = Mjml.run_mjml(command)
raise ParseError.new(stderr.chomp) unless stderr.blank?
_, stderr, status = Mjml.run_mjml(command)
raise ParseError.new(stderr.chomp) unless status.success?
Mjml.logger.warn(stderr.chomp) unless stderr.blank?
out_tmp_file.read
end
end
Expand Down
22 changes: 22 additions & 0 deletions test/mjml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def inform_contact(recipient)
format.html
end
end

def invalid_template(recipient)
@recipient = recipient

mail(to: @recipient, from: "app@example.com") do |format|
format.html
end
end
end

class NoLayoutMailer < ActionMailer::Base
Expand Down Expand Up @@ -58,6 +66,20 @@ class NotifierMailerTest < ActiveSupport::TestCase
assert email.text_part.body.match(/We inform you about something/)
assert email.text_part.body.match(%r{Please visit https://www.example.com})
end

test "Invalid template raises error with validation level strict" do
with_settings(validation_level: 'strict') do
email = NotifierMailer.invalid_template("user@example.com")
assert_raise(ActionView::Template::Error) { email.html_part.to_s }
end
end

test "Invalid template gets compiled with validation level soft" do
with_settings(validation_level: 'soft') do
email = NotifierMailer.invalid_template("user@example.com")
assert email.html_part.to_s.blank?
end
end
end

class NotifierMailerTest < ActiveSupport::TestCase
Expand Down
32 changes: 10 additions & 22 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,42 @@
parser.stubs(:run).raises(error)
end

describe 'when render exception raising is enabled' do
before do
Mjml.setup do |config|
config.raise_render_exception = true
end
end

it 'raises exception' do
it 'raises exception with render exception enabled' do
with_settings(raise_render_exception: true) do
err = expect { parser.render }.must_raise(custom_error_class)
expect(err.message).must_equal error.message
end
end

describe 'when render exception raising is disabled' do
before do
Mjml.setup do |config|
config.raise_render_exception = false
end
end

it 'returns empty string' do
it 'returns empty string with exception raising disabled' do
with_settings(raise_render_exception: false) do
expect(parser.render).must_equal ''
end
end
end

describe 'can read beautify, minify, and validation_level configs' do
it 'use defaults if no config is set' do
it 'uses defaults if no config is set' do
expect(Mjml.beautify).must_equal(true)
expect(Mjml.minify).must_equal(false)
expect(Mjml.validation_level).must_equal('soft')
expect(Mjml.validation_level).must_equal('strict')
end

it 'use setup config' do
it 'uses setup config' do
Mjml.setup do |config|
config.beautify = false
config.minify = true
config.validation_level = 'strict'
config.validation_level = 'soft'
end

expect(Mjml.beautify).must_equal(false)
expect(Mjml.minify).must_equal(true)
expect(Mjml.validation_level).must_equal('strict')
expect(Mjml.validation_level).must_equal('soft')

Mjml.setup do |config|
config.beautify = true
config.minify = false
config.validation_level = 'soft'
config.validation_level = 'strict'
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@

ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true

def with_settings(settings)
original_settings =
settings.each_with_object({}) do |(key, _), agg|
agg[key] = Mjml.public_send(key)
end

settings.each do |key, value|
Mjml.public_send("#{key}=", value)
end

yield
ensure
original_settings.each do |key, value|
Mjml.public_send("#{key}=", value)
end
end
3 changes: 3 additions & 0 deletions test/views/notifier_mailer/invalid_template.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<mj-invalid>
This is invalid
</mj-text>