Files
fizzy/test/controllers/users_controller_test.rb
T
Jeremy Daer 49c4f2adc6 Fix ActiveStorage FileNotFoundError with immediate variant processing (#2022)
When ActiveStorage::Record uses `connects_to` for read replica support,
it creates a separate connection pool from ApplicationRecord. This causes
`after_commit` callbacks to fire in non-deterministic order - the
Attachment's `create_variants` callback can fire before the User model's
upload callback completes, resulting in FileNotFoundError.

The fix removes replica connection configuration from ActiveStorage::Record
so it shares the same connection pool as application models, ensuring
proper callback ordering.

Also reverts test workarounds that were added to work around this issue,
since the root cause is now fixed.

See: https://github.com/rails/rails/issues/53694
2025-12-08 14:44:49 -08:00

90 lines
2.3 KiB
Ruby

require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
test "show" do
sign_in_as :kevin
get user_path(users(:david))
assert_in_body users(:david).name
end
test "update oneself" do
sign_in_as :kevin
get edit_user_path(users(:kevin))
assert_response :ok
put user_path(users(:kevin)), params: { user: { name: "New Kevin" } }
assert_redirected_to user_path(users(:kevin))
assert_equal "New Kevin", users(:kevin).reload.name
end
test "update other as admin" do
sign_in_as :kevin
get edit_user_path(users(:david))
assert_response :ok
put user_path(users(:david)), params: { user: { name: "New David" } }
assert_redirected_to user_path(users(:david))
assert_equal "New David", users(:david).reload.name
end
test "destroy" do
sign_in_as :kevin
assert_difference -> { User.active.count }, -1 do
delete user_path(users(:david))
end
assert_redirected_to users_path
assert_nil User.active.find_by(id: users(:david).id)
end
test "admin cannot deactivate the owner" do
sign_in_as :kevin
assert users(:jason).owner?
assert users(:jason).active
assert_no_difference -> { User.active.count } do
delete user_path(users(:jason))
end
assert_response :forbidden
assert users(:jason).reload.active
end
test "non-admins cannot perform actions" do
sign_in_as :jz
put user_path(users(:david)), params: { user: { role: "admin" } }
assert_response :forbidden
delete user_path(users(:david))
assert_response :forbidden
end
test "update with invalid avatar shows validation error" do
sign_in_as :kevin
svg_file = fixture_file_upload("avatar.svg", "image/svg+xml")
put user_path(users(:kevin)), params: { user: { avatar: svg_file } }
assert_response :unprocessable_entity
assert_select "form[action='#{user_path(users(:kevin))}']"
assert_select ".txt-negative", text: /must be a JPEG, PNG, GIF, or WebP image/
end
test "update with valid avatar" do
sign_in_as :kevin
png_file = fixture_file_upload("avatar.png", "image/png")
put user_path(users(:kevin)), params: { user: { avatar: png_file } }
assert_redirected_to user_path(users(:kevin))
assert users(:kevin).reload.avatar.attached?
assert_equal "image/png", users(:kevin).avatar.content_type
end
end