rack-webconsole-pry 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'ostruct'
4
+
5
+ module Rack
6
+ describe Webconsole::Assets do
7
+
8
+ it 'initializes with an app' do
9
+ @app = stub
10
+ @assets = Webconsole::Assets.new(@app)
11
+ @assets.instance_variable_get(:@app).must_equal @app
12
+ end
13
+
14
+ describe "#code" do
15
+ it 'injects the token and key_code' do
16
+ Webconsole::Repl.stubs(:token).returns('fake_generated_token')
17
+ Webconsole.key_code = "96"
18
+
19
+ @assets = Webconsole::Assets.new(nil)
20
+ assets_code = @assets.code
21
+
22
+ assets_code.must_match /fake_generated_token/
23
+ assets_code.must_match /event\.which == 96/
24
+ end
25
+ end
26
+
27
+ describe "#call" do
28
+
29
+ describe 'when the call is not appropriate to inject the view code' do
30
+ # Different invalid cases
31
+ [
32
+ [200, {'Content-Type' => 'text/html'}, ['Whatever']],
33
+ [200, {'Content-Type' => 'text/plain'}, ['Hello World']],
34
+ [404, {'Content-Type' => 'text/html'}, ['Hello World']],
35
+ [404, {'Content-Type' => 'text/html'}, ['Hello, World']],
36
+
37
+ ].each do |invalid_response|
38
+ it 'passes the call untouched' do
39
+ @app = lambda { |env| invalid_response }
40
+
41
+ assets = Webconsole::Assets.new(@app)
42
+ assets.expects(:inject_code).never
43
+
44
+ assets.call({}).last.first.must_equal invalid_response.last.first
45
+ end
46
+ end
47
+ end
48
+
49
+ describe 'otherwise' do
50
+
51
+ it 'injects the view code before the body ending' do
52
+
53
+ valid_html = "<!DOCTYPE html>\n<html>\n<head>\n <title>Testapp</title>\n <link href=\"/assets/application.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n <script src=\"/assets/application.js\" type=\"text/javascript\"></script>\n <meta content=\"authenticity_token\" name=\"csrf-param\" />\n<meta content=\"26Ls63zdKBiCXoqU5CuG6KqVbeMYydRqOuovP+DXx8g=\" name=\"csrf-token\" />\n</head>\n<body>\n\n<h1> Hello bitches </h1>\n\n<p> Lorem ipsum dolor sit amet. </p>\n\n\n</body>\n</html>\n"
54
+
55
+ html = [valid_html]
56
+
57
+ @app = lambda { |env| [200, {'Content-Type' => 'text/html'}, html] }
58
+
59
+ assets = Webconsole::Assets.new(@app)
60
+ response = assets.call({}).last.first
61
+
62
+ response.must_match /input name/m # html
63
+ response.must_match /text\/css/m # css
64
+ response.must_match /escapeHTML/m # js
65
+ end
66
+
67
+ it "works with Rails' particular conception of what a response is" do
68
+
69
+ valid_html = "<!DOCTYPE html>\n<html>\n<head>\n <title>Testapp</title>\n <link href=\"/assets/application.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n <script src=\"/assets/application.js\" type=\"text/javascript\"></script>\n <meta content=\"authenticity_token\" name=\"csrf-param\" />\n<meta content=\"26Ls63zdKBiCXoqU5CuG6KqVbeMYydRqOuovP+DXx8g=\" name=\"csrf-token\" />\n</head>\n<body>\n\n<h1> Hello bitches </h1>\n\n<p> Lorem ipsum dolor sit amet. </p>\n\n\n</body>\n</html>\n"
70
+
71
+ @app = lambda { |env| [200, {'Content-Type' => 'text/html'}, OpenStruct.new({:body => valid_html})] }
72
+
73
+ assets = Webconsole::Assets.new(@app)
74
+
75
+ response = assets.call({}).last.first
76
+
77
+ response.must_match /input name/m # html
78
+ response.must_match /text\/css/m # css
79
+ response.must_match /escapeHTML/m # js
80
+ end
81
+
82
+ it 'exposes the request object to the console' do
83
+ valid_html = "<!DOCTYPE html>\n<html>\n<head>\n <title>Testapp</title>\n <link href=\"/assets/application.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n <script src=\"/assets/application.js\" type=\"text/javascript\"></script>\n <meta content=\"authenticity_token\" name=\"csrf-param\" />\n<meta content=\"26Ls63zdKBiCXoqU5CuG6KqVbeMYydRqOuovP+DXx8g=\" name=\"csrf-token\" />\n</head>\n<body>\n\n<h1> Hello bitches </h1>\n\n<p> Lorem ipsum dolor sit amet. </p>\n\n\n</body>\n</html>\n"
84
+
85
+ @app = lambda { |env| [200, {'Content-Type' => 'text/html'}, OpenStruct.new({:body => valid_html})] }
86
+
87
+ env = {'PATH_INFO' => '/some_path'}
88
+ assets = Webconsole::Assets.new(@app)
89
+
90
+ assets.call(env)
91
+
92
+ Webconsole::Repl.request.env['PATH_INFO'].must_equal '/some_path'
93
+ end
94
+
95
+ end
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ module Rack
5
+ describe Webconsole::Repl do
6
+
7
+ it 'initializes with an app' do
8
+ @app = stub
9
+ @repl = Webconsole::Repl.new(@app)
10
+
11
+ @repl.instance_variable_get(:@app).must_equal @app
12
+ end
13
+
14
+ describe "#call" do
15
+ it 'evaluates the :query param in a sandbox and returns the result' do
16
+ @app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['hello world']] }
17
+ env = {}
18
+ Webconsole::Repl.stubs(:token).returns('abc')
19
+ request = OpenStruct.new(:params => {'query' => 'a = 4; a * 2', 'token' => 'abc'}, :post? => true)
20
+ Rack::Request.stubs(:new).returns request
21
+
22
+ @repl = Webconsole::Repl.new(@app)
23
+
24
+ response = @repl.call(env).last.first
25
+
26
+ MultiJson.decode(response)['result'].must_include "8" # include because of coloring characters
27
+ end
28
+
29
+ it 'maintains local state in subsequent calls thanks to an evil global variable' do
30
+ @app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['hello world']] }
31
+ env = {}
32
+ Webconsole::Repl.stubs(:token).returns('abc')
33
+ request = OpenStruct.new(:params => {'query' => 'a = 4', 'token' => 'abc'}, :post? => true)
34
+ Rack::Request.stubs(:new).returns request
35
+ @repl = Webconsole::Repl.new(@app)
36
+
37
+ @repl.call(env) # call 1 sets a to 4
38
+
39
+ request = OpenStruct.new(:params => {'query' => 'a * 8', 'token' => 'abc'}, :post? => true)
40
+ Rack::Request.stubs(:new).returns request
41
+
42
+ response = @repl.call(env).last.first # call 2 retrieves a and multiplies it by 8
43
+
44
+ MultiJson.decode(response)['result'].must_include "32" # include because of coloring characters
45
+ end
46
+
47
+ it "returns any found errors prepended with 'Error:'" do
48
+ @app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['hello world']] }
49
+ env = {}
50
+ Webconsole::Repl.stubs(:token).returns('abc')
51
+ request = OpenStruct.new(:params => {'query' => 'unknown_method', 'token' => 'abc'}, :post? => true)
52
+ Rack::Request.stubs(:new).returns request
53
+ @repl = Webconsole::Repl.new(@app)
54
+
55
+ response = @repl.call(env).last.first
56
+
57
+ MultiJson.decode(response)['result'].must_match /Error:/
58
+ end
59
+
60
+ it 'rejects non-post requests' do
61
+ @app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['hello world']] }
62
+ env = {}
63
+ Webconsole::Repl.stubs(:token).returns('abc')
64
+ request = OpenStruct.new(:params => {'query' => 'unknown_method', 'token' => 'abc'}, :post? => false)
65
+ Rack::Request.stubs(:new).returns request
66
+ @repl = Webconsole::Repl.new(@app)
67
+
68
+ $sandbox.expects(:instance_eval).never
69
+
70
+ @repl.call(env).must_equal @app.call(env)
71
+ end
72
+
73
+ it 'rejects requests with invalid token' do
74
+ @app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['hello world']] }
75
+ env = {}
76
+ Webconsole::Repl.stubs(:token).returns('abc')
77
+ request = OpenStruct.new(:params => {'query' => 'unknown_method', 'token' => 'cba'}, :post? => true)
78
+ Rack::Request.stubs(:new).returns request
79
+ @repl = Webconsole::Repl.new(@app)
80
+
81
+ $sandbox.expects(:instance_eval).never
82
+
83
+ @repl.call(env).must_equal @app.call(env)
84
+ end
85
+ end
86
+
87
+ describe 'class methods' do
88
+ describe '#reset_token and #token' do
89
+ it 'returns the security token' do
90
+ Webconsole::Repl.reset_token
91
+ Webconsole::Repl.token.must_be_kind_of String
92
+ end
93
+ end
94
+ describe '#request= and #request' do
95
+ it 'returns the request object' do
96
+ request = stub
97
+ Webconsole::Repl.request = request
98
+ Webconsole::Repl.request.must_equal request
99
+ end
100
+ end
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module Rack
4
+ describe Webconsole do
5
+
6
+ it 'initializes with an app' do
7
+ @app = stub
8
+ @webconsole = Webconsole.new(@app)
9
+
10
+ @webconsole.instance_variable_get(:@app).must_equal @app
11
+ end
12
+
13
+ describe "#call" do
14
+ it 'delegates the call to the Repl middleware when the path is /webconsole' do
15
+ @app = stub
16
+ @webconsole = Webconsole.new(@app)
17
+ @env = {'PATH_INFO' => '/webconsole'}
18
+
19
+ repl = stub
20
+ Webconsole::Repl.expects(:new).with(@app).returns repl
21
+ repl.expects(:call).with @env
22
+
23
+ @webconsole.call(@env)
24
+ end
25
+
26
+ it 'passes the call to the Assets middleware otherwise' do
27
+ @app = stub
28
+ @webconsole = Webconsole.new(@app)
29
+ @env = {'PATH_INFO' => '/whatever'}
30
+
31
+ assets = stub
32
+ Webconsole::Assets.expects(:new).with(@app).returns assets
33
+ assets.expects(:call).with @env
34
+
35
+ @webconsole.call(@env)
36
+ end
37
+ end
38
+
39
+ describe 'class methods' do
40
+ it '#inject_jquery accessors' do
41
+ Webconsole.inject_jquery.must_equal false
42
+ Webconsole.inject_jquery = true
43
+ Webconsole.inject_jquery.must_equal true
44
+ end
45
+ it '#key_code accessors' do
46
+ Webconsole.key_code.must_equal "96"
47
+ Webconsole.key_code = "97"
48
+ Webconsole.key_code.must_equal "97"
49
+ end
50
+ it '#key_code setter cast parameter type' do
51
+ Webconsole.key_code = 96
52
+ Webconsole.key_code.must_equal "96"
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+
3
+ gem 'minitest'
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+ require 'purdytest'
7
+ require 'mocha'
8
+
9
+ require 'rack-webconsole'
10
+
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-webconsole-pry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josep M. Bach
9
+ - Josep Jaume Rey
10
+ - Oriol Gual
11
+ - Jan Berdajs
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2012-06-10 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rack
19
+ requirement: !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: multi_json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.3
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 1.0.3
49
+ - !ruby/object:Gem::Dependency
50
+ name: pry
51
+ requirement: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ - !ruby/object:Gem::Dependency
66
+ name: minitest
67
+ requirement: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: purdytest
83
+ requirement: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: bluecloth
115
+ requirement: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ - !ruby/object:Gem::Dependency
130
+ name: rake
131
+ requirement: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ - !ruby/object:Gem::Dependency
146
+ name: pry
147
+ requirement: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ type: :development
154
+ prerelease: false
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ description: Rack-based console inside your web applications, using pry
162
+ email:
163
+ - mrbrdo@gmail.com
164
+ executables: []
165
+ extensions: []
166
+ extra_rdoc_files: []
167
+ files:
168
+ - .gitignore
169
+ - .travis.yml
170
+ - Gemfile
171
+ - Gemfile.lock
172
+ - History
173
+ - Rakefile
174
+ - Readme.md
175
+ - lib/rack-webconsole.rb
176
+ - lib/rack/webconsole.rb
177
+ - lib/rack/webconsole/asset_helpers.rb
178
+ - lib/rack/webconsole/assets.rb
179
+ - lib/rack/webconsole/railtie.rb
180
+ - lib/rack/webconsole/repl.rb
181
+ - lib/rack/webconsole/version.rb
182
+ - public/jquery.html
183
+ - public/webconsole.css
184
+ - public/webconsole.html
185
+ - public/webconsole.js
186
+ - rack-webconsole-pry.gemspec
187
+ - spec/rack/webconsole/asset_helpers_spec.rb
188
+ - spec/rack/webconsole/assets_spec.rb
189
+ - spec/rack/webconsole/repl_spec.rb
190
+ - spec/rack/webconsole_spec.rb
191
+ - spec/spec_helper.rb
192
+ homepage: http://github.com/mrbrdo/rack-webconsole
193
+ licenses: []
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ none: false
200
+ requirements:
201
+ - - ! '>='
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ segments:
205
+ - 0
206
+ hash: -636117117429631465
207
+ required_rubygems_version: !ruby/object:Gem::Requirement
208
+ none: false
209
+ requirements:
210
+ - - ! '>='
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ segments:
214
+ - 0
215
+ hash: -636117117429631465
216
+ requirements: []
217
+ rubyforge_project: rack-webconsole-pry
218
+ rubygems_version: 1.8.24
219
+ signing_key:
220
+ specification_version: 3
221
+ summary: Rack-based console inside your web applications, using pry
222
+ test_files:
223
+ - spec/rack/webconsole/asset_helpers_spec.rb
224
+ - spec/rack/webconsole/assets_spec.rb
225
+ - spec/rack/webconsole/repl_spec.rb
226
+ - spec/rack/webconsole_spec.rb
227
+ - spec/spec_helper.rb
228
+ has_rdoc: