switch_user 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Gemfile +2 -0
  4. data/README.md +1 -0
  5. data/Rakefile +2 -0
  6. data/app/controllers/switch_user_controller.rb +6 -4
  7. data/app/helpers/switch_user_helper.rb +8 -8
  8. data/config/routes.rb +2 -0
  9. data/lib/generators/switch_user/install/install_generator.rb +2 -1
  10. data/lib/generators/switch_user/install/templates/switch_user.rb +5 -3
  11. data/lib/switch_user.rb +12 -12
  12. data/lib/switch_user/base_guard.rb +4 -2
  13. data/lib/switch_user/data_source.rb +6 -6
  14. data/lib/switch_user/lambda_guard.rb +2 -0
  15. data/lib/switch_user/provider.rb +15 -13
  16. data/lib/switch_user/provider/authlogic.rb +6 -6
  17. data/lib/switch_user/provider/base.rb +5 -4
  18. data/lib/switch_user/provider/clearance.rb +5 -3
  19. data/lib/switch_user/provider/devise.rb +2 -0
  20. data/lib/switch_user/provider/dummy.rb +8 -12
  21. data/lib/switch_user/provider/restful_authentication.rb +5 -3
  22. data/lib/switch_user/provider/session.rb +5 -3
  23. data/lib/switch_user/provider/sorcery.rb +6 -6
  24. data/lib/switch_user/rails.rb +3 -1
  25. data/lib/switch_user/rspec.rb +2 -2
  26. data/lib/switch_user/rspec/feature_helpers.rb +6 -7
  27. data/lib/switch_user/user_loader.rb +5 -2
  28. data/lib/switch_user/user_set.rb +5 -3
  29. data/lib/switch_user/version.rb +3 -1
  30. data/spec/controllers/switch_user_controller_spec.rb +21 -18
  31. data/spec/helpers/switch_user_helper_spec.rb +45 -46
  32. data/spec/integration/switch_user_spec.rb +91 -30
  33. data/spec/provider/authlogic_spec.rb +3 -1
  34. data/spec/provider/clearance_spec.rb +3 -1
  35. data/spec/provider/devise_spec.rb +17 -17
  36. data/spec/provider/dummy_spec.rb +3 -1
  37. data/spec/provider/restful_authentication_spec.rb +3 -1
  38. data/spec/provider/session_spec.rb +3 -1
  39. data/spec/provider/sorcery_spec.rb +3 -1
  40. data/spec/provider_spec.rb +3 -1
  41. data/spec/rspec/feature_helpers_spec.rb +31 -30
  42. data/spec/spec_helper.rb +3 -1
  43. data/spec/support/application.rb +13 -12
  44. data/spec/support/provider.rb +11 -9
  45. data/spec/switch_user/data_source_spec.rb +3 -1
  46. data/spec/switch_user/lambda_guard_spec.rb +4 -2
  47. data/spec/switch_user/user_loader_spec.rb +18 -16
  48. data/spec/switch_user/user_set_spec.rb +9 -7
  49. data/spec/switch_user_spec.rb +6 -4
  50. data/switch_user.gemspec +24 -23
  51. metadata +19 -19
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  module Provider
3
5
  class Session < Base
@@ -5,15 +7,15 @@ module SwitchUser
5
7
  @controller = controller
6
8
  end
7
9
 
8
- def login(user, scope = nil)
10
+ def login(user, _scope = nil)
9
11
  @controller.session[session_key] = user.id
10
12
  end
11
13
 
12
- def logout(scope = nil)
14
+ def logout(_scope = nil)
13
15
  @controller.session.delete(session_key)
14
16
  end
15
17
 
16
- def current_user(scope = nil)
18
+ def current_user(_scope = nil)
17
19
  @controller.current_user
18
20
  end
19
21
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  module Provider
3
5
  class Sorcery < Base
@@ -5,14 +7,12 @@ module SwitchUser
5
7
  @controller = controller
6
8
  end
7
9
 
8
- def login(user, scope = nil)
10
+ def login(user, _scope = nil)
9
11
  @controller.auto_login(user)
10
12
  end
11
13
 
12
- def logout(scope = nil)
13
- if SwitchUser.switch_back
14
- save_original_user_identifier
15
- end
14
+ def logout(_scope = nil)
15
+ save_original_user_identifier if SwitchUser.switch_back
16
16
 
17
17
  @controller.logout
18
18
 
@@ -29,7 +29,7 @@ module SwitchUser
29
29
  end
30
30
  end
31
31
 
