2021-04-16 10:07:32 +02:00
|
|
|
# frozen_string_literal: true
|
2019-12-04 12:13:51 +01:00
|
|
|
|
|
|
|
describe CustomWizard::Builder do
|
2024-10-16 13:52:03 +02:00
|
|
|
fab!(:trusted_user) do
|
|
|
|
Fabricate(:user, username: "angus", email: "angus@email.com", trust_level: TrustLevel[3])
|
|
|
|
end
|
|
|
|
fab!(:user)
|
|
|
|
fab!(:category1) { Fabricate(:category, name: "cat1") }
|
|
|
|
fab!(:category2) { Fabricate(:category, name: "cat2") }
|
|
|
|
fab!(:group)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-07 14:06:13 +02:00
|
|
|
let(:wizard_template) { get_wizard_fixture("wizard") }
|
|
|
|
let(:required_data_json) { get_wizard_fixture("step/required_data") }
|
|
|
|
let(:permitted_json) { get_wizard_fixture("wizard/permitted") }
|
|
|
|
let(:permitted_param_json) { get_wizard_fixture("step/permitted_params") }
|
|
|
|
let(:user_condition_json) { get_wizard_fixture("condition/user_condition") }
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
let(:boolean_field_condition_json) do
|
2021-10-20 15:19:21 +02:00
|
|
|
JSON.parse(
|
|
|
|
File.open(
|
2024-10-16 13:52:03 +02:00
|
|
|
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/condition/boolean_field_condition.json",
|
|
|
|
).read,
|
2021-10-20 15:19:21 +02:00
|
|
|
)
|
2024-10-16 13:52:03 +02:00
|
|
|
end
|
2021-10-20 15:19:21 +02:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
before do
|
|
|
|
Group.refresh_automatic_group!(:trust_level_3)
|
2021-09-07 14:06:13 +02:00
|
|
|
CustomWizard::Template.save(wizard_template, skip_jobs: true)
|
2024-10-16 13:52:03 +02:00
|
|
|
@template = CustomWizard::Template.find("super_mega_fun_wizard")
|
2019-12-09 06:51:42 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
context "disabled" do
|
|
|
|
before { SiteSetting.custom_wizard_enabled = false }
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
it "returns nil" do
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build).to eq(nil)
|
2019-12-09 06:51:42 +01:00
|
|
|
end
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
context "enabled" do
|
|
|
|
before { SiteSetting.custom_wizard_enabled = true }
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
it "returns wizard metadata" do
|
|
|
|
wizard = CustomWizard::Builder.new(@template[:id], user).build
|
|
|
|
expect(wizard.id).to eq("super_mega_fun_wizard")
|
|
|
|
expect(wizard.name).to eq("Super Mega Fun Wizard")
|
|
|
|
expect(wizard.background).to eq("#333333")
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2019-12-06 10:05:19 +01:00
|
|
|
it "returns steps" do
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.steps.length).to eq(3)
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
context "with multiple submissions disabled" do
|
|
|
|
before do
|
|
|
|
@template[:multiple_submissions] = false
|
|
|
|
CustomWizard::Template.save(@template.as_json)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns steps if user has not completed it" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.steps.length).to eq(3)
|
2020-10-20 01:15:03 +02:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns no steps if user has completed it" do
|
2020-10-31 08:05:50 +01:00
|
|
|
@template[:steps].each do |step|
|
2023-01-18 19:53:36 +01:00
|
|
|
CustomWizard::UserHistory.create!(
|
|
|
|
action: CustomWizard::UserHistory.actions[:step],
|
|
|
|
actor_id: user.id,
|
|
|
|
context: @template[:id],
|
2024-10-16 13:52:03 +02:00
|
|
|
subject: step[:id],
|
2020-10-31 08:05:50 +01:00
|
|
|
)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.steps.length).to eq(0)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
context "with restricted permissions" do
|
|
|
|
before do
|
2022-03-25 17:08:24 +01:00
|
|
|
enable_subscription("standard")
|
2020-10-31 08:05:50 +01:00
|
|
|
@template[:permitted] = permitted_json["permitted"]
|
|
|
|
CustomWizard::Template.save(@template.as_json)
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "is not permitted if user is not in permitted group" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.permitted?).to eq(false)
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "user cannot access if not permitted" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.can_access?).to eq(false)
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns wizard metadata if user is not permitted" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.name).to eq(
|
|
|
|
"Super Mega Fun Wizard",
|
|
|
|
)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns no steps if user is not permitted" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.steps.length).to eq(0)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "is permitted if user is in permitted group" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], trusted_user).build.permitted?).to eq(true)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "user can access if permitted" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], trusted_user).build.can_access?).to eq(
|
|
|
|
true,
|
|
|
|
)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns steps if user is permitted" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], trusted_user).build.steps.length).to eq(3)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns prefilled data" do
|
2020-10-31 08:05:50 +01:00
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
CustomWizard::Builder.new(@template[:id], user).build.steps.first.fields.first.value,
|
|
|
|
).to eq("I am prefilled")
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
context "user has partially completed" do
|
|
|
|
before do
|
2020-11-03 01:24:20 +01:00
|
|
|
wizard = CustomWizard::Wizard.new(@template, user)
|
2024-10-16 13:52:03 +02:00
|
|
|
data = { step_1_field_1: "I am a user submission" }
|
2021-06-17 09:50:22 +02:00
|
|
|
CustomWizard::Submission.new(wizard, data).save
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns saved submissions" do
|
2020-10-20 01:15:03 +02:00
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
CustomWizard::Builder.new(@template[:id], user).build.steps.first.fields.first.value,
|
|
|
|
).to eq("I am a user submission")
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
context "restart is enabled" do
|
|
|
|
before do
|
2021-11-09 15:57:33 +01:00
|
|
|
enable_subscription("standard")
|
2020-10-31 08:05:50 +01:00
|
|
|
@template[:restart_on_revisit] = true
|
|
|
|
CustomWizard::Template.save(@template.as_json)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "does not return saved submissions" do
|
2020-10-31 08:05:50 +01:00
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
CustomWizard::Builder.new(@template[:id], user).build.steps.first.fields.first.value,
|
|
|
|
).to eq("I am prefilled")
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
context "building step" do
|
|
|
|
it "returns step metadata" do
|
|
|
|
first_step = CustomWizard::Builder.new(@template[:id], user).build(reset: true).steps.first
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
expect(first_step.id).to eq("step_1")
|
|
|
|
expect(first_step.title).to eq("Text")
|
|
|
|
expect(first_step.description).to eq("<p>Text inputs!</p>")
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
context "with required data" do
|
2020-10-31 08:05:50 +01:00
|
|
|
before do
|
2021-11-09 15:57:33 +01:00
|
|
|
enable_subscription("standard")
|
2024-10-16 13:52:03 +02:00
|
|
|
@template[:steps][0][:required_data] = required_data_json["required_data"]
|
|
|
|
@template[:steps][0][:required_data_message] = required_data_json["required_data_message"]
|
2020-10-31 08:05:50 +01:00
|
|
|
CustomWizard::Template.save(@template.as_json)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "is not permitted if required data is not present" do
|
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.steps.first.permitted).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "it shows required data message" do
|
2020-10-31 08:05:50 +01:00
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
CustomWizard::Builder.new(@template[:id], user).build.steps.first.permitted_message,
|
2020-10-31 08:05:50 +01:00
|
|
|
).to eq("Missing required data")
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "is permitted if required data is present" do
|
|
|
|
wizard = CustomWizard::Wizard.create("super_mega_fun_wizard", user)
|
2021-06-23 08:13:58 +02:00
|
|
|
CustomWizard::Submission.new(wizard, step_1_field_1: "required").save
|
2021-06-17 09:50:22 +02:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(CustomWizard::Builder.new(@template[:id], user).build.steps.first.permitted).to eq(
|
|
|
|
true,
|
|
|
|
)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
context "with permitted params" do
|
|
|
|
before do
|
2021-11-09 15:57:33 +01:00
|
|
|
enable_subscription("standard")
|
2024-10-16 13:52:03 +02:00
|
|
|
@template[:steps][0][:permitted_params] = permitted_param_json["permitted_params"]
|
2020-10-31 08:05:50 +01:00
|
|
|
CustomWizard::Template.save(@template.as_json)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "saves permitted params" do
|
|
|
|
wizard = CustomWizard::Builder.new(@template[:id], user).build({}, param: "param_value")
|
|
|
|
expect(wizard.current_submission.fields["saved_param"]).to eq("param_value")
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
|
2021-09-07 14:11:50 +02:00
|
|
|
context "with condition" do
|
2021-04-20 19:58:19 +02:00
|
|
|
before do
|
2021-11-09 15:57:33 +01:00
|
|
|
enable_subscription("standard")
|
2024-10-16 13:52:03 +02:00
|
|
|
@template[:steps][0][:condition] = user_condition_json["condition"]
|
2021-04-20 19:58:19 +02:00
|
|
|
CustomWizard::Template.save(@template.as_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds step when condition is passed" do
|
|
|
|
wizard = CustomWizard::Builder.new(@template[:id], trusted_user).build
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(wizard.steps.first.id).to eq(@template[:steps][0]["id"])
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not add step when condition is not passed" do
|
|
|
|
wizard = CustomWizard::Builder.new(@template[:id], user).build
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(wizard.steps.first.id).to eq(@template[:steps][1]["id"])
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
|
|
|
end
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
context "building field" do
|
|
|
|
it "returns field metadata" do
|
2020-10-31 08:05:50 +01:00
|
|
|
wizard = CustomWizard::Builder.new(@template[:id], user).build
|
|
|
|
field = wizard.steps.first.fields.first
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
expect(field.label).to eq("<p>Text</p>")
|
|
|
|
expect(field.type).to eq("text")
|
|
|
|
expect(field.id).to eq("step_1_field_1")
|
|
|
|
expect(field.min_length).to eq("3")
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "returns all step fields" do
|
2020-10-31 08:05:50 +01:00
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
CustomWizard::Builder.new(@template[:id], user).build.steps.first.fields.length,
|
2022-01-31 10:41:14 +01:00
|
|
|
).to eq(@template[:steps][0][:fields].length)
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
|
|
|
|
context "with condition" do
|
|
|
|
before do
|
2021-11-09 15:57:33 +01:00
|
|
|
enable_subscription("standard")
|
2024-10-16 13:52:03 +02:00
|
|
|
@template[:steps][0][:fields][0][:condition] = user_condition_json["condition"]
|
|
|
|
@template[:steps][2][:fields][0][:condition] = boolean_field_condition_json["condition"]
|
2021-04-20 19:58:19 +02:00
|
|
|
CustomWizard::Template.save(@template.as_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds field when condition is passed" do
|
|
|
|
wizard = CustomWizard::Builder.new(@template[:id], trusted_user).build
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(wizard.steps.first.fields.first.id).to eq(@template[:steps][0][:fields][0]["id"])
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not add field when condition is not passed" do
|
|
|
|
wizard = CustomWizard::Builder.new(@template[:id], user).build
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(wizard.steps.first.fields.first.id).to eq(@template[:steps][0][:fields][1]["id"])
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
2021-10-20 15:19:21 +02:00
|
|
|
|
|
|
|
it "works if a field condition uses 'is true/false'" do
|
|
|
|
builder = CustomWizard::Builder.new(@template[:id], user)
|
|
|
|
wizard = builder.build
|
2024-10-16 13:52:03 +02:00
|
|
|
wizard.create_updater("step_2", step_2_field_5: "true").update
|
2021-10-20 15:19:21 +02:00
|
|
|
|
|
|
|
builder = CustomWizard::Builder.new(@template[:id], user)
|
|
|
|
wizard = builder.build
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(wizard.steps.last.fields.last.id).to eq(@template[:steps][2][:fields][0]["id"])
|
2021-10-20 15:19:21 +02:00
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
context "on update" do
|
2020-10-31 08:05:50 +01:00
|
|
|
def perform_update(step_id, submission)
|
2021-06-17 09:50:22 +02:00
|
|
|
updater = @wizard.create_updater(step_id, submission)
|
2020-10-31 08:05:50 +01:00
|
|
|
updater.update
|
|
|
|
updater
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
it "saves submissions" do
|
2021-06-17 09:50:22 +02:00
|
|
|
@wizard = CustomWizard::Builder.new(@template[:id], user).build
|
2024-10-16 13:52:03 +02:00
|
|
|
perform_update("step_1", step_1_field_1: "Text input")
|
|
|
|
expect(@wizard.current_submission.fields["step_1_field_1"]).to eq("Text input")
|
2019-12-09 06:51:42 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
context "save submissions disabled" do
|
2020-10-31 08:05:50 +01:00
|
|
|
before do
|
2021-11-09 15:57:33 +01:00
|
|
|
enable_subscription("standard")
|
2020-10-31 08:05:50 +01:00
|
|
|
@template[:save_submissions] = false
|
|
|
|
CustomWizard::Template.save(@template.as_json)
|
2021-06-17 09:50:22 +02:00
|
|
|
@wizard = CustomWizard::Builder.new(@template[:id], user).build
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
it "does not save submissions" do
|
2024-10-16 13:52:03 +02:00
|
|
|
perform_update("step_1", step_1_field_1: "Text input")
|
2021-06-23 08:13:58 +02:00
|
|
|
expect(@wizard.current_submission.present?).to eq(false)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
|
|
|
end
|
2019-12-04 12:13:51 +01:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
end
|