diff --git a/lib/custom_wizard/extensions/users_controller.rb b/lib/custom_wizard/extensions/users_controller.rb index 1dd37cc70..e9d25e3d4 100644 --- a/lib/custom_wizard/extensions/users_controller.rb +++ b/lib/custom_wizard/extensions/users_controller.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true module CustomWizardUsersController def account_created - if current_user.present? && (wizard = CustomWizard::Wizard.after_signup(current_user)) - return redirect_to "/w/#{wizard.id.dasherize}" + if wizard_id = current_user&.redirect_to_wizard + return redirect_to "/w/#{wizard_id.dasherize}" end super end diff --git a/lib/custom_wizard/wizard.rb b/lib/custom_wizard/wizard.rb index 6f7882443..b5745f6cf 100644 --- a/lib/custom_wizard/wizard.rb +++ b/lib/custom_wizard/wizard.rb @@ -374,6 +374,22 @@ def self.after_signup(user) wizards.any? ? wizards.first : false end + def self.set_after_signup_redirect(user) + return false if user.blank? || user.anonymous_shadow? + + wizard = after_signup(user) + return false if !wizard || wizard.completed? + + set_user_redirect(wizard.id, user) + true + rescue StandardError => e + Discourse.warn_exception( + e, + message: "Custom Wizard: failed to set after signup redirect for user #{user&.id}", + ) + false + end + def self.prompt_completion(user) wizards = list( diff --git a/plugin.rb b/plugin.rb index 648e86587..09c736194 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.18.19 +# version: 2.18.20 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech @@ -112,16 +112,9 @@ wizard_result = self.new(user).requires_completion? return wizard_result if wizard_result - custom_redirect = false + return false if user.blank? || user.first_seen_at.present? - if user && user.first_seen_at.blank? && wizard = CustomWizard::Wizard.after_signup(user) - if !wizard.completed? - custom_redirect = true - CustomWizard::Wizard.set_user_redirect(wizard.id, user) - end - end - - !!custom_redirect + CustomWizard::Wizard.set_after_signup_redirect(user) end add_to_class(:user, :redirect_to_wizard) { custom_fields["redirect_to_wizard"].presence } @@ -137,11 +130,8 @@ add_to_serializer(:current_user, :redirect_to_wizard) { object.redirect_to_wizard } add_to_serializer(:admin_user_list, :redirect_to_wizard) { object.redirect_to_wizard } - on(:user_approved) do |user| - if wizard = CustomWizard::Wizard.after_signup(user) - CustomWizard::Wizard.set_user_redirect(wizard.id, user) - end - end + on(:user_approved) { |user| CustomWizard::Wizard.set_after_signup_redirect(user) } + on(:user_first_logged_in) { |user| CustomWizard::Wizard.set_after_signup_redirect(user) } add_to_class(:application_controller, :redirect_to_wizard_if_required) do @excluded_routes ||= SiteSetting.wizard_redirect_exclude_paths.split("|") + ["/w/"] diff --git a/spec/extensions/users_controller_spec.rb b/spec/extensions/users_controller_spec.rb index aa972fba8..90100ca43 100644 --- a/spec/extensions/users_controller_spec.rb +++ b/spec/extensions/users_controller_spec.rb @@ -2,14 +2,31 @@ describe CustomWizardUsersController, type: :request do let(:template) { get_wizard_fixture("wizard") } + fab!(:user) - before { @controller = UsersController.new } - - it "redirects a user to wizard after sign up if after signup is enabled" do + before do template["after_signup"] = true CustomWizard::Template.save(template, skip_jobs: true) - sign_in(Fabricate(:user)) + end + + it "redirects a user to wizard after sign up if after signup is enabled" do + sign_in(user) get "/u/account-created" expect(response).to redirect_to("/w/super-mega-fun-wizard") end + + it "does not redirect a user who has completed the wizard" do + template["steps"].each do |step| + CustomWizard::UserHistory.create!( + action: CustomWizard::UserHistory.actions[:step], + actor_id: user.id, + context: template["id"], + subject: step["id"], + ) + end + + sign_in(user) + get "/u/account-created" + expect(response).to redirect_to("/") + end end diff --git a/spec/requests/custom_wizard/after_signup_spec.rb b/spec/requests/custom_wizard/after_signup_spec.rb new file mode 100644 index 000000000..45a9c8452 --- /dev/null +++ b/spec/requests/custom_wizard/after_signup_spec.rb @@ -0,0 +1,105 @@ +# frozen_string_literal: true + +describe "After signup wizard redirect" do + let(:template) { get_wizard_fixture("wizard") } + + before do + template["after_signup"] = true + CustomWizard::Template.save(template, skip_jobs: true) + UsersController.any_instance.stubs(:honeypot_or_challenge_fails?).returns(false) + UsersController.any_instance.stubs(:honeypot_value).returns(nil) + UsersController.any_instance.stubs(:challenge_value).returns(nil) + end + + def sign_up + post "/u.json", + params: { + name: "New User", + username: "newuser", + password: "strongpassword123", + email: "newuser@example.com", + } + expect(response.status).to eq(200) + expect(response.parsed_body["success"]).to eq(true) + User.find_by(username: "newuser") + end + + def sign_up_with_login_code + SiteSetting.enable_local_logins_via_code = true + SiteSetting.must_approve_users = false + login_code = EmailLoginCode.generate!(email: "codeuser@example.com") + + post "/session/login-code/verify.json", + params: { + email: login_code.email, + code: login_code.code, + username: "codeuser", + } + expect(response.status).to eq(200) + expect(response.parsed_body["account_created"]).to eq(true) + User.find_by_email("codeuser@example.com") + end + + it "redirects a newly activated user to the wizard" do + user = sign_up + token = Fabricate(:email_token, user: user) + + put "/u/activate-account/#{token.token}.json" + expect(response.status).to eq(200) + expect(response.parsed_body["redirect_to"]).to end_with("/w/super-mega-fun-wizard") + + get "/" + expect(response).to redirect_to("/w/super-mega-fun-wizard") + end + + it "redirects a user who signs up with an email login code to the wizard" do + user = sign_up_with_login_code + expect(user.reload.redirect_to_wizard).to eq("super_mega_fun_wizard") + + get "/" + expect(response).to redirect_to("/w/super-mega-fun-wizard") + end + + it "does not set a redirect on first login when after signup is disabled" do + template["after_signup"] = false + CustomWizard::Template.save(template, skip_jobs: true) + + expect(sign_up_with_login_code.redirect_to_wizard).to be_nil + end + + it "does not block the first login if setting the redirect fails" do + CustomWizard::Wizard.stubs(:after_signup).raises(StandardError, "broken template") + Discourse.expects(:warn_exception).with(instance_of(StandardError), anything).at_least_once + + expect(sign_up_with_login_code.redirect_to_wizard).to be_nil + end + + describe "on first login" do + fab!(:user) + + it "sets the redirect" do + DiscourseEvent.trigger(:user_first_logged_in, user) + expect(user.reload.redirect_to_wizard).to eq("super_mega_fun_wizard") + end + + it "does not set the redirect if the user has completed the wizard" do + template["steps"].each do |step| + CustomWizard::UserHistory.create!( + action: CustomWizard::UserHistory.actions[:step], + actor_id: user.id, + context: template["id"], + subject: step["id"], + ) + end + + DiscourseEvent.trigger(:user_first_logged_in, user) + expect(user.reload.redirect_to_wizard).to be_nil + end + + it "does not set the redirect for an anonymous shadow user" do + anonymous = Fabricate(:anonymous) + DiscourseEvent.trigger(:user_first_logged_in, anonymous) + expect(anonymous.reload.redirect_to_wizard).to be_nil + end + end +end