ruby-recaptcha 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,3 @@
1
- === 1.0.3
2
- New history file, new Google urls
3
- === 1.0.1
4
- Lost History file
1
+ === 1.0.4 / 2011-11-12
2
+
3
+ * 1 merged patch for ruby 1.8/1.9 support
data/lib/recaptcha.rb CHANGED
@@ -68,14 +68,19 @@ module ReCaptcha
68
68
  # to determine whether the ReCaptcha challenge was completed successfully.
69
69
  # Simply include this module in your controller class
70
70
  module AppHelper
71
+ DEFAULT_CAPTCHA_FAILURE_MESSAGE = 'Captcha failed.'
72
+
71
73
  # Validate recaptcha from passed in params. Sets errors into the errors hash.
72
74
  #
73
75
  # [p] request parameters. Requires :recaptcha_challenge_field and :recaptcha_response_field
74
76
  # [errors] errors hash-like thing. Usually ActiveRecord::Base.errors
75
77
  # [options] Options hash. currently only uses :rcc_pub and :rcc_priv options for passing in ReCaptcha keys.
76
- def validate_recap(p, errors, options = {})
78
+ def validate_recap(p, errors, options = {:msg => DEFAULT_CAPTCHA_FAILURE_MESSAGE})
79
+ unless options.has_key?(:msg)
80
+ options[:msg] = DEFAULT_CAPTCHA_FAILURE_MESSAGE
81
+ end
77
82
  rcc=ReCaptcha::Client.new(options[:rcc_pub] || RCC_PUB, options[:rcc_priv] || RCC_PRIV)
78
- res = rcc.validate(request.remote_ip, p[:recaptcha_challenge_field], p[:recaptcha_response_field], errors)
83
+ res = rcc.validate(request.remote_ip, p[:recaptcha_challenge_field], p[:recaptcha_response_field], errors, options[:msg])
79
84
  session[:rcc_err]=rcc.last_error
80
85
  res
81
86
  end
@@ -154,7 +159,7 @@ module ReCaptcha
154
159
  s << "\n</script>\n"
155
160
  end
156
161
  errslug = (error.empty?||error==nil||error=="success") ? '' : "&error=#{CGI.escape(error)}"
157
- s <<<<-EOF
162
+ s << %{
158
163
  <script type="text/javascript" src="#{@proto}://#{@host}/recaptcha/api/challenge?k=#{CGI.escape(@pubkey)}#{errslug}"> </script>
159
164
  <noscript>
160
165
  <iframe src="#{@proto}://#{@host}/recaptcha/api/noscript?k=#{CGI.escape(@pubkey)}#{errslug}"
@@ -164,7 +169,7 @@ module ReCaptcha
164
169
  <input type="hidden" name="recaptcha_response_field"
165
170
  value="manual_challenge">
166
171
  </noscript>
167
- EOF
172
+ }
168
173
  end
169
174
 
170
175
  # Validate request. Note that this function actually makes a network request.
@@ -172,8 +177,7 @@ module ReCaptcha
172
177
  # [challenge] reCaptcha challenge
173
178
  # [response] reCaptcha response
174
179
  # [errors] errors hash-likethingy (usually from ActiveRecord::Base.errors)
175
- def validate(remoteip, challenge, response, errors)
176
- msg = "Captcha failed."
180
+ def validate(remoteip, challenge, response, errors, msg)
177
181
  unless response and challenge
178
182
  errors.add_to_base(msg)
179
183
  return false
@@ -183,8 +187,8 @@ module ReCaptcha
183
187
  http = Net::HTTP::Proxy(proxy_host, proxy_port).start(@vhost)
184
188
  path='/recaptcha/api/verify'
185
189
  data = "privatekey=#{CGI.escape(@privkey)}&remoteip=#{CGI.escape(remoteip)}&challenge=#{CGI.escape(challenge)}&response=#{CGI.escape(response)}"
186
- resp, data = http.post(path, data, {'Content-Type'=>'application/x-www-form-urlencoded'})
187
- response = data.split
190
+ resp = http.post(path, data, {'Content-Type'=>'application/x-www-form-urlencoded'})
191
+ response = resp.body.split
188
192
  result = response[0].chomp
189
193
  @last_error=response[1].chomp
190
194
  errors.add_to_base(msg) if result != 'true'
@@ -1,4 +1,4 @@
1
1
  require 'recaptcha'
2
2
  module RubyRecaptcha
3
- VERSION = '1.0.3'
3
+ VERSION = '1.0.4'
4
4
  end
@@ -82,8 +82,7 @@ class TestRecaptcha < Test::Unit::TestCase
82
82
  def test_nil_challenge
83
83
  client = new_client
