From 1a175b4d2ba8efa80b13169dae155f5c86bae74f Mon Sep 17 00:00:00 2001 From: angus Date: Fri, 19 Feb 2021 17:10:59 +1100 Subject: [PATCH] Ensure prompt completion works as intended - Users who have completed should never see prompt - Add rspec test for this case --- lib/custom_wizard/wizard.rb | 10 ++++---- spec/components/custom_wizard/wizard_spec.rb | 25 +++++++++++++------- spec/fixtures/wizard.json | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/custom_wizard/wizard.rb b/lib/custom_wizard/wizard.rb index cf19dc26..4033d4a1 100644 --- a/lib/custom_wizard/wizard.rb +++ b/lib/custom_wizard/wizard.rb @@ -229,12 +229,14 @@ class CustomWizard::Wizard end end - def self.list(user, template_opts: {}) + def self.list(user, template_opts: {}, not_completed: false) return [] unless user CustomWizard::Template.list(template_opts).reduce([]) do |result, template| wizard = new(template, user) - result.push(wizard) if wizard.can_access? + result.push(wizard) if wizard.can_access? && ( + !not_completed || !wizard.completed? + ) result end end @@ -256,9 +258,9 @@ class CustomWizard::Wizard template_opts: { setting: 'prompt_completion', order: "(value::json ->> 'permitted') IS NOT NULL DESC" - } + }, + not_completed: true ) - if wizards.any? wizards.map do |w| { diff --git a/spec/components/custom_wizard/wizard_spec.rb b/spec/components/custom_wizard/wizard_spec.rb index 069b8369..93ac7972 100644 --- a/spec/components/custom_wizard/wizard_spec.rb +++ b/spec/components/custom_wizard/wizard_spec.rb @@ -29,11 +29,11 @@ describe CustomWizard::Wizard do end end - def progress_step(step_id, acting_user = user) + def progress_step(step_id, acting_user: user, wizard: @wizard) UserHistory.create( action: UserHistory.actions[:custom_wizard_step], acting_user_id: acting_user.id, - context: @wizard.id, + context: wizard.id, subject: step_id ) end @@ -117,9 +117,9 @@ describe CustomWizard::Wizard do end it "lets a permitted user access a complete wizard with multiple submissions" do - progress_step("step_1", trusted_user) - progress_step("step_2", trusted_user) - progress_step("step_3", trusted_user) + progress_step("step_1", acting_user: trusted_user) + progress_step("step_2", acting_user: trusted_user) + progress_step("step_3", acting_user: trusted_user) expect( CustomWizard::Wizard.new(@permitted_template, trusted_user).can_access? @@ -127,9 +127,9 @@ describe CustomWizard::Wizard do end it "does not let an unpermitted user access a complete wizard without multiple submissions" do - progress_step("step_1", trusted_user) - progress_step("step_2", trusted_user) - progress_step("step_3", trusted_user) + progress_step("step_1", acting_user: trusted_user) + progress_step("step_2", acting_user: trusted_user) + progress_step("step_3", acting_user: trusted_user) @permitted_template['multiple_submissions'] = false @@ -189,6 +189,7 @@ describe CustomWizard::Wizard do template_json_3 = template_json.dup template_json_3["id"] = 'super_mega_fun_wizard_3' template_json_3["after_signup"] = true + template_json_3["prompt_completion"] = true CustomWizard::Template.save(template_json_3, skip_jobs: true) end @@ -204,6 +205,14 @@ describe CustomWizard::Wizard do it "lists prompt completion wizards" do expect(CustomWizard::Wizard.prompt_completion(user).length).to eq(2) end + + it "prompt completion does not include wizards user has completed" do + 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 end it "sets wizard redirects if user is permitted" do diff --git a/spec/fixtures/wizard.json b/spec/fixtures/wizard.json index 287f3b60..ffad241c 100644 --- a/spec/fixtures/wizard.json +++ b/spec/fixtures/wizard.json @@ -5,7 +5,7 @@ "save_submissions": true, "multiple_submissions": true, "after_signup": false, - "prompt_completion": true, + "prompt_completion": false, "theme_id": 2, "steps": [ {