litecreations-recaptcha 0.2.3.12

Sign up to get free protection for your applications and to get access to all the features.
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,123 @@
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
11
+ views you can use the +recaptcha_tags+ method to embed the needed javascript,
12
+ and you can validate in your controllers with +verify_recaptcha+.
13
+
14
+ Beforehand you need to configure Recaptcha with your custom private and public
15
+ key. You may find detailed examples below. Exceptions will be raised if you
16
+ call these methods and the keys can't be found.
17
+
18
+ == About this fork
19
+
20
+ This fork tries to introduces a more convenient way to configure recaptcha's
21
+ settings. The API will be inspired by {Thoughtbot's
22
+ Hoptoad}[http://robots.thoughtbot.com/post/344833329/mygem-configure-block].
23
+
24
+ == Rails Installation
25
+
26
+ reCAPTCHA for Rails can be installed as a gem:
27
+
28
+ config.gem "recaptcha", :lib => "recaptcha/rails"
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 multiple ways to setup your reCAPTCHA API key once you
45
+ {obtain}[http://recaptcha.net/whyrecaptcha.html] a pair.
46
+
47
+ === Recaptcha.configure
48
+
49
+ You may use the block style configuration. The following code could be placed
50
+ into a +config/initializers/recaptcha.rb+ when used in a Rails project.
51
+
52
+ Recaptcha.configure do |config|
53
+ config.public_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
54
+ config.private_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
55
+ end
56
+
57
+ This way, you may also set additional options to fit recaptcha into your
58
+ deployment environment.
59
+
60
+ === Shell environment
61
+
62
+ Or, you can keep your keys out of your code base by exporting the following
63
+ environment variables. You might do this in the .profile/rc, or equivalent for
64
+ the user running your application:
65
+
66
+ export RECAPTCHA_PUBLIC_KEY = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
67
+ export RECAPTCHA_PRIVATE_KEY = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
68
+
69
+ === Per call
70
+
71
+ You can also pass in your keys as options at runtime, for example:
72
+
73
+ recaptcha_tags :public_key => '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
74
+
75
+ and later,
76
+
77
+ verify_recaptcha :private_key => '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
78
+
79
+ This option might be useful, if the same code base is used for multiple
80
+ reCAPTCHA setups.
81
+
82
+ == +recaptcha_tags+
83
+
84
+ Some of the options available:
85
+
86
+ <tt>:ssl</tt>:: Uses secure http for captcha widget (default +false+)
87
+ <tt>:noscript</tt>:: Include <noscript> content (default +true+)
88
+ <tt>:display</tt>:: Takes a hash containing the +theme+ and +tabindex+ options per the API. (default +nil+)
89
+ <tt>:ajax</tt>:: Render the dynamic AJAX captcha per the API. (default +false+)
90
+ <tt>:public_key</tt>:: Your public API key, takes precedence over the ENV variable (default +nil+)
91
+ <tt>:error</tt>:: Override the error code returned from the reCAPTCHA API (default +nil+)
92
+
93
+ You can also override the html attributes for the sizes of the generated +textarea+ and +iframe+
94
+ elements, if CSS isn't your thing. Inspect the source of +recaptcha_tags+ to see these options.
95
+
96
+ == +verify_recaptcha+
97
+
98
+ This method returns +true+ or +false+ after processing the parameters from the reCAPTCHA widget. Why
99
+ isn't this a model validation? Because that violates MVC. Use can use it like this, or how ever you
100
+ like. Passing in the ActiveRecord object is optional, if you do--and the captcha fails to verify--an
101
+ error will be added to the object for you to use.
102
+
103
+ Some of the options available:
104
+
105
+ <tt>:model</tt>:: Model to set errors
106
+ <tt>:attribute</tt>:: Model attribute to receive errors (default :base)
107
+ <tt>:message</tt>:: Custom error message
108
+ <tt>:private_key</tt>:: Your private API key, takes precedence over the ENV variable (default +nil+).
109
+ <tt>:timeout</tt>:: The number of seconds to wait for reCAPTCHA servers before give up. (default +3+)
110
+
111
+ respond_to do |format|
112
+ if verify_recaptcha(:model => @post, :message => "Oh! It's error with reCAPTCHA!") && @post.save
113
+ # ...
114
+ else
115
+ # ...
116
+ end
117
+ end
118
+
119
+ == TODO
120
+ * Remove Rails/ActionController dependencies
121
+ * Framework agnostic
122
+ * Add some helpers to use in before_filter and what not
123
+ * 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.12
data/init.rb ADDED
@@ -0,0 +1,6 @@
1
+ # Rails plugin initialization.
2
+ # You can also install it as a gem:
3
+ # config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com"
4
+
5
+ require 'net/http'
6
+ require 'recaptcha/rails'
data/lib/recaptcha.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'recaptcha/configuration'
2
+ require 'recaptcha/client_helper'
3
+ require 'recaptcha/verify'
4
+
5
+ module Recaptcha
6
+ module VERSION #:nodoc:
7
+ MAJOR = 0
8
+ MINOR = 2
9
+ TINY = 2
10
+ PATCH = 1
11
+
12
+ STRING = [MAJOR, MINOR, TINY, PATCH].join('.')
13
+ end
14
+
15
+
16
+ RECAPTCHA_API_SERVER_URL = 'http://www.google.com/recaptcha/api'
17
+ RECAPTCHA_API_SECURE_SERVER_URL = 'https://www.google.com/recaptcha/api'
18
+ RECAPTCHA_VERIFY_URL = 'http://www.google.com/recaptcha/api'
19
+
20
+ SKIP_VERIFY_ENV = ['test', 'cucumber']
21
+
22
+ # Gives access to the current Configuration.
23
+ def self.configuration
24
+ @configuration ||= Configuration.new
25
+ end
26
+
27
+ # Allows easy setting of multiple configuration options. See Configuration
28
+ # for all available options.
29
+ #--
30
+ # The temp assignment is only used to get a nicer rdoc. Feel free to remove
31
+ # this hack.
32
+ #++
33
+ def self.configure
34
+ config = configuration
35
+ yield(config)
36
+ end
37
+
38
+ class RecaptchaError < StandardError
39
+ end
40
+ end
@@ -0,0 +1,45 @@
1
+ module Recaptcha
2
+ module ClientHelper
3
+
4
+ # Your public API can be specified in the +options+ hash or preferably
5
+ # using the Configuration.
6
+ def recaptcha_tags(options = {})
7
+ # Default options
8
+ key = options[:public_key] ||= Recaptcha.configuration.public_key
9
+ raise RecaptchaError, "No public key specified." unless key
10
+ error = options[:error] ||= (defined? flash ? flash[:recaptcha_error] : "")
11
+ uri = Recaptcha.configuration.api_server_url(options[:ssl])
12
+ html = ""
13
+ if options[:display]
14
+ html << %{<script type="text/javascript">\n}
15
+ html << %{ var RecaptchaOptions = #{options[:display].to_json};\n}
16
+ html << %{</script>\n}
17
+ end
18
+ if options[:ajax]
19
+ html << %{<script type="text/javascript" src="#{uri}/js/recaptcha_ajax.js"></script>\n}
20
+ html << %{<div id="dynamic_recaptcha"></div>}
21
+ html << %{<script type="text/javascript">\n}
22
+ html << %{setTimeout("Func1()", 100);}
23
+ html << %{ function Func1() { Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{options[:display] ? ',RecaptchaOptions' : ''});}}
24
+ html << %{</script>\n}
25
+
26
+ else
27
+ html << %{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
28
+ html << %{#{error ? "&amp;error=#{CGI::escape(error)}" : ""}"></script>\n}
29
+ unless options[:noscript] == false
30
+ html << %{<noscript>\n }
31
+ html << %{<iframe src="#{uri}/noscript?k=#{key}" }
32
+ html << %{height="#{options[:iframe_height] ||= 300}" }
33
+ html << %{width="#{options[:iframe_width] ||= 500}" }
34
+ html << %{style="border:none;"></iframe><br/>\n }
35
+ html << %{<textarea name="recaptcha_challenge_field" }
36
+ html << %{rows="#{options[:textarea_rows] ||= 3}" }
37
+ html << %{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n }
38
+ html << %{<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>}
39
+ html << %{</noscript>\n}
40
+ end
41
+ end
42
+ return html.html_safe
43
+ end # recaptcha_tags
44
+ end # ClientHelper
45
+ end # Recaptcha
@@ -0,0 +1,52 @@
1
+ module Recaptcha
2
+ # This class enables detailed configuration of the recaptcha services.
3
+ #
4
+ # By calling
5
+ #
6
+ # Recaptcha.configuration # => instance of Recaptcha::Configuration
7
+ #
8
+ # or
9
+ # Recaptcha.configure do |config|
10
+ # config # => instance of Recaptcha::Configuration
11
+ # end
12
+ #
13
+ # you are able to perform configuration updates.
14
+ #
15
+ # Your are able to customize all attributes listed below. All values have
16
+ # sensitive default and will very likely not need to be changed.
17
+ #
18
+ # Please note that the public and private key for the reCAPTCHA API Access
19
+ # have no useful default value. The keys may be set via the Shell enviroment
20
+ # or using this configuration. Settings within this configuration always take
21
+ # precedence.
22
+ #
23
+ # Setting the keys with this Configuration
24
+ #
25
+ # Recaptcha.configure do |config|
26
+ # config.public_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
27
+ # config.private_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
28
+ # end
29
+ #
30
+ class Configuration
31
+ attr_accessor :nonssl_api_server_url,
32
+ :ssl_api_server_url,
33
+ :verify_url,
34
+ :skip_verify_env,
35
+ :private_key,
36
+ :public_key
37
+
38
+ def initialize #:nodoc:
39
+ @nonssl_api_server_url = RECAPTCHA_API_SERVER_URL
40
+ @ssl_api_server_url = RECAPTCHA_API_SECURE_SERVER_URL
41
+ @verify_url = RECAPTCHA_VERIFY_URL
42
+ @skip_verify_env = SKIP_VERIFY_ENV
43
+
44
+ @private_key = ENV['RECAPTCHA_PRIVATE_KEY']
45
+ @public_key = ENV['RECAPTCHA_PUBLIC_KEY']
46
+ end
47
+
48
+ def api_server_url(ssl = false) #:nodoc:
49
+ ssl ? ssl_api_server_url : nonssl_api_server_url
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ require 'recaptcha'
2
+
3
+ Merb::GlobalHelpers.send(:include, Recaptcha::ClientHelper)
4
+ Merb::Controller.send(:include, Recaptcha::Verify)
@@ -0,0 +1,4 @@
1
+ require 'recaptcha'
2
+
3
+ ActionView::Base.send(:include, Recaptcha::ClientHelper)
4
+ ActionController::Base.send(:include, Recaptcha::Verify)
@@ -0,0 +1,51 @@
1
+ module Recaptcha
2
+ module Verify
3
+ # Your private API can be specified in the +options+ hash or preferably
4
+ # using the Configuration.
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 Recaptcha.configuration.skip_verify_env.include? env
12
+ model = options[:model]
13
+ attribute = options[:attribute] || :base
14
+ private_key = options[:private_key] || Recaptcha.configuration.private_key
15
+ raise RecaptchaError, "No private key specified." unless private_key
16
+
17
+ begin
18
+ recaptcha = nil
19
+ Timeout::timeout(options[:timeout] || 3) do
20
+ recaptcha = Net::HTTP.post_form URI.parse(Recaptcha.configuration.verify_url), {
21
+ "privatekey" => private_key,
22
+ "remoteip" => request.remote_ip,
23
+ "challenge" => params[:recaptcha_challenge_field],
24
+ "response" => params[:recaptcha_response_field]
25
+ }
26
+ end
27
+ answer, error = recaptcha.body.split.map { |s| s.chomp }
28
+ unless answer == 'true'
29
+ flash[:recaptcha_error] = error
30
+ if model
31
+ model.valid?
32
+ model.errors.add attribute, options[:message] || "Word verification response is incorrect, please try again."
33
+ end
34
+ return false
35
+ else
36
+ flash[:recaptcha_error] = nil
37
+ return true
38
+ end
39
+ rescue Timeout::Error
40
+ flash[:recaptcha_error] = "recaptcha-not-reachable"
41
+ if model
42
+ model.valid?
43
+ model.errors.add attribute, options[:message] || "Oops, we failed to validate your word verification response. Please try again."
44
+ end
45
+ return false
46
+ rescue Exception => e
47
+ raise RecaptchaError, e.message, e.backtrace
48
+ end
49
+ end # verify_recaptcha
50
+ end # Verify
51
+ end # Recaptcha
data/recaptcha.gemspec ADDED
@@ -0,0 +1,57 @@
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{litecreations-recaptcha}
8
+ s.version = "0.2.3.12"
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-23}
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/configuration.rb",
29
+ "lib/recaptcha/merb.rb",
30
+ "lib/recaptcha/rails.rb",
31
+ "lib/recaptcha/verify.rb",
32
+ "recaptcha.gemspec",
33
+ "tasks/recaptcha_tasks.rake",
34
+ "test/recaptcha_test.rb",
35
+ "test/verify_recaptcha_test.rb"
36
+ ]
37
+ s.homepage = %q{http://ambethia.com/recaptcha}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.5}
41
+ s.summary = %q{Helpers for the reCAPTCHA API}
42
+ s.test_files = [
43
+ "test/recaptcha_test.rb",
44
+ "test/verify_recaptcha_test.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ else
53
+ end
54
+ else
55
+ end
56
+ end
57
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :recaptcha do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require 'cgi'
3
+ require File.dirname(File.expand_path(__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
+ Recaptcha.configure do |config|
15
+ config.public_key = '0000000000000000000000000000000000000000'
16
+ config.private_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
17
+ end
18
+ end
19
+
20
+ def test_recaptcha_tags
21
+ # Might as well match something...
22
+ assert_match /http:\/\/api.recaptcha.net/, recaptcha_tags
23
+ end
24
+
25
+ def test_recaptcha_tags_with_ssl
26
+ assert_match /https:\/\/api-secure.recaptcha.net/, recaptcha_tags(:ssl => true)
27
+ end
28
+
29
+ def test_recaptcha_tags_without_noscript
30
+ assert_no_match /noscript/, recaptcha_tags(:noscript => false)
31
+ end
32
+
33
+ def test_should_raise_exception_without_public_key
34
+ assert_raise RecaptchaError do
35
+ Recaptcha.configuration.public_key = nil
36
+ recaptcha_tags
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,95 @@
1
+ require 'test/unit'
2
+ require 'rails/version' # For getting the rails version constants
3
+ require 'rubygems'
4
+ require 'mocha'
5
+ require 'net/http'
6
+ require File.dirname(File.expand_path(__FILE__)) + '/../lib/recaptcha'
7
+
8
+ class RecaptchaVerifyTest < Test::Unit::TestCase
9
+ def setup
10
+ Recaptcha.configuration.private_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
11
+ @controller = TestController.new
12
+ @controller.request = stub(:remote_ip => "1.1.1.1")
13
+ @controller.params = {:recaptcha_challenge_field => "challenge", :recaptcha_response_field => "response"}
14
+
15
+ @expected_post_data = {}
16
+ @expected_post_data["privatekey"] = Recaptcha.configuration.private_key
17
+ @expected_post_data["remoteip"] = @controller.request.remote_ip
18
+ @expected_post_data["challenge"] = "challenge"
19
+ @expected_post_data["response"] = "response"
20
+
21
+ @expected_uri = URI.parse(Recaptcha.configuration.verify_url)
22
+ end
23
+
24
+ def test_should_raise_exception_without_private_key
25
+ assert_raise Recaptcha::RecaptchaError do
26
+ Recaptcha.configuration.private_key = nil
27
+ @controller.verify_recaptcha
28
+ end
29
+ end
30
+
31
+ def test_should_return_false_when_key_is_invalid
32
+ expect_http_post(response_with_body("false\ninvalid-site-private-key"))
33
+
34
+ assert !@controller.verify_recaptcha
35
+ assert_equal "invalid-site-private-key", @controller.flash[:recaptcha_error]
36
+ end
37
+
38
+ def test_returns_true_on_success
39
+ @controller.flash[:recaptcha_error] = "previous error that should be cleared"
40
+ expect_http_post(response_with_body("true\n"))
41
+
42
+ assert @controller.verify_recaptcha
43
+ assert_nil @controller.flash[:recaptcha_error]
44
+ end
45
+
46
+ def test_errors_should_be_added_to_model
47
+ expect_http_post(response_with_body("false\nbad-news"))
48
+
49
+ errors = mock
50
+ errors.expects(:add).with(:base, "Word verification response is incorrect, please try again.")
51
+ model = mock(:valid? => false, :errors => errors)
52
+
53
+ assert !@controller.verify_recaptcha(:model => model)
54
+ assert_equal "bad-news", @controller.flash[:recaptcha_error]
55
+ end
56
+
57
+ def test_returns_true_on_success_with_optional_key
58
+ @controller.flash[:recaptcha_error] = "previous error that should be cleared"
59
+ # reset private key
60
+ @expected_post_data["privatekey"] = 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX'
61
+ expect_http_post(response_with_body("true\n"))
62
+
63
+ assert @controller.verify_recaptcha(:private_key => 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX')
64
+ assert_nil @controller.flash[:recaptcha_error]
65
+ end
66
+
67
+ def test_timeout
68
+ expect_http_post(Timeout::Error, :exception => true)
69
+ assert !@controller.verify_recaptcha()
70
+ assert_equal "recaptcha-not-reachable", @controller.flash[:recaptcha_error]
71
+ end
72
+
73
+ private
74
+
75
+ class TestController
76
+ include Recaptcha::Verify
77
+ attr_accessor :request, :params, :flash
78
+
79
+ def initialize
80
+ @flash = {}
81
+ end
82
+ end
83
+
84
+ def expect_http_post(response, options = {})
85
+ unless options[:exception]
86
+ Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)
87
+ else
88
+ Net::HTTP.expects(:post_form).raises response
89
+ end
90
+ end
91
+
92
+ def response_with_body(body)
93
+ stub(:body => body)
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: litecreations-recaptcha
3
+ version: !ruby/object:Gem::Version
4
+ hash: 75
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 3
10
+ - 12
11
+ version: 0.2.3.12
12
+ platform: ruby
13
+ authors:
14
+ - Jason L. Perry
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2009-10-23 00:00:00 +11:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: "This plugin adds helpers for the reCAPTCHA API "
24
+ email: jasper@ambethia.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - LICENSE
31
+ - README.rdoc
32
+ files:
33
+ - CHANGELOG
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - VERSION
38
+ - init.rb
39
+ - lib/recaptcha.rb
40
+ - lib/recaptcha/client_helper.rb
41
+ - lib/recaptcha/configuration.rb
42
+ - lib/recaptcha/merb.rb
43
+ - lib/recaptcha/rails.rb
44
+ - lib/recaptcha/verify.rb
45
+ - recaptcha.gemspec
46
+ - tasks/recaptcha_tasks.rake
47
+ - test/recaptcha_test.rb
48
+ - test/verify_recaptcha_test.rb
49
+ has_rdoc: true
50
+ homepage: http://ambethia.com/recaptcha
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Helpers for the reCAPTCHA API
83
+ test_files:
84
+ - test/recaptcha_test.rb
85
+ - test/verify_recaptcha_test.rb