2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2020-11-30 22:20:10 +01:00
|
|
|
require_relative '../../plugin_helper'
|
2020-11-03 01:24:20 +01:00
|
|
|
|
2020-11-26 04:05:50 +01:00
|
|
|
describe CustomWizard::TemplateValidator do
|
2020-11-03 01:24:20 +01:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
let(:template) {
|
|
|
|
JSON.parse(File.open(
|
|
|
|
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
|
|
|
|
).read).with_indifferent_access
|
|
|
|
}
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it "validates valid templates" do
|
|
|
|
expect(
|
2020-11-26 04:05:50 +01:00
|
|
|
CustomWizard::TemplateValidator.new(template).perform
|
2020-11-03 01:24:20 +01:00
|
|
|
).to eq(true)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it "invalidates templates without required attributes" do
|
|
|
|
template.delete(:id)
|
|
|
|
expect(
|
2020-11-26 04:05:50 +01:00
|
|
|
CustomWizard::TemplateValidator.new(template).perform
|
2020-11-03 01:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it "invalidates templates with duplicate ids if creating a new template" do
|
|
|
|
CustomWizard::Template.save(template)
|
|
|
|
expect(
|
2020-11-26 04:05:50 +01:00
|
|
|
CustomWizard::TemplateValidator.new(template, create: true).perform
|
2020-11-03 01:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-01-31 10:18:04 +01:00
|
|
|
it "only allows one after signup wizard at a time" do
|
|
|
|
wizard_id = template[:id]
|
|
|
|
template[:after_signup] = true
|
|
|
|
CustomWizard::Template.save(template)
|
|
|
|
|
|
|
|
template[:id] = "wizard_2"
|
|
|
|
template[:after_signup] = true
|
|
|
|
|
|
|
|
validator = CustomWizard::TemplateValidator.new(template)
|
|
|
|
expect(validator.perform).to eq(false)
|
|
|
|
expect(validator.errors.first.type).to eq(
|
|
|
|
I18n.t("wizard.validation.after_signup", wizard_id: wizard_id)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "only allows a wizard with after signup to be validated twice" do
|
|
|
|
template[:after_signup] = true
|
|
|
|
CustomWizard::Template.save(template)
|
|
|
|
expect(CustomWizard::TemplateValidator.new(template).perform).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "only allows one after _ setting per wizard" do
|
|
|
|
template[:after_signup] = true
|
|
|
|
template[:after_time] = true
|
|
|
|
|
|
|
|
validator = CustomWizard::TemplateValidator.new(template)
|
|
|
|
expect(validator.perform).to eq(false)
|
|
|
|
expect(validator.errors.first.type).to eq(
|
|
|
|
I18n.t("wizard.validation.after_signup_after_time")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it "validates after time settings" do
|
2020-11-23 01:40:46 +01:00
|
|
|
template[:after_time] = true
|
|
|
|
template[:after_time_scheduled] = (Time.now + 3.hours).iso8601
|
2020-11-03 01:24:20 +01:00
|
|
|
expect(
|
2020-11-26 04:05:50 +01:00
|
|
|
CustomWizard::TemplateValidator.new(template).perform
|
2020-11-03 01:24:20 +01:00
|
|
|
).to eq(true)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it "invalidates invalid after time settings" do
|
2020-11-23 01:40:46 +01:00
|
|
|
template[:after_time] = true
|
2020-11-03 01:24:20 +01:00
|
|
|
template[:after_time_scheduled] = "not a time"
|
|
|
|
expect(
|
2020-11-26 04:05:50 +01:00
|
|
|
CustomWizard::TemplateValidator.new(template).perform
|
2020-11-03 01:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-12-07 09:50:34 +01:00
|
|
|
|
|
|
|
context "steps" do
|
|
|
|
CustomWizard::TemplateValidator.required[:step].each do |attribute|
|
|
|
|
it "invalidates if \"#{attribute.to_s}\" is not present" do
|
|
|
|
template[:steps][0][attribute] = nil
|
|
|
|
expect(
|
|
|
|
CustomWizard::TemplateValidator.new(template).perform
|
|
|
|
).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "fields" do
|
|
|
|
CustomWizard::TemplateValidator.required[:field].each do |attribute|
|
|
|
|
it "invalidates if \"#{attribute.to_s}\" is not present" do
|
|
|
|
template[:steps][0][:fields][0][attribute] = nil
|
|
|
|
expect(
|
|
|
|
CustomWizard::TemplateValidator.new(template).perform
|
|
|
|
).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "actions" do
|
|
|
|
CustomWizard::TemplateValidator.required[:action].each do |attribute|
|
|
|
|
it "invalidates if \"#{attribute.to_s}\" is not present" do
|
|
|
|
template[:actions][0][attribute] = nil
|
|
|
|
expect(
|
|
|
|
CustomWizard::TemplateValidator.new(template).perform
|
|
|
|
).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
end
|