alm-recaptcha 0.2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +23 -0
- data/LICENSE +19 -0
- data/README.rdoc +100 -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 +71 -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,100 @@
|
|
1
|
+
= reCAPTCHA
|
2
|
+
|
3
|
+
Author:: Jason L Perry (http://ambethia.com)
|
4
|
+
Copyright:: Copyright (c) 2007 Jason L Perry
|
5
|
+
License:: {MIT}[http://creativecommons.org/licenses/MIT/]
|
6
|
+
Info:: http://ambethia.com/recaptcha
|
7
|
+
Git:: http://github.com/ambethia/recaptcha/tree/master
|
8
|
+
Bugs:: http://github.com/ambethia/recaptcha/issues
|
9
|
+
|
10
|
+
This plugin adds helpers for the {reCAPTCHA API}[http://recaptcha.net]. In your views you can use
|
11
|
+
the +recaptcha_tags+ method to embed the needed javascript, and you can validate in your controllers
|
12
|
+
with +verify_recaptcha+.
|
13
|
+
|
14
|
+
You'll want to add your public and private API keys in the environment variables +RECAPTCHA_PUBLIC_KEY+
|
15
|
+
and +RECAPTCHA_PRIVATE_KEY+, respectively. You could also specify them in <tt>config/environment.rb</tt> if you
|
16
|
+
are so inclined (see below). Exceptions will be raised if you call these methods and the keys can't be found.
|
17
|
+
|
18
|
+
== Rails Installation
|
19
|
+
|
20
|
+
reCAPTCHA for Rails can be installed as a gem:
|
21
|
+
|
22
|
+
config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com"
|
23
|
+
|
24
|
+
Or, as a standard rails plugin:
|
25
|
+
|
26
|
+
script/plugin install git://github.com/ambethia/recaptcha.git
|
27
|
+
|
28
|
+
== Merb Installation
|
29
|
+
|
30
|
+
reCAPTCHA can also be used in a Merb application when installed as a gem:
|
31
|
+
|
32
|
+
dependency "alm-recaptcha", ">=0.2.2.1", :require_as => "recaptcha/merb"
|
33
|
+
|
34
|
+
Initial Merb compatability funded by ALM Labs.
|
35
|
+
|
36
|
+
== Setting up your API Keys
|
37
|
+
|
38
|
+
There are two ways to setup your reCAPTCHA API keys once you {obtain}[http://recaptcha.net/whyrecaptcha.html]
|
39
|
+
a pair. You can pass in your keys as options at runtime, for example:
|
40
|
+
|
41
|
+
recaptcha_tags :public_key => '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
|
42
|
+
|
43
|
+
and later,
|
44
|
+
|
45
|
+
verify_recaptcha :private_key => '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
|
46
|
+
|
47
|
+
|
48
|
+
Or, preferably, you can keep your keys out of your code base by exporting the environment variables
|
49
|
+
mentioned earlier. You might do this in the .profile/rc, or equivalent for the user running your
|
50
|
+
application:
|
51
|
+
|
52
|
+
export RECAPTCHA_PUBLIC_KEY = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
|
53
|
+
export RECAPTCHA_PRIVATE_KEY = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
|
54
|
+
|
55
|
+
If that's not your thing, and dropping things into <tt>config/environment.rb</tt> is, you can just do:
|
56
|
+
|
57
|
+
ENV['RECAPTCHA_PUBLIC_KEY'] = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
|
58
|
+
ENV['RECAPTCHA_PRIVATE_KEY'] = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
|
59
|
+
|
60
|
+
== +recaptcha_tags+
|
61
|
+
|
62
|
+
Some of the options available:
|
63
|
+
|
64
|
+
<tt>:ssl</tt>:: Uses secure http for captcha widget (default +false+)
|
65
|
+
<tt>:noscript</tt>:: Include <noscript> content (default +true+)
|
66
|
+
<tt>:display</tt>:: Takes a hash containing the +theme+ and +tabindex+ options per the API. (default +nil+)
|
67
|
+
<tt>:ajax</tt>:: Render the dynamic AJAX captcha per the API. (default +false+)
|
68
|
+
<tt>:public_key</tt>:: Your public API key, takes precedence over the ENV variable (default +nil+)
|
69
|
+
<tt>:error</tt>:: Override the error code returned from the reCAPTCHA API (default +nil+)
|
70
|
+
|
71
|
+
You can also override the html attributes for the sizes of the generated +textarea+ and +iframe+
|
72
|
+
elements, if CSS isn't your thing. Inspect the source of +recaptcha_tags+ to see these options.
|
73
|
+
|
74
|
+
== +verify_recaptcha+
|
75
|
+
|
76
|
+
This method returns +true+ or +false+ after processing the parameters from the reCAPTCHA widget. Why
|
77
|
+
isn't this a model validation? Because that violates MVC. Use can use it like this, or how ever you
|
78
|
+
like. Passing in the ActiveRecord object is optional, if you do--and the captcha fails to verify--an
|
79
|
+
error will be added to the object for you to use.
|
80
|
+
|
81
|
+
Some of the options available:
|
82
|
+
|
83
|
+
<tt>:model</tt>:: Model to set errors
|
84
|
+
<tt>:message</tt>:: Custom error message
|
85
|
+
<tt>:private_key</tt>:: Your private API key, takes precedence over the ENV variable (default +nil+).
|
86
|
+
<tt>:timeout</tt>:: The number of seconds to wait for reCAPTCHA servers before give up. (default +3+)
|
87
|
+
|
88
|
+
respond_to do |format|
|
89
|
+
if verify_recaptcha(:model => @post, :message => 'Oh! It's error with reCAPTCHA!') && @post.save
|
90
|
+
# ...
|
91
|
+
else
|
92
|
+
# ...
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
== TODO
|
97
|
+
* Remove Rails/ActionController dependencies
|
98
|
+
* Framework agnostic
|
99
|
+
* Add some helpers to use in before_filter and what not
|
100
|
+
* 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.2
|
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 :base, 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 :base, 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{alm-recaptcha}
|
8
|
+
s.version = "0.2.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jason L. Perry"]
|
12
|
+
s.date = %q{2009-10-20}
|
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,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alm-recaptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason L. Perry
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-20 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "This plugin adds helpers for the reCAPTCHA API "
|
17
|
+
email: jasper@ambethia.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- CHANGELOG
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- init.rb
|
32
|
+
- lib/recaptcha.rb
|
33
|
+
- lib/recaptcha/client_helper.rb
|
34
|
+
- lib/recaptcha/merb.rb
|
35
|
+
- lib/recaptcha/rails.rb
|
36
|
+
- lib/recaptcha/verify.rb
|
37
|
+
- recaptcha.gemspec
|
38
|
+
- tasks/recaptcha_tasks.rake
|
39
|
+
- test/recaptcha_test.rb
|
40
|
+
- test/verify_recaptcha_test.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://ambethia.com/recaptcha
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.5
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Helpers for the reCAPTCHA API
|
69
|
+
test_files:
|
70
|
+
- test/recaptcha_test.rb
|
71
|
+
- test/verify_recaptcha_test.rb
|