Skip to content

Commit

Permalink
Merge pull request ManageIQ#17575 from kbrock/authentication_helper_s…
Browse files Browse the repository at this point in the history
…tubless

remove most of test authentication stubbing
  • Loading branch information
bdunne authored Aug 2, 2018
2 parents 818ed48 + 5b77924 commit e83fd17
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions spec/support/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,29 @@ def http_login(username = 'username', password = 'password')
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(username, password)
end

def login_as(user)
def login_as(user, stub_controller: false)
User.current_user = user
STDERR.puts "WARNING: double stubbing user - only use login_as or stub_user once" if user != User.current_user
session[:userid] = user.userid
session[:group] = user.current_group_id
allow(controller).to receive(:current_user).and_return(user) if stub_controller
user
end

# TODO: Stub specific features, document use
def stub_user(features:, user: FactoryGirl.build(:user_with_group))
allow(controller).to receive(:current_user).and_return(user)
allow(User).to receive(:current_user).and_return(user)
# Stubs the user in context for a controller
# @param features (:all|:none|Array<>,String,Symbol) features for a user
# :all means all features are avaiable, essentially "super_administrator"
# :none means no features are available. (feature "none" ends up being assigned - which does nothing)
def stub_user(features:)
allow(User).to receive(:server_timezone).and_return("UTC")
allow_any_instance_of(described_class).to receive(:set_user_time_zone)

stub_bool = case features
when :all then true
when :none then false
else
raise ArgumentError, <<-EOS
Unknown features option. You must pass :all or :none to #stub_user.
If you need specific features, use #login_as with an actual User model instead.
EOS
end
allow(controller).to receive(:check_privileges).and_return(stub_bool)
allow(controller).to receive(:assert_rbac).and_return(stub_bool)
allow(Rbac).to receive(:role_allows?).and_return(stub_bool)

login_as user
user
features = "everything" if features == :all
login_as FactoryGirl.create(:user, :features => Array.wrap(features).map(&:to_s)), :stub_controller => true
end

def stub_admin
stub_user(:features => :all, :user => FactoryGirl.create(:user_admin))
stub_user(:features => :all)
end
end

Expand Down

0 comments on commit e83fd17

Please sign in to comment.