32
- def current_user(scope = nil)
32
+ def current_user(_scope = nil)
33
33
  @controller.current_user
34
34
  end
35
35
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  class Engine < Rails::Engine
3
- initializer "switch_user.view" do
5
+ initializer 'switch_user.view' do
4
6
  ActiveSupport.on_load(:action_view) do
5
7
  include SwitchUserHelper
6
8
  end
@@ -1,13 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'switch_user/rspec/feature_helpers'
2
4
 
3
5
  require 'rspec/core'
4
6
 
5
7
  RSpec.configure do |config|
6
-
7
8
  config.include SwitchUser::RSpecFeatureHelpers, type: :feature
8
9
 
9
10
  config.before(:each, type: :feature) do
10
11
  allow_any_instance_of(SwitchUserController).to receive(:available?).and_return(true)
11
12
  end
12
-
13
13
  end
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  module RSpecFeatureHelpers
3
-
4
5
  class InvalidArgument < StandardError; end
5
6
 
6
7
  def switch_user(user_record_or_scope, user_id = nil)
@@ -14,8 +15,8 @@ module SwitchUser
14
15
 
15
16
  _user_scope = _user_scope.to_s
16
17
 
17
- unless (SwitchUser.available_scopes.include?(_user_scope) or SwitchUser.available_scopes.include?(_user_scope.to_sym))
18
- raise SwitchUser::InvalidScope.new("don't allow this user sign in, please check config.available_users")
18
+ unless SwitchUser.available_scopes.include?(_user_scope) || SwitchUser.available_scopes.include?(_user_scope.to_sym)
19
+ raise SwitchUser::InvalidScope, "don't allow this user sign in, please check config.available_users"
19
20
  end
20
21
 
21
22
  _user_id =
@@ -23,7 +24,7 @@ module SwitchUser
23
24
  when ActiveRecord::Base
24
25
  identifier = SwitchUser.available_users_identifiers[_user_scope] || SwitchUser.available_users_identifiers[_user_scope.to_sym]
25
26
  if identifier.nil?
26
- raise SwitchUser::InvalidScope.new("don't allow switch this user, please check config.available_users_identifiers")
27
+ raise SwitchUser::InvalidScope, "don't allow switch this user, please check config.available_users_identifiers"
27
28
  end
28
29
  user_record_or_scope.send identifier
29
30
  else
@@ -31,14 +32,12 @@ module SwitchUser
31
32
  end
32
33
 
33
34
  if _user_id.to_s.empty?
34
- raise InvalidArgument.new("don't allow switch this user, user_id is empty")
35
+ raise InvalidArgument, "don't allow switch this user, user_id is empty"
35
36
  end
36
37
 
37
38
  scope_identifier = "#{_user_scope}_#{_user_id}"
38
39
 
39
40
  visit "/switch_user?scope_identifier=#{scope_identifier}"
40
41
  end
41
-
42
42
  end
43
-
44
43
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  class UserLoader
3
5
  attr_reader :scope
@@ -8,7 +10,8 @@ module SwitchUser
8
10
 
9
11
  if options[:scope_identifier]
10
12
  options[:scope_identifier] =~ /^(.*)_([^_]+)$/
11
- scope, id = $1, $2
13
+ scope = Regexp.last_match(1)
14
+ id = Regexp.last_match(2)
12
15
  else
13
16
  scope, id = args
14
17
  end
@@ -30,7 +33,7 @@ module SwitchUser
30
33
  if scope && SwitchUser.available_scopes.include?(scope.to_sym)
31
34
  @scope = scope
32
35
  else
33
- raise InvalidScope.new("#{scope} is invalid and is not listed in SwitchUser#available_users")
36
+ raise InvalidScope, "#{scope} is invalid and is not listed in SwitchUser#available_users"
34
37
  end
35
38
  end
36
39
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  class UserSet
3
5
  def self.init_from_config
@@ -9,9 +11,9 @@ module SwitchUser
9
11
  end
10
12
 
11
13
  def self.users
12
- init_from_config.flat_map { |user_set|
14
+ init_from_config.flat_map do |user_set|
13
15
  user_set.users.map { |user| Record.build(user, user_set) }
14
- }
16
+ end
15
17
  end
16
18
 
17
19
  attr_reader :scope, :user_class, :identifier, :label, :base_scope
@@ -26,7 +28,7 @@ module SwitchUser
26
28
  def find_user(id)
27
29
  Record.build(users.where(id: id).first, self)
28
30
  end
29
- alias :[] :find_user
31
+ alias [] find_user
30
32
 
