Skip to content

Commit

Permalink
Fix tests with Rails main
Browse files Browse the repository at this point in the history
Rails main / 7.1.0.alpha introduced a change to improve typography by
default, by converting all apostrophes to be single quotation marks.
rails/rails#45463

The change caused all our text based matching to fail, this updates the
tests to ensure compatibility.

Model tests were changed to test against the error type & information
rather than the translated string, which I think is an improvement
overall that should make them a little less brittle. I thought of using
[of_kind?] but that isn't available on all Rails versions we currently
support, while `added?` is. The drawback is that `added?` require full
details like the `:confirmation` example which requires the related
attribute that is being confirmed, but that's a small price to pay.

Integration tests were changed to match on a regexp that accepts both
quotes. I could've used a simple `.` to match anything there, but
thought I'd just keep it specific for clarity on what it is really
expected to match there. Plus, since it's integration testing against a
rendered response body, it's better to match the actual text rather than
resort on other ways. (like using I18n directly, etc.)

[of_kind?] https://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-of_kind-3F
  • Loading branch information
carlosantoniodasilva committed Mar 17, 2023
1 parent afec665 commit 232c855
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions test/integration/confirmable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ def resend_confirmation
test "should not be able to confirm an email with a blank confirmation token" do
visit_user_confirmation_with_token("")

