0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 10:40:28 +01:00
discourse-custom-wizard/spec/components/custom_wizard/builder_spec.rb

387 Zeilen
11 KiB
Ruby

# frozen_string_literal: true
require_relative '../../plugin_helper'
2019-12-04 12:13:51 +01:00
describe CustomWizard::Builder do
2020-10-31 08:05:50 +01:00
fab!(:trusted_user) {
Fabricate(
:user,
username: 'angus',
email: "angus@email.com",
trust_level: TrustLevel[3]
2021-03-11 07:30:15 +01:00
)
2020-10-31 08:05:50 +01:00
}
fab!(:user) { Fabricate(:user) }
2019-12-12 00:20:10 +01:00
fab!(:category1) { Fabricate(:category, name: 'cat1') }
fab!(:category2) { Fabricate(:category, name: 'cat2') }
2019-12-09 06:51:42 +01:00
fab!(:group) { Fabricate(:group) }
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
let(:required_data_json) {
JSON.parse(
File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/step/required_data.json"
).read
)
2020-10-31 08:05:50 +01:00
}
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
let(:permitted_json) {
JSON.parse(
File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard/permitted.json"
).read
)
2020-10-31 08:05:50 +01:00
}
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
let(:permitted_param_json) {
JSON.parse(
File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/step/permitted_params.json"
).read
)
}
let(:user_condition_json) {
JSON.parse(
File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/condition/user_condition.json"
).read
)
2020-10-31 08:05:50 +01:00
}
2021-03-11 07:30:15 +01:00
2021-10-20 15:19:21 +02:00
let(:boolean_field_condition_json) {
JSON.parse(
File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/condition/boolean_field_condition.json"
).read
)
}
2020-10-31 08:05:50 +01:00
before do
Group.refresh_automatic_group!(:trust_level_3)
2020-11-03 01:24:20 +01:00
CustomWizard::Template.save(
2020-10-31 08:05:50 +01:00
JSON.parse(File.open(
2020-11-03 01:24:20 +01:00
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
).read),
skip_jobs: true)
2020-10-31 08:05:50 +01: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
2019-12-09 06:51:42 +01:00
context 'disabled' do
before do
SiteSetting.custom_wizard_enabled = false
end
2021-03-11 07:30:15 +01:00
2020-10-20 01:15:03 +02:00
it "returns nil" do
2020-10-31 08:05:50 +01: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
context 'enabled' do
before do
SiteSetting.custom_wizard_enabled = true
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
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
2020-10-20 01:15:03 +02:00
expect(
2020-10-31 08:05:50 +01:00
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
2020-10-31 08:05:50 +01: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
2020-10-31 08:05:50 +01:00
it 'returns no steps if user has completed it' do
@template[:steps].each do |step|
UserHistory.create!(
{
action: UserHistory.actions[:custom_wizard_step],
acting_user_id: user.id,
context: @template[:id]
}.merge(
subject: step[:id]
)
)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
expect(
CustomWizard::Builder.new(@template[:id], user).build
.steps.length
).to eq(0)
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
@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
2020-10-31 08:05:50 +01: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
2020-10-31 08:05:50 +01:00
it 'user cannot access if not permitted' do
2020-10-20 01:15:03 +02:00
expect(
2020-10-31 08:05:50 +01:00
CustomWizard::Builder.new(@template[:id], user).build
.can_access?
2020-10-20 01:15:03 +02:00
).to eq(false)
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
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")
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'returns no steps if user is not permitted' do
expect(
CustomWizard::Builder.new(@template[:id], user).build
.steps.length
).to eq(0)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'is permitted if user is in permitted group' do
expect(
CustomWizard::Builder.new(@template[:id], trusted_user).build
.permitted?
).to eq(true)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'user can access if permitted' do
expect(
CustomWizard::Builder.new(@template[:id], trusted_user).build
.can_access?
).to eq(true)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'returns steps if user is permitted' do
expect(
CustomWizard::Builder.new(@template[:id], trusted_user).build
.steps.length
).to eq(3)
end
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'returns prefilled data' do
expect(
CustomWizard::Builder.new(@template[:id], user).build
.steps.first
.fields.first
.value
).to eq('I am prefilled')
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)
data = {
step_1_field_1: 'I am a user submission'
}
CustomWizard::Submission.new(wizard, data).save
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
it 'returns saved submissions' do
2020-10-20 01:15:03 +02:00
expect(
2020-10-31 08:05:50 +01:00
CustomWizard::Builder.new(@template[:id], user).build
.steps.first
.fields.first
.value
).to eq('I am a user submission')
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
context "restart is enabled" do
before do
@template[:restart_on_revisit] = true
CustomWizard::Template.save(@template.as_json)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'does not return saved submissions' do
expect(
CustomWizard::Builder.new(@template[:id], user).build
.steps.first
.fields.first
.value
).to eq('I am prefilled')
end
end
end
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
2020-10-31 08:05:50 +01:00
context 'with required data' do
before do
@template[:steps][0][:required_data] = required_data_json['required_data']
@template[:steps][0][:required_data_message] = required_data_json['required_data_message']
CustomWizard::Template.save(@template.as_json)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01: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)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'it shows required data message' do
expect(
CustomWizard::Builder.new(@template[:id], user).build
.steps.first
.permitted_message
).to eq("Missing required data")
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'is permitted if required data is present' do
wizard = CustomWizard::Wizard.create('super_mega_fun_wizard', user)
CustomWizard::Submission.new(wizard, step_1_field_1: "required").save
2020-10-31 08:05:50 +01:00
expect(
CustomWizard::Builder.new(@template[:id], user).build
.steps.first
.permitted
).to eq(true)
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
@template[:steps][0][:permitted_params] = permitted_param_json['permitted_params']
CustomWizard::Template.save(@template.as_json)
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
it 'saves permitted params' do
2020-11-03 01:24:20 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build({},
2020-10-31 08:05:50 +01:00
param: 'param_value'
2021-03-11 07:30:15 +01:00
)
expect(wizard.current_submission.fields['saved_param']).to eq('param_value')
2020-10-31 08:05:50 +01:00
end
end
context "with condition" do
before do
@template[:steps][0][:condition] = user_condition_json['condition']
CustomWizard::Template.save(@template.as_json)
end
it "adds step when condition is passed" do
wizard = CustomWizard::Builder.new(@template[:id], trusted_user).build
expect(wizard.steps.first.id).to eq(@template[:steps][0]['id'])
end
it "does not add step when condition is not passed" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
expect(wizard.steps.first.id).to eq(@template[:steps][1]['id'])
end
end
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 'building field' do
2019-12-09 02:43:30 +01:00
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
2020-10-31 08:05:50 +01:00
it 'returns all step fields' do
expect(
CustomWizard::Builder.new(@template[:id], user)
.build
.steps.first
.fields.length
).to eq(4)
2019-12-04 12:13:51 +01:00
end
context "with condition" do
before do
@template[:steps][0][:fields][0][:condition] = user_condition_json['condition']
2021-10-20 15:19:21 +02:00
@template[:steps][2][:fields][5][:condition] = boolean_field_condition_json['condition']
CustomWizard::Template.save(@template.as_json)
end
it "adds field when condition is passed" do
wizard = CustomWizard::Builder.new(@template[:id], trusted_user).build
expect(wizard.steps.first.fields.first.id).to eq(@template[:steps][0][:fields][0]['id'])
end
it "does not add field when condition is not passed" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
expect(wizard.steps.first.fields.first.id).to eq(@template[:steps][0][:fields][1]['id'])
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
wizard.create_updater('step_2', step_2_field_5: 'true').update
builder = CustomWizard::Builder.new(@template[:id], user)
wizard = builder.build
expect(wizard.steps.last.fields.last.id).to eq(@template[:steps][2][:fields][5]['id'])
end
end
end
2021-03-11 07:30:15 +01:00
context 'on update' do
2020-10-31 08:05:50 +01:00
def perform_update(step_id, submission)
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
2019-12-09 06:51:42 +01:00
it 'saves submissions' do
@wizard = CustomWizard::Builder.new(@template[:id], user).build
2020-10-31 08:05:50 +01: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
2020-10-31 08:05:50 +01:00
context 'save submissions disabled' do
before do
@template[:save_submissions] = false
CustomWizard::Template.save(@template.as_json)
@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
perform_update('step_1', step_1_field_1: 'Text input')
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