31
33
  def users
32
34
  base_scope
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
- VERSION = "1.5.0"
4
+ VERSION = '1.5.1'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user'
3
5
  require 'switch_user_controller'
@@ -8,31 +10,32 @@ RSpec.describe SwitchUserController, type: :controller do
8
10
  end
9
11
 
10
12
  let(:admin) { double(:admin, admin?: true) }
11
- let(:provider) { double(:provider,
12
- original_user: admin,
13
- current_user: nil)
13
+ let(:provider) {
14
+ double(:provider,
15
+ original_user: admin,
16
+ current_user: nil)
14
17
  }
15
- describe "#set_current_user" do
16
- it "redirects the user to the specified location" do
17
- SwitchUser.redirect_path = ->(_, _) { "/path" }
18
+ describe '#set_current_user' do
19
+ it 'redirects the user to the specified location' do
20
+ SwitchUser.redirect_path = ->(_, _) { '/path' }
18
21
  allow(controller).to receive(:available?).and_return(true)
19
- get :set_current_user, params: { scope_identifier: "user_1" }
22
+ get :set_current_user, params: { scope_identifier: 'user_1' }
20
23
 
21
- expect(response).to redirect_to("/path")
24
+ expect(response).to redirect_to('/path')
22
25
  end
23
26
 
24
- it "denies access according to the guard block" do
27
+ it 'denies access according to the guard block' do
25
28
  SwitchUser.controller_guard = ->(_, _, _) { false }
26
29
  expect {
27
30
  get :set_current_user
28
31
  }.to raise_error(ActionController::RoutingError)
29
32
  end
30
33
 
31
- describe "requests with a privileged original_user" do
34
+ describe 'requests with a privileged original_user' do
32
35
  before do
33
36
  SwitchUser.controller_guard = ->(current_user, _, original_user) { current_user.try(:admin?) || original_user.try(:admin?) }
34
37
  end
35
- it "allows access using the original_user param" do
38
+ it 'allows access using the original_user param' do
36
39
  allow(controller).to receive(:provider).and_return(provider)
37
40
 
38
41
  expect(provider).to receive(:logout_all)
@@ -44,26 +47,26 @@ RSpec.describe SwitchUserController, type: :controller do
44
47
  end
45
48
  end
46
49
 
47
- describe "#remember_user" do
50
+ describe '#remember_user' do
48
51
  before do
49
52
  allow(controller).to receive(:provider).and_return(provider)
50
53
  SwitchUser.switch_back = true
51
54
  end
52
- it "can remember the current user" do
55
+ it 'can remember the current user' do
53
56
  expect(provider).to receive(:remember_current_user).with(true)
54
57
 
55
- get :remember_user, params: { remember: "true" }
58
+ get :remember_user, params: { remember: 'true' }
56
59
  end
57
- it "can forget the current user" do
60
+ it 'can forget the current user' do
58
61
  expect(provider).to receive(:remember_current_user).with(false)
59
62
 
60
- get :remember_user, params: { remember: "false" }
63
+ get :remember_user, params: { remember: 'false' }
61
64
  end
62
- it "does nothing if switch_back is not enabled" do
65
+ it 'does nothing if switch_back is not enabled' do
63
66
  SwitchUser.switch_back = false
64
67
  expect(provider).not_to receive(:remember_current_user)
65
68
 
66
- get :remember_user, params: { remember: "true" }
69
+ get :remember_user, params: { remember: 'true' }
67
70
  end
68
71
  end
69
72
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'switch_user'
3
5
  require 'switch_user_helper'
@@ -14,17 +16,16 @@ RSpec.describe SwitchUserHelper, type: :helper do
14
16
  _provider
15
17
  }
16
18
 
17
- describe "#switch_user_select" do
19
+ describe '#switch_user_select' do
18
20
  let(:guest_record) { SwitchUser::GuestRecord.new }
19
21
  let(:user_record) { double(:user_record, user: user, scope: :user, label: 'user1', scope_id: 'user_1') }
20
22
  let(:admin_record) { double(:admin_record, user: admin, scope: :admin, label: 'admin1', scope_id: 'admin_1') }
21
23
 
22
- let(:guest_option_tags) { %Q^<optgroup label="Guest"><option value="">Guest</option></optgroup>^ }
23
- let(:user_option_tags) { %Q^<optgroup label="User"><option value="user_1">user1</option></optgroup>^ }
24
- let(:user_selected_option_tags) { %Q^<optgroup label="User"><option selected="selected" value="user_1">user1</option></optgroup>^ }
25
- let(:admin_option_tags) { %Q^<optgroup label="Admin"><option value="admin_1">admin1</option></optgroup>^ }
26
- let(:admin_selected_option_tags) { %Q^<optgroup label="Admin"><option selected="selected" value="admin_1">admin1</option></optgroup>^ }
27
-
24
+ let(:guest_option_tags) { '<optgroup label="Guest"><option value="">Guest</option></optgroup>' }
25
+ let(:user_option_tags) { '<optgroup label="User"><option value="user_1">user1</option></optgroup>' }
26
+ let(:user_selected_option_tags) { '<optgroup label="User"><option selected="selected" value="user_1">user1</option></optgroup>' }
27
+ let(:admin_option_tags) { '<optgroup label="Admin"><option value="admin_1">admin1</option></optgroup>' }
28
+ let(:admin_selected_option_tags) { '<optgroup label="Admin"><option selected="selected" value="admin_1">admin1</option></optgroup>' }
28
29
 
29
30
  before do
30
31
  allow(SwitchUser).to receive(:switch_back).and_return(false)
@@ -39,64 +40,64 @@ RSpec.describe SwitchUserHelper, type: :helper do
39
40
  allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
40
41
  end
41
42
 
42
- it "when unavailable" do
43
+ it 'when unavailable' do
43
44
  allow(helper).to receive(:available?).and_return(false)
44
45
 
45
46
  expect(helper.switch_user_select).to eq(nil)
46
47
  end
47
48
 
48
- it "when current_user is nil and all_users is []" do
49
+ it 'when current_user is nil and all_users is []' do
49
50
  allow(provider).to receive(:current_user).and_return(nil)
50
51
  allow(SwitchUser).to receive(:all_users).and_return([])
51
52
 
52
53
  expect(helper.switch_user_select).not_to match(%r{</option>})
53
54
  end
54
55
 
55
- it "when current_user is nil and all_users is [guest_record]" do
56
+ it 'when current_user is nil and all_users is [guest_record]' do
56
57
  allow(provider).to receive(:current_user).and_return(nil)
57
58
  allow(SwitchUser).to receive(:all_users).and_return([guest_record])
58
59
 
