|
9 | 9 | "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json" |
10 | 10 | ).read).with_indifferent_access |
11 | 11 | } |
| 12 | + let(:valid_liquid_template) { |
| 13 | + <<-LIQUID.strip |
| 14 | + {%- assign hello = "Topic Form 1" %} |
| 15 | + LIQUID |
| 16 | + } |
| 17 | + |
| 18 | + let(:invalid_liquid_template) { |
| 19 | + <<-LIQUID.strip |
| 20 | + {%- assign hello = "Topic Form 1" % |
| 21 | + LIQUID |
| 22 | + } |
| 23 | + |
| 24 | + let(:liquid_syntax_error) { |
| 25 | + "Liquid syntax error: Tag '{%' was not properly terminated with regexp: /\\%\\}/" |
| 26 | + } |
| 27 | + |
| 28 | + def expect_validation_success |
| 29 | + expect( |
| 30 | + CustomWizard::TemplateValidator.new(template).perform |
| 31 | + ).to eq(true) |
| 32 | + end |
| 33 | + |
| 34 | + def expect_validation_failure(object_id, message) |
| 35 | + validator = CustomWizard::TemplateValidator.new(template) |
| 36 | + expect(validator.perform).to eq(false) |
| 37 | + expect(validator.errors.first.message).to eq("Liquid syntax error in #{object_id}: #{message}") |
| 38 | + end |
12 | 39 |
|
13 | 40 | it "validates valid templates" do |
14 | 41 | expect( |
|
110 | 137 | end |
111 | 138 | end |
112 | 139 | end |
| 140 | + |
| 141 | + context "liquid templates" do |
| 142 | + it "validates if no liquid syntax in use" do |
| 143 | + expect_validation_success |
| 144 | + end |
| 145 | + |
| 146 | + it "validates if liquid syntax in use is correct" do |
| 147 | + template[:steps][0][:raw_description] = valid_liquid_template |
| 148 | + expect_validation_success |
| 149 | + end |
| 150 | + |
| 151 | + it "doesn't validate if liquid syntax in use is incorrect" do |
| 152 | + template[:steps][0][:raw_description] = invalid_liquid_template |
| 153 | + expect_validation_failure("step_1.raw_description", liquid_syntax_error) |
| 154 | + end |
| 155 | + |
| 156 | + context "validation targets" do |
| 157 | + context "fields" do |
| 158 | + it "validates descriptions" do |
| 159 | + template[:steps][0][:fields][0][:description] = invalid_liquid_template |
| 160 | + expect_validation_failure("step_1_field_1.description", liquid_syntax_error) |
| 161 | + end |
| 162 | + |
| 163 | + it "validates placeholders" do |
| 164 | + template[:steps][0][:fields][0][:placeholder] = invalid_liquid_template |
| 165 | + expect_validation_failure("step_1_field_1.placeholder", liquid_syntax_error) |
| 166 | + end |
| 167 | + |
| 168 | + it "validates preview templates" do |
| 169 | + template[:steps][0][:fields][4][:preview_template] = invalid_liquid_template |
| 170 | + expect_validation_failure("step_1_field_5.preview_template", liquid_syntax_error) |
| 171 | + end |
| 172 | + end |
| 173 | + |
| 174 | + context "steps" do |
| 175 | + it "validates descriptions" do |
| 176 | + template[:steps][0][:raw_description] = invalid_liquid_template |
| 177 | + expect_validation_failure("step_1.raw_description", liquid_syntax_error) |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | + context "actions" do |
| 182 | + it "validates post builder" do |
| 183 | + action = nil |
| 184 | + action_index = nil |
| 185 | + |
| 186 | + template[:actions].each_with_index do |a, i| |
| 187 | + if a["post_builder"] |
| 188 | + action = a |
| 189 | + action_index = i |
| 190 | + break |
| 191 | + end |
| 192 | + end |
| 193 | + template[:actions][action_index][:post_template] = invalid_liquid_template |
| 194 | + |
| 195 | + expect_validation_failure("#{action[:id]}.post_template", liquid_syntax_error) |
| 196 | + end |
| 197 | + end |
| 198 | + end |
| 199 | + end |
113 | 200 | end |
0 commit comments