2015-12-16 14:16:02 +00:00
|
|
|
# https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
|
2016-01-26 10:42:24 +00:00
|
|
|
|
|
|
|
require 'devise'
|
|
|
|
|
2015-12-16 14:16:02 +00:00
|
|
|
module ControllerHelpers
|
2015-12-16 14:33:29 +00:00
|
|
|
# rubocop:disable Metrics/AbcSize
|
2015-12-16 14:16:02 +00:00
|
|
|
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
|
2016-01-26 10:47:42 +00:00
|
|
|
allow_message_expectations_on_nil
|
2015-12-16 14:33:29 +00:00
|
|
|
allow(request.env['warden']).to(
|
|
|
|
receive(:authenticate!).and_return(user)
|
|
|
|
)
|
2015-12-16 14:16:02 +00:00
|
|
|
end
|
2015-12-16 14:33:29 +00:00
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
2015-12-16 14:16:02 +00:00
|
|
|
end
|
2015-12-16 14:33:29 +00:00
|
|
|
# rubocop:enable Metrics/AbcSize
|
2015-12-16 14:16:02 +00:00
|
|
|
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
|