Add JSON API support for email address changes (#2807)

This commit is contained in:
Rob Zolkos
2026-04-08 09:09:36 -04:00
committed by GitHub
parent d08aa11bfb
commit 1975a04f14
5 changed files with 186 additions and 8 deletions
@@ -27,4 +27,42 @@ class Users::EmailAddresses::ConfirmationsControllerTest < ActionDispatch::Integ
assert_response :unprocessable_entity
assert_match /Link expired/, response.body
end
test "create as JSON" do
post user_email_address_confirmation_path(user_id: @user.id, email_address_token: @token, script_name: @user.account.slug), as: :json
assert_response :no_content
assert_equal @new_email, @user.reload.identity.email_address
end
test "create as JSON with invalid token" do
post user_email_address_confirmation_path(user_id: @user.id, email_address_token: "invalid", script_name: @user.account.slug), as: :json
assert_response :unprocessable_entity
assert_equal "Token is invalid or has expired", @response.parsed_body["error"]
assert_equal @old_email, @user.reload.identity.email_address
end
test "create as JSON with expired token" do
expired_token = @user.send(:generate_email_address_change_token, to: @new_email, expires_in: 0.seconds)
travel_to 1.minute.from_now do
post user_email_address_confirmation_path(user_id: @user.id, email_address_token: expired_token, script_name: @user.account.slug), as: :json
assert_response :unprocessable_entity
assert_equal "Token is invalid or has expired", @response.parsed_body["error"]
assert_equal @old_email, @user.reload.identity.email_address
end
end
test "create as JSON changes identity" do
assert_equal @old_email, @user.identity.email_address
post user_email_address_confirmation_path(user_id: @user.id, email_address_token: @token, script_name: @user.account.slug), as: :json
assert_response :no_content
@user.reload
assert_equal @new_email, @user.identity.email_address
assert_not_equal @old_email, @user.identity.email_address
end
end
@@ -20,6 +20,22 @@ class Users::EmailAddressesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
test "create with invalid email format" do
assert_no_emails do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: "notanemail" }
end
assert_redirected_to new_user_email_address_path(@user)
assert_equal "Please enter a valid email address", flash[:alert]
end
test "create with same email as current" do
assert_no_emails do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: @user.identity.email_address }
end
assert_redirected_to new_user_email_address_path(@user)
assert_equal "That is already your email address", flash[:alert]
end
test "create with existing email in same account" do
existing_user = users(:kevin)
existing_email = existing_user.identity.email_address
@@ -37,4 +53,61 @@ class Users::EmailAddressesControllerTest < ActionDispatch::IntegrationTest
end
assert_response :not_found
end
test "create as JSON" do
assert_emails 1 do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: "newemail@example.com" }, as: :json
end
assert_response :created
end
test "create as JSON with existing email in same account" do
existing_email = users(:kevin).identity.email_address
assert_no_emails do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: existing_email }, as: :json
end
assert_response :unprocessable_entity
assert_equal "You already have a user in this account with that email address", @response.parsed_body["error"]
end
test "create as JSON with blank email" do
assert_no_emails do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: "" }, as: :json
end
assert_response :bad_request
end
test "create as JSON with same email as current" do
assert_no_emails do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: @user.identity.email_address }, as: :json
end
assert_response :unprocessable_entity
assert_equal "That is already your email address", @response.parsed_body["error"]
end
test "create as JSON with same email different case" do
assert_no_emails do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: @user.identity.email_address.upcase }, as: :json
end
assert_response :unprocessable_entity
assert_equal "That is already your email address", @response.parsed_body["error"]
end
test "create as JSON with invalid email format" do
assert_no_emails do
post user_email_addresses_path(@user, script_name: @user.account.slug), params: { email_address: "notanemail" }, as: :json
end
assert_response :unprocessable_entity
assert_equal "Please enter a valid email address", @response.parsed_body["error"]
end
test "create as JSON for other user" do
other_user = users(:kevin)
assert_no_emails do
post user_email_addresses_path(other_user, script_name: @user.account.slug), params: { email_address: "newemail@example.com" }, as: :json
end
assert_response :not_found
end
end