From 7a08d1bb56d058dde924553ce1df015ec733a460 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Wed, 17 Nov 2021 17:18:03 +0530 Subject: [PATCH 01/11] DEV: validate liquid templates on wizard save --- config/locales/server.en.yml | 1 + lib/custom_wizard/validators/template.rb | 40 +++++++++++++++++++ plugin.rb | 1 + .../custom_wizard/template_validator_spec.rb | 24 +++++++++++ 4 files changed, 66 insertions(+) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 7e507450f8..8fc22fcfcd 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -49,6 +49,7 @@ en: required: "%{property} is required" conflict: "Wizard with id '%{wizard_id}' already exists" after_time: "After time setting is invalid" + liquid_syntax_error: "Syntax error in liquid template" site_settings: custom_wizard_enabled: "Enable custom wizards." diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index c90944e9f2..eda5fb0642 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -17,10 +17,12 @@ def perform data[:steps].each do |step| check_required(step, :step) + validate_liquid_template(step, :step) if data[:fields].present? data[:fields].each do |field| check_required(field, :field) + validate_liquid_template(field, :field) end end end @@ -28,6 +30,7 @@ def perform if data[:actions].present? data[:actions].each do |action| check_required(action, :action) + validate_liquid_template(action, :action) end end @@ -80,4 +83,41 @@ def validate_after_time errors.add :base, I18n.t("wizard.validation.after_time") end end + + def validate_liquid_template(object, type) + valid = true + + case type + when :field + is_description_valid = is_liquid_template_valid?(object['description']) + is_placeholder_valid = is_liquid_template_valid?(object['placeholder']) + is_preview_template_valid = begin + return true unless object[:type] == 'composer_preview' + is_liquid_template_valid?(object['preview_template']) + end + + if !is_description_valid || !is_placeholder_valid || !is_preview_template_valid + valid = false + end + when :step + if !is_liquid_template_valid?(object['description']) + valid = false + end + when :action + if object['post_builder'] + valid = is_liquid_template_valid?(object['post_template']) + end + end + + errors.add :base, I18n.t("wizard.validation.liquid_syntax_error") unless valid + end + + def is_liquid_template_valid?(template) + begin + Liquid::Template.parse(template) + true + rescue Liquid::SyntaxError + false + end + end end diff --git a/plugin.rb b/plugin.rb index b4d09421ca..de1b193578 100644 --- a/plugin.rb +++ b/plugin.rb @@ -116,6 +116,7 @@ def process_require_tree_discourse_directive(path = ".") load File.expand_path(path, __FILE__) end + Liquid::Template.error_mode = :strict Liquid::Template.register_filter(::CustomWizard::LiquidFilter::FirstNonEmpty) add_class_method(:wizard, :user_requires_completion?) do |user| diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index c8ce915a84..0d8d68a2b0 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -45,4 +45,28 @@ CustomWizard::TemplateValidator.new(template).perform ).to eq(false) end + + it "validates if no liquid syntax is in use" do + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(true) + end + + it "validates if liquid syntax in use is correct" do + template[:steps][0][:description] = <<-LIQUID.strip + {%- assign hello = "Topic Form 1" %} + LIQUID + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(true) + end + + it "doesn't validate if liquid syntax in use is incorrect" do + template[:steps][0][:description] = <<-LIQUID.strip + {%- assign hello = "Topic Form 1" % + LIQUID + expect( + CustomWizard::TemplateValidator.new(template[:steps][0]).perform + ).to eq(false) + end end From 4f8c1082f54d3e308fd5784ec665ffe3326ba231 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 22 Nov 2021 01:18:24 +0530 Subject: [PATCH 02/11] minor fix --- spec/components/custom_wizard/template_validator_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index 0d8d68a2b0..3a3b55df07 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -66,7 +66,7 @@ {%- assign hello = "Topic Form 1" % LIQUID expect( - CustomWizard::TemplateValidator.new(template[:steps][0]).perform + CustomWizard::TemplateValidator.new(template).perform ).to eq(false) end end From b018256564ca75cf9da7252107cc04f8f90afabf Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 7 Dec 2021 11:05:26 +0530 Subject: [PATCH 03/11] code improvements and spec --- lib/custom_wizard/validators/template.rb | 8 +- .../custom_wizard/template_validator_spec.rb | 90 ++++++++++++++----- spec/fixtures/wizard.json | 9 +- 3 files changed, 84 insertions(+), 23 deletions(-) diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index eda5fb0642..219926a429 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -92,8 +92,11 @@ def validate_liquid_template(object, type) is_description_valid = is_liquid_template_valid?(object['description']) is_placeholder_valid = is_liquid_template_valid?(object['placeholder']) is_preview_template_valid = begin - return true unless object[:type] == 'composer_preview' - is_liquid_template_valid?(object['preview_template']) + if object[:type] == 'composer_preview' + is_liquid_template_valid?(object['preview_template']) + else + true + end end if !is_description_valid || !is_placeholder_valid || !is_preview_template_valid @@ -110,6 +113,7 @@ def validate_liquid_template(object, type) end errors.add :base, I18n.t("wizard.validation.liquid_syntax_error") unless valid + valid end def is_liquid_template_valid?(template) diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index 3a3b55df07..39b9ad25b8 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -9,6 +9,29 @@ "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json" ).read).with_indifferent_access } + let(:valid_liquid_template) { + <<-LIQUID.strip + {%- assign hello = "Topic Form 1" %} + LIQUID + } + + let(:invalid_liquid_template) { + <<-LIQUID.strip + {%- assign hello = "Topic Form 1" % + LIQUID + } + + def expect_validation_success + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(true) + end + + def expect_validation_failure + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(false) + end it "validates valid templates" do expect( @@ -46,27 +69,54 @@ ).to eq(false) end - it "validates if no liquid syntax is in use" do - expect( - CustomWizard::TemplateValidator.new(template).perform - ).to eq(true) - end + context "liquid templates" do + it "validates if no liquid syntax in use" do + expect_validation_success + end - it "validates if liquid syntax in use is correct" do - template[:steps][0][:description] = <<-LIQUID.strip - {%- assign hello = "Topic Form 1" %} - LIQUID - expect( - CustomWizard::TemplateValidator.new(template).perform - ).to eq(true) - end + it "validates if liquid syntax in use is correct" do + template[:steps][0][:description] = valid_liquid_template + expect_validation_success + end - it "doesn't validate if liquid syntax in use is incorrect" do - template[:steps][0][:description] = <<-LIQUID.strip - {%- assign hello = "Topic Form 1" % - LIQUID - expect( - CustomWizard::TemplateValidator.new(template).perform - ).to eq(false) + it "doesn't validate if liquid syntax in use is incorrect" do + template[:steps][0][:description] = invalid_liquid_template + expect_validation_failure + end + + context "validation targets" do + context "fields" do + it "validates descriptions" do + template[:steps][0][:fields][0][:description] = invalid_liquid_template + expect_validation_failure + end + + it "validates preview templates" do + template[:steps][0][:fields][4][:preview_template] = invalid_liquid_template + expect_validation_failure + end + + it "validates placeholders" do + template[:steps][0][:fields][0][:description] = invalid_liquid_template + expect_validation_failure + end + end + + context "steps" do + it "validates descriptions" do + template[:steps][0][:description] = invalid_liquid_template + expect_validation_failure + end + end + + context "actions" do + it "validates post builder" do + action_index = template[:actions].index { |action| action[:post_builder] } + template[:actions][action_index][:post_template] = invalid_liquid_template + + expect_validation_failure + end + end + end end end diff --git a/spec/fixtures/wizard.json b/spec/fixtures/wizard.json index 4727c7a890..01421c7fe4 100644 --- a/spec/fixtures/wizard.json +++ b/spec/fixtures/wizard.json @@ -43,6 +43,13 @@ "label": "I'm only text", "description": "", "type": "text_only" + }, + { + "id": "step_1_field_5", + "label": "I'm a preview", + "description": "", + "type": "composer_preview", + "preview_template": "w{step_1_field_1}" } ], "description": "Text inputs!" @@ -576,4 +583,4 @@ ] } ] -} \ No newline at end of file +} From ae224a6843ac7e224845286c55a8effcdc59c5b8 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 7 Dec 2021 11:07:46 +0530 Subject: [PATCH 04/11] version bump --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index de1b193578..89a259fba8 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Create custom wizards -# version: 1.15.3 +# version: 1.15.6 # authors: Angus McLeod # url: https://github.com/paviliondev/discourse-custom-wizard # contact emails: angus@thepavilion.io From 47145d99b2022df4f4424f13bcd074e690cf8998 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 7 Dec 2021 15:52:42 +0530 Subject: [PATCH 05/11] fixed failing specs --- spec/components/custom_wizard/builder_spec.rb | 2 +- spec/serializers/custom_wizard/wizard_field_serializer_spec.rb | 2 +- spec/serializers/custom_wizard/wizard_step_serializer_spec.rb | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/components/custom_wizard/builder_spec.rb b/spec/components/custom_wizard/builder_spec.rb index def54fc46b..099d868136 100644 --- a/spec/components/custom_wizard/builder_spec.rb +++ b/spec/components/custom_wizard/builder_spec.rb @@ -324,7 +324,7 @@ .build .steps.first .fields.length - ).to eq(4) + ).to eq(@template[:steps][0][:fields].length) end context "with condition" do diff --git a/spec/serializers/custom_wizard/wizard_field_serializer_spec.rb b/spec/serializers/custom_wizard/wizard_field_serializer_spec.rb index 1fa9671c59..a5a5e721b0 100644 --- a/spec/serializers/custom_wizard/wizard_field_serializer_spec.rb +++ b/spec/serializers/custom_wizard/wizard_field_serializer_spec.rb @@ -21,7 +21,7 @@ scope: Guardian.new(user) ).as_json - expect(json_array.size).to eq(4) + expect(json_array.size).to eq(@wizard.steps.first.fields.size) expect(json_array[0][:label]).to eq("

