From a00f3c51a8e606cc0343f9d31ef63671a96c915d Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 28 Sep 2018 14:52:06 +0200 Subject: [PATCH] Improved RSpec suite by having dedicated last_admin_check meta attribute instead of polluting the whole suite (and adding side effects by being to assumtious). --- .../sequence/import/ldap/users_spec.rb | 4 ++-- spec/requests/api_auth_spec.rb | 4 ++-- spec/support/last_admin_check.rb | 20 +++++++++++++++++++ spec/support/system_init.rb | 15 -------------- 4 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 spec/support/last_admin_check.rb delete mode 100644 spec/support/system_init.rb diff --git a/spec/lib/sequencer/sequence/import/ldap/users_spec.rb b/spec/lib/sequencer/sequence/import/ldap/users_spec.rb index 6e32ca609..949545f84 100644 --- a/spec/lib/sequencer/sequence/import/ldap/users_spec.rb +++ b/spec/lib/sequencer/sequence/import/ldap/users_spec.rb @@ -6,7 +6,7 @@ RSpec.describe ::Sequencer::Sequence::Import::Ldap::Users, sequencer: :sequence context 'config "unassigned_users": "skip_sync"' do - it 'disables user' do + it 'disables user', last_admin_check: false do user_entry = build(:ldap_entry) user_entry['objectguid'] = ['user1337'] @@ -99,7 +99,7 @@ RSpec.describe ::Sequencer::Sequence::Import::Ldap::Users, sequencer: :sequence context 'config "unassigned_users": nil / "sigup_roles"' do - it 'assigns signup roles' do + it 'assigns signup roles', last_admin_check: false do user_entry = build(:ldap_entry) user_entry['objectguid'] = ['user1337'] diff --git a/spec/requests/api_auth_spec.rb b/spec/requests/api_auth_spec.rb index 61a495108..df8de3414 100644 --- a/spec/requests/api_auth_spec.rb +++ b/spec/requests/api_auth_spec.rb @@ -68,7 +68,7 @@ RSpec.describe 'Api Auth', type: :request do expect(json_response).to be_truthy end - it 'does token auth - admin' do + it 'does token auth - admin', last_admin_check: false do admin_token = create( :token, @@ -289,7 +289,7 @@ RSpec.describe 'Api Auth', type: :request do expect(response).to have_http_status(401) end - it 'does token auth - invalid user - admin' do + it 'does token auth - invalid user - admin', last_admin_check: false do admin_token = create( :token, diff --git a/spec/support/last_admin_check.rb b/spec/support/last_admin_check.rb new file mode 100644 index 000000000..4e18bae7c --- /dev/null +++ b/spec/support/last_admin_check.rb @@ -0,0 +1,20 @@ +RSpec.configure do |config| + + config.around(:example, last_admin_check: false) do |example| + + User.class_eval do + alias_method :original_last_admin_check_admin_count, :last_admin_check_admin_count + + def last_admin_check_admin_count + 1 + end + end + + example.run + + User.class_eval do + alias_method :last_admin_check_admin_count, :original_last_admin_check_admin_count + remove_method :original_last_admin_check_admin_count + end + end +end diff --git a/spec/support/system_init.rb b/spec/support/system_init.rb deleted file mode 100644 index a7d554f83..000000000 --- a/spec/support/system_init.rb +++ /dev/null @@ -1,15 +0,0 @@ -RSpec.configure do |config| - config.before(:suite) do - - email = 'admin@example.com' - if !::User.exists?(email: email) - FactoryBot.create(:user, - login: 'admin', - firstname: 'Admin', - lastname: 'Admin', - email: email, - password: 'admin', - roles: [Role.lookup(name: 'Admin')],) - end - end -end