Skip to content
Open
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
8 changes: 6 additions & 2 deletions app/controllers/sessions/magic_links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def sign_in(magic_link)

respond_to do |format|
format.html { redirect_to after_sign_in_url(magic_link) }
format.json { render json: { session_token: session_token } }
format.json { render json: { session_token: session_token, requires_signup_completion: requires_signup_completion?(magic_link) } }
end
end

Expand All @@ -68,7 +68,7 @@ def invalid_code
end

def after_sign_in_url(magic_link)
if magic_link.for_sign_up?
if requires_signup_completion?(magic_link)
new_signup_completion_path
else
after_authentication_url
Expand All @@ -82,4 +82,8 @@ def rate_limit_exceeded
format.json { render json: { message: rate_limit_exceeded_message }, status: :too_many_requests }
end
end

def requires_signup_completion?(magic_link)
magic_link.for_sign_up?
end
end
23 changes: 20 additions & 3 deletions app/controllers/signups/completions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,32 @@ def create
@signup = Signup.new(signup_params)

if @signup.complete
flash[:welcome_letter] = true
redirect_to landing_url(script_name: @signup.account.slug)
welcome_to_account
else
render :new, status: :unprocessable_entity
invalid_signup
end
end

private
def signup_params
params.expect(signup: %i[ full_name ]).with_defaults(identity: Current.identity)
end

def welcome_to_account
respond_to do |format|
format.html do
flash[:welcome_letter] = true
redirect_to landing_url(script_name: @signup.account.slug)
end

format.json { render json: { account_id: @signup.account.external_account_id }, status: :created }
end
end

def invalid_signup
respond_to do |format|
format.html { render :new, status: :unprocessable_entity }
format.json { render json: { errors: @signup.errors.full_messages }, status: :unprocessable_entity }
end
end
end
16 changes: 15 additions & 1 deletion test/controllers/sessions/magic_links_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Sessions::MagicLinksControllerTest < ActionDispatch::IntegrationTest
assert MagicLink.exists?(expired_link.id), "Expired magic link should not be consumed"
end

test "create via JSON" do
test "create via JSON for sign in" do
identity = identities(:david)
magic_link = identity.send_magic_link

Expand All @@ -89,6 +89,20 @@ class Sessions::MagicLinksControllerTest < ActionDispatch::IntegrationTest
post session_magic_link_path(format: :json), params: { code: magic_link.code }
assert_response :success
assert @response.parsed_body["session_token"].present?
assert_equal false, @response.parsed_body["requires_signup_completion"]
end
end

test "create via JSON for sign up" do
identity = identities(:david)
magic_link = identity.send_magic_link(for: :sign_up)

untenanted do
post session_path(format: :json), params: { email_address: identity.email_address }
post session_magic_link_path(format: :json), params: { code: magic_link.code }
assert_response :success
assert @response.parsed_body["session_token"].present?
assert_equal true, @response.parsed_body["requires_signup_completion"]
end
end

Expand Down
26 changes: 26 additions & 0 deletions test/controllers/signup/completions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,30 @@ class Signup::CompletionsControllerTest < ActionDispatch::IntegrationTest
assert_select "li", text: "Full name can't be blank"
end
end

test "create via JSON" do
untenanted do
post signup_completion_path(format: :json), params: {
signup: {
full_name: @signup.full_name
}
}
end

assert_response :created
assert @response.parsed_body["account_id"].present?
end

test "create via JSON with blank name" do
untenanted do
post signup_completion_path(format: :json), params: {
signup: {
full_name: ""
}
}
end

assert_response :unprocessable_entity
assert_includes @response.parsed_body["errors"], "Full name can't be blank"
end
end