From 68881113e4eae88aef666573f8f67daae3b2cd5c Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 8 Mar 2018 13:23:37 +0100 Subject: [PATCH] Refactored FactoryBot User factory. --- spec/factories/user.rb | 8 ++++++-- spec/models/user_spec.rb | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 4272f225d..38cfeb0c9 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -22,7 +22,11 @@ FactoryBot.define do role_ids { Role.signup_role_ids.sort } end - factory :user_login_failed, parent: :user do - login_failed { (Setting.get('password_max_login_failed').to_i || 10) + 1 } + factory :agent_user, parent: :user do + roles { Role.where(name: 'Agent') } + end + + factory :admin_user, parent: :user do + roles { Role.where(name: %w[Admin Agent]) } end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6e814ec6a..79aa14f33 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -5,8 +5,8 @@ require 'models/concerns/has_groups_permissions_examples' RSpec.describe User do - let(:group_access_instance) { create(:user, roles: [Role.find_by(name: 'Agent')]) } - let(:new_group_access_instance) { build(:user, roles: [Role.find_by(name: 'Agent')]) } + let(:group_access_instance) { create(:agent_user) } + let(:new_group_access_instance) { build(:agent_user) } let(:group_access_no_permission_instance) { build(:user) } include_examples 'HasGroups' @@ -18,7 +18,10 @@ RSpec.describe User do context 'password' do it 'resets login_failed on password change' do - user = create(:user_login_failed) + + failed_logins = (Setting.get('password_max_login_failed').to_i || 10) + 1 + user = create(:user, login_failed: failed_logins) + expect do user.password = new_password user.save