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

18 lines
596 B
Ruby
Raw Normal View History

# https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
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(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