From cc857a74bf04b3e4f0aaf1aa2021fcc3450fbf5c Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 14 Apr 2023 00:15:33 +0200 Subject: [PATCH] Randomize sign-in tests --- test/system/users_test.rb | 14 +++++++++----- test/test_helper.rb | 8 ++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/test/system/users_test.rb b/test/system/users_test.rb index a7e0fcf..8311d8d 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -7,17 +7,21 @@ class UsersTest < ApplicationSystemTestCase test "sign in" do visit new_user_session_url - fill_in User.human_attribute_name(:email), with: @admin.email - fill_in User.human_attribute_name(:password), with: 'admin' + users.sample.then do |user| + fill_in User.human_attribute_name(:email).capitalize, with: user.email + fill_in User.human_attribute_name(:password).capitalize, with: randomize_user_password!(user) + end click_on t(:sign_in) assert_no_current_path new_user_session_path assert_text t('devise.sessions.signed_in') end - test "sign in fails with invalid credentials" do + test "sign in fails with invalid password" do visit new_user_session_url - fill_in User.human_attribute_name(:email), with: @admin.email - fill_in User.human_attribute_name(:password), with: 'badpass' + users.sample.then do |user| + fill_in User.human_attribute_name(:email).capitalize, with: user.email + fill_in User.human_attribute_name(:password).capitalize, with: random_password + end click_on t(:sign_in) assert_current_path new_user_session_path assert_text t('devise.failure.invalid', authentication_keys: User.human_attribute_name(:email)) diff --git a/test/test_helper.rb b/test/test_helper.rb index d2472e0..d84ad99 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,4 +10,12 @@ class ActiveSupport::TestCase fixtures :all include AbstractController::Translation + + def randomize_user_password!(user) + random_password.tap { |p| user.update!(password: p) } + end + + def random_password + SecureRandom.alphanumeric rand(Rails.configuration.devise.password_length) + end end