switch_user 1.4.0 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +22 -0
  3. data/Gemfile +2 -0
  4. data/README.md +22 -20
  5. data/Rakefile +2 -0
  6. data/app/controllers/switch_user_controller.rb +25 -11
  7. data/app/helpers/switch_user_helper.rb +16 -19
  8. data/app/views/switch_user/_widget.html.erb +2 -2
  9. data/config/routes.rb +4 -2
  10. data/lib/generators/switch_user/install/install_generator.rb +3 -2
  11. data/lib/generators/switch_user/install/templates/switch_user.rb +8 -6
  12. data/lib/switch_user.rb +22 -21
  13. data/lib/switch_user/base_guard.rb +7 -5
  14. data/lib/switch_user/data_source.rb +8 -8
  15. data/lib/switch_user/lambda_guard.rb +2 -0
  16. data/lib/switch_user/provider.rb +16 -13
  17. data/lib/switch_user/provider/authlogic.rb +6 -6
  18. data/lib/switch_user/provider/base.rb +7 -8
  19. data/lib/switch_user/provider/clearance.rb +8 -6
  20. data/lib/switch_user/provider/devise.rb +5 -3
  21. data/lib/switch_user/provider/dummy.rb +8 -12
  22. data/lib/switch_user/provider/restful_authentication.rb +5 -3
  23. data/lib/switch_user/provider/session.rb +5 -3
  24. data/lib/switch_user/provider/sorcery.rb +8 -9
  25. data/lib/switch_user/rails.rb +7 -3
  26. data/lib/switch_user/rspec.rb +4 -4
  27. data/lib/switch_user/rspec/feature_helpers.rb +13 -14
  28. data/lib/switch_user/user_loader.rb +5 -2
  29. data/lib/switch_user/user_set.rb +14 -9
  30. data/lib/switch_user/version.rb +3 -1
  31. data/spec/controllers/switch_user_controller_spec.rb +24 -27
  32. data/spec/helpers/switch_user_helper_spec.rb +58 -55
  33. data/spec/integration/switch_user_spec.rb +105 -21
  34. data/spec/provider/authlogic_spec.rb +3 -1
  35. data/spec/provider/clearance_spec.rb +3 -1
  36. data/spec/provider/devise_spec.rb +19 -18
  37. data/spec/provider/dummy_spec.rb +4 -3
  38. data/spec/provider/restful_authentication_spec.rb +3 -1
  39. data/spec/provider/session_spec.rb +4 -2
  40. data/spec/provider/sorcery_spec.rb +3 -1
  41. data/spec/provider_spec.rb +3 -1
  42. data/spec/rspec/feature_helpers_spec.rb +38 -37
  43. data/spec/spec_helper.rb +4 -2
  44. data/spec/support/application.rb +35 -23
  45. data/spec/support/provider.rb +13 -11
  46. data/spec/switch_user/data_source_spec.rb +7 -5
  47. data/spec/switch_user/lambda_guard_spec.rb +6 -4
  48. data/spec/switch_user/user_loader_spec.rb +23 -23
  49. data/spec/switch_user/user_set_spec.rb +12 -10
  50. data/spec/switch_user_spec.rb +6 -4
  51. data/switch_user.gemspec +24 -23
  52. metadata +28 -28
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user/provider/authlogic'
3
5
 
@@ -33,5 +35,5 @@ RSpec.describe SwitchUser::Provider::Authlogic do
33
35
  let(:controller) { AuthlogicController.new }
34
36
  let(:provider) { SwitchUser::Provider::Authlogic.new(controller) }
35
37
 
36
- it_behaves_like "a provider"
38
+ it_behaves_like 'a provider'
37
39
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user/provider/clearance'
3
5
 
@@ -19,5 +21,5 @@ RSpec.describe SwitchUser::Provider::Clearance do
19
21
  let(:controller) { ClearanceController.new }
20
22
  let(:provider) { SwitchUser::Provider::Clearance.new(controller) }
21
23
 
22
- it_behaves_like "a provider"
24
+ it_behaves_like 'a provider'
23
25
  end
@@ -1,15 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user/provider/devise'
3
5
 
4
6
  class FakeWardenSessionSerializer
5
7
  attr_accessor :user_hash
6
8
 
7
-
8
9
  def store(user, scope)
9
10
  return unless user
