2015-12-16 14:16:02 +00:00
|
|
|
# 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
|
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
|
|
|
|
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
|