diff --git a/config/routes.rb b/config/routes.rb index 0950f78..88f7962 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true -Rails.application.routes.draw do - get :switch_user, to: 'switch_user#set_current_user' - get 'switch_user/remember_user', to: 'switch_user#remember_user' -end +Rails + .application + .routes + .draw do + get :switch_user, to: 'switch_user#set_current_user' + get 'switch_user/remember_user', to: 'switch_user#remember_user' + end diff --git a/lib/switch_user/provider.rb b/lib/switch_user/provider.rb index 8bdecbd..bea145a 100644 --- a/lib/switch_user/provider.rb +++ b/lib/switch_user/provider.rb @@ -12,12 +12,7 @@ module Provider autoload :Session, 'switch_user/provider/session' def self.init(controller) - klass_part = - if SwitchUser.provider.is_a?(Hash) - SwitchUser.provider[:name] - else - SwitchUser.provider - end + klass_part = SwitchUser.provider.is_a?(Hash) ? SwitchUser.provider[:name] : SwitchUser.provider klass_part = klass_part.to_s.classify diff --git a/lib/switch_user/provider/base.rb b/lib/switch_user/provider/base.rb index 44e0dc1..4e63639 100644 --- a/lib/switch_user/provider/base.rb +++ b/lib/switch_user/provider/base.rb @@ -4,10 +4,12 @@ module SwitchUser module Provider class Base def current_users_without_scope - SwitchUser.available_scopes.each_with_object([]) do |scope, users| - user = current_user(scope) - users << user if user - end + SwitchUser + .available_scopes + .each_with_object([]) do |scope, users| + user = current_user(scope) + users << user if user + end end def login_exclusive(user, args) @@ -25,9 +27,7 @@ def login_inclusive(user, args) end def logout_all - SwitchUser.available_scopes.each do |scope| - logout(scope) - end + SwitchUser.available_scopes.each { |scope| logout(scope) } end def original_user diff --git a/lib/switch_user/rails.rb b/lib/switch_user/rails.rb index 7264c8c..d4e0792 100644 --- a/lib/switch_user/rails.rb +++ b/lib/switch_user/rails.rb @@ -3,11 +3,7 @@ module SwitchUser class Engine < Rails::Engine initializer 'switch_user.view' do - config.to_prepare do - ActiveSupport.on_load(:action_view) do - include SwitchUserHelper - end - end + config.to_prepare { ActiveSupport.on_load(:action_view) { include SwitchUserHelper } } end end end diff --git a/lib/switch_user/rspec/feature_helpers.rb b/lib/switch_user/rspec/feature_helpers.rb index d9e1854..199ce75 100644 --- a/lib/switch_user/rspec/feature_helpers.rb +++ b/lib/switch_user/rspec/feature_helpers.rb @@ -15,9 +15,10 @@ def switch_user(user_record_or_scope, user_id = nil) _user_scope = _user_scope.to_s - raise SwitchUser::InvalidScope, - "don't allow this user sign in, please check config.available_users" unless SwitchUser.available_scopes - .include?(_user_scope) || SwitchUser.available_scopes.include?(_user_scope.to_sym) + unless SwitchUser.available_scopes.include?(_user_scope) || + SwitchUser.available_scopes.include?(_user_scope.to_sym) + raise SwitchUser::InvalidScope, "don't allow this user sign in, please check config.available_users" + end _user_id = case user_record_or_scope @@ -25,8 +26,10 @@ def switch_user(user_record_or_scope, user_id = nil) identifier = SwitchUser.available_users_identifiers[_user_scope] || SwitchUser.available_users_identifiers[_user_scope.to_sym] - raise SwitchUser::InvalidScope, - "don't allow switch this user, please check config.available_users_identifiers" if identifier.nil? + if identifier.nil? + raise SwitchUser::InvalidScope, + "don't allow switch this user, please check config.available_users_identifiers" + end user_record_or_scope.send identifier else diff --git a/lib/switch_user/user_set.rb b/lib/switch_user/user_set.rb index b30b44f..3671a9c 100644 --- a/lib/switch_user/user_set.rb +++ b/lib/switch_user/user_set.rb @@ -11,9 +11,7 @@ def self.init_from_config end def self.users - init_from_config.flat_map do |user_set| - user_set.users.map { |user| Record.build(user, user_set) } - end + init_from_config.flat_map { |user_set| user_set.users.map { |user| Record.build(user, user_set) } } end attr_reader :scope, :user_class, :identifier, :label, :base_scope @@ -39,11 +37,7 @@ def users private def normalize_class(klass) - if klass.is_a?(Class) - klass - else - klass.to_s.classify.constantize - end + klass.is_a?(Class) ? klass : klass.to_s.classify.constantize end def normalize_scope(scope) diff --git a/spec/controllers/switch_user_controller_spec.rb b/spec/controllers/switch_user_controller_spec.rb index be935d0..aa5bc89 100644 --- a/spec/controllers/switch_user_controller_spec.rb +++ b/spec/controllers/switch_user_controller_spec.rb @@ -5,9 +5,7 @@ require 'switch_user_controller' RSpec.describe SwitchUserController, type: :controller do - before do - SwitchUser.provider = :dummy - end + before { SwitchUser.provider = :dummy } let(:admin) { double(:admin, admin?: true) } let(:provider) { double(:provider, original_user: admin, current_user: nil) } @@ -27,9 +25,8 @@ describe 'requests with a privileged original_user' do before do - SwitchUser.controller_guard = lambda do |current_user, _, original_user| - current_user.try(:admin?) || original_user.try(:admin?) - end + SwitchUser.controller_guard = + lambda { |current_user, _, original_user| current_user.try(:admin?) || original_user.try(:admin?) } end it 'allows access using the original_user param' do allow(controller).to receive(:provider).and_return(provider) diff --git a/spec/helpers/switch_user_helper_spec.rb b/spec/helpers/switch_user_helper_spec.rb index 6afbc1e..4a4e4db 100644 --- a/spec/helpers/switch_user_helper_spec.rb +++ b/spec/helpers/switch_user_helper_spec.rb @@ -5,9 +5,7 @@ require 'switch_user_helper' RSpec.describe SwitchUserHelper, type: :helper do - before do - SwitchUser.provider = :dummy - end + before { SwitchUser.provider = :dummy } let(:user) { double(:user, id: 1) } let(:admin) { double(:admin, id: 1) } diff --git a/spec/integration/switch_user_spec.rb b/spec/integration/switch_user_spec.rb index 3d2dc98..cedbc3b 100644 --- a/spec/integration/switch_user_spec.rb +++ b/spec/integration/switch_user_spec.rb @@ -29,9 +29,10 @@ context 'using switch_back' do before do SwitchUser.switch_back = true - SwitchUser.controller_guard = lambda do |current_user, _request, original_user| - current_user && current_user.admin? || original_user && original_user.admin? - end + SwitchUser.controller_guard = + lambda do |current_user, _request, original_user| + current_user && current_user.admin? || original_user && original_user.admin? + end end it 'can switch back to a different user through remember_user endpoint' do @@ -84,13 +85,9 @@ end context 'when non-default identifier' do - before do - SwitchUser.available_users_identifiers = { user: :email } - end + before { SwitchUser.available_users_identifiers = { user: :email } } - after do - SwitchUser.available_users_identifiers = { user: :id } - end + after { SwitchUser.available_users_identifiers = { user: :id } } it 'can switch back to a different user through remember_user endpoint' do # login diff --git a/spec/provider/session_spec.rb b/spec/provider/session_spec.rb index a9eea3d..498980f 100644 --- a/spec/provider/session_spec.rb +++ b/spec/provider/session_spec.rb @@ -10,9 +10,7 @@ def current_user end RSpec.describe SwitchUser::Provider::Session do - before do - SwitchUser.session_key = :uid - end + before { SwitchUser.session_key = :uid } let(:controller) { SessionController.new } let(:provider) { SwitchUser::Provider::Session.new(controller) } diff --git a/spec/rspec/feature_helpers_spec.rb b/spec/rspec/feature_helpers_spec.rb index 8af4f2c..ffe7539 100644 --- a/spec/rspec/feature_helpers_spec.rb +++ b/spec/rspec/feature_helpers_spec.rb @@ -27,9 +27,7 @@ scenario 'when controller_guard return false' do allow(SwitchUser).to receive(:controller_guard).and_return(->(_current_user, _request) { false }) - expect do - switch_user @user - end.not_to raise_error + expect { switch_user @user }.not_to raise_error end scenario 'when controller_guard return false and controller call original available?' do @@ -37,46 +35,34 @@ allow_any_instance_of(SwitchUserController).to receive(:available?).and_call_original - expect do - switch_user @user - end.to raise_error ActionController::RoutingError, /Do not try to hack us/ + expect { switch_user @user }.to raise_error ActionController::RoutingError, /Do not try to hack us/ end scenario 'arg is @user, available_users is default, and available_users_identifiers is default' do - expect do - switch_user @user - end.not_to raise_error + expect { switch_user @user }.not_to raise_error end scenario 'arg is @user, available_users is default, and available_users_identifiers is {user: id}' do allow(SwitchUser).to receive(:available_users_identifiers).and_return(user: :id) - expect do - switch_user @user - end.not_to raise_error + expect { switch_user @user }.not_to raise_error end scenario 'arg is @user, available_users is default, and available_users_identifiers is {:client => :id}' do allow(SwitchUser).to receive(:available_users_identifiers).and_return(client: :id) allow(SwitchUser).to receive(:available_users_names).and_return(client: :email) - expect do - switch_user @user - end.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/ + expect { switch_user @user }.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/ end scenario 'arg is @client, available_users is default, and available_users_identifiers is default' do - expect do - switch_user @client - end.to raise_error SwitchUser::InvalidScope, /config.available_users/ + expect { switch_user @client }.to raise_error SwitchUser::InvalidScope, /config.available_users/ end scenario 'arg is @client, available_users is {:user => lambda { User.all }, :client => lambda {Client.all}}, and available_users_identifiers is default' do allow(SwitchUser).to receive(:available_users).and_return(user: -> { User.all }, client: -> { Client.all }) - expect do - switch_user @client - end.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/ + expect { switch_user @client }.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/ end scenario 'arg is @client, available_users is {:user => lambda { User.all }, :client => lambda {Client.all}}, and available_users_identifiers is {:user => id, :client => id}' do @@ -85,44 +71,31 @@ allow(SwitchUser).to receive(:available_users_identifiers).and_return(user: :id, client: :id) allow(SwitchUser).to receive(:available_users_names).and_return(user: :email, client: :email) - expect do - switch_user @client - end.not_to raise_error + expect { switch_user @client }.not_to raise_error end scenario 'args is :user and @user.id, available_users is default, and available_users_identifiers is default' do - expect do - switch_user :user, @user.id - end.not_to raise_error + expect { switch_user :user, @user.id }.not_to raise_error end scenario 'arg is :client and @client.id, available_users is default, and available_users_identifiers is default' do - expect do - switch_user :client, @client.id - end.to raise_error SwitchUser::InvalidScope, /config.available_users/ + expect { switch_user :client, @client.id }.to raise_error SwitchUser::InvalidScope, /config.available_users/ end scenario 'args is :user, available_users is default, and available_users_identifiers is default' do - expect do - switch_user :user - end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/ + expect { switch_user :user }.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/ end scenario 'args is :user and nil, available_users is default, and available_users_identifiers is default' do - expect do - switch_user :user, nil - end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/ + expect { switch_user :user, nil }.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, + /user_id is empty/ end scenario "args is :user and '', available_users is default, and available_users_identifiers is default" do - expect do - switch_user :user, '' - end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/ + expect { switch_user :user, '' }.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/ end scenario 'args is :user and 0, available_users is default, and available_users_identifiers is default' do - expect do - switch_user :user, 0 - end.not_to raise_error + expect { switch_user :user, 0 }.not_to raise_error end end diff --git a/spec/support/application.rb b/spec/support/application.rb index bb9b5ae..c8d5363 100644 --- a/spec/support/application.rb +++ b/spec/support/application.rb @@ -68,15 +68,18 @@ class Application < Rails::Application end Rails.application.initialize! -Rails.application.routes.draw do - get 'dummy/protected', to: 'dummy#protected' - get 'dummy/open', to: 'dummy#open' - post 'login', to: 'dummy#login' - get 'logout', to: 'dummy#logout' - get 'authenticated', to: 'dummy#authenticated' - get :switch_user, to: 'switch_user#set_current_user' - get 'switch_user/remember_user', to: 'switch_user#remember_user' -end +Rails + .application + .routes + .draw do + get 'dummy/protected', to: 'dummy#protected' + get 'dummy/open', to: 'dummy#open' + post 'login', to: 'dummy#login' + get 'logout', to: 'dummy#logout' + get 'authenticated', to: 'dummy#authenticated' + get :switch_user, to: 'switch_user#set_current_user' + get 'switch_user/remember_user', to: 'switch_user#remember_user' + end connection = ActiveRecord::Base.connection connection.create_table :users do |t|