switch_user 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/CHANGELOG.md +5 -0
- data/README.md +34 -1
- data/app/controllers/switch_user_controller.rb +1 -1
- data/app/helpers/switch_user_helper.rb +4 -2
- data/app/views/switch_user/_widget.html.erb +1 -1
- data/lib/switch_user/rspec/feature_helpers.rb +44 -0
- data/lib/switch_user/rspec.rb +13 -0
- data/lib/switch_user/version.rb +1 -1
- data/spec/integration/switch_user_spec.rb +1 -1
- data/spec/rspec/feature_helpers_spec.rb +127 -0
- data/spec/support/application.rb +14 -2
- data/switch_user.gemspec +1 -0
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b175adec75aec167e65b5c1269743f536dbdeb7
|
4
|
+
data.tar.gz: 208fbd14a66917e00d9f9bcac28776bff5b92684
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c40a13d365afe03d78be753805be4b30f54a9a30813a11771b68f05046c0a808fe2cf36f87dbf58deb9e483b73bd4d0709f203c2ea7542656232fb36f46c5b6
|
7
|
+
data.tar.gz: 132785e64d55ba85b06b80a3677177268ae4f0773d27c84a119f31eb76cb8faae1bd13b88af4faa81e559d94084d3ec26519d5526213a39b5f22e626e8effe56
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -36,6 +36,12 @@ or haml
|
|
36
36
|
|
37
37
|
= switch_user_select
|
38
38
|
|
39
|
+
If you want to add a class or styles
|
40
|
+
|
41
|
+
<%= switch_user_select class: 'special-select', styles: 'width: 220px' %>
|
42
|
+
|
43
|
+
= switch_user_select class: 'special-select', styles: 'width: 220px'
|
44
|
+
|
39
45
|
If there are too many users (on production), the switch_user_select is not a good choice, you should call the switch user request by yourself.
|
40
46
|
|
41
47
|
<%= link_to user.login, "/switch_user?scope_identifier=user_#{user.id}" %>
|
@@ -47,7 +53,8 @@ If there are too many users (on production), the switch_user_select is not a goo
|
|
47
53
|
If you have a wildcard route in your project, add a route before the wildcard route.
|
48
54
|
```ruby
|
49
55
|
# config/routes.rb
|
50
|
-
get 'switch_user'
|
56
|
+
get 'switch_user', to: 'switch_user#set_current_user'
|
57
|
+
get 'switch_user/remember_user', to: 'switch_user#remember_user'
|
51
58
|
# wildcard route that will get
|
52
59
|
get ':id' => 'pages#show'
|
53
60
|
```
|
@@ -137,6 +144,32 @@ config.controller_guard = lambda { |current_user, request, original_user|
|
|
137
144
|
```
|
138
145
|
This example would allow an admin user to user switch_user, but would only let you switch back to another user if the original user was a super admin.
|
139
146
|
|
147
|
+
## Using SwitchUser with RSpec and Capybara
|
148
|
+
|
149
|
+
Add the following code to spec/support/switch_user.rb or spec/spec_helper.rb :
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
require 'switch_user/rspec'
|
153
|
+
```
|
154
|
+
|
155
|
+
You can now write your specs like so :
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
feature "Your feature", type: :feature do
|
159
|
+
background do
|
160
|
+
@user = User.make(:email => 'user@example.com', :password => 'password')
|
161
|
+
end
|
162
|
+
|
163
|
+
scenario "Your scenario" do
|
164
|
+
switch_user @user
|
165
|
+
# or
|
166
|
+
# switch_user :user, @user.id
|
167
|
+
|
168
|
+
visit '/'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
```
|
172
|
+
|
140
173
|
### How it works
|
141
174
|
|
142
175
|
Click the checkbox next to switch_user_select menu to remember that user for this session. Once this
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module SwitchUserHelper
|
2
2
|
SelectOption = Struct.new(:label, :scope_id)
|
3
|
-
def switch_user_select
|
3
|
+
def switch_user_select(options = {})
|
4
4
|
return unless available?
|
5
5
|
|
6
6
|
if provider.current_user
|
@@ -21,7 +21,9 @@ module SwitchUserHelper
|
|
21
21
|
|
22
22
|
render :partial => "switch_user/widget",
|
23
23
|
:locals => {
|
24
|
-
:option_tags => option_tags
|
24
|
+
:option_tags => option_tags,
|
25
|
+
:classes => options[:class],
|
26
|
+
:styles => options[:style],
|
25
27
|
}
|
26
28
|
end
|
27
29
|
|
@@ -1,4 +1,4 @@
|
|
1
1
|
<% if SwitchUser.switch_back %>
|
2
2
|
<%= check_box_tag "remember_user", "remember_user", provider.original_user.present?, :onchange => "location.href = '/switch_user/remember_user?remember=' + encodeURIComponent(this.checked)" %>
|
3
3
|
<% end %>
|
4
|
-
<%= select_tag "switch_user_identifier", option_tags, :onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)" %>
|
4
|
+
<%= select_tag "switch_user_identifier", option_tags, :onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)", :class => classes, :style => styles %>
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module SwitchUser
|
2
|
+
module RSpecFeatureHelpers
|
3
|
+
|
4
|
+
class InvalidArgument < StandardError; end
|
5
|
+
|
6
|
+
def switch_user(user_record_or_scope, user_id=nil)
|
7
|
+
_user_scope =
|
8
|
+
case user_record_or_scope
|
9
|
+
when ActiveRecord::Base
|
10
|
+
user_record_or_scope.model_name.singular
|
11
|
+
else
|
12
|
+
user_record_or_scope
|
13
|
+
end
|
14
|
+
|
15
|
+
_user_scope = _user_scope.to_s
|
16
|
+
|
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")
|
19
|
+
end
|
20
|
+
|
21
|
+
_user_id =
|
22
|
+
case user_record_or_scope
|
23
|
+
when ActiveRecord::Base
|
24
|
+
identifier = SwitchUser.available_users_identifiers[_user_scope] || SwitchUser.available_users_identifiers[_user_scope.to_sym]
|
25
|
+
if identifier.nil?
|
26
|
+
raise SwitchUser::InvalidScope.new("don't allow switch this user, please check config.available_users_identifiers")
|
27
|
+
end
|
28
|
+
user_record_or_scope.send identifier
|
29
|
+
else
|
30
|
+
user_id
|
31
|
+
end
|
32
|
+
|
33
|
+
if _user_id.to_s.empty?
|
34
|
+
raise InvalidArgument.new("don't allow switch this user, user_id is empty")
|
35
|
+
end
|
36
|
+
|
37
|
+
scope_identifier = "#{_user_scope}_#{_user_id}"
|
38
|
+
|
39
|
+
visit "/switch_user?scope_identifier=#{scope_identifier}"
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'switch_user/rspec/feature_helpers'
|
2
|
+
|
3
|
+
require 'rspec/core'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
|
7
|
+
config.include SwitchUser::RSpecFeatureHelpers, :type => :feature
|
8
|
+
|
9
|
+
config.before(:each, :type => :feature) do
|
10
|
+
allow_any_instance_of(SwitchUserController).to receive(:available?).and_return(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
data/lib/switch_user/version.rb
CHANGED
@@ -8,7 +8,7 @@ RSpec.describe "Using SwitchUser", :type => :request do
|
|
8
8
|
SwitchUser.reset_config
|
9
9
|
SwitchUser.provider = :session
|
10
10
|
SwitchUser.controller_guard = lambda { |current_user, request| Rails.env.test? }
|
11
|
-
SwitchUser.redirect_path = lambda {|_,_| "/
|
11
|
+
SwitchUser.redirect_path = lambda {|_,_| "/dummy/open"}
|
12
12
|
end
|
13
13
|
|
14
14
|
it "signs a user in using switch_user" do
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'capybara/rspec'
|
4
|
+
require 'capybara/rails'
|
5
|
+
Capybara.app = MyApp::Application
|
6
|
+
|
7
|
+
require 'switch_user/rspec'
|
8
|
+
|
9
|
+
RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
|
10
|
+
background do
|
11
|
+
@user = User.create!(:email => "foo@bar.com", :admin => true)
|
12
|
+
@client = Client.create!(:email => "foo@bar.com")
|
13
|
+
end
|
14
|
+
|
15
|
+
before(:each) do
|
16
|
+
allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| true})
|
17
|
+
|
18
|
+
allow(SwitchUser).to receive(:available_users).and_return({:user => lambda { User.all }})
|
19
|
+
|
20
|
+
allow(SwitchUser).to receive(:available_users_identifiers).and_return({:user => :id})
|
21
|
+
|
22
|
+
allow(SwitchUser).to receive(:available_users_names).and_return({:user => :email})
|
23
|
+
end
|
24
|
+
|
25
|
+
scenario "when controller_guard return false" do
|
26
|
+
allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| false})
|
27
|
+
|
28
|
+
expect do
|
29
|
+
switch_user @user
|
30
|
+
end.to_not raise_error ActionController::RoutingError, /Do not try to hack us/
|
31
|
+
end
|
32
|
+
|
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
|
+
|
36
|
+
allow_any_instance_of(SwitchUserController).to receive(:available?).and_call_original
|
37
|
+
|
38
|
+
expect do
|
39
|
+
switch_user @user
|
40
|
+
end.to raise_error ActionController::RoutingError, /Do not try to hack us/
|
41
|
+
end
|
42
|
+
|
43
|
+
scenario "arg is @user, available_users is default, and available_users_identifiers is default" do
|
44
|
+
expect do
|
45
|
+
switch_user @user
|
46
|
+
end.to_not raise_error
|
47
|
+
end
|
48
|
+
|
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
|
+
|
52
|
+
expect do
|
53
|
+
switch_user @user
|
54
|
+
end.to_not raise_error
|
55
|
+
end
|
56
|
+
|
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})
|
60
|
+
|
61
|
+
expect do
|
62
|
+
switch_user @user
|
63
|
+
end.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/
|
64
|
+
end
|
65
|
+
|
66
|
+
scenario "arg is @client, available_users is default, and available_users_identifiers is default" do
|
67
|
+
expect do
|
68
|
+
switch_user @client
|
69
|
+
end.to raise_error SwitchUser::InvalidScope, /config.available_users/
|
70
|
+
end
|
71
|
+
|
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
|
+
|
75
|
+
expect do
|
76
|
+
switch_user @client
|
77
|
+
end.to raise_error SwitchUser::InvalidScope, /config.available_users_identifiers/
|
78
|
+
end
|
79
|
+
|
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
|
+
|
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
|
+
|
86
|
+
expect do
|
87
|
+
switch_user @client
|
88
|
+
end.to_not raise_error
|
89
|
+
end
|
90
|
+
|
91
|
+
scenario "args is :user and @user.id, available_users is default, and available_users_identifiers is default" do
|
92
|
+
expect do
|
93
|
+
switch_user :user, @user.id
|
94
|
+
end.to_not raise_error
|
95
|
+
end
|
96
|
+
|
97
|
+
scenario "arg is :client and @client.id, available_users is default, and available_users_identifiers is default" do
|
98
|
+
expect do
|
99
|
+
switch_user :client, @client.id
|
100
|
+
end.to raise_error SwitchUser::InvalidScope, /config.available_users/
|
101
|
+
end
|
102
|
+
|
103
|
+
scenario "args is :user, available_users is default, and available_users_identifiers is default" do
|
104
|
+
expect do
|
105
|
+
switch_user :user
|
106
|
+
end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/
|
107
|
+
end
|
108
|
+
|
109
|
+
scenario "args is :user and nil, available_users is default, and available_users_identifiers is default" do
|
110
|
+
expect do
|
111
|
+
switch_user :user, nil
|
112
|
+
end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/
|
113
|
+
end
|
114
|
+
|
115
|
+
scenario "args is :user and '', available_users is default, and available_users_identifiers is default" do
|
116
|
+
expect do
|
117
|
+
switch_user :user, ''
|
118
|
+
end.to raise_error SwitchUser::RSpecFeatureHelpers::InvalidArgument, /user_id is empty/
|
119
|
+
end
|
120
|
+
|
121
|
+
scenario "args is :user and 0, available_users is default, and available_users_identifiers is default" do
|
122
|
+
expect do
|
123
|
+
switch_user :user, 0
|
124
|
+
end.to_not raise_error
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
data/spec/support/application.rb
CHANGED
@@ -5,7 +5,7 @@ require 'switch_user/rails'
|
|
5
5
|
|
6
6
|
class ApplicationController < ActionController::Base
|
7
7
|
def require_user
|
8
|
-
current_user || redirect_to("/
|
8
|
+
current_user || redirect_to("/dummy/open")
|
9
9
|
end
|
10
10
|
|
11
11
|
def current_user
|
@@ -16,11 +16,13 @@ class ApplicationController < ActionController::Base
|
|
16
16
|
user = User.find(params[:id])
|
17
17
|
session[SwitchUser.session_key] = user.id
|
18
18
|
|
19
|
-
redirect_to("/
|
19
|
+
redirect_to("/dummy/protected")
|
20
20
|
end
|
21
21
|
|
22
22
|
def logout
|
23
23
|
session[SwitchUser.session_key] = nil
|
24
|
+
|
25
|
+
redirect_to("/dummy/open")
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
@@ -46,6 +48,9 @@ module MyApp
|
|
46
48
|
config.secret_key_base = "abc123"
|
47
49
|
config.eager_load = true
|
48
50
|
config.secret_token = '153572e559247c7aedd1bca5a246874d'
|
51
|
+
|
52
|
+
# should set it
|
53
|
+
config.action_dispatch.show_exceptions = false
|
49
54
|
end
|
50
55
|
end
|
51
56
|
Rails.application.initialize!
|
@@ -67,3 +72,10 @@ end
|
|
67
72
|
|
68
73
|
class User < ActiveRecord::Base
|
69
74
|
end
|
75
|
+
|
76
|
+
connection.create_table :clients do |t|
|
77
|
+
t.column :email, :string
|
78
|
+
end
|
79
|
+
|
80
|
+
class Client < ActiveRecord::Base
|
81
|
+
end
|
data/switch_user.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.add_development_dependency "activerecord"
|
26
26
|
s.add_development_dependency "awesome_print"
|
27
27
|
s.add_development_dependency "pry"
|
28
|
+
s.add_development_dependency "capybara"
|
28
29
|
|
29
30
|
s.files = `git ls-files`.split("\n")
|
30
31
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switch_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-07-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -165,6 +165,20 @@ dependencies:
|
|
165
165
|
- - ">="
|
166
166
|
- !ruby/object:Gem::Version
|
167
167
|
version: '0'
|
168
|
+
- !ruby/object:Gem::Dependency
|
169
|
+
name: capybara
|
170
|
+
requirement: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
type: :development
|
176
|
+
prerelease: false
|
177
|
+
version_requirements: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
168
182
|
description: Easily switch current user to speed up development
|
169
183
|
email:
|
170
184
|
- flyerhzm@gmail.com
|
@@ -202,6 +216,8 @@ files:
|
|
202
216
|
- lib/switch_user/provider/session.rb
|
203
217
|
- lib/switch_user/provider/sorcery.rb
|
204
218
|
- lib/switch_user/rails.rb
|
219
|
+
- lib/switch_user/rspec.rb
|
220
|
+
- lib/switch_user/rspec/feature_helpers.rb
|
205
221
|
- lib/switch_user/user_loader.rb
|
206
222
|
- lib/switch_user/user_set.rb
|
207
223
|
- lib/switch_user/version.rb
|
@@ -216,6 +232,7 @@ files:
|
|
216
232
|
- spec/provider/session_spec.rb
|
217
233
|
- spec/provider/sorcery_spec.rb
|
218
234
|
- spec/provider_spec.rb
|
235
|
+
- spec/rspec/feature_helpers_spec.rb
|
219
236
|
- spec/spec_helper.rb
|
220
237
|
- spec/support/application.rb
|
221
238
|
- spec/support/provider.rb
|
@@ -244,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
244
261
|
version: 1.3.6
|
245
262
|
requirements: []
|
246
263
|
rubyforge_project: switch_user
|
247
|
-
rubygems_version: 2.
|
264
|
+
rubygems_version: 2.5.1
|
248
265
|
signing_key:
|
249
266
|
specification_version: 4
|
250
267
|
summary: Easily switch current user to speed up development
|