11
+
10
12
  user_hash[scope] = user
11
13
  end
12
-
13
14
  end
14
15
 
15
16
  class FakeWarden
@@ -54,34 +55,34 @@ RSpec.describe SwitchUser::Provider::Devise do
54
55
  let(:provider) { SwitchUser::Provider::Devise.new(controller) }
55
56
  let(:user) { double(:user) }
56
57
 
57
- it_behaves_like "a provider"
58
+ it_behaves_like 'a provider'
58
59
 
59
- it "can use alternate scopes" do
60
+ it 'can use alternate scopes' do
60
61
  user = double(:user)
61
62
  provider.login(user, :admin)
62
63
 
63
64
  expect(provider.current_user(:admin)).to eq user
64
65
  end
65
66
 
66
- describe "#login_exclusive" do
67
+ describe '#login_exclusive' do
67
68
  before do
68
- allow(SwitchUser).to receive(:available_users).and_return({:user => nil, :admin => nil})
69
+ allow(SwitchUser).to receive(:available_users).and_return(user: nil, admin: nil)
69
70
  provider.login(user, :admin)
70
- provider.login_exclusive(user, :scope => "user")
71
+ provider.login_exclusive(user, scope: 'user')
71
72
  end
72
73
 
73
- it "logs the user in" do
74
- expect(provider.current_user).to eq user
74
+ it 'logs the user in' do
75
+ expect(provider.current_user(:user)).to eq user
75
76
  end
76
77
 
77
- it "logs out other scopes" do
78
+ it 'logs out other scopes' do
78
79
  expect(provider.current_user(:admin)).to be_nil
79
80
  end
80
81
  end
81
82
 
82
- describe "#logout_all" do
83
- it "logs out users under all scopes" do
84
- allow(SwitchUser).to receive(:available_users).and_return({:user => nil, :admin => nil})
83
+ describe '#logout_all' do
84
+ it 'logs out users under all scopes' do
85
+ allow(SwitchUser).to receive(:available_users).and_return(user: nil, admin: nil)
85
86
  provider.login(user, :admin)
86
87
  provider.login(user, :user)
87
88
 
@@ -92,17 +93,17 @@ RSpec.describe SwitchUser::Provider::Devise do
92
93
  end
93
94
  end
94
95
 
95
- describe "#all_current_users" do
96
- it "pulls users from an alternate scope" do
97
- allow(SwitchUser).to receive(:available_users).and_return({:user => nil, :admin => nil})
96
+ describe '#all_current_users' do
97
+ it 'pulls users from an alternate scope' do
98
+ allow(SwitchUser).to receive(:available_users).and_return(user: nil, admin: nil)
98
99
  provider.login(user, :admin)
99
100
 
100
101
  expect(provider.current_users_without_scope).to eq [user]
101
102
  end
102
103
  end
103
104
 
104
- describe "#current_user?" do
105
- it "logs the user in" do
105
+ describe '#current_user?' do
106
+ it 'logs the user in' do
106
107
  user = double(:user)
107
108
  provider.login(user, :user)
108
109
 
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user/provider/dummy'
3
5
 
4
- class SessionController < TestController
5
- end
6
+ class SessionController < TestController; end
6
7
 
7
8
  RSpec.describe SwitchUser::Provider::Session do
8
9
  let(:controller) { SessionController.new }
9
10
  let(:provider) { SwitchUser::Provider::Dummy.new(controller) }
10
11
 
11
- it_behaves_like "a provider"
12
+ it_behaves_like 'a provider'
12
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user/provider/restful_authentication'
3
5
 
@@ -13,5 +15,5 @@ RSpec.describe SwitchUser::Provider::RestfulAuthentication do
13
15
  let(:controller) { RestfulAuthenticationController.new }
14
16
  let(:provider) { SwitchUser::Provider::RestfulAuthentication.new(controller) }
15
17
 
16
- it_behaves_like "a provider"
18
+ it_behaves_like 'a provider'
17
19
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user/provider/session'
3
5
 
4
6
  class SessionController < TestController
5
7
  def current_user
6
- User.find_by_id(session[:uid]) if session[:uid]
8
+ User.find_by(id: session[:uid]) if session[:uid]
7
9
  end
8
10
  end
9
11
 
@@ -14,5 +16,5 @@ RSpec.describe SwitchUser::Provider::Session do
14
16
  let(:controller) { SessionController.new }
15
17
  let(:provider) { SwitchUser::Provider::Session.new(controller) }
16
18
 
17
- it_behaves_like "a provider"
19
+ it_behaves_like 'a provider'
18
20
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user/provider/sorcery'
3
5
 
@@ -20,5 +22,5 @@ RSpec.describe SwitchUser::Provider::Sorcery do
20
22
  let(:controller) { SorceryController.new }
21
23
  let(:provider) { SwitchUser::Provider::Sorcery.new(controller) }
22
24
 
23
- it_behaves_like "a provider"
25
+ it_behaves_like 'a provider'
24
26
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module SwitchUser
4
6
  RSpec.describe Provider do
5
- it "initializes the provider" do
7
+ it 'initializes the provider' do
6
8
  SwitchUser.provider = :dummy
7
9
  expect(Provider.init(double(:controller))).to be_a(Provider::Dummy)
8
10
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  require 'capybara/rspec'
@@ -6,32 +8,32 @@ Capybara.app = MyApp::Application
6
8
 
7
9
  require 'switch_user/rspec'
8
10
 
9
- RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
11
+ RSpec.feature 'SwitchUser::RSpecFeatureHelpers', type: :feature do
10
12
  background do
11
- @user = User.create!(:email => "foo@bar.com", :admin => true)
12
- @client = Client.create!(:email => "foo@bar.com")
13
+ @user = User.create!(email: 'foo@bar.com', admin: true)
14
+ @client = Client.create!(email: 'foo@bar.com')
13
15
  end
14
16
 
15
- before(:each) do
16
- allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| true})
17
+ before(:example) do
18
+ allow(SwitchUser).to receive(:controller_guard).and_return(->(_current_user, _request) { true })
17
19
 
18
- allow(SwitchUser).to receive(:available_users).and_return({:user => lambda { User.all }})
20
+ allow(SwitchUser).to receive(:available_users).and_return(user: -> { User.all })
19
21
 
20
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({:user => :id})
22
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return(user: :id)
21
23
 
22
- allow(SwitchUser).to receive(:available_users_names).and_return({:user => :email})
24
+ allow(SwitchUser).to receive(:available_users_names).and_return(user: :email)
23
25
  end
24
26
 
25
- scenario "when controller_guard return false" do
26
- allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| false})
27
+ scenario 'when controller_guard return false' do
28
+ allow(SwitchUser).to receive(:controller_guard).and_return(->(_current_user, _request) { false })
27
29
 
28
30
  expect do
29
31
  switch_user @user
30
- end.to_not raise_error ActionController::RoutingError, /Do not try to hack us/
32
+ end.not_to raise_error
31
33
  end
32
34
 
33
- scenario "when controller_guard return false and controller call original available?" do
34
- allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| false})
35
+ scenario 'when controller_guard return false and controller call original available?' do
36
+ allow(SwitchUser).to receive(:controller_guard).and_return(->(_current_user, _request) { false })
35
37
 
36
38
  allow_any_instance_of(SwitchUserController).to receive(:available?).and_call_original
37
39
 
@@ -40,73 +42,73 @@ RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
40
42
  end.to raise_error ActionController::RoutingError, /Do not try to hack us/
41
43
  end
42
44
 
43
- scenario "arg is @user, available_users is default, and available_users_identifiers is default" do
45
+ scenario 'arg is @user, available_users is default, and available_users_identifiers is default' do
44
46
  expect do
45
47
  switch_user @user
46
- end.to_not raise_error
48
+ end.not_to raise_error
47
49
  end
48
50
 
49
- scenario "arg is @user, available_users is default, and available_users_identifiers is {user: id}" do
50
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({user: :id})
51
+ scenario 'arg is @user, available_users is default, and available_users_identifiers is {user: id}' do
52
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return(user: :id)
51
53
 
52
54
  expect do
53
55
  switch_user @user
54
- end.to_not raise_error
56
+ end.not_to raise_error
55
57
  end
56
58
 
57
- scenario "arg is @user, available_users is default, and available_users_identifiers is {:client => :id}" do
58
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({:client => :id})
59
- allow(SwitchUser).to receive(:available_users_names).and_return({:client => :email})
59
+ scenario 'arg is @user, available_users is default, and available_users_identifiers is {:client => :id}' do
60
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return(client: :id)
61
+ allow(SwitchUser).to receive(:available_users_names).and_return(client: :email)
60
62
 
61
63
  expect do
62
64
  switch_user @user
63
65
  end.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/
64
66
  end
65
67
 
66
- scenario "arg is @client, available_users is default, and available_users_identifiers is default" do
68
+ scenario 'arg is @client, available_users is default, and available_users_identifiers is default' do
67
69
  expect do
68
70
  switch_user @client
69
71
  end.to raise_error SwitchUser::InvalidScope, /config.available_users/
70
72
  end
71
73
 
72
- scenario "arg is @client, available_users is {:user => lambda { User.all }, :client => lambda {Client.all}}, and available_users_identifiers is default" do
73
- allow(SwitchUser).to receive(:available_users).and_return({:user => lambda { User.all }, :client => lambda {Client.all}})
74
+ scenario 'arg is @client, available_users is {:user => lambda { User.all }, :client => lambda {Client.all}}, and available_users_identifiers is default' do
75
+ allow(SwitchUser).to receive(:available_users).and_return(user: -> { User.all }, client: -> { Client.all })
74
76
 
75
77
  expect do
76
78
  switch_user @client
77
79
  end.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/
78
80
  end
79
81
 
80
- 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
81
- allow(SwitchUser).to receive(:available_users).and_return({:user => lambda { User.all }, :client => lambda {Client.all}})
82
+ 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
83
+ allow(SwitchUser).to receive(:available_users).and_return(user: -> { User.all }, client: -> { Client.all })
82
84
 
83
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({:user => :id, :client => :id})
84
- allow(SwitchUser).to receive(:available_users_names).and_return({:user => :email, :client => :email})
85
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return(user: :id, client: :id)
86
+ allow(SwitchUser).to receive(:available_users_names).and_return(user: :email, client: :email)
85
87
 
86
88
  expect do
87
89
  switch_user @client
88
- end.to_not raise_error
90
+ end.not_to raise_error
89
91
  end
90
92
 
91
- scenario "args is :user and @user.id, available_users is default, and available_users_identifiers is default" do
93
+ scenario 'args is :user and @user.id, available_users is default, and available_users_identifiers is default' do
92
94
  expect do
93
95
  switch_user :user, @user.id
94
- end.to_not raise_error
96
+ end.not_to raise_error
95
97
  end
96
98
 
97
- scenario "arg is :client and @client.id, available_users is default, and available_users_identifiers is default" do
99
+ scenario 'arg is :client and @client.id, available_users is default, and available_users_identifiers is default' do
98
100
  expect do
99
101
  switch_user :client, @client.id
100
102
  end.to raise_error SwitchUser::InvalidScope, /config.available_users/
101
103
  end
102
104
 
103
- scenario "args is :user, available_users is default, and available_users_identifiers is default" do
105
+ scenario 'args is :user, available_users is default, and available_users_identifiers is default' do
104
106
  expect do
105
107
  switch_user :user
106
108
  end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/
107
109
  end
108
110
 
109
- scenario "args is :user and nil, available_users is default, and available_users_identifiers is default" do
111
+ scenario 'args is :user and nil, available_users is default, and available_users_identifiers is default' do
110
112
  expect do
111
113
  switch_user :user, nil
112
114
  end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/
@@ -118,10 +120,9 @@ RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
118
120
  end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/
119
121
  end
120
122
 
121
- scenario "args is :user and 0, available_users is default, and available_users_identifiers is default" do
123
+ scenario 'args is :user and 0, available_users is default, and available_users_identifiers is default' do
122
124
  expect do
123
125
  switch_user :user, 0
124
- end.to_not raise_error
126
+ end.not_to raise_error
125
127
  end
126
-
127
128
  end
@@ -1,4 +1,6 @@
1
- ENV["RAILS_ENV"] = "test"
1
+ # frozen_string_literal: true
2
+
3
+ ENV['RAILS_ENV'] = 'test'
2
4
  require 'support/provider'
3
5
  require 'support/application'
4
6
  require 'rspec/rails'
@@ -7,7 +9,7 @@ require 'pry'
7
9
  require 'awesome_print'
8
10
 
9
11
  RSpec.configure do |config|
10
- config.filter_run :focus => true
12
+ config.filter_run focus: true
11
13
  config.run_all_when_everything_filtered = true
