Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/custom_wizard/extensions/users_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/custom_wizard/wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 5 additions & 15 deletions plugin.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -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/"]
Expand Down
25 changes: 21 additions & 4 deletions spec/extensions/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
105 changes: 105 additions & 0 deletions spec/requests/custom_wizard/after_signup_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading