rack_session_access 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -3
- data/README.md +25 -13
- data/apps/test_rails_app.rb +2 -1
- data/lib/rack_session_access/capybara.rb +11 -0
- data/lib/rack_session_access/middleware.rb +20 -12
- data/lib/rack_session_access/version.rb +1 -1
- data/spec/middleware_spec.rb +69 -21
- metadata +6 -6
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -24,23 +24,19 @@ Add to `Gemfile`:
|
|
24
24
|
|
25
25
|
gem 'rack_session_access'
|
26
26
|
|
27
|
-
Add to
|
27
|
+
Add RackSessionAccess middleware to rails middleware stack.
|
28
|
+
Add the following in`config/environments/test.rb`:
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
[MyRailsApp]::Application.configure do
|
31
|
+
...
|
32
|
+
# Access to rack session
|
33
|
+
config.middleware.use RackSessionAccess::Middleware
|
34
|
+
...
|
33
35
|
end
|
34
36
|
|
35
|
-
*Note* Ensure you include rack_session_access middleware only for test environment
|
37
|
+
*Note* Ensure you include rack_session_access middleware only for *test* environment
|
36
38
|
otherwise you will have security issue.
|
37
39
|
|
38
|
-
If you use rspec you may prefer to inject middleware only for rspec tests:
|
39
|
-
Put into `spec/spec_helper`:
|
40
|
-
|
41
|
-
Rails.application.configure do
|
42
|
-
config.middleware.use RackSessionAccess::Middleware
|
43
|
-
end
|
44
40
|
|
45
41
|
## Using with Sinatra
|
46
42
|
|
@@ -74,7 +70,11 @@ Add to `spec/spec_helper.rb`
|
|
74
70
|
|
75
71
|
require "rack_session_access/capybara"
|
76
72
|
|
77
|
-
|
73
|
+
Use:
|
74
|
+
|
75
|
+
* `page.set_rack_session` to set your desired session data
|
76
|
+
* `page.get_rack_session` to obtain your application session data
|
77
|
+
* `page.get_rack_session_key` to obtain certain key if your application session data
|
78
78
|
|
79
79
|
Example:
|
80
80
|
|
@@ -90,8 +90,20 @@ Example:
|
|
90
90
|
page.visit "/profile"
|
91
91
|
page.should have_content("Hi, jack@daniels.com")
|
92
92
|
end
|
93
|
+
|
94
|
+
scenario "visit landing page" do
|
95
|
+
page.visit "/landing?ref=123"
|
96
|
+
page.get_rack_session_key('ref').should == "123"
|
97
|
+
end
|
93
98
|
end
|
94
99
|
|
100
|
+
## Authlogic integration
|
101
|
+
|
102
|
+
page.set_rack_session("user_credentials" => @user.persistence_token)
|
103
|
+
|
104
|
+
## Devise integration
|
105
|
+
|
106
|
+
page.set_rack_session("warden.user.user.key" => User.serialize_into_session(@user).unshift("User"))
|
95
107
|
|
96
108
|
## Notes
|
97
109
|
|
data/apps/test_rails_app.rb
CHANGED
@@ -9,6 +9,17 @@ module RackSessionAccess
|
|
9
9
|
click_button "Update"
|
10
10
|
has_content?("Rack session data")
|
11
11
|
end
|
12
|
+
|
13
|
+
def get_rack_session
|
14
|
+
visit ::RackSessionAccess.path + '.raw'
|
15
|
+
has_content?("Raw rack session data")
|
16
|
+
raw_data = find(:xpath, '//body/pre').text
|
17
|
+
::RackSessionAccess.decode(raw_data)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_rack_session_key(key)
|
21
|
+
get_rack_session.fetch(key)
|
22
|
+
end
|
12
23
|
end
|
13
24
|
end
|
14
25
|
|
@@ -38,18 +38,26 @@ module RackSessionAccess
|
|
38
38
|
|
39
39
|
# List session data
|
40
40
|
def show(request)
|
41
|
-
#
|
42
|
-
request.env[@key].
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
41
|
+
# force load session because it can be lazy loaded
|
42
|
+
request.env[@key].delete(:rack_session_access_force_load_session)
|
43
|
+
|
44
|
+
case File.extname(request.path)
|
45
|
+
when ".raw"
|
46
|
+
render do |xml|
|
47
|
+
xml.h2 "Raw rack session data"
|
48
|
+
xml.pre RackSessionAccess.encode(request.env[@key].to_hash)
|
50
49
|
end
|
51
|
-
|
52
|
-
|
50
|
+
else
|
51
|
+
render do |xml|
|
52
|
+
xml.h2 "Rack session data"
|
53
|
+
xml.ul do |xml|
|
54
|
+
request.env[@key].each do |k,v|
|
55
|
+
xml.li("#{k.inspect} : #{v.inspect}")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
xml.p do |xml|
|
59
|
+
xml.a("Edit", :href => action_path(:edit))
|
60
|
+
end
|
53
61
|
end
|
54
62
|
end
|
55
63
|
end
|
@@ -93,7 +101,7 @@ module RackSessionAccess
|
|
93
101
|
# Dispatch action from request
|
94
102
|
def dispatch_action(request)
|
95
103
|
method = request_method(request)
|
96
|
-
path = request.path
|
104
|
+
path = request.path.sub(/\.\w+$/, '')
|
97
105
|
route = @routing.detect { |r| r[0] == method && r[1] == path }
|
98
106
|
route[2] if route
|
99
107
|
end
|
data/spec/middleware_spec.rb
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
shared_examples "common scenarios" do
|
4
|
+
let(:data) do
|
5
|
+
{
|
6
|
+
'user_email' => 'jack@daniels.com',
|
7
|
+
'user_profile' => { :age => 12 },
|
8
|
+
'role_ids' => [1, 20, 30]
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
4
12
|
scenario "changing session data" do
|
5
13
|
page.visit RackSessionAccess.edit_path
|
6
14
|
page.should have_content("Update rack session")
|
@@ -32,16 +40,43 @@ shared_examples "common scenarios" do
|
|
32
40
|
end
|
33
41
|
|
34
42
|
scenario "modify session data with set_rack_session helper" do
|
35
|
-
page.set_rack_session(
|
36
|
-
|
37
|
-
'user_profile' => { :age => 12 },
|
38
|
-
'role_ids' => [1, 20, 30]
|
39
|
-
})
|
43
|
+
page.set_rack_session(data)
|
44
|
+
|
40
45
|
page.visit(RackSessionAccess.path)
|
41
46
|
page.should have_content('"user_email" : "jack@daniels.com"')
|
42
47
|
page.should have_content('"user_profile" : {:age=>12}')
|
43
48
|
page.should have_content('"role_ids" : [1, 20, 30]')
|
44
49
|
end
|
50
|
+
|
51
|
+
scenario "accessing raw session data" do
|
52
|
+
page.set_rack_session(data)
|
53
|
+
|
54
|
+
page.visit(RackSessionAccess.path + '.raw')
|
55
|
+
raw_data = page.find(:xpath, "//body/pre").text
|
56
|
+
raw_data.should be_present
|
57
|
+
actual_data = RackSessionAccess.decode(raw_data)
|
58
|
+
actual_data.should be_kind_of(Hash)
|
59
|
+
data.each do |key, value|
|
60
|
+
actual_data[key].should == value
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
scenario "accessing raw session data using get_rack_session helper" do
|
65
|
+
page.set_rack_session(data)
|
66
|
+
|
67
|
+
actual_data = page.get_rack_session
|
68
|
+
|
69
|
+
actual_data.should be_kind_of(Hash)
|
70
|
+
data.each do |key, value|
|
71
|
+
actual_data[key].should == value
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
scenario "accessing raw session data using get_rack_session_key helper" do
|
76
|
+
page.set_rack_session(data)
|
77
|
+
|
78
|
+
page.get_rack_session_key('role_ids').should == [1, 20, 30]
|
79
|
+
end
|
45
80
|
end
|
46
81
|
|
47
82
|
shared_examples "rack scenarios" do
|
@@ -97,33 +132,46 @@ feature "manage rack session", %q(
|
|
97
132
|
So I can write faster tests
|
98
133
|
) do
|
99
134
|
|
135
|
+
context ":rack_test driver", :driver => :rack_test do
|
136
|
+
context "rack application" do
|
137
|
+
background { Capybara.app = TestRackApp }
|
138
|
+
include_examples "common scenarios"
|
139
|
+
include_examples "rack scenarios"
|
140
|
+
end
|
141
|
+
|
142
|
+
context "sinatra application" do
|
143
|
+
background { Capybara.app = TestSinatraApp }
|
144
|
+
include_examples "common scenarios"
|
145
|
+
include_examples "sinatra scenarios"
|
146
|
+
end
|
147
|
+
|
148
|
+
context "rails application" do
|
149
|
+
background { Capybara.app = TestRailsApp::Application }
|
150
|
+
include_examples "common scenarios"
|
151
|
+
include_examples "rails scenarios"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
100
155
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
Capybara.app = TestRackApp
|
106
|
-
end
|
156
|
+
|
157
|
+
context ":selenium driver", :driver => :selenium do
|
158
|
+
context "rack application" do
|
159
|
+
background { Capybara.app = TestRackApp }
|
107
160
|
include_examples "common scenarios"
|
108
161
|
include_examples "rack scenarios"
|
109
162
|
end
|
110
163
|
|
111
|
-
context "
|
112
|
-
background
|
113
|
-
Capybara.current_driver.should == driver_name
|
114
|
-
Capybara.app = TestSinatraApp
|
115
|
-
end
|
164
|
+
context "sinatra application" do
|
165
|
+
background { Capybara.app = TestSinatraApp }
|
116
166
|
include_examples "common scenarios"
|
117
167
|
include_examples "sinatra scenarios"
|
118
168
|
end
|
119
169
|
|
120
|
-
context "
|
121
|
-
background
|
122
|
-
Capybara.current_driver.should == driver_name
|
123
|
-
Capybara.app = TestRailsApp::Application
|
124
|
-
end
|
170
|
+
context "rails application" do
|
171
|
+
background { Capybara.app = TestRailsApp::Application }
|
125
172
|
include_examples "common scenarios"
|
126
173
|
include_examples "rails scenarios"
|
127
174
|
end
|
128
175
|
end
|
176
|
+
|
129
177
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack_session_access
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &17313460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *17313460
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: builder
|
27
|
-
requirement: &
|
27
|
+
requirement: &17328860 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 2.0.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *17328860
|
36
36
|
description:
|
37
37
|
email:
|
38
38
|
- andriy.yanko@gmail.com
|