diff --git a/app/controllers/sessions/magic_links_controller.rb b/app/controllers/sessions/magic_links_controller.rb index 32257d9415..d2b25594c5 100644 --- a/app/controllers/sessions/magic_links_controller.rb +++ b/app/controllers/sessions/magic_links_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/controllers/signups/completions_controller.rb b/app/controllers/signups/completions_controller.rb index 4fcaaabd36..2d8c3693c1 100644 --- a/app/controllers/signups/completions_controller.rb +++ b/app/controllers/signups/completions_controller.rb @@ -11,10 +11,9 @@ 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 @@ -22,4 +21,22 @@ def create 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 diff --git a/test/controllers/sessions/magic_links_controller_test.rb b/test/controllers/sessions/magic_links_controller_test.rb index 83b228416e..edcb22d3df 100644 --- a/test/controllers/sessions/magic_links_controller_test.rb +++ b/test/controllers/sessions/magic_links_controller_test.rb @@ -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 @@ -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 diff --git a/test/controllers/signup/completions_controller_test.rb b/test/controllers/signup/completions_controller_test.rb index 126ae6acfb..ccba9e8d4f 100644 --- a/test/controllers/signup/completions_controller_test.rb +++ b/test/controllers/signup/completions_controller_test.rb @@ -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