84
84
  estub = stub_everything('errors')
85
- client.validate('abc', nil, 'foo', estub)
86
-
85
+ client.validate('abc', nil, 'foo', estub, 'Captcha failed.')
87
86
  end
88
87
 
89
88
  def test_constructor
@@ -109,6 +108,16 @@ class TestRecaptcha < Test::Unit::TestCase
109
108
  client = new_client
110
109
  assert_match(/theme \: \"white\"/, client.get_challenge('somerror', :options => {:theme => 'white', :tabindex => 10}))
111
110
  end
111
+
112
+ class ResponseMock
113
+ def initialize(response)
114
+ @response = response
115
+ end
116
+
117
+ def body
118
+ @response[1]
119
+ end
120
+ end
112
121
 
113
122
  def test_validate_fails
114
123
  badwords_resp="false\r\n360 incorrect-captcha-sol"
@@ -117,10 +126,10 @@ class TestRecaptcha < Test::Unit::TestCase
117
126
  stub_proxy=mock('proxy')
118
127
  stub_http = mock('http mock')
119
128
  stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
120
- stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(['foo', badwords_resp])
129
+ 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', badwords_resp]))
121
130
  Net::HTTP.expects(:Proxy).returns(stub_proxy)
122
131
  client = new_client
123
- assert !client.validate('localhost', 'abc', 'def', err_stub)
132
+ assert !client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
124
133
  end
125
134
  def test_validate_good
126
135
  goodwords_resp="true\r\nsuccess"
@@ -128,10 +137,10 @@ class TestRecaptcha < Test::Unit::TestCase
128
137
  stub_proxy=mock('proxy')
129
138
  stub_http = mock('http mock')
130
139
  stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
131
- stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(['foo', goodwords_resp])
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]))
132
141
  Net::HTTP.expects(:Proxy).with(nil, nil).returns(stub_proxy)
133
142
  client = new_client
134
- assert client.validate('localhost', 'abc', 'def', err_stub)
143
+ assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
135
144
  end
136
145
  def test_validate_good_proxy
137
146
  ENV['proxy_host']='fubar:8080'
@@ -140,19 +149,19 @@ class TestRecaptcha < Test::Unit::TestCase
140
149
  stub_proxy=mock('proxy')
141
150
  stub_http = mock('http mock')
142
151
  stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
143
- stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(['foo', goodwords_resp])
152
+ 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]))
144
153
  Net::HTTP.expects(:Proxy).with('fubar', '8080').returns(stub_proxy)
145
154
  client = new_client
146
- assert client.validate('localhost', 'abc', 'def', err_stub)
155
+ assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
147
156
  ENV['proxy_host']='fubar'
148
157
  err_stub=mock()
149
158
  stub_proxy=mock('proxy')
150
159
  stub_http = mock('http mock')
151
160
  stub_proxy.expects(:start).with('www.google.com').returns(stub_http)
