- Add `password_plain` method to all `User` records created by FactoryBot to access the plain password for reuse to avoid variable spaming.
- Enable Capybara system test meta attribute `:authenticate` to handle a user as lambda return value which will be used for authentication instead of default `master@example.com`.
This commit is contained in:
Thorsten Eckel 2020-04-27 15:34:52 +02:00
parent d9bda79385
commit 9402699a42
2 changed files with 26 additions and 6 deletions

View file

@ -25,5 +25,11 @@ FactoryBot.define do
factory :admin_user, aliases: %i[admin] do factory :admin_user, aliases: %i[admin] do
roles { Role.where(name: %w[Admin Agent]) } roles { Role.where(name: %w[Admin Agent]) }
end end
# make given password accessible for e.g. authentication logic
before(:create) do |user|
password_plain = user.password
user.define_singleton_method(:password_plain, -> { password_plain })
end
end end
end end

View file

@ -14,12 +14,26 @@ RSpec.configure do |config|
next if !example.metadata.fetch(:set_up, true) next if !example.metadata.fetch(:set_up, true)
# check if authentication should be performed # check if authentication should be performed
next if example.metadata.fetch(:authenticated, true).blank? authenticated = example.metadata.fetch(:authenticated, true)
next if authenticated.blank?
# authenticate if authenticated.is_a?(Proc)
login( user = instance_exec(&authenticated)
username: 'master@example.com', password = user.password_plain
password: 'test',
) raise "Can't authenticate user that has no password set" if password.blank?
credentials = {
username: user.email,
password: password,
}
else
credentials = {
username: 'master@example.com',
password: 'test',
}
end
login(credentials)
end end
end end