ruby-recaptcha 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -171,6 +171,13 @@ module ReCaptcha
171
171
  </noscript>
172
172
  }
173
173
  end
174
+ def add_base_error(msg, errors)
175
+ if errors.respond_to?(:add_to_base)
176
+ errors.add_to_base(msg)
177
+ else
178
+ errors.add(:base, msg)
179
+ end
180
+ end
174
181
 
175
182
  # Validate request. Note that this function actually makes a network request.
176
183
  # [remoteip] request remote ip address
@@ -179,19 +186,22 @@ module ReCaptcha
179
186
  # [errors] errors hash-likethingy (usually from ActiveRecord::Base.errors)
180
187
  def validate(remoteip, challenge, response, errors, msg)
181
188
  unless response and challenge
182
- errors.add_to_base(msg)
189
+ add_base_error(msg, errors)
183
190
  return false
184
191
  end
185
192
  proxy_host, proxy_port = nil, nil
186
193
  proxy_host, proxy_port = ENV['proxy_host'].split(':') if ENV.has_key?('proxy_host')
187
- http = Net::HTTP::Proxy(proxy_host, proxy_port).start(@vhost)
194
+ proxy_user=ENV['proxy_user']
195
+ proxy_pass=ENV['proxy_pass']
196
+ http = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).start(@vhost)
188
197
  path='/recaptcha/api/verify'
189
198
  data = "privatekey=#{CGI.escape(@privkey)}&remoteip=#{CGI.escape(remoteip)}&challenge=#{CGI.escape(challenge)}&response=#{CGI.escape(response)}"
190
199
  resp = http.post(path, data, {'Content-Type'=>'application/x-www-form-urlencoded'})
200
+ data = resp.body
191
201
  response = resp.body.split
192
202
  result = response[0].chomp
193
203
  @last_error=response[1].chomp
194
- errors.add_to_base(msg) if result != 'true'
204
+ add_base_error(msg, errors) if result != 'true'
195
205
  result == 'true'
196
206
  end
197
207
  end
@@ -1,4 +1,4 @@
1
1
  require 'recaptcha'
2
2
  module RubyRecaptcha
3
- VERSION = '1.0.4'
3
+ VERSION = '1.0.5'
4
4
  end
@@ -138,7 +138,30 @@ class TestRecaptcha < Test::Unit::TestCase
138
138
  stub_http = mock('http mock')
139
139
  stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
140
140
  stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(ResponseMock.new(['foo', goodwords_resp]))
141
- Net::HTTP.expects(:Proxy).with(nil, nil).returns(stub_proxy)
141
+ Net::HTTP.expects(:Proxy).with(nil, nil, nil, nil).returns(stub_proxy)
142
+ client = new_client
143
+ assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
144
+ end
145
+ def test_validate_good_authenticated_proxy
146
+ ENV['proxy_host']='fubar:8080'
147
+ ENV['proxy_user']='user'
148
+ ENV['proxy_pass']='secret'
149
+ goodwords_resp="true\r\nsuccess"
150
+ err_stub=mock()
151
+ stub_proxy=mock('proxy')
152
+ stub_http = mock('http mock')
153
+ stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
154
+ stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(ResponseMock.new(['foo', goodwords_resp]))
155
+ Net::HTTP.expects(:Proxy).with('fubar', '8080','user','secret').returns(stub_proxy)
156
+ client = new_client
157
+ assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
158
+ ENV['proxy_host']='fubar'
159
+ err_stub=mock()
160
+ stub_proxy=mock('proxy')
161
+ stub_http = mock('http mock')
162
+ stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
163
+ stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(ResponseMock.new(['foo', goodwords_resp]))
164
+ Net::HTTP.expects(:Proxy).with('fubar', nil, 'user', 'secret').returns(stub_proxy)
142
165
  client = new_client
143
166
  assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
144
167
  end
@@ -150,7 +173,7 @@ class TestRecaptcha < Test::Unit::TestCase
150
173
  stub_http = mock('http mock')
151
174
  stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
152
175
  stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(ResponseMock.new(['foo', goodwords_resp]))
153
- Net::HTTP.expects(:Proxy).with('fubar', '8080').returns(stub_proxy)
176
+ Net::HTTP.expects(:Proxy).with('fubar', '8080',nil,nil).returns(stub_proxy)
154
177
  client = new_client
155
178
  assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
156
179
  ENV['proxy_host']='fubar'
@@ -159,7 +182,7 @@ class TestRecaptcha < Test::Unit::TestCase
159
182
  stub_http = mock('http mock')
160
183
  stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
161
184
  stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(ResponseMock.new(['foo', goodwords_resp]))
162
- Net::HTTP.expects(:Proxy).with('fubar', nil).returns(stub_proxy)
185
+ Net::HTTP.expects(:Proxy).with('fubar', nil, nil, nil).returns(stub_proxy)
163
186
  client = new_client
164
187
  assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
165
188
  end
@@ -306,6 +329,16 @@ class TestRecaptcha < Test::Unit::TestCase
306
329
  mock.expects(:validate).with('0.0.0.0', nil, nil, e, 'Captcha failed.').returns(true)
307
330
  assert @cf.validate_recap({}, e, {:rcc_pub => 'foobar', :rcc_priv => 'blegga'})
308
331
  end
332
+ def test_safe_errors
333
+ r31err = mock('rails 31 error')
334
+ msg = "hi"
335
+ r31err.expects(:add).with(:base, msg)
336
+ ReCaptcha::Client.new(nil, nil).add_base_error(msg, r31err)
337
+ r21err = mock('rails sub3 error')
338
+ r21err.expects(:respond_to?).with(:add_to_base).returns(true)
339
+ r21err.expects(:add_to_base).with(msg)
340
+ ReCaptcha::Client.new(nil, nil).add_base_error(msg, r21err)
341
+ end
309
342
  def test_validate_recap_is_correct_with_constants_and_with_options_and_non_default_msg
310
343
  # first, with constants
311
344
  e = mock('errors')
@@ -349,6 +382,12 @@ class TestRecaptcha < Test::Unit::TestCase
349
382
  assert_equal(((expected % ['foobar', 'foobar']).strip), actual.strip)
350
383
  end
351
384
 
385
+ def teardown
386
+ ENV['proxy_user']=nil
387
+ ENV['proxy_pass']=nil
388
+ ENV['proxy_host']=nil
389
+ end
390
+
352
391
  private
353
392
 
354
393
  def new_client(pubkey='abc', privkey='def', ssl=false)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-recaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: hoe
17
- requirement: &9701860 !ruby/object:Gem::Requirement
17
+ requirement: &7491720 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 2.5.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *9701860
25
+ version_requirements: *7491720
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: hoe
28
- requirement: &9700580 !ruby/object:Gem::Requirement
28
+ requirement: &7469900 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '2.12'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *9700580
36
+ version_requirements: *7469900
37
37
  description: ''
38
38
  email:
39
39
  - m@loonsoft.com