Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto corrected by following Format Ruby Code #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 1 addition & 6 deletions lib/switch_user/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions lib/switch_user/provider/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions lib/switch_user/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 8 additions & 5 deletions lib/switch_user/rspec/feature_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ 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
when ActiveRecord::Base
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
Expand Down
10 changes: 2 additions & 8 deletions lib/switch_user/user_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions spec/controllers/switch_user_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions spec/helpers/switch_user_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
15 changes: 6 additions & 9 deletions spec/integration/switch_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions spec/provider/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
57 changes: 15 additions & 42 deletions spec/rspec/feature_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,42 @@
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
allow(SwitchUser).to receive(:controller_guard).and_return(->(_current_user, _request) { false })

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
Expand All @@ -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
21 changes: 12 additions & 9 deletions spec/support/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down