152
- stub_http.expects(:post).with('/recaptcha/api/verify', 'privatekey=def&remoteip=localhost&challenge=abc&response=def', {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(['foo', goodwords_resp])
161
+ 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
162
  Net::HTTP.expects(:Proxy).with('fubar', nil).returns(stub_proxy)
154
163
  client = new_client
155
- assert client.validate('localhost', 'abc', 'def', err_stub)
164
+ assert client.validate('localhost', 'abc', 'def', err_stub, 'Captcha failed.')
156
165
  end
157
166
 
158
167
  #
@@ -275,7 +284,7 @@ class TestRecaptcha < Test::Unit::TestCase
275
284
  def test_validate_recap_succeeds_without_key_constants_but_with_options
276
285
  mock = mock('client')
277
286
  ReCaptcha::Client.expects(:new).returns(mock)
278
- mock.expects(:validate).with('0.0.0.0', nil, nil, {}).returns(true)
287
+ mock.expects(:validate).with('0.0.0.0', nil, nil, {}, 'Captcha failed.').returns(true)
279
288
  mock.expects(:last_error)
280
289
  assert !ReCaptcha::AppHelper.const_defined?(:RCC_PUB)
281
290
  assert !ReCaptcha::AppHelper.const_defined?(:RCC_PRIV)
@@ -286,7 +295,7 @@ class TestRecaptcha < Test::Unit::TestCase
286
295
  e = mock('errors')
287
296
  mock = mock('client')
288
297
  ReCaptcha::Client.expects(:new).returns(mock).times(2)
289
- mock.expects(:validate).with('0.0.0.0', nil, nil, {}).returns(true)
298
+ mock.expects(:validate).with('0.0.0.0', nil, nil, {}, 'Captcha failed.').returns(true)
290
299
  mock.expects(:last_error).times(2)
291
300
  ReCaptcha::AppHelper.define_public_key # 'foo'
292
301
  ReCaptcha::AppHelper.define_private_key # 'bar'
@@ -294,9 +303,25 @@ class TestRecaptcha < Test::Unit::TestCase
294
303
  ReCaptcha::AppHelper.undefine_public_key
295
304
  ReCaptcha::AppHelper.undefine_private_key
296
305
  # next, with options
297
- mock.expects(:validate).with('0.0.0.0', nil, nil, e).returns(true)
306
+ mock.expects(:validate).with('0.0.0.0', nil, nil, e, 'Captcha failed.').returns(true)
298
307
  assert @cf.validate_recap({}, e, {:rcc_pub => 'foobar', :rcc_priv => 'blegga'})
299
308
  end
309
+ def test_validate_recap_is_correct_with_constants_and_with_options_and_non_default_msg
310
+ # first, with constants
311
+ e = mock('errors')
312
+ mock = mock('client')
313
+ ReCaptcha::Client.expects(:new).returns(mock).times(2)
314
+ mock.expects(:validate).with('0.0.0.0', nil, nil, {}, 'Captcha failed.').returns(true)
315
+ mock.expects(:last_error).times(2)
316
+ ReCaptcha::AppHelper.define_public_key # 'foo'
317
+ ReCaptcha::AppHelper.define_private_key # 'bar'
318
+ assert @cf.validate_recap({}, {})
319
+ ReCaptcha::AppHelper.undefine_public_key
320
+ ReCaptcha::AppHelper.undefine_private_key
321
+ # next, with options
322
+ mock.expects(:validate).with('0.0.0.0', nil, nil, e, 'CAPTCHA failed.').returns(true)
323
+ assert @cf.validate_recap({}, e, {:rcc_pub => 'foobar', :rcc_priv => 'blegga', :msg => 'CAPTCHA failed.'})
324
+ end
300
325
 
301
326
  #
302
327
  # unit tests for HTTP/HTTPS-variants of get_captcha() method
metadata CHANGED
@@ -1,108 +1,82 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-recaptcha
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 3
10
- version: 1.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - McClain Looney
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-04-27 00:00:00 -05:00
12
+ date: 2011-11-12 00:00:00.000000000 -06:00
19
13
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: hoe
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &9701860 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 27
30
- segments:
31
- - 2
32
- - 5
33
- - 0
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
34
22
  version: 2.5.0
35
23
  type: :development
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: hoe
39
24
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *9701860
26
+ - !ruby/object:Gem::Dependency
27
+ name: hoe
28
+ requirement: &9700580 !ruby/object:Gem::Requirement
41
29
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 35
46
- segments:
47
- - 2
48
- - 9
49
- - 4
50
- version: 2.9.4
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.12'
51
34
  type: :development
52
- version_requirements: *id002
53
- description: ""
54
- email:
35
+ prerelease: false
36
+ version_requirements: *9700580
37
+ description: ''
38
+ email:
55
39
  - m@loonsoft.com
56
40
  executables: []
57
-
58
41
  extensions: []
59
-
60
- extra_rdoc_files:
42
+ extra_rdoc_files:
61
43
  - History.txt
62
44
  - README.txt
63
- files:
45
+ files:
64
46
  - History.txt
65
47
  - README.txt
66
48
  - lib/recaptcha.rb
67
49
  - lib/ruby-recaptcha.rb
68
- - test/test_helper.rb
69
50
  - test/test_recaptcha.rb
51
+ - test/test_helper.rb
70
52
  - .gemtest
71
53
  has_rdoc: true
72
54
  homepage: http://www.bitbucket.org/mml/ruby-recaptcha
73
55
  licenses: []
74
-
75
56
  post_install_message:
76
- rdoc_options:
57
+ rdoc_options:
77
58
  - --main
78
59
  - README.txt
79
- require_paths:
60
+ require_paths:
80
61
  - lib
81
- required_ruby_version: !ruby/object:Gem::Requirement
62
+ required_ruby_version: !ruby/object:Gem::Requirement
82
63
  none: false
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- hash: 3
87
- segments:
88
- - 0
89
- version: "0"
90
- required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
69
  none: false
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- hash: 3
96
- segments:
97
- - 0
98
- version: "0"
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
99
74
  requirements: []
100
-
101
75
  rubyforge_project: ruby-recaptcha
102
- rubygems_version: 1.4.2
76
+ rubygems_version: 1.6.2
103
77
  signing_key:
104
78
  specification_version: 3
105
- summary: ""
106
- test_files:
107
- - test/test_helper.rb
79
+ summary: ''
80
+ test_files:
108
81
  - test/test_recaptcha.rb
82
+ - test/test_helper.rb