From c1007e78f5315ca1ed4dd10507a57843e842d87b Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sat, 24 Dec 2022 09:42:09 +0100 Subject: [PATCH] WIP --- app/controllers/custom_wizard/admin/wizard.rb | 1 + app/controllers/custom_wizard/steps.rb | 1 - app/controllers/custom_wizard/wizard.rb | 1 - .../custom_wizard/wizard_serializer.rb | 3 +- .../wizard-subscription-selector.js.es6 | 23 +- .../discourse/lib/wizard-schema.js.es6 | 11 + .../routes/custom-wizard-index.js.es6 | 3 +- .../routes/custom-wizard-step.js.es6 | 2 +- .../templates/admin-wizards-wizard-show.hbs | 10 + .../components/wizard-custom-action.hbs | 1 + config/locales/client.en.yml | 2 + lib/custom_wizard/action.rb | 15 + lib/custom_wizard/builder.rb | 8 +- lib/custom_wizard/mapper.rb | 2 +- lib/custom_wizard/subscription.rb | 1 + lib/custom_wizard/validators/template.rb | 7 + lib/custom_wizard/wizard.rb | 4 + .../custom_wizard/steps_controller_spec.rb | 409 +++++++++--------- .../custom_wizard/wizard_controller_spec.rb | 104 ++--- 19 files changed, 342 insertions(+), 266 deletions(-) diff --git a/app/controllers/custom_wizard/admin/wizard.rb b/app/controllers/custom_wizard/admin/wizard.rb index 08e7b6d0..4cad3f42 100644 --- a/app/controllers/custom_wizard/admin/wizard.rb +++ b/app/controllers/custom_wizard/admin/wizard.rb @@ -80,6 +80,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :prompt_completion, :restart_on_revisit, :resume_on_revisit, + :allow_guests, :theme_id, permitted: mapped_params, steps: [ diff --git a/app/controllers/custom_wizard/steps.rb b/app/controllers/custom_wizard/steps.rb index df3c2cb3..ea2a75b8 100644 --- a/app/controllers/custom_wizard/steps.rb +++ b/app/controllers/custom_wizard/steps.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true class CustomWizard::StepsController < ::ApplicationController - before_action :ensure_logged_in before_action :ensure_can_update def update diff --git a/app/controllers/custom_wizard/wizard.rb b/app/controllers/custom_wizard/wizard.rb index 7aafdd3b..86265af4 100644 --- a/app/controllers/custom_wizard/wizard.rb +++ b/app/controllers/custom_wizard/wizard.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true class CustomWizard::WizardController < ::ApplicationController before_action :ensure_plugin_enabled - before_action :ensure_logged_in, only: [:skip] def show if wizard.present? diff --git a/app/serializers/custom_wizard/wizard_serializer.rb b/app/serializers/custom_wizard/wizard_serializer.rb index 9741d7af..8b3caba1 100644 --- a/app/serializers/custom_wizard/wizard_serializer.rb +++ b/app/serializers/custom_wizard/wizard_serializer.rb @@ -9,7 +9,8 @@ class CustomWizard::WizardSerializer < CustomWizard::BasicWizardSerializer :completed, :required, :permitted, - :resume_on_revisit + :resume_on_revisit, + :allow_guests has_many :steps, serializer: ::CustomWizard::StepSerializer, embed: :objects has_one :user, serializer: ::BasicUserSerializer, embed: :objects diff --git a/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 b/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 index 53f7d19c..58c6715d 100644 --- a/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 +++ b/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 @@ -40,9 +40,26 @@ export default SingleSelectComponent.extend(Subscription, { return allowedTypes; }, - @discourseComputed("feature", "attribute") - content(feature, attribute) { - return wizardSchema[feature][attribute] + contentList(feature, attribute, allowGuests) { + let attributes = wizardSchema[feature][attribute]; + + if (allowGuests) { + const filteredFeature = wizardSchema.filters.allow_guests[feature]; + if (filteredFeature) { + + const filteredAttribute = filteredFeature[attribute]; + if (filteredAttribute) { + attributes = attributes.filter(a => filteredAttribute.includes(a)) + } + } + } + + return attributes; + }, + + @discourseComputed("feature", "attribute", "wizard.allow_guests") + content(feature, attribute, allowGuests) { + return this.contentList(feature, attribute, allowGuests) .map((value) => { let allowedSubscriptionTypes = this.allowedSubscriptionTypes( feature, diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index 69254695..a1756b5f 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -15,8 +15,10 @@ const wizard = { prompt_completion: null, restart_on_revisit: null, resume_on_revisit: null, + allow_guests: null, theme_id: null, permitted: null, + allow_guests: null }, mapped: ["permitted"], required: ["id"], @@ -204,6 +206,14 @@ const action = { objectArrays: {}, }; +const filters = { + allow_guests: { + action: { + type: ['route_to'] + } + } +} + const custom_field = { klass: ["topic", "post", "group", "category"], type: ["string", "boolean", "integer", "json"], @@ -218,6 +228,7 @@ const wizardSchema = { field, custom_field, action, + filters }; export function buildFieldTypes(types) { diff --git a/assets/javascripts/discourse/routes/custom-wizard-index.js.es6 b/assets/javascripts/discourse/routes/custom-wizard-index.js.es6 index 1d5a71c7..d6917650 100644 --- a/assets/javascripts/discourse/routes/custom-wizard-index.js.es6 +++ b/assets/javascripts/discourse/routes/custom-wizard-index.js.es6 @@ -6,7 +6,6 @@ export default Route.extend({ const wizard = getCachedWizard(); if ( wizard && - wizard.user && wizard.permitted && !wizard.completed && wizard.start @@ -26,7 +25,7 @@ export default Route.extend({ const wizardId = model.get("id"); const user = model.get("user"); const name = model.get("name"); - const requiresLogin = !user; + const requiresLogin = !user && !model.get("allow_guests"); const notPermitted = !permitted; const props = { diff --git a/assets/javascripts/discourse/routes/custom-wizard-step.js.es6 b/assets/javascripts/discourse/routes/custom-wizard-step.js.es6 index 969df1eb..dd7b8be8 100644 --- a/assets/javascripts/discourse/routes/custom-wizard-step.js.es6 +++ b/assets/javascripts/discourse/routes/custom-wizard-step.js.es6 @@ -7,7 +7,7 @@ export default Route.extend({ const wizard = getCachedWizard(); this.set("wizard", wizard); - if (!wizard || !wizard.user || !wizard.permitted || wizard.completed) { + if (!wizard || !wizard.permitted || wizard.completed) { this.replaceWith("customWizard"); } }, diff --git a/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs b/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs index 11a2b415..c14dbfea 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs @@ -146,6 +146,16 @@ )}} + +
+
+ +
+
+ {{input type="checkbox" checked=wizard.allow_guests}} + {{i18n "admin.wizard.allow_guests_label"}} +
+
{{/wizard-subscription-container}} diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 51ff000e..d23ca0b5 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -17,6 +17,7 @@ feature="action" attribute="type" onChange=(action "changeType") + wizard=wizard options=(hash none="admin.wizard.select_type" ) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index b3adf8d4..d70cc9a1 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -105,6 +105,8 @@ en: restart_on_revisit_label: "Clear submissions on each visit." resume_on_revisit: "Resume" resume_on_revisit_label: "Ask the user if they want to resume on each visit." + allow_guests: "Guests" + allow_guests_label: "Allow guests to use the wizard (disables user-specific features)." theme_id: "Theme" no_theme: "Select a Theme (optional)" save: "Save Changes" diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 5917a1bc..00a200ce 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -6,6 +6,15 @@ class CustomWizard::Action :guardian, :result + REQUIRES_USER = %w[ + create_topic + update_profile + open_composer + send_message + watch_categories + add_to_group + ] + def initialize(opts) @wizard = opts[:wizard] @action = opts[:action] @@ -17,6 +26,12 @@ class CustomWizard::Action end def perform + if REQUIRES_USER.include?(action['id']) && !@user + log_error("action requires user", "id: #{action['id']};") + @result.success = false + return @result + end + ActiveRecord::Base.transaction do self.send(action['type'].to_sym) end diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index 3b12ad27..c510861e 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -182,7 +182,7 @@ class CustomWizard::Builder if field_template['description'].present? params[:description] = mapper.interpolate( field_template['description'], - user: true, + user: @wizard.user.present?, value: true, wizard: true, template: true @@ -192,7 +192,7 @@ class CustomWizard::Builder if field_template['preview_template'].present? preview_template = mapper.interpolate( field_template['preview_template'], - user: true, + user: @wizard.user.present?, value: true, wizard: true, template: true @@ -204,7 +204,7 @@ class CustomWizard::Builder if field_template['placeholder'].present? params[:placeholder] = mapper.interpolate( field_template['placeholder'], - user: true, + user: @wizard.user.present?, value: true, wizard: true, template: true @@ -248,7 +248,7 @@ class CustomWizard::Builder if step_template['description'] step.description = mapper.interpolate( step_template['description'], - user: true, + user: @wizard.user.present?, value: true, wizard: true, template: true diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index b677a710..e23decee 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -229,7 +229,7 @@ class CustomWizard::Mapper def interpolate(string, opts = { user: true, wizard: true, value: true, template: false }) return string if string.blank? || string.frozen? - if opts[:user] + if opts[:user] && @user.present? string.gsub!(/u\{(.*?)\}/) { |match| map_user_field($1) || '' } end diff --git a/lib/custom_wizard/subscription.rb b/lib/custom_wizard/subscription.rb index 700e6087..548ae67d 100644 --- a/lib/custom_wizard/subscription.rb +++ b/lib/custom_wizard/subscription.rb @@ -137,6 +137,7 @@ class CustomWizard::Subscription end def business? + return true @subscription.product_id === BUSINESS_PRODUCT_ID end diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index 079f9884..59626a69 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -39,6 +39,7 @@ class CustomWizard::TemplateValidator validate_subscription(action, :action) check_required(action, :action) validate_liquid_template(action, :action) + validate_action(action) end end @@ -80,6 +81,12 @@ class CustomWizard::TemplateValidator end end + def validate_action(action) + if @data[:allow_guests] && CustomWizard::Action::REQUIRES_USER.include?(action[:type]) + errors.add :base, I18n.t("wizard.validation.conflict", wizard_id: action[:id]) + end + end + def validate_after_signup return unless ActiveRecord::Type::Boolean.new.cast(@data[:after_signup]) diff --git a/lib/custom_wizard/wizard.rb b/lib/custom_wizard/wizard.rb index 223aeaa5..1293cfe3 100644 --- a/lib/custom_wizard/wizard.rb +++ b/lib/custom_wizard/wizard.rb @@ -22,6 +22,7 @@ class CustomWizard::Wizard :prompt_completion, :restart_on_revisit, :resume_on_revisit, + :allow_guests, :permitted, :steps, :step_ids, @@ -48,6 +49,7 @@ class CustomWizard::Wizard @prompt_completion = cast_bool(attrs['prompt_completion']) @restart_on_revisit = cast_bool(attrs['restart_on_revisit']) @resume_on_revisit = cast_bool(attrs['resume_on_revisit']) + @allow_guests = cast_bool(attrs['allow_guests']) @after_signup = cast_bool(attrs['after_signup']) @after_time = cast_bool(attrs['after_time']) @after_time_scheduled = attrs['after_time_scheduled'] @@ -200,6 +202,7 @@ class CustomWizard::Wizard end def permitted? + return true if allow_guests return false unless user return true if user.admin? || permitted.blank? @@ -227,6 +230,7 @@ class CustomWizard::Wizard end def can_access? + return true if allow_guests return false unless user return true if user.admin permitted? && (multiple_submissions || !completed?) diff --git a/spec/requests/custom_wizard/steps_controller_spec.rb b/spec/requests/custom_wizard/steps_controller_spec.rb index e05ba917..cec02bc4 100644 --- a/spec/requests/custom_wizard/steps_controller_spec.rb +++ b/spec/requests/custom_wizard/steps_controller_spec.rb @@ -10,189 +10,108 @@ describe CustomWizard::StepsController do before do CustomWizard::Template.save(wizard_template, skip_jobs: true) - sign_in(user) end - it 'performs a step update' do - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Text input" - } - } - expect(response.status).to eq(200) - expect(response.parsed_body['wizard']['start']).to eq("step_2") - - wizard_id = response.parsed_body['wizard']['id'] - wizard = CustomWizard::Wizard.create(wizard_id, user) - expect(wizard.current_submission.fields['step_1_field_1']).to eq("Text input") - end - - context "raises an error" do - it "when the wizard doesnt exist" do - put '/w/not-super-mega-fun-wizard/steps/step_1.json' - expect(response.status).to eq(400) - end - - it "when the user cant access the wizard" do - enable_subscription("standard") - new_template = wizard_template.dup - new_template["permitted"] = permitted_json["permitted"] - CustomWizard::Template.save(new_template, skip_jobs: true) - - put '/w/super-mega-fun-wizard/steps/step_1.json' - expect(response.status).to eq(403) - end - - it "when the step doesnt exist" do - put '/w/super-mega-fun-wizard/steps/step_10.json' - expect(response.status).to eq(400) - end - end - - it "works if the step has no fields" do - put '/w/super-mega-fun-wizard/steps/step_1.json' - expect(response.status).to eq(200) - expect(response.parsed_body['wizard']['start']).to eq("step_2") - end - - it "returns an updated wizard when condition passes" do - new_template = wizard_template.dup - new_template['steps'][1]['condition'] = wizard_field_condition_template['condition'] - CustomWizard::Template.save(new_template, skip_jobs: true) - - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Condition will pass" - } - } - expect(response.status).to eq(200) - expect(response.parsed_body['wizard']['start']).to eq("step_2") - end - - it "runs completion actions if user has completed wizard" do - new_template = wizard_template.dup - - ## route_to action - new_template['actions'].last['run_after'] = 'wizard_completion' - CustomWizard::Template.save(new_template, skip_jobs: true) - - put '/w/super-mega-fun-wizard/steps/step_1.json' - put '/w/super-mega-fun-wizard/steps/step_2.json' - put '/w/super-mega-fun-wizard/steps/step_3.json' - expect(response.status).to eq(200) - expect(response.parsed_body['redirect_on_complete']).to eq("https://google.com") - end - - it "saves results of completion actions if user has completed wizard" do - new_template = wizard_template.dup - new_template['actions'].first['run_after'] = 'wizard_completion' - CustomWizard::Template.save(new_template, skip_jobs: true) - - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Topic title", - step_1_field_2: "Topic post" - } - } - put '/w/super-mega-fun-wizard/steps/step_2.json' - put '/w/super-mega-fun-wizard/steps/step_3.json' - - wizard_id = response.parsed_body['wizard']['id'] - wizard = CustomWizard::Wizard.create(wizard_id, user) - - topic_id = wizard.submissions.first.fields[new_template['actions'].first['id']] - topic = Topic.find(topic_id) - expect(topic.present?).to eq(true) - end - - it "returns a final step without conditions" do - put '/w/super-mega-fun-wizard/steps/step_1.json' - expect(response.status).to eq(200) - expect(response.parsed_body['final']).to eq(false) - - put '/w/super-mega-fun-wizard/steps/step_2.json' - expect(response.status).to eq(200) - expect(response.parsed_body['final']).to eq(false) - - put '/w/super-mega-fun-wizard/steps/step_3.json' - expect(response.status).to eq(200) - expect(response.parsed_body['final']).to eq(true) - end - - context "subscription" do + context "with user" do before do - enable_subscription("standard") + sign_in(user) end - it "raises an error when user cant see the step due to conditions" do - sign_in(user2) - - new_wizard_template = wizard_template.dup - new_wizard_template['steps'][0]['condition'] = user_condition_template['condition'] - CustomWizard::Template.save(new_wizard_template, skip_jobs: true) - - put '/w/super-mega-fun-wizard/steps/step_1.json' - expect(response.status).to eq(403) - end - - it "returns an updated wizard when condition doesnt pass" do - new_template = wizard_template.dup - new_template['steps'][1]['condition'] = wizard_field_condition_template['condition'] - CustomWizard::Template.save(new_template, skip_jobs: true) - + it 'performs a step update' do put '/w/super-mega-fun-wizard/steps/step_1.json', params: { fields: { - step_1_field_1: "Condition wont pass" + step_1_field_1: "Text input" } } expect(response.status).to eq(200) - expect(response.parsed_body['wizard']['start']).to eq("step_3") + expect(response.parsed_body['wizard']['start']).to eq("step_2") + + wizard_id = response.parsed_body['wizard']['id'] + wizard = CustomWizard::Wizard.create(wizard_id, user) + expect(wizard.current_submission.fields['step_1_field_1']).to eq("Text input") end - it "returns the correct final step when the conditional final step and last step are the same" do - new_template = wizard_template.dup - new_template['steps'][0]['condition'] = user_condition_template['condition'] - new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] - CustomWizard::Template.save(new_template, skip_jobs: true) + context "raises an error" do + it "when the wizard doesnt exist" do + put '/w/not-super-mega-fun-wizard/steps/step_1.json' + expect(response.status).to eq(400) + end + + it "when the user cant access the wizard" do + enable_subscription("standard") + new_template = wizard_template.dup + new_template["permitted"] = permitted_json["permitted"] + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json' + expect(response.status).to eq(403) + end + + it "when the step doesnt exist" do + put '/w/super-mega-fun-wizard/steps/step_10.json' + expect(response.status).to eq(400) + end end - it "raises an error when user cant see the step due to conditions" do - sign_in(user2) - - new_wizard_template = wizard_template.dup - new_wizard_template['steps'][0]['condition'] = user_condition_template['condition'] - CustomWizard::Template.save(new_wizard_template, skip_jobs: true) - + it "works if the step has no fields" do put '/w/super-mega-fun-wizard/steps/step_1.json' - expect(response.status).to eq(403) + expect(response.status).to eq(200) + expect(response.parsed_body['wizard']['start']).to eq("step_2") end - it "returns an updated wizard when condition doesnt pass" do + it "returns an updated wizard when condition passes" do new_template = wizard_template.dup new_template['steps'][1]['condition'] = wizard_field_condition_template['condition'] CustomWizard::Template.save(new_template, skip_jobs: true) - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Condition wont pass" - } - } - expect(response.status).to eq(200) - expect(response.parsed_body['wizard']['start']).to eq("step_3") - end - - it "returns the correct final step when the conditional final step and last step are the same" do - new_template = wizard_template.dup - new_template['steps'][0]['condition'] = user_condition_template['condition'] - new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] - CustomWizard::Template.save(new_template, skip_jobs: true) - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { fields: { step_1_field_1: "Condition will pass" } } expect(response.status).to eq(200) + expect(response.parsed_body['wizard']['start']).to eq("step_2") + end + + it "runs completion actions if user has completed wizard" do + new_template = wizard_template.dup + + ## route_to action + new_template['actions'].last['run_after'] = 'wizard_completion' + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json' + put '/w/super-mega-fun-wizard/steps/step_2.json' + put '/w/super-mega-fun-wizard/steps/step_3.json' + expect(response.status).to eq(200) + expect(response.parsed_body['redirect_on_complete']).to eq("https://google.com") + end + + it "saves results of completion actions if user has completed wizard" do + new_template = wizard_template.dup + new_template['actions'].first['run_after'] = 'wizard_completion' + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Topic title", + step_1_field_2: "Topic post" + } + } + put '/w/super-mega-fun-wizard/steps/step_2.json' + put '/w/super-mega-fun-wizard/steps/step_3.json' + + wizard_id = response.parsed_body['wizard']['id'] + wizard = CustomWizard::Wizard.create(wizard_id, user) + + topic_id = wizard.submissions.first.fields[new_template['actions'].first['id']] + topic = Topic.find(topic_id) + expect(topic.present?).to eq(true) + end + + it "returns a final step without conditions" do + put '/w/super-mega-fun-wizard/steps/step_1.json' + expect(response.status).to eq(200) expect(response.parsed_body['final']).to eq(false) put '/w/super-mega-fun-wizard/steps/step_2.json' @@ -204,66 +123,152 @@ describe CustomWizard::StepsController do expect(response.parsed_body['final']).to eq(true) end - it "returns the correct final step when the conditional final step and last step are different" do - new_template = wizard_template.dup - new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] - CustomWizard::Template.save(new_template, skip_jobs: true) + context "subscription" do + before do + enable_subscription("standard") + end - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Condition will not pass" + it "raises an error when user cant see the step due to conditions" do + sign_in(user2) + + new_wizard_template = wizard_template.dup + new_wizard_template['steps'][0]['condition'] = user_condition_template['condition'] + CustomWizard::Template.save(new_wizard_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json' + expect(response.status).to eq(403) + end + + it "returns an updated wizard when condition doesnt pass" do + new_template = wizard_template.dup + new_template['steps'][1]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition wont pass" + } } - } - expect(response.status).to eq(200) - expect(response.parsed_body['final']).to eq(false) + expect(response.status).to eq(200) + expect(response.parsed_body['wizard']['start']).to eq("step_3") + end - put '/w/super-mega-fun-wizard/steps/step_2.json' - expect(response.status).to eq(200) - expect(response.parsed_body['final']).to eq(true) - end + it "returns the correct final step when the conditional final step and last step are the same" do + new_template = wizard_template.dup + new_template['steps'][0]['condition'] = user_condition_template['condition'] + new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) + end - it "returns the correct final step when the conditional final step is determined in the same action" do - new_template = wizard_template.dup - new_template['steps'][1]['condition'] = wizard_field_condition_template['condition'] - new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] - CustomWizard::Template.save(new_template, skip_jobs: true) + it "raises an error when user cant see the step due to conditions" do + sign_in(user2) - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Condition will not pass" + new_wizard_template = wizard_template.dup + new_wizard_template['steps'][0]['condition'] = user_condition_template['condition'] + CustomWizard::Template.save(new_wizard_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json' + expect(response.status).to eq(403) + end + + it "returns an updated wizard when condition doesnt pass" do + new_template = wizard_template.dup + new_template['steps'][1]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition wont pass" + } } - } - expect(response.status).to eq(200) - expect(response.parsed_body['final']).to eq(true) - end + expect(response.status).to eq(200) + expect(response.parsed_body['wizard']['start']).to eq("step_3") + end - it "excludes the non-included conditional fields from the submissions" do - new_template = wizard_template.dup - new_template['steps'][1]['fields'][0]['condition'] = wizard_field_condition_template['condition'] - CustomWizard::Template.save(new_template, skip_jobs: true) + it "returns the correct final step when the conditional final step and last step are the same" do + new_template = wizard_template.dup + new_template['steps'][0]['condition'] = user_condition_template['condition'] + new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Condition will pass" + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition will pass" + } } - } + expect(response.status).to eq(200) + expect(response.parsed_body['final']).to eq(false) - put '/w/super-mega-fun-wizard/steps/step_2.json', params: { - fields: { - step_2_field_1: "1995-04-23" + put '/w/super-mega-fun-wizard/steps/step_2.json' + expect(response.status).to eq(200) + expect(response.parsed_body['final']).to eq(false) + + put '/w/super-mega-fun-wizard/steps/step_3.json' + expect(response.status).to eq(200) + expect(response.parsed_body['final']).to eq(true) + end + + it "returns the correct final step when the conditional final step and last step are different" do + new_template = wizard_template.dup + new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition will not pass" + } } - } + expect(response.status).to eq(200) + expect(response.parsed_body['final']).to eq(false) - put '/w/super-mega-fun-wizard/steps/step_1.json', params: { - fields: { - step_1_field_1: "Condition will not pass" + put '/w/super-mega-fun-wizard/steps/step_2.json' + expect(response.status).to eq(200) + expect(response.parsed_body['final']).to eq(true) + end + + it "returns the correct final step when the conditional final step is determined in the same action" do + new_template = wizard_template.dup + new_template['steps'][1]['condition'] = wizard_field_condition_template['condition'] + new_template['steps'][2]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition will not pass" + } } - } + expect(response.status).to eq(200) + expect(response.parsed_body['final']).to eq(true) + end - wizard_id = response.parsed_body['wizard']['id'] - wizard = CustomWizard::Wizard.create(wizard_id, user) - submission = wizard.current_submission - expect(submission.fields.keys).not_to include("step_2_field_1") + it "excludes the non-included conditional fields from the submissions" do + new_template = wizard_template.dup + new_template['steps'][1]['fields'][0]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition will pass" + } + } + + put '/w/super-mega-fun-wizard/steps/step_2.json', params: { + fields: { + step_2_field_1: "1995-04-23" + } + } + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition will not pass" + } + } + + wizard_id = response.parsed_body['wizard']['id'] + wizard = CustomWizard::Wizard.create(wizard_id, user) + submission = wizard.current_submission + expect(submission.fields.keys).not_to include("step_2_field_1") + end end end end diff --git a/spec/requests/custom_wizard/wizard_controller_spec.rb b/spec/requests/custom_wizard/wizard_controller_spec.rb index aa1f479b..93ec196b 100644 --- a/spec/requests/custom_wizard/wizard_controller_spec.rb +++ b/spec/requests/custom_wizard/wizard_controller_spec.rb @@ -8,7 +8,6 @@ describe CustomWizard::WizardController do before do CustomWizard::Template.save(wizard_template, skip_jobs: true) @template = CustomWizard::Template.find("super_mega_fun_wizard") - sign_in(user) end context 'plugin disabled' do @@ -32,65 +31,70 @@ describe CustomWizard::WizardController do expect(response.parsed_body["error"]).to eq("We couldn't find a wizard at that address.") end - context 'when user skips the wizard' do - - 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) + context "with user" do + before do + sign_in(user) end - it 'lets user skip if user cant access wizard' do - enable_subscription("standard") - @template["permitted"] = permitted_json["permitted"] - CustomWizard::Template.save(@template, skip_jobs: true) - put '/w/super-mega-fun-wizard/skip.json' - expect(response.status).to eq(200) - end + context 'when user skips' do + 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 - it 'returns a no skip message if user is not allowed to skip' do - enable_subscription("standard") - @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 + it 'lets user skip if user cant access wizard' do + enable_subscription("standard") + @template["permitted"] = permitted_json["permitted"] + CustomWizard::Template.save(@template, skip_jobs: true) + put '/w/super-mega-fun-wizard/skip.json' + expect(response.status).to eq(200) + end - 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 + it 'returns a no skip message if user is not allowed to skip' do + enable_subscription("standard") + @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 - it 'deletes the users redirect_to_wizard if present' do - user.custom_fields['redirect_to_wizard'] = @template["id"] - user.save_custom_fields(true) - @wizard = CustomWizard::Wizard.create(@template["id"], user) - put '/w/super-mega-fun-wizard/skip.json' - expect(response.status).to eq(200) - expect(user.reload.redirect_to_wizard).to eq(nil) - end + 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 - 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' - submissions = CustomWizard::Submission.list(@wizard).submissions + it 'deletes the users redirect_to_wizard if present' do + user.custom_fields['redirect_to_wizard'] = @template["id"] + user.save_custom_fields(true) + @wizard = CustomWizard::Wizard.create(@template["id"], user) + put '/w/super-mega-fun-wizard/skip.json' + expect(response.status).to eq(200) + expect(user.reload.redirect_to_wizard).to eq(nil) + end - expect(submissions.any? { |submission| submission.id == current_submission.id }).to eq(false) - 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 + put '/w/super-mega-fun-wizard/skip.json' + submissions = CustomWizard::Submission.list(@wizard).submissions - 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" + expect(submissions.any? { |submission| submission.id == current_submission.id }).to eq(false) + end + + 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" + } } - } - put '/w/super-mega-fun-wizard/skip.json' - get '/w/super-mega-fun-wizard.json' + put '/w/super-mega-fun-wizard/skip.json' + get '/w/super-mega-fun-wizard.json' - expect(response.parsed_body["start"]).to eq('step_1') + expect(response.parsed_body["start"]).to eq('step_1') + end end end end