0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-24 02:10:29 +01:00
discourse-custom-wizard/spec/components/custom_wizard/wizard_spec.rb

308 Zeilen
10 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-11-03 01:24:20 +01:00
describe CustomWizard::Wizard do
fab!(:user)
2021-03-11 07:30:15 +01:00
fab!(:trusted_user) { Fabricate(:user, trust_level: TrustLevel[3]) }
fab!(:admin_user) { Fabricate(:user, admin: true) }
2021-09-07 14:06:13 +02:00
let(:template_json) { get_wizard_fixture("wizard") }
let(:permitted_json) { get_wizard_fixture("wizard/permitted") }
let(:guests_permitted_json) { get_wizard_fixture("wizard/guests_permitted") }
let(:step_json) { get_wizard_fixture("step/step") }
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
before do
Group.refresh_automatic_group!(:trust_level_3)
@permitted_template = template_json.dup
@permitted_template["permitted"] = permitted_json["permitted"]
@guests_permitted_template = template_json.dup
@guests_permitted_template["permitted"] = guests_permitted_json["permitted"]
2020-11-03 01:24:20 +01:00
@wizard = CustomWizard::Wizard.new(template_json, user)
end
def append_steps
2024-10-16 13:52:03 +02:00
template_json["steps"].each { |step_template| @wizard.append_step(step_template["id"]) }
@wizard.update!
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2023-01-18 19:53:36 +01:00
def progress_step(step_id, actor_id: user.id, wizard: @wizard)
CustomWizard::UserHistory.create(
action: CustomWizard::UserHistory.actions[:step],
actor_id: actor_id,
context: wizard.id,
2024-10-16 13:52:03 +02:00
subject: step_id,
2020-11-03 01:24:20 +01:00
)
@wizard.update!
2021-03-11 07:30:15 +01:00
end
it "appends steps" do
append_steps
2020-11-03 01:24:20 +01:00
expect(@wizard.steps.length).to eq(3)
end
2021-03-11 07:30:15 +01:00
it "appends steps with indexes" do
append_steps
expect(@wizard.steps.first.index).to eq(0)
expect(@wizard.steps.last.index).to eq(2)
end
it "appends steps with custom indexes" do
2024-10-16 13:52:03 +02:00
template_json["steps"][0]["index"] = 2
template_json["steps"][1]["index"] = 1
template_json["steps"][2]["index"] = 0
2024-10-16 13:52:03 +02:00
template_json["steps"].each do |step_template|
@wizard.append_step(step_template["id"]) do |step|
step.index = step_template["index"] if step_template["index"]
end
end
expect(@wizard.steps.first.index).to eq(2)
expect(@wizard.steps.last.index).to eq(0)
@wizard.update!
expect(@wizard.steps.first.id).to eq("step_3")
expect(@wizard.steps.last.id).to eq("step_1")
expect(@wizard.steps.first.next.id).to eq("step_2")
expect(@wizard.steps.last.next).to eq(nil)
end
2020-11-03 01:24:20 +01:00
it "determines the user's current step" do
append_steps
2024-10-16 13:52:03 +02:00
expect(@wizard.start).to eq("step_1")
progress_step("step_1")
expect(@wizard.start).to eq("step_2")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
it "determines the user's current step if steps are added" do
append_steps
2024-10-16 13:52:03 +02:00
progress_step("step_1")
progress_step("step_2")
progress_step("step_3")
fourth_step = step_json.dup
2024-10-16 13:52:03 +02:00
fourth_step["id"] = "step_4"
template = template_json.dup
2024-10-16 13:52:03 +02:00
template["steps"] << fourth_step
CustomWizard::Template.save(template, skip_jobs: true)
wizard = CustomWizard::Wizard.new(template, user)
2024-10-16 13:52:03 +02:00
template["steps"].each { |step_template| wizard.append_step(step_template["id"]) }
expect(wizard.steps.size).to eq(4)
expect(wizard.start).to eq(nil)
end
2020-11-03 01:24:20 +01:00
it "creates a step updater" do
2024-10-16 13:52:03 +02:00
expect(@wizard.create_updater("step_1", step_1_field_1: "Text input").class).to eq(
CustomWizard::StepUpdater,
)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "determines whether a wizard is unfinished" do
append_steps
2020-11-03 01:24:20 +01:00
expect(@wizard.unfinished?).to eq(true)
progress_step("step_1")
expect(@wizard.unfinished?).to eq(true)
progress_step("step_2")
expect(@wizard.unfinished?).to eq(true)
progress_step("step_3")
expect(@wizard.unfinished?).to eq(false)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "determines whether a wizard has been completed by a user" do
append_steps
2020-11-03 01:24:20 +01:00
expect(@wizard.completed?).to eq(false)
progress_step("step_1")
progress_step("step_2")
progress_step("step_3")
expect(@wizard.completed?).to eq(true)
end
2021-03-11 07:30:15 +01:00
context "without mutliple submissions" do
it "is completed if steps submitted before after time" do
append_steps
progress_step("step_1")
progress_step("step_2")
progress_step("step_3")
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
template_json["after_time"] = true
template_json["after_time_scheduled"] = Time.now + 3.hours
2021-03-11 07:30:15 +01:00
wizard = CustomWizard::Wizard.new(template_json, user)
expect(wizard.completed?).to eq(true)
end
end
context "with multiple submissions" do
it "is completed if steps submitted before after time" do
append_steps
progress_step("step_1")
progress_step("step_2")
progress_step("step_3")
2024-10-16 13:52:03 +02:00
template_json["after_time"] = true
template_json["multiple_submissions"] = true
template_json["after_time_scheduled"] = Time.now + 3.hours
wizard = CustomWizard::Wizard.new(template_json, user)
expect(wizard.completed?).to eq(false)
end
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
context "with subscription" do
2024-10-16 13:52:03 +02:00
before { enable_subscription("standard") }
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it "permits admins" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@permitted_template, admin_user).permitted?).to eq(true)
2022-03-25 17:08:24 +01:00
end
2022-03-25 17:08:24 +01:00
it "permits permitted users" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@permitted_template, trusted_user).permitted?).to eq(true)
2022-03-25 17:08:24 +01:00
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it "permits everyone if everyone is permitted" do
2024-10-16 13:52:03 +02:00
@permitted_template["permitted"][0]["output"] = Group::AUTO_GROUPS[:everyone]
expect(CustomWizard::Wizard.new(@permitted_template, user).permitted?).to eq(true)
2022-03-25 17:08:24 +01:00
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it "does not permit unpermitted users" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@permitted_template, user).permitted?).to eq(false)
2022-03-25 17:08:24 +01:00
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it "does not let an unpermitted user access a wizard" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@permitted_template, user).can_access?).to eq(false)
2022-03-25 17:08:24 +01:00
end
2021-03-11 07:30:15 +01:00
2022-03-25 17:08:24 +01:00
it "lets a permitted user access an incomplete wizard" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@permitted_template, trusted_user).can_access?).to eq(true)
2022-03-25 17:08:24 +01:00
end
2022-03-25 17:08:24 +01:00
it "lets a permitted user access a complete wizard with multiple submissions" do
append_steps
2021-03-11 07:30:15 +01:00
2023-01-18 19:53:36 +01:00
progress_step("step_1", actor_id: trusted_user.id)
progress_step("step_2", actor_id: trusted_user.id)
progress_step("step_3", actor_id: trusted_user.id)
2022-03-25 17:08:24 +01:00
@permitted_template["multiple_submissions"] = true
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@permitted_template, trusted_user).can_access?).to eq(true)
2022-03-25 17:08:24 +01:00
end
2022-03-25 17:08:24 +01:00
it "does not let an unpermitted user access a complete wizard without multiple submissions" do
append_steps
2021-03-11 07:30:15 +01:00
2023-01-18 19:53:36 +01:00
progress_step("step_1", actor_id: trusted_user.id)
progress_step("step_2", actor_id: trusted_user.id)
progress_step("step_3", actor_id: trusted_user.id)
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
@permitted_template["multiple_submissions"] = false
2022-03-25 17:08:24 +01:00
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@permitted_template, trusted_user).can_access?).to eq(false)
2022-03-25 17:08:24 +01:00
end
it "sets wizard redirects if user is permitted" do
CustomWizard::Template.save(@permitted_template, skip_jobs: true)
2024-10-16 13:52:03 +02:00
CustomWizard::Wizard.set_user_redirect("super_mega_fun_wizard", trusted_user)
expect(trusted_user.custom_fields["redirect_to_wizard"]).to eq("super_mega_fun_wizard")
2022-03-25 17:08:24 +01:00
end
it "does not set a wizard redirect if user is not permitted" do
CustomWizard::Template.save(@permitted_template, skip_jobs: true)
2024-10-16 13:52:03 +02:00
CustomWizard::Wizard.set_user_redirect("super_mega_fun_wizard", user)
expect(trusted_user.custom_fields["redirect_to_wizard"]).to eq(nil)
2022-03-25 17:08:24 +01:00
end
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
context "with subscription and restart upon revisit" do
before do
enable_subscription("standard")
@wizard.restart_on_revisit = true
2024-10-16 13:52:03 +02:00
CustomWizard::Template.save(CustomWizard::WizardSerializer.new(@wizard, root: false).as_json)
end
it "returns to step 1 if option to clear submissions on each visit is set" do
append_steps
expect(@wizard.unfinished?).to eq(true)
2024-10-16 13:52:03 +02:00
progress_step("step_1")
expect(@wizard.start).to eq("step_1")
end
end
2023-03-10 15:42:42 +01:00
context "with subscription and guest wizard" do
2024-10-16 13:52:03 +02:00
before { enable_subscription("standard") }
2023-03-10 15:42:42 +01:00
it "permits admins" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@guests_permitted_template, admin_user).permitted?).to eq(
true,
)
2023-03-10 15:42:42 +01:00
end
it "permits regular users" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.new(@guests_permitted_template, user).permitted?).to eq(true)
2023-03-10 15:42:42 +01:00
end
it "permits guests" do
expect(
2024-10-16 13:52:03 +02:00
CustomWizard::Wizard.new(@guests_permitted_template, nil, "guest123").permitted?,
2023-03-10 15:42:42 +01:00
).to eq(true)
end
end
2020-11-03 01:24:20 +01:00
context "submissions" do
2024-10-16 13:52:03 +02:00
before { CustomWizard::Submission.new(@wizard, step_1_field_1: "I am a user submission").save }
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "lists the user's submissions" do
expect(@wizard.submissions.length).to eq(1)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "returns the user's current submission" do
expect(@wizard.current_submission.fields["step_1_field_1"]).to eq("I am a user submission")
2020-11-03 01:24:20 +01:00
end
end
2021-03-11 07:30:15 +01:00
context "class methods" do
2020-11-03 01:24:20 +01:00
before do
2022-03-25 17:08:24 +01:00
enable_subscription("standard")
2020-11-03 01:24:20 +01:00
CustomWizard::Template.save(@permitted_template, skip_jobs: true)
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
template_json_2 = template_json.dup
2024-10-16 13:52:03 +02:00
template_json_2["id"] = "super_mega_fun_wizard_2"
2020-11-03 01:24:20 +01:00
template_json_2["prompt_completion"] = true
CustomWizard::Template.save(template_json_2, skip_jobs: true)
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
template_json_3 = template_json.dup
2024-10-16 13:52:03 +02:00
template_json_3["id"] = "super_mega_fun_wizard_3"
2020-11-03 01:24:20 +01:00
template_json_3["after_signup"] = true
template_json_3["prompt_completion"] = true
2020-11-03 01:24:20 +01:00
CustomWizard::Template.save(template_json_3, skip_jobs: true)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "lists wizards the user can see" do
expect(CustomWizard::Wizard.list(user).length).to eq(2)
expect(CustomWizard::Wizard.list(trusted_user).length).to eq(3)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "returns the first after signup wizard" do
2024-10-16 13:52:03 +02:00
expect(CustomWizard::Wizard.after_signup(user).id).to eq("super_mega_fun_wizard_3")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "lists prompt completion wizards" do
expect(CustomWizard::Wizard.prompt_completion(user).length).to eq(2)
end
2021-03-11 07:30:15 +01:00
it "prompt completion does not include wizards user has completed" do
2024-10-16 13:52:03 +02:00
wizard_2 =
CustomWizard::Wizard.new(CustomWizard::Template.find("super_mega_fun_wizard_2"), user)
progress_step("step_1", wizard: wizard_2)
progress_step("step_2", wizard: wizard_2)
progress_step("step_3", wizard: wizard_2)
expect(CustomWizard::Wizard.prompt_completion(user).length).to eq(1)
end
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
end