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
|
|
|
|
|
|
|
describe CustomWizard::WizardController do
|
|
|
|
fab!(:user) {
|
|
|
|
Fabricate(
|
|
|
|
:user,
|
|
|
|
username: 'angus',
|
|
|
|
email: "angus@email.com",
|
|
|
|
trust_level: TrustLevel[3]
|
2021-03-11 07:30:15 +01:00
|
|
|
)
|
2020-11-03 01:24:20 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 06:24:07 +02:00
|
|
|
let(:permitted_json) {
|
|
|
|
JSON.parse(
|
|
|
|
File.open(
|
|
|
|
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard/permitted.json"
|
|
|
|
).read
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
before do
|
|
|
|
CustomWizard::Template.save(
|
|
|
|
JSON.parse(File.open(
|
|
|
|
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
|
|
|
|
).read),
|
|
|
|
skip_jobs: true)
|
|
|
|
@template = CustomWizard::Template.find("super_mega_fun_wizard")
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
context 'plugin disabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.custom_wizard_enabled = false
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it 'redirects to root' do
|
|
|
|
get '/w/super-mega-fun-wizard', xhr: true
|
|
|
|
expect(response).to redirect_to("/")
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it 'returns wizard' do
|
|
|
|
get '/w/super-mega-fun-wizard.json'
|
|
|
|
expect(response.parsed_body["id"]).to eq("super_mega_fun_wizard")
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
it 'returns missing message if no wizard exists' do
|
|
|
|
get '/w/super-mega-fun-wizards.json'
|
|
|
|
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
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
context 'when user skips the wizard' do
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
it 'skips a wizard if user is allowed to skip' do
|
|
|
|
put '/w/super-mega-fun-wizard/skip.json'
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2021-06-16 06:24:07 +02:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
it 'lets user skip if user cant access wizard' do
|
|
|
|
@template["permitted"] = permitted_json["permitted"]
|
|
|
|
CustomWizard::Template.save(@template, skip_jobs: true)
|
2021-06-16 06:24:07 +02:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
put '/w/super-mega-fun-wizard/skip.json'
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
it 'returns a no skip message if user is not allowed to skip' do
|
|
|
|
@template['required'] = 'true'
|
|
|
|
CustomWizard::Template.save(@template)
|
|
|
|
put '/w/super-mega-fun-wizard/skip.json'
|
|
|
|
expect(response.parsed_body['error']).to eq("Wizard can't be skipped")
|
|
|
|
end
|
2021-08-12 13:41:58 +02:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
it 'skip response contains a redirect_to if in users submissions' do
|
|
|
|
@wizard = CustomWizard::Wizard.create(@template["id"], user)
|
|
|
|
CustomWizard::Submission.new(@wizard, redirect_to: "/t/2").save
|
|
|
|
put '/w/super-mega-fun-wizard/skip.json'
|
|
|
|
expect(response.parsed_body['redirect_to']).to eq('/t/2')
|
|
|
|
end
|
2021-08-12 13:41:58 +02:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
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
|
|
|
|
put '/w/super-mega-fun-wizard/skip.json'
|
|
|
|
list = CustomWizard::Submission.list(@wizard)
|
|
|
|
|
|
|
|
expect(list.any? { |submission| submission.id == current_submission.id }).to eq(false)
|
|
|
|
end
|
2021-08-13 14:19:31 +02:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
it "starts from the first step if user visits after skipping the wizard" do
|
|
|
|
put '/w/super-mega-fun-wizard/steps/step_1.json', params: {
|
|
|
|
fields: {
|
|
|
|
step_1_field_1: "Text input"
|
|
|
|
}
|
2021-08-13 14:19:31 +02:00
|
|
|
}
|
2021-08-13 15:28:33 +02:00
|
|
|
put '/w/super-mega-fun-wizard/skip.json'
|
|
|
|
get '/w/super-mega-fun-wizard.json'
|
2021-08-13 14:19:31 +02:00
|
|
|
|
2021-08-13 15:28:33 +02:00
|
|
|
expect(response.parsed_body["start"]).to eq('step_1')
|
|
|
|
end
|
2021-08-13 14:19:31 +02:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
end
|