metamaps--metamaps/spec/support/controller_helpers.rb

27 lines
804 B
Ruby
Raw Normal View History

# https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
2016-01-26 10:42:24 +00:00
require 'devise'
module ControllerHelpers
2015-12-16 14:33:29 +00:00
# rubocop:disable Metrics/AbcSize
def sign_in(user = create(:user))
2015-12-16 14:33:29 +00:00
if user.nil? # simulate unauthenticated
allow(request.env['warden']).to(
receive(:authenticate!).and_throw(:warden, scope: :user)
)
else # simulate authenticated
allow_message_expectations_on_nil
2015-12-16 14:33:29 +00:00
allow(request.env['warden']).to(
receive(:authenticate!).and_return(user)
)
end
2015-12-16 14:33:29 +00:00
allow(controller).to receive(:current_user).and_return(user)
end
2015-12-16 14:33:29 +00:00
# rubocop:enable Metrics/AbcSize
end
2016-01-26 10:42:24 +00:00
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
config.include ControllerHelpers, :type => :controller
end