Text

") expect(json_array[0][:description]).to eq("Text field description.") expect(json_array[3][:index]).to eq(3) diff --git a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb index 35ce0fd2ff..2134535221 100644 --- a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb +++ b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb @@ -43,7 +43,8 @@ each_serializer: described_class, scope: Guardian.new(user) ).as_json - expect(json_array[0][:fields].length).to eq(4) + + expect(json_array[0][:fields].length).to eq(@wizard.steps[0].fields.length) end context 'with required data' do From 12b29351f7d1056651d453f683ef1d6723db281c Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 27 Jan 2022 17:19:34 +0530 Subject: [PATCH 06/11] FIX: handle displaying backend validation errors on frontend --- .../controllers/admin-wizards-wizard-show.js.es6 | 14 ++++++++++---- .../discourse/models/custom-wizard.js.es6 | 2 +- controllers/custom_wizard/admin/wizard.rb | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 index 332efedd2a..c278847ba1 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 @@ -86,12 +86,18 @@ export default Controller.extend({ if (result.error) { errorType = result.error.type; errorParams = result.error.params; + this.set( + "error", + I18n.t(`admin.wizard.error.${errorType}`, errorParams) + ); } - this.set( - "error", - I18n.t(`admin.wizard.error.${errorType}`, errorParams) - ); + if (result.backend_validation_error) { + this.set( + "error", + result.backend_validation_error + ); + } later(() => this.set("error", null), 10000); }) diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index e6a8408d51..2fa6bc4c1c 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -28,7 +28,7 @@ const CustomWizard = EmberObject.extend({ contentType: "application/json", data: JSON.stringify(data), }).then((result) => { - if (result.error) { + if (result.backend_validation_error) { reject(result); } else { resolve(result); diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index e2edfba027..0947168026 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -37,7 +37,7 @@ def save wizard_id = template.save(create: params[:create]) if template.errors.any? - render json: failed_json.merge(errors: template.errors.full_messages) + render json: failed_json.merge(backend_validation_error: template.errors.full_messages.join("\n\n")) else render json: success_json.merge(wizard_id: wizard_id) end From 2cfd82786f817c9ab4d5fb6ae0832f5ba70a3f53 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 27 Jan 2022 17:45:17 +0530 Subject: [PATCH 07/11] fixed linting --- .../discourse/controllers/admin-wizards-wizard-show.js.es6 | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 index c278847ba1..7cdd606600 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 @@ -93,10 +93,7 @@ export default Controller.extend({ } if (result.backend_validation_error) { - this.set( - "error", - result.backend_validation_error - ); + this.set("error", result.backend_validation_error); } later(() => this.set("error", null), 10000); From 06fae31dde38f8e9f6cbb7484245e8454169d9bd Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 27 Jan 2022 18:33:58 +0530 Subject: [PATCH 08/11] improved error display --- .../admin-wizards-wizard-show.js.es6 | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 index 7cdd606600..1eeb62e6ac 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 @@ -58,6 +58,21 @@ export default Controller.extend({ } return wizardFieldList(steps); }, + getErrorMessage(result) { + if (result.backend_validation_error) { + return result.backend_validation_error; + } + + let errorType = "failed"; + let errorParams = {}; + + if (result.error) { + errorType = result.error.type; + errorParams = result.error.params; + } + + return I18n.t(`admin.wizard.error.${errorType}`, errorParams); + }, actions: { save() { @@ -80,21 +95,7 @@ export default Controller.extend({ this.send("afterSave", result.wizard_id); }) .catch((result) => { - let errorType = "failed"; - let errorParams = {}; - - if (result.error) { - errorType = result.error.type; - errorParams = result.error.params; - this.set( - "error", - I18n.t(`admin.wizard.error.${errorType}`, errorParams) - ); - } - - if (result.backend_validation_error) { - this.set("error", result.backend_validation_error); - } + this.set("error", this.getErrorMessage(result)); later(() => this.set("error", null), 10000); }) From 7e1e203e3a6876dcd88d069a319289cfbf03f77b Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 27 Jan 2022 19:18:13 +0530 Subject: [PATCH 09/11] validate raw description for steps --- lib/custom_wizard/validators/template.rb | 2 +- spec/components/custom_wizard/template_validator_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index d9693b9727..a36b78717e 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -103,7 +103,7 @@ def validate_liquid_template(object, type) valid = false end when :step - if !is_liquid_template_valid?(object['description']) + if !is_liquid_template_valid?(object['raw_description']) valid = false end when :action diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index ea687f6666..197a92c52d 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -108,12 +108,12 @@ def expect_validation_failure end it "validates if liquid syntax in use is correct" do - template[:steps][0][:description] = valid_liquid_template + template[:steps][0][:raw_description] = valid_liquid_template expect_validation_success end it "doesn't validate if liquid syntax in use is incorrect" do - template[:steps][0][:description] = invalid_liquid_template + template[:steps][0][:raw_description] = invalid_liquid_template expect_validation_failure end @@ -137,7 +137,7 @@ def expect_validation_failure context "steps" do it "validates descriptions" do - template[:steps][0][:description] = invalid_liquid_template + template[:steps][0][:raw_description] = invalid_liquid_template expect_validation_failure end end From 144aae52e03f219f1be84567596d1537e042f6cf Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 27 Jan 2022 19:32:56 +0530 Subject: [PATCH 10/11] refactor conditional --- lib/custom_wizard/validators/template.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index a36b78717e..1d372a7363 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -91,12 +91,10 @@ def validate_liquid_template(object, type) when :field is_description_valid = is_liquid_template_valid?(object['description']) is_placeholder_valid = is_liquid_template_valid?(object['placeholder']) - is_preview_template_valid = begin - if object[:type] == 'composer_preview' - is_liquid_template_valid?(object['preview_template']) - else - true - end + is_preview_template_valid = if object[:type] == 'composer_preview' + is_liquid_template_valid?(object['preview_template']) + else + true end if !is_description_valid || !is_placeholder_valid || !is_preview_template_valid From ad5dadb71dce9d9cbc90fb651e5cc23b3cdd6d89 Mon Sep 17 00:00:00 2001 From: angusmcleod Date: Mon, 31 Jan 2022 16:44:22 +0800 Subject: [PATCH 11/11] Identify attribute with liquid template error and pass syntax error --- config/locales/server.en.yml | 2 +- lib/custom_wizard/validators/template.rb | 47 +++++++------------ .../custom_wizard/template_validator_spec.rb | 43 +++++++++++------ 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 8fc22fcfcd..2d1499d97c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -49,7 +49,7 @@ en: required: "%{property} is required" conflict: "Wizard with id '%{wizard_id}' already exists" after_time: "After time setting is invalid" - liquid_syntax_error: "Syntax error in liquid template" + liquid_syntax_error: "Liquid syntax error in %{attribute}: %{message}" site_settings: custom_wizard_enabled: "Enable custom wizards." diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index d9693b9727..197afa8644 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -85,43 +85,32 @@ def validate_after_time end def validate_liquid_template(object, type) - valid = true - - case type - when :field - is_description_valid = is_liquid_template_valid?(object['description']) - is_placeholder_valid = is_liquid_template_valid?(object['placeholder']) - is_preview_template_valid = begin - if object[:type] == 'composer_preview' - is_liquid_template_valid?(object['preview_template']) - else - true + %w[ + description + placeholder + preview_template + post_template + ].each do |field| + if template = object[field] + result = is_liquid_template_valid?(template) + + unless "valid" == result + error = I18n.t("wizard.validation.liquid_syntax_error", + attribute: "#{object[:id]}.#{field}", + message: result + ) + errors.add :base, error end end - - if !is_description_valid || !is_placeholder_valid || !is_preview_template_valid - valid = false - end - when :step - if !is_liquid_template_valid?(object['description']) - valid = false - end - when :action - if object['post_builder'] - valid = is_liquid_template_valid?(object['post_template']) - end end - - errors.add :base, I18n.t("wizard.validation.liquid_syntax_error") unless valid - valid end def is_liquid_template_valid?(template) begin Liquid::Template.parse(template) - true - rescue Liquid::SyntaxError - false + 'valid' + rescue Liquid::SyntaxError => error + error.message end end end diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index ea687f6666..c8c61574ff 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -21,16 +21,20 @@ LIQUID } + let(:liquid_syntax_error) { + "Liquid syntax error: Tag '{%' was not properly terminated with regexp: /\\%\\}/" + } + def expect_validation_success expect( CustomWizard::TemplateValidator.new(template).perform ).to eq(true) end - def expect_validation_failure - expect( - CustomWizard::TemplateValidator.new(template).perform - ).to eq(false) + def expect_validation_failure(object_id, message) + validator = CustomWizard::TemplateValidator.new(template) + expect(validator.perform).to eq(false) + expect(validator.errors.first.message).to eq("Liquid syntax error in #{object_id}: #{message}") end it "validates valid templates" do @@ -114,40 +118,49 @@ def expect_validation_failure it "doesn't validate if liquid syntax in use is incorrect" do template[:steps][0][:description] = invalid_liquid_template - expect_validation_failure + expect_validation_failure("step_1.description", liquid_syntax_error) end context "validation targets" do context "fields" do it "validates descriptions" do template[:steps][0][:fields][0][:description] = invalid_liquid_template - expect_validation_failure + expect_validation_failure("step_1_field_1.description", liquid_syntax_error) end - it "validates preview templates" do - template[:steps][0][:fields][4][:preview_template] = invalid_liquid_template - expect_validation_failure + it "validates placeholders" do + template[:steps][0][:fields][0][:placeholder] = invalid_liquid_template + expect_validation_failure("step_1_field_1.placeholder", liquid_syntax_error) end - it "validates placeholders" do - template[:steps][0][:fields][0][:description] = invalid_liquid_template - expect_validation_failure + it "validates preview templates" do + template[:steps][0][:fields][4][:preview_template] = invalid_liquid_template + expect_validation_failure("step_1_field_5.preview_template", liquid_syntax_error) end end context "steps" do it "validates descriptions" do template[:steps][0][:description] = invalid_liquid_template - expect_validation_failure + expect_validation_failure("step_1.description", liquid_syntax_error) end end context "actions" do it "validates post builder" do - action_index = template[:actions].index { |action| action[:post_builder] } + action = nil + action_index = nil + + template[:actions].each_with_index do |a, i| + if a["post_builder"] + action = a + action_index = i + break + end + end template[:actions][action_index][:post_template] = invalid_liquid_template - expect_validation_failure + expect_validation_failure("#{action[:id]}.post_template", liquid_syntax_error) end end end