59
- expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
60
+ expect(helper.switch_user_select).to match(/#{guest_option_tags}/)
60
61
  end
61
62
 
62
- it "when current_user is nil and all_users is [guest_record, user_record]" do
63
+ it 'when current_user is nil and all_users is [guest_record, user_record]' do
63
64
  allow(provider).to receive(:current_user).and_return(nil)
64
65
  allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
65
66
 
66
- expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
67
- expect(helper.switch_user_select).to match(%r{#{user_option_tags}})
67
+ expect(helper.switch_user_select).to match(/#{guest_option_tags}/)
68
+ expect(helper.switch_user_select).to match(/#{user_option_tags}/)
68
69
  end
69
70
 
70
- it "when current_user is user and all_users is []" do
71
+ it 'when current_user is user and all_users is []' do
71
72
  allow(provider).to receive(:current_user).and_return(user)
72
73
  allow(SwitchUser).to receive(:all_users).and_return([])
73
74
 
74
75
  expect(helper.switch_user_select).not_to match(%r{</option>})
75
76
  end
76
77
 
77
- it "when current_user is user and all_users is [guest_record, user_record]" do
78
+ it 'when current_user is user and all_users is [guest_record, user_record]' do
78
79
  allow(provider).to receive(:current_user).and_return(user)
79
80
  allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
80
81
 
81
- expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
82
- expect(helper.switch_user_select).to match(%r{#{user_selected_option_tags}})
82
+ expect(helper.switch_user_select).to match(/#{guest_option_tags}/)
83
+ expect(helper.switch_user_select).to match(/#{user_selected_option_tags}/)
83
84
  end
84
85
 
85
- it "when current_user is default allow and all_users is default allow" do
86
- expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
87
- expect(helper.switch_user_select).to match(%r{#{user_selected_option_tags}})
86
+ it 'when current_user is default allow and all_users is default allow' do
87
+ expect(helper.switch_user_select).to match(/#{guest_option_tags}/)
88
+ expect(helper.switch_user_select).to match(/#{user_selected_option_tags}/)
88
89
  end
89
90
 
90
- it "when current_user is user and all_users is [guest_record, user_record, admin_record]" do
91
+ it 'when current_user is user and all_users is [guest_record, user_record, admin_record]' do
91
92
  allow(provider).to receive(:current_user).and_return(user)
92
93
  allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record, admin_record])
93
94
 
94
- expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
95
- expect(helper.switch_user_select).to match(%r{#{user_selected_option_tags}})
96
- expect(helper.switch_user_select).to match(%r{#{admin_option_tags}})
95
+ expect(helper.switch_user_select).to match(/#{guest_option_tags}/)
96
+ expect(helper.switch_user_select).to match(/#{user_selected_option_tags}/)
97
+ expect(helper.switch_user_select).to match(/#{admin_option_tags}/)
97
98
  end
98
99
 
99
- it "when current_user is admin and all_users is [guest_record, user_record, admin_record]" do
100
+ it 'when current_user is admin and all_users is [guest_record, user_record, admin_record]' do
100
101
  provider.instance_variable_set(:@user, admin)
101
102
  allow(helper).to receive(:provider).and_return(provider)
102
103
 
@@ -104,12 +105,12 @@ RSpec.describe SwitchUserHelper, type: :helper do
104
105
 
105
106
  allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record, admin_record])
106
107
 
107
- expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
108
- expect(helper.switch_user_select).to match(%r{#{user_option_tags}})
109
- expect(helper.switch_user_select).to match(%r{#{admin_selected_option_tags}})
108
+ expect(helper.switch_user_select).to match(/#{guest_option_tags}/)
109
+ expect(helper.switch_user_select).to match(/#{user_option_tags}/)
110
+ expect(helper.switch_user_select).to match(/#{admin_selected_option_tags}/)
110
111
  end
111
112
 
112
- it "when current_user is admin and all_users is [guest_record, user_record]" do
113
+ it 'when current_user is admin and all_users is [guest_record, user_record]' do
113
114
  provider.instance_variable_set(:@user, admin)
114
115
  allow(helper).to receive(:provider).and_return(provider)
115
116
 
@@ -117,29 +118,29 @@ RSpec.describe SwitchUserHelper, type: :helper do
117
118
 
118
119
  allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
119
120
 
120
- expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
121
- expect(helper.switch_user_select).to match(%r{#{user_option_tags}})
122
- expect(helper.switch_user_select).to_not match(%r{#{admin_option_tags}})
121
+ expect(helper.switch_user_select).to match(/#{guest_option_tags}/)
122
+ expect(helper.switch_user_select).to match(/#{user_option_tags}/)
123
+ expect(helper.switch_user_select).to_not match(/#{admin_option_tags}/)
123
124
  end
124
125
  end
125
126
 
126
- describe "#user_tag_value" do
127
- it "for user" do
127
+ describe '#user_tag_value' do
128
+ it 'for user' do
128
129
  user = double(:user, id: 1)
129
130
 
130
131
  expect(helper.send(:user_tag_value, user, :id, :user)).to eq('user_1')
131
132
  end
132
133
  end
133
134
 
134
- describe "#user_tag_label" do
135
- it "when name has call method" do
135
+ describe '#user_tag_label' do
136
+ it 'when name has call method' do
136
137
  user = double(:user)
137
- name = ->(user) { 'user1' }
138
+ name = ->(_user) { 'user1' }
138
139
 
139
140
  expect(helper.send(:user_tag_label, user, name)).to eq('user1')
140
141
  end
141
142
 
142
- it "when name not has call method" do
143
+ it 'when name not has call method' do
143
144
  user = double(:name, name: 'user1')
144
145
  name = :name
145
146
 
@@ -147,27 +148,25 @@ RSpec.describe SwitchUserHelper, type: :helper do
147
148
  end
148
149
  end
149
150
 
150
- describe "#available?" do
151
- it "return true" do
151
+ describe '#available?' do
152
+ it 'return true' do
152
153
  allow_any_instance_of(SwitchUser.guard_class).to receive(:view_available?).and_return(true)
153
154
 
154
155
  expect(helper.send(:available?)).to eq(true)
155
156
  end
156
157
 
157
- it "return false" do
158
+ it 'return false' do
158
159
  allow_any_instance_of(SwitchUser.guard_class).to receive(:view_available?).and_return(false)
159
160
 
160
161
  expect(helper.send(:available?)).to eq(false)
161
162
  end
162
163
  end
163
164
 
164
- describe "#provider" do
165
- it "normal" do
165
+ describe '#provider' do
166
+ it 'normal' do
166
167
  allow(SwitchUser::Provider).to receive(:init).with(controller).and_return(provider)
167
168
 
168
169
  expect(helper.send(:provider)).to eq(provider)
169
170
  end
170
171
  end
171
-
172
-
173
172
  end