0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 18:50:27 +01:00
discourse-custom-wizard/spec/requests/custom_wizard/wizard_controller_spec.rb

102 Zeilen
3,6 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-11-03 01:24:20 +01:00
describe CustomWizard::WizardController do
2024-10-16 13:52:03 +02:00
fab!(:user) do
Fabricate(:user, username: "angus", email: "angus@email.com", trust_level: TrustLevel[3])
end
2021-09-07 14:06:13 +02:00
let(:wizard_template) { get_wizard_fixture("wizard") }
let(:permitted_json) { get_wizard_fixture("wizard/permitted") }
2021-06-16 06:24:07 +02:00
2020-11-03 01:24:20 +01:00
before do
2021-09-07 14:06:13 +02:00
CustomWizard::Template.save(wizard_template, skip_jobs: true)
2020-11-03 01:24:20 +01:00
@template = CustomWizard::Template.find("super_mega_fun_wizard")
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
context "plugin disabled" do
before { SiteSetting.custom_wizard_enabled = false }
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
it "redirects to root" do
get "/w/super-mega-fun-wizard", xhr: true
2020-11-03 01:24:20 +01:00
expect(response).to redirect_to("/")
end
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
it "returns wizard" do
get "/w/super-mega-fun-wizard.json"
2020-11-03 01:24:20 +01:00
expect(response.parsed_body["id"]).to eq("super_mega_fun_wizard")
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
it "returns missing message if no wizard exists" do
get "/w/super-mega-fun-wizards.json"
2020-11-03 01:24:20 +01:00
expect(response.parsed_body["error"]).to eq("We couldn't find a wizard at that address.")
end
2021-03-11 07:30:15 +01:00
2022-12-24 09:42:09 +01:00
context "with user" do
2024-10-16 13:52:03 +02:00
before { sign_in(user) }
2024-10-16 13:52:03 +02:00
context "when user skips" do
it "skips a wizard if user is allowed to skip" do
put "/w/super-mega-fun-wizard/skip.json"
2022-12-24 09:42:09 +01:00
expect(response.status).to eq(200)
end
2024-10-16 13:52:03 +02:00
it "lets user skip if user cant access wizard" do
2022-12-24 09:42:09 +01:00
enable_subscription("standard")
@template["permitted"] = permitted_json["permitted"]
CustomWizard::Template.save(@template, skip_jobs: true)
2024-10-16 13:52:03 +02:00
put "/w/super-mega-fun-wizard/skip.json"
2022-12-24 09:42:09 +01:00
expect(response.status).to eq(200)
end
2024-10-16 13:52:03 +02:00
it "returns a no skip message if user is not allowed to skip" do
2022-12-24 09:42:09 +01:00
enable_subscription("standard")
2024-10-16 13:52:03 +02:00
@template["required"] = "true"
2022-12-24 09:42:09 +01:00
CustomWizard::Template.save(@template)
2024-10-16 13:52:03 +02:00
put "/w/super-mega-fun-wizard/skip.json"
expect(response.parsed_body["error"]).to eq("Wizard can't be skipped")
2022-12-24 09:42:09 +01:00
end
2024-10-16 13:52:03 +02:00
it "skip response contains a redirect_to if in users submissions" do
2022-12-24 09:42:09 +01:00
@wizard = CustomWizard::Wizard.create(@template["id"], user)
CustomWizard::Submission.new(@wizard, redirect_to: "/t/2").save
2024-10-16 13:52:03 +02:00
put "/w/super-mega-fun-wizard/skip.json"
expect(response.parsed_body["redirect_to"]).to eq("/t/2")
2022-12-24 09:42:09 +01:00
end
2024-10-16 13:52:03 +02:00
it "deletes the users redirect_to_wizard if present" do
user.custom_fields["redirect_to_wizard"] = @template["id"]
2022-12-24 09:42:09 +01:00
user.save_custom_fields(true)
@wizard = CustomWizard::Wizard.create(@template["id"], user)
2024-10-16 13:52:03 +02:00
put "/w/super-mega-fun-wizard/skip.json"
2022-12-24 09:42:09 +01:00
expect(response.status).to eq(200)
expect(user.reload.redirect_to_wizard).to eq(nil)
end
it "deletes the submission if user has filled up some data" do
@wizard = CustomWizard::Wizard.create(@template["id"], user)
CustomWizard::Submission.new(@wizard, step_1_field_1: "Hello World").save
current_submission = @wizard.current_submission
2024-10-16 13:52:03 +02:00
put "/w/super-mega-fun-wizard/skip.json"
2022-12-24 09:42:09 +01:00
submissions = CustomWizard::Submission.list(@wizard).submissions
2024-10-16 13:52:03 +02:00
expect(submissions.any? { |submission| submission.id == current_submission.id }).to eq(
false,
)
2022-12-24 09:42:09 +01:00
end
it "starts from the first step if user visits after skipping the wizard" do
2024-10-16 13:52:03 +02:00
put "/w/super-mega-fun-wizard/steps/step_1.json",
params: {
fields: {
step_1_field_1: "Text input",
},
}
put "/w/super-mega-fun-wizard/skip.json"
get "/w/super-mega-fun-wizard.json"
expect(response.parsed_body["start"]).to eq("step_1")
2022-12-24 09:42:09 +01:00
end
end
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
end