2015-12-16 14:16:02 +00:00
|
|
|
# https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
|
|
|
|
module ControllerHelpers
|
|
|
|
def sign_in(user = create(:user))
|
|
|
|
if user.nil?
|
|
|
|
# simulate unauthenticated
|
2015-12-16 14:19:58 +00:00
|
|
|
allow(request.env['warden']).to receive(:authenticate!).and_throw(:warden, scope: :user)
|
2015-12-16 14:16:02 +00:00
|
|
|
allow(controller).to receive(:current_user).and_return(nil)
|
|
|
|
else
|
|
|
|
# simulate authenticated
|
|
|
|
allow(request.env['warden']).to receive(:authenticate!).and_return(user)
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|