assert_contain "Confirmation token can't be blank"
assert_contain %r{Confirmation token can['’]t be blank}
end

test "should not be able to confirm an email with a nil confirmation token" do
visit_user_confirmation_with_token(nil)

assert_contain "Confirmation token can't be blank"
assert_contain %r{Confirmation token can['’]t be blank}
end

test "should not be able to confirm user with blank confirmation token" do
Expand All @@ -193,7 +193,7 @@ def resend_confirmation

visit_user_confirmation_with_token("")

assert_contain "Confirmation token can't be blank"
assert_contain %r{Confirmation token can['’]t be blank}
end

test "should not be able to confirm user with nil confirmation token" do
Expand All @@ -202,7 +202,7 @@ def resend_confirmation

visit_user_confirmation_with_token(nil)

assert_contain "Confirmation token can't be blank"
assert_contain %r{Confirmation token can['’]t be blank}
end

test 'error message is configurable by resource name' do
Expand Down
2 changes: 1 addition & 1 deletion test/integration/recoverable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def reset_password(options = {}, &block)
assert_response :success
assert_current_url '/users/password'
assert_have_selector '#error_explanation'
assert_contain "Password confirmation doesn't match Password"
assert_contain %r{Password confirmation doesn['’]t match Password}
assert_not user.reload.valid_password?('987654321')
end

Expand Down
4 changes: 2 additions & 2 deletions test/integration/registerable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def user_sign_up
assert_template 'registrations/new'
assert_have_selector '#error_explanation'
assert_contain "Email is invalid"
assert_contain "Password confirmation doesn't match Password"
assert_contain %r{Password confirmation doesn['’]t match Password}
assert_contain "2 errors prohibited"
assert_nil User.to_adapter.find_first

Expand Down Expand Up @@ -251,7 +251,7 @@ def user_sign_up
fill_in 'current password', with: '12345678'
click_button 'Update'

assert_contain "Password confirmation doesn't match Password"
assert_contain %r{Password confirmation doesn['’]t match Password}
assert_not User.to_adapter.find_first.valid_password?('pas123')
end

Expand Down
4 changes: 2 additions & 2 deletions test/models/authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class AuthenticatableTest < ActiveSupport::TestCase

test 'find_or_initialize_with_errors adds blank error' do
user_with_error = User.find_or_initialize_with_errors([:email], { email: "" })
assert_equal ["Email can't be blank"], user_with_error.errors.full_messages_for(:email)
assert user_with_error.errors.added?(:email, :blank)
end

test 'find_or_initialize_with_errors adds invalid error' do
user_with_error = User.find_or_initialize_with_errors([:email], { email: "[email protected]" })
assert_equal ["Email is invalid"], user_with_error.errors.full_messages_for(:email)
assert user_with_error.errors.added?(:email, :invalid)
end

if defined?(ActionController::Parameters)
Expand Down
8 changes: 4 additions & 4 deletions test/models/confirmable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def setup
test 'should return a new record with errors when a blank token is given' do
confirmed_user = User.confirm_by_token('')
assert_not confirmed_user.persisted?
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
assert confirmed_user.errors.added?(:confirmation_token, :blank)
end

test 'should return a new record with errors when a blank token is given and a record exists on the database' do
Expand All @@ -83,7 +83,7 @@ def setup
confirmed_user = User.confirm_by_token('')

assert_not user.reload.confirmed?
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
assert confirmed_user.errors.added?(:confirmation_token, :blank)
end

test 'should return a new record with errors when a nil token is given and a record exists on the database' do
Expand All @@ -92,7 +92,7 @@ def setup
confirmed_user = User.confirm_by_token(nil)

assert_not user.reload.confirmed?
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
assert confirmed_user.errors.added?(:confirmation_token, :blank)
end

test 'should generate errors for a user email if user is already confirmed' do
Expand Down Expand Up @@ -314,7 +314,7 @@ def setup
user = create_user
confirm_user = User.send_confirmation_instructions(email: user.email)
assert_not confirm_user.persisted?
assert_equal "can't be blank", confirm_user.errors[:username].join
assert confirm_user.errors.added?(:username, :blank)
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/models/database_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def setup
assert_not user.update_with_password(password: 'pass4321',
password_confirmation: 'pass4321')
assert user.reload.valid_password?('12345678')
assert_match "can't be blank", user.errors[:current_password].join
assert user.errors.added?(:current_password, :blank)
end

test 'should run validations even when current password is invalid or blank' do
Expand All @@ -181,7 +181,7 @@ def setup
assert user.persisted?
assert_not user.update_with_password(username: "")
assert_match "usertest", user.reload.username
assert_match "can't be blank", user.errors[:username].join
assert user.errors.added?(:username, :blank)
end

test 'should ignore password and its confirmation if they are blank' do
Expand Down Expand Up @@ -235,7 +235,7 @@ def setup
user = create_user
assert_not user.destroy_with_password(nil)
assert user.persisted?
assert_match "can't be blank", user.errors[:current_password].join
assert user.errors.added?(:current_password, :blank)
end

test 'should not email on password change' do
Expand Down
4 changes: 2 additions & 2 deletions test/models/lockable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def setup
test 'should return a new record with errors when a blank token is given' do
locked_user = User.unlock_access_by_token('')
assert_not locked_user.persisted?
assert_equal "can't be blank", locked_user.errors[:unlock_token].join
assert locked_user.errors.added?(:unlock_token, :blank)
end

test 'should find a user to send unlock instructions' do
Expand Down Expand Up @@ -246,7 +246,7 @@ def setup
user = create_user
unlock_user = User.send_unlock_instructions(email: user.email)
assert_not unlock_user.persisted?
assert_equal "can't be blank", unlock_user.errors[:username].join
assert unlock_user.errors.added?(:username, :blank)
end
end

Expand Down
18 changes: 9 additions & 9 deletions test/models/recoverable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ def setup
end

test 'should require all reset_password_keys' do
swap Devise, reset_password_keys: [:username, :email] do
user = create_user
reset_password_user = User.send_reset_password_instructions(email: user.email)
assert_not reset_password_user.persisted?
assert_equal "can't be blank", reset_password_user.errors[:username].join
end
swap Devise, reset_password_keys: [:username, :email] do
user = create_user
reset_password_user = User.send_reset_password_instructions(email: user.email)
assert_not reset_password_user.persisted?
assert reset_password_user.errors.added?(:username, :blank)
end
end

test 'should reset reset_password_token before send the reset instructions email' do
Expand Down Expand Up @@ -173,7 +173,7 @@ def setup
test 'should return a new record with errors if reset_password_token is blank' do
reset_password_user = User.reset_password_by_token(reset_password_token: '')
assert_not reset_password_user.persisted?
assert_match "can't be blank", reset_password_user.errors[:reset_password_token].join
assert reset_password_user.errors.added?(:reset_password_token, :blank)
end

test 'should return a new record with errors if password is blank' do
Expand All @@ -182,7 +182,7 @@ def setup

reset_password_user = User.reset_password_by_token(reset_password_token: raw, password: '')
assert_not reset_password_user.errors.empty?
assert_match "can't be blank", reset_password_user.errors[:password].join
assert reset_password_user.errors.added?(:password, :blank)
assert_equal raw, reset_password_user.reset_password_token
end

Expand All @@ -192,7 +192,7 @@ def setup

reset_password_user = User.reset_password_by_token(reset_password_token: raw)
assert_not reset_password_user.errors.empty?
assert_match "can't be blank", reset_password_user.errors[:password].join
assert reset_password_user.errors.added?(:password, :blank)
assert_equal raw, reset_password_user.reset_password_token
end

Expand Down
10 changes: 5 additions & 5 deletions test/models/validatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ValidatableTest < ActiveSupport::TestCase
user = new_user(email: nil)
assert user.invalid?
assert user.errors[:email]
assert_equal 'can\'t be blank', user.errors[:email].join
assert user.errors.added?(:email, :blank)
end

test 'should require uniqueness of email if email has changed, allowing blank' do
Expand Down Expand Up @@ -52,14 +52,14 @@ class ValidatableTest < ActiveSupport::TestCase
test 'should require password to be set when creating a new record' do
user = new_user(password: '', password_confirmation: '')
assert user.invalid?
assert_equal 'can\'t be blank', user.errors[:password].join
assert user.errors.added?(:password, :blank)
end

test 'should require confirmation to be set when creating a new record' do
user = new_user(password: 'new_password', password_confirmation: 'blabla')
assert user.invalid?

assert_equal 'doesn\'t match Password', user.errors[:password_confirmation].join
assert user.errors.added?(:password_confirmation, :confirmation, attribute: "Password")
end

test 'should require password when updating/resetting password' do
Expand All @@ -69,15 +69,15 @@ class ValidatableTest < ActiveSupport::TestCase
user.password_confirmation = ''

assert user.invalid?
assert_equal 'can\'t be blank', user.errors[:password].join
assert user.errors.added?(:password, :blank)
end

test 'should require confirmation when updating/resetting password' do
user = create_user
user.password_confirmation = 'another_password'
assert user.invalid?

assert_equal 'doesn\'t match Password', user.errors[:password_confirmation].join
assert user.errors.added?(:password_confirmation, :confirmation, attribute: "Password")
end

test 'should require a password with minimum of 7 characters' do
Expand Down

0 comments on commit 232c855

Please sign in to comment.