glennr-recaptcha 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +23 -0
- data/LICENSE +19 -0
- data/README.rdoc +106 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/init.rb +6 -0
- data/lib/recaptcha.rb +23 -0
- data/lib/recaptcha/client_helper.rb +42 -0
- data/lib/recaptcha/merb.rb +4 -0
- data/lib/recaptcha/rails.rb +4 -0
- data/lib/recaptcha/verify.rb +50 -0
- data/recaptcha.gemspec +56 -0
- data/tasks/recaptcha_tasks.rake +4 -0
- data/test/recaptcha_test.rb +37 -0
- data/test/verify_recaptcha_test.rb +96 -0
- metadata +72 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
== 0.2.2 / 2009-09-14
|
2
|
+
|
3
|
+
* Add a timeout to the validator
|
4
|
+
* Give the documentation some love
|
5
|
+
|
6
|
+
== 0.2.1 / 2009-09-14
|
7
|
+
|
8
|
+
* Removed Ambethia namespace, and restructured classes a bit
|
9
|
+
* Added an example rails app in the example-rails branch
|
10
|
+
|
11
|
+
== 0.2.0 / 2009-09-12
|
12
|
+
|
13
|
+
* RecaptchaOptions AJAX API Fix
|
14
|
+
* Added 'cucumber' as a test environment to skip
|
15
|
+
* Ruby 1.9 compat fixes
|
16
|
+
* Added option :message => 'Custom error message' to verify_recaptcha
|
17
|
+
* Removed dependency on ActiveRecord constant
|
18
|
+
* Add I18n
|
19
|
+
|
20
|
+
== 0.1.0 / 2008-2-8
|
21
|
+
|
22
|
+
* 1 major enhancement
|
23
|
+
* Initial Gem Release
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2007 Jason L Perry
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
== Changes in 0.2.4 (Glenn Roberts)
|
2
|
+
|
3
|
+
Now adds model errors to :recaptcha, not :base.
|
4
|
+
|
5
|
+
Handy if you like to use inline errors in your forms.
|
6
|
+
|
7
|
+
= reCAPTCHA
|
8
|
+
|
9
|
+
Author:: Jason L Perry (http://ambethia.com)
|
10
|
+
Copyright:: Copyright (c) 2007 Jason L Perry
|
11
|
+
License:: {MIT}[http://creativecommons.org/licenses/MIT/]
|
12
|
+
Info:: http://ambethia.com/recaptcha
|
13
|
+
Git:: http://github.com/ambethia/recaptcha/tree/master
|
14
|
+
Bugs:: http://github.com/ambethia/recaptcha/issues
|
15
|
+
|
16
|
+
This plugin adds helpers for the {reCAPTCHA API}[http://recaptcha.net]. In your views you can use
|
17
|
+
the +recaptcha_tags+ method to embed the needed javascript, and you can validate in your controllers
|
18
|
+
with +verify_recaptcha+.
|
19
|
+
|
20
|
+
You'll want to add your public and private API keys in the environment variables +RECAPTCHA_PUBLIC_KEY+
|
21
|
+
and +RECAPTCHA_PRIVATE_KEY+, respectively. You could also specify them in <tt>config/environment.rb</tt> if you
|
22
|
+
are so inclined (see below). Exceptions will be raised if you call these methods and the keys can't be found.
|
23
|
+
|
24
|
+
== Rails Installation
|
25
|
+
|
26
|
+
reCAPTCHA for Rails can be installed as a gem:
|
27
|
+
|
28
|
+
config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com"
|
29
|
+
|
30
|
+
Or, as a standard rails plugin:
|
31
|
+
|
32
|
+
script/plugin install git://github.com/ambethia/recaptcha.git
|
33
|
+
|
34
|
+
== Merb Installation
|
35
|
+
|
36
|
+
reCAPTCHA can also be used in a Merb application when installed as a gem:
|
37
|
+
|
38
|
+
dependency "alm-recaptcha", ">=0.2.2.1", :require_as => "recaptcha/merb"
|
39
|
+
|
40
|
+
Initial Merb compatability funded by ALM Labs.
|
41
|
+
|
42
|
+
== Setting up your API Keys
|
43
|
+
|
44
|
+
There are two ways to setup your reCAPTCHA API keys once you {obtain}[http://recaptcha.net/whyrecaptcha.html]
|
45
|
+
a pair. You can pass in your keys as options at runtime, for example:
|
46
|
+
|
47
|
+
recaptcha_tags :public_key => '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
|
48
|
+
|
49
|
+
and later,
|
50
|
+
|
51
|
+
verify_recaptcha :private_key => '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
|
52
|
+
|
53
|
+
|
54
|
+
Or, preferably, you can keep your keys out of your code base by exporting the environment variables
|
55
|
+
mentioned earlier. You might do this in the .profile/rc, or equivalent for the user running your
|
56
|
+
application:
|
57
|
+
|
58
|
+
export RECAPTCHA_PUBLIC_KEY = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
|
59
|
+
export RECAPTCHA_PRIVATE_KEY = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
|
60
|
+
|
61
|
+
If that's not your thing, and dropping things into <tt>config/environment.rb</tt> is, you can just do:
|
62
|
+
|
63
|
+
ENV['RECAPTCHA_PUBLIC_KEY'] = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
|
64
|
+
ENV['RECAPTCHA_PRIVATE_KEY'] = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
|
65
|
+
|
66
|
+
== +recaptcha_tags+
|
67
|
+
|
68
|
+
Some of the options available:
|
69
|
+
|
70
|
+
<tt>:ssl</tt>:: Uses secure http for captcha widget (default +false+)
|
71
|
+
<tt>:noscript</tt>:: Include <noscript> content (default +true+)
|
72
|
+
<tt>:display</tt>:: Takes a hash containing the +theme+ and +tabindex+ options per the API. (default +nil+)
|
73
|
+
<tt>:ajax</tt>:: Render the dynamic AJAX captcha per the API. (default +false+)
|
74
|
+
<tt>:public_key</tt>:: Your public API key, takes precedence over the ENV variable (default +nil+)
|
75
|
+
<tt>:error</tt>:: Override the error code returned from the reCAPTCHA API (default +nil+)
|
76
|
+
|
77
|
+
You can also override the html attributes for the sizes of the generated +textarea+ and +iframe+
|
78
|
+
elements, if CSS isn't your thing. Inspect the source of +recaptcha_tags+ to see these options.
|
79
|
+
|
80
|
+
== +verify_recaptcha+
|
81
|
+
|
82
|
+
This method returns +true+ or +false+ after processing the parameters from the reCAPTCHA widget. Why
|
83
|
+
isn't this a model validation? Because that violates MVC. Use can use it like this, or how ever you
|
84
|
+
like. Passing in the ActiveRecord object is optional, if you do--and the captcha fails to verify--an
|
85
|
+
error will be added to the object for you to use.
|
86
|
+
|
87
|
+
Some of the options available:
|
88
|
+
|
89
|
+
<tt>:model</tt>:: Model to set errors
|
90
|
+
<tt>:message</tt>:: Custom error message
|
91
|
+
<tt>:private_key</tt>:: Your private API key, takes precedence over the ENV variable (default +nil+).
|
92
|
+
<tt>:timeout</tt>:: The number of seconds to wait for reCAPTCHA servers before give up. (default +3+)
|
93
|
+
|
94
|
+
respond_to do |format|
|
95
|
+
if verify_recaptcha(:model => @post, :message => 'Oh! It's error with reCAPTCHA!') && @post.save
|
96
|
+
# ...
|
97
|
+
else
|
98
|
+
# ...
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
== TODO
|
103
|
+
* Remove Rails/ActionController dependencies
|
104
|
+
* Framework agnostic
|
105
|
+
* Add some helpers to use in before_filter and what not
|
106
|
+
* Better documentation
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gem|
|
6
|
+
gem.name = "recaptcha"
|
7
|
+
gem.description = "This plugin adds helpers for the reCAPTCHA API "
|
8
|
+
gem.summary = "Helpers for the reCAPTCHA API"
|
9
|
+
gem.homepage = "http://ambethia.com/recaptcha"
|
10
|
+
gem.authors = ["Jason L. Perry"]
|
11
|
+
gem.email = "jasper@ambethia.com"
|
12
|
+
gem.files.reject! { |fn| fn.include? ".gitignore" }
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/rdoctask'
|
20
|
+
Rake::RDocTask.new do |rd|
|
21
|
+
if File.exist?('VERSION.yml')
|
22
|
+
config = YAML.load(File.read('VERSION.yml'))
|
23
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
24
|
+
else
|
25
|
+
version = ""
|
26
|
+
end
|
27
|
+
|
28
|
+
rd.main = "README.rdoc"
|
29
|
+
rd.rdoc_files.include "README.rdoc", "LICENSE", "lib/**/*.rb"
|
30
|
+
rd.rdoc_dir = 'rdoc'
|
31
|
+
rd.options << '-N' # line numbers
|
32
|
+
rd.options << '-S' # inline source
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rake/testtask'
|
36
|
+
Rake::TestTask.new(:test) do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/*_test.rb'
|
39
|
+
# test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
begin
|
43
|
+
require 'rcov/rcovtask'
|
44
|
+
Rcov::RcovTask.new do |test|
|
45
|
+
test.libs << 'test'
|
46
|
+
test.pattern = 'test/**/*_test.rb'
|
47
|
+
test.verbose = true
|
48
|
+
end
|
49
|
+
rescue LoadError
|
50
|
+
task :rcov do
|
51
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
task :default => :test
|
56
|
+
|
57
|
+
|
58
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.3
|
data/init.rb
ADDED
data/lib/recaptcha.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'recaptcha/client_helper'
|
2
|
+
require 'recaptcha/verify'
|
3
|
+
|
4
|
+
module Recaptcha
|
5
|
+
module VERSION #:nodoc:
|
6
|
+
MAJOR = 0
|
7
|
+
MINOR = 2
|
8
|
+
TINY = 2
|
9
|
+
PATCH = 1
|
10
|
+
|
11
|
+
STRING = [MAJOR, MINOR, TINY, PATCH].join('.')
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
RECAPTCHA_API_SERVER = 'http://api.recaptcha.net';
|
16
|
+
RECAPTCHA_API_SECURE_SERVER = 'https://api-secure.recaptcha.net';
|
17
|
+
RECAPTCHA_VERIFY_SERVER = 'api-verify.recaptcha.net';
|
18
|
+
|
19
|
+
SKIP_VERIFY_ENV = ['test', 'cucumber']
|
20
|
+
|
21
|
+
class RecaptchaError < StandardError
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Recaptcha
|
2
|
+
module ClientHelper
|
3
|
+
# Your public API can be specified in the +options+ hash or preferably
|
4
|
+
# the environment variable +RECAPTCHA_PUBLIC_KEY+.
|
5
|
+
def recaptcha_tags(options = {})
|
6
|
+
# Default options
|
7
|
+
key = options[:public_key] ||= ENV['RECAPTCHA_PUBLIC_KEY']
|
8
|
+
raise RecaptchaError, "No public key specified." unless key
|
9
|
+
error = options[:error] ||= (defined? flash ? flash[:recaptcha_error] : "")
|
10
|
+
uri = options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER
|
11
|
+
html = ""
|
12
|
+
if options[:display]
|
13
|
+
html << %{<script type="text/javascript">\n}
|
14
|
+
html << %{ var RecaptchaOptions = #{options[:display].to_json};\n}
|
15
|
+
html << %{</script>\n}
|
16
|
+
end
|
17
|
+
if options[:ajax]
|
18
|
+
html << %{<div id="dynamic_recaptcha"></div>}
|
19
|
+
html << %{<script type="text/javascript" src="#{uri}/js/recaptcha_ajax.js"></script>\n}
|
20
|
+
html << %{<script type="text/javascript">\n}
|
21
|
+
html << %{ Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{options[:display] ? ',RecaptchaOptions' : ''});}
|
22
|
+
html << %{</script>\n}
|
23
|
+
else
|
24
|
+
html << %{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
|
25
|
+
html << %{#{error ? "&error=#{CGI::escape(error)}" : ""}"></script>\n}
|
26
|
+
unless options[:noscript] == false
|
27
|
+
html << %{<noscript>\n }
|
28
|
+
html << %{<iframe src="#{uri}/noscript?k=#{key}" }
|
29
|
+
html << %{height="#{options[:iframe_height] ||= 300}" }
|
30
|
+
html << %{width="#{options[:iframe_width] ||= 500}" }
|
31
|
+
html << %{frameborder="0"></iframe><br/>\n }
|
32
|
+
html << %{<textarea name="recaptcha_challenge_field" }
|
33
|
+
html << %{rows="#{options[:textarea_rows] ||= 3}" }
|
34
|
+
html << %{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n }
|
35
|
+
html << %{<input type="hidden" name="recaptcha_response_field" value="manual_challenge">}
|
36
|
+
html << %{</noscript>\n}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
return html
|
40
|
+
end # recaptcha_tags
|
41
|
+
end # ClientHelper
|
42
|
+
end # Recaptcha
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Recaptcha
|
2
|
+
module Verify
|
3
|
+
# Your private API can be specified in the +options+ hash or preferably
|
4
|
+
# the environment variable +RECAPTCHA_PUBLIC_KEY+.
|
5
|
+
def verify_recaptcha(options = {})
|
6
|
+
if !options.is_a? Hash
|
7
|
+
options = {:model => options}
|
8
|
+
end
|
9
|
+
|
10
|
+
env = options[:env] || ENV['RAILS_ENV']
|
11
|
+
return true if SKIP_VERIFY_ENV.include? env
|
12
|
+
model = options[:model]
|
13
|
+
private_key = options[:private_key] || ENV['RECAPTCHA_PRIVATE_KEY']
|
14
|
+
raise RecaptchaError, "No private key specified." unless private_key
|
15
|
+
|
16
|
+
begin
|
17
|
+
recaptcha = nil
|
18
|
+
Timeout::timeout(options[:timeout] || 3) do
|
19
|
+
recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), {
|
20
|
+
"privatekey" => private_key,
|
21
|
+
"remoteip" => request.remote_ip,
|
22
|
+
"challenge" => params[:recaptcha_challenge_field],
|
23
|
+
"response" => params[:recaptcha_response_field]
|
24
|
+
}
|
25
|
+
end
|
26
|
+
answer, error = recaptcha.body.split.map { |s| s.chomp }
|
27
|
+
unless answer == 'true'
|
28
|
+
flash[:recaptcha_error] = error
|
29
|
+
if model
|
30
|
+
model.valid?
|
31
|
+
model.errors.add :recaptcha, options[:message] || "Word verification response is incorrect, please try again."
|
32
|
+
end
|
33
|
+
return false
|
34
|
+
else
|
35
|
+
flash[:recaptcha_error] = nil
|
36
|
+
return true
|
37
|
+
end
|
38
|
+
rescue Timeout::Error
|
39
|
+
flash[:recaptcha_error] = "recaptcha-not-reachable"
|
40
|
+
if model
|
41
|
+
model.valid?
|
42
|
+
model.errors.add :recaptcha, options[:message] || "Oops, we failed to validate your word verification response. Please try again."
|
43
|
+
end
|
44
|
+
return false
|
45
|
+
rescue Exception => e
|
46
|
+
raise RecaptchaError, e.message, e.backtrace
|
47
|
+
end
|
48
|
+
end # verify_recaptcha
|
49
|
+
end # Verify
|
50
|
+
end # Recaptcha
|
data/recaptcha.gemspec
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{glennr-recaptcha}
|
8
|
+
s.version = "0.2.4"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jason L. Perry", "Glenn Roberts"]
|
12
|
+
s.date = %q{2009-12-21}
|
13
|
+
s.description = %q{This plugin adds helpers for the reCAPTCHA API }
|
14
|
+
s.email = %q{jasper@ambethia.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"CHANGELOG",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"init.rb",
|
26
|
+
"lib/recaptcha.rb",
|
27
|
+
"lib/recaptcha/client_helper.rb",
|
28
|
+
"lib/recaptcha/merb.rb",
|
29
|
+
"lib/recaptcha/rails.rb",
|
30
|
+
"lib/recaptcha/verify.rb",
|
31
|
+
"recaptcha.gemspec",
|
32
|
+
"tasks/recaptcha_tasks.rake",
|
33
|
+
"test/recaptcha_test.rb",
|
34
|
+
"test/verify_recaptcha_test.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://ambethia.com/recaptcha}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{Helpers for the reCAPTCHA API}
|
41
|
+
s.test_files = [
|
42
|
+
"test/recaptcha_test.rb",
|
43
|
+
"test/verify_recaptcha_test.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
else
|
52
|
+
end
|
53
|
+
else
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'cgi'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/recaptcha'
|
4
|
+
|
5
|
+
class RecaptchaClientHelperTest < Test::Unit::TestCase
|
6
|
+
include Recaptcha
|
7
|
+
include Recaptcha::ClientHelper
|
8
|
+
include Recaptcha::Verify
|
9
|
+
|
10
|
+
attr_accessor :session
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@session = {}
|
14
|
+
ENV['RECAPTCHA_PUBLIC_KEY'] = '0000000000000000000000000000000000000000'
|
15
|
+
ENV['RECAPTCHA_PRIVATE_KEY'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_recaptcha_tags
|
19
|
+
# Might as well match something...
|
20
|
+
assert_match /http:\/\/api.recaptcha.net/, recaptcha_tags
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_recaptcha_tags_with_ssl
|
24
|
+
assert_match /https:\/\/api-secure.recaptcha.net/, recaptcha_tags(:ssl => true)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_recaptcha_tags_without_noscript
|
28
|
+
assert_no_match /noscript/, recaptcha_tags(:noscript => false)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_raise_exception_without_public_key
|
32
|
+
assert_raise RecaptchaError do
|
33
|
+
ENV['RECAPTCHA_PUBLIC_KEY'] = nil
|
34
|
+
recaptcha_tags
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rails/version' # For getting the rails version constants
|
3
|
+
require 'active_support/vendor' # For loading I18n
|
4
|
+
require 'mocha'
|
5
|
+
require 'net/http'
|
6
|
+
require File.dirname(__FILE__) + '/../lib/recaptcha'
|
7
|
+
|
8
|
+
class RecaptchaVerifyTest < Test::Unit::TestCase
|
9
|
+
def setup
|
10
|
+
|
11
|
+
ENV['RECAPTCHA_PRIVATE_KEY'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
12
|
+
@controller = TestController.new
|
13
|
+
@controller.request = stub(:remote_ip => "1.1.1.1")
|
14
|
+
@controller.params = {:recaptcha_challenge_field => "challenge", :recaptcha_response_field => "response"}
|
15
|
+
|
16
|
+
@expected_post_data = {}
|
17
|
+
@expected_post_data["privatekey"] = ENV['RECAPTCHA_PRIVATE_KEY']
|
18
|
+
@expected_post_data["remoteip"] = @controller.request.remote_ip
|
19
|
+
@expected_post_data["challenge"] = "challenge"
|
20
|
+
@expected_post_data["response"] = "response"
|
21
|
+
|
22
|
+
@expected_uri = URI.parse("http://#{Recaptcha::RECAPTCHA_VERIFY_SERVER}/verify")
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_raise_exception_without_private_key
|
26
|
+
assert_raise Recaptcha::RecaptchaError do
|
27
|
+
ENV['RECAPTCHA_PRIVATE_KEY'] = nil
|
28
|
+
@controller.verify_recaptcha
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_return_false_when_key_is_invalid
|
33
|
+
expect_http_post(response_with_body("false\ninvalid-site-private-key"))
|
34
|
+
|
35
|
+
assert !@controller.verify_recaptcha
|
36
|
+
assert_equal "invalid-site-private-key", @controller.session[:recaptcha_error]
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_returns_true_on_success
|
40
|
+
@controller.session[:recaptcha_error] = "previous error that should be cleared"
|
41
|
+
expect_http_post(response_with_body("true\n"))
|
42
|
+
|
43
|
+
assert @controller.verify_recaptcha
|
44
|
+
assert_nil @controller.session[:recaptcha_error]
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_errors_should_be_added_to_model
|
48
|
+
expect_http_post(response_with_body("false\nbad-news"))
|
49
|
+
|
50
|
+
errors = mock
|
51
|
+
errors.expects(:add).with(:base, "Captcha response is incorrect, please try again.")
|
52
|
+
model = mock(:valid? => false, :errors => errors)
|
53
|
+
|
54
|
+
assert !@controller.verify_recaptcha(:model => model)
|
55
|
+
assert_equal "bad-news", @controller.session[:recaptcha_error]
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_returns_true_on_success_with_optional_key
|
59
|
+
@controller.session[:recaptcha_error] = "previous error that should be cleared"
|
60
|
+
# reset private key
|
61
|
+
@expected_post_data["privatekey"] = 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX'
|
62
|
+
expect_http_post(response_with_body("true\n"))
|
63
|
+
|
64
|
+
assert @controller.verify_recaptcha(:private_key => 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX')
|
65
|
+
assert_nil @controller.session[:recaptcha_error]
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_timeout
|
69
|
+
expect_http_post(Timeout::Error, :exception => true)
|
70
|
+
assert !@controller.verify_recaptcha()
|
71
|
+
assert_equal "recaptcha-not-reachable", @controller.session[:recaptcha_error]
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
class TestController
|
77
|
+
include Recaptcha::Verify
|
78
|
+
attr_accessor :request, :params, :session
|
79
|
+
|
80
|
+
def initialize
|
81
|
+
@session = {}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def expect_http_post(response, options = {})
|
86
|
+
unless options[:exception]
|
87
|
+
Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)
|
88
|
+
else
|
89
|
+
Net::HTTP.expects(:post_form).raises response
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def response_with_body(body)
|
94
|
+
stub(:body => body)
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glennr-recaptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason L. Perry
|
8
|
+
- Glenn Roberts
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-12-21 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: "This plugin adds helpers for the reCAPTCHA API "
|
18
|
+
email: jasper@ambethia.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- LICENSE
|
25
|
+
- README.rdoc
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- init.rb
|
33
|
+
- lib/recaptcha.rb
|
34
|
+
- lib/recaptcha/client_helper.rb
|
35
|
+
- lib/recaptcha/merb.rb
|
36
|
+
- lib/recaptcha/rails.rb
|
37
|
+
- lib/recaptcha/verify.rb
|
38
|
+
- recaptcha.gemspec
|
39
|
+
- tasks/recaptcha_tasks.rake
|
40
|
+
- test/recaptcha_test.rb
|
41
|
+
- test/verify_recaptcha_test.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://ambethia.com/recaptcha
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Helpers for the reCAPTCHA API
|
70
|
+
test_files:
|
71
|
+
- test/recaptcha_test.rb
|
72
|
+
- test/verify_recaptcha_test.rb
|