12
14
  config.use_transactional_fixtures = true
13
15
  config.expose_dsl_globally = false
@@ -1,51 +1,64 @@
1
- require "rails"
2
- require "rails/all"
3
- require 'switch_user/rails'
1
+ # frozen_string_literal: true
4
2
 
3
+ require 'rails'
4
+ require 'rails/all'
5
+ require 'switch_user/rails'
5
6
 
6
7
  class ApplicationController < ActionController::Base
7
8
  def require_user
8
- current_user || redirect_to("/dummy/open")
9
+ current_user || redirect_to('/dummy/open')
9
10
  end
10
11
 
11
12
  def current_user
12
- User.find_by_id(session[SwitchUser.session_key])
13
+ User.find_by(id: session[SwitchUser.session_key])
13
14
  end
14
15
 
15
16
  def login
16
17
  user = User.find(params[:id])
17
18
  session[SwitchUser.session_key] = user.id
18
19
 
19
- redirect_to("/dummy/protected")
20
+ redirect_to('/dummy/protected')
20
21
  end
21
22
 
22
23
  def logout
23
24
  session[SwitchUser.session_key] = nil
24
25
 
25
- redirect_to("/dummy/open")
26
+ redirect_to('/dummy/open')
26
27
  end
27
28
  end
28
29
 
29
30
  class DummyController < ApplicationController
30
- before_filter :require_user, :only => :protected
31
+ before_action :require_user, only: :protected
31
32
 
32
33
  def authenticated
33
- render :text => current_user.inspect
34
+ if Rails.version.to_i >= 5
35
+ render plain: current_user.inspect
36
+ else
37
+ render text: current_user.inspect
38
+ end
34
39
  end
35
40
 
36
41
  def open
37
- render :text => view_context.switch_user_select
42
+ if Rails.version.to_i >= 5
43
+ render plain: view_context.switch_user_select
44
+ else
45
+ render text: view_context.switch_user_select
46
+ end
38
47
  end
39
48
 
40
49
  def protected
41
- render :text => view_context.switch_user_select
50
+ if Rails.version.to_i >= 5
51
+ render plain: view_context.switch_user_select
52
+ else
53
+ render text: view_context.switch_user_select
54
+ end
42
55
  end
43
56
  end
44
57
 
45
58
  module MyApp
46
59
  class Application < Rails::Application
47
60
  config.active_support.deprecation = :log
48
- config.secret_key_base = "abc123"
61
+ config.secret_key_base = 'abc123'
49
62
  config.eager_load = true
50
63
  config.secret_token = '153572e559247c7aedd1bca5a246874d'
51
64
 
@@ -53,15 +66,16 @@ module MyApp
53
66
  config.action_dispatch.show_exceptions = false
54
67
  end
55
68
  end
69
+
56
70
  Rails.application.initialize!
57
71
  Rails.application.routes.draw do
58
- get 'dummy/protected', :to => "dummy#protected"
59
- get 'dummy/open', :to => "dummy#open"
60
- post 'login', :to => "dummy#login"
61
- get 'logout', :to => "dummy#logout"
62
- get 'authenticated', :to => "dummy#authenticated"
63
- get :switch_user, :to => 'switch_user#set_current_user'
64
- get 'switch_user/remember_user', :to => 'switch_user#remember_user'
72
+ get 'dummy/protected', to: 'dummy#protected'
73
+ get 'dummy/open', to: 'dummy#open'
74
+ post 'login', to: 'dummy#login'
75
+ get 'logout', to: 'dummy#logout'
76
+ get 'authenticated', to: 'dummy#authenticated'
77
+ get :switch_user, to: 'switch_user#set_current_user'
78
+ get 'switch_user/remember_user', to: 'switch_user#remember_user'
65
79
  end
66
80
 
67
81
  connection = ActiveRecord::Base.connection
@@ -70,12 +84,10 @@ connection.create_table :users do |t|
70
84
  t.column :admin, :boolean
71
85
  end
72
86
 
73
- class User < ActiveRecord::Base
74
- end
87
+ class User < ActiveRecord::Base; end
75
88
 
76
89
  connection.create_table :clients do |t|
77
90
  t.column :email, :string
78
91
  end
79
92
 
80
- class Client < ActiveRecord::Base
81
- end
93
+ class Client < ActiveRecord::Base; end