rack-recaptcha2 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +23 -0
- data/LICENSE +20 -0
- data/README.md +211 -0
- data/Rakefile +12 -0
- data/lib/rack/recaptcha.rb +82 -0
- data/lib/rack/recaptcha/helpers.rb +48 -0
- data/rack-recaptcha.gemspec +23 -0
- data/test/helpers_test.rb +92 -0
- data/test/recaptcha_test.rb +31 -0
- data/test/test_helper.rb +43 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fe4b7b672ad738889bca4ded27940d615d43631d
|
4
|
+
data.tar.gz: 9ec20d4976ccc435823dfc803c7543e808a49a96
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: debec378c1bdbb90e1ca672611b8bd1edcf75a4132376d40d08c04a3df6a957eaafb09914e3b90dcc413452ae0e0492f13ff6854289f2d932eb66244479f1d21
|
7
|
+
data.tar.gz: c1eeee981b98b8953fe907acc66c45273dd6a1901b640d728b8919029ab528e3b634b6aa6f01866c22bcd33a979ec84b2d54bacb714abda46d9d545dd7ea52e0
|
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Arthur Chiu
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
# rack-recaptcha [![](http://stillmaintained.com/achiu/rack-recaptcha.png)](http://stillmaintained.com/achiu/rack-recaptcha)
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/achiu/rack-recaptcha.png)](http://travis-ci.org/achiu/rack-recaptcha)
|
4
|
+
|
5
|
+
Drop this Rack middleware in your web application to enable CAPTCHA verification via Recaptcha API.
|
6
|
+
|
7
|
+
## How to Use
|
8
|
+
|
9
|
+
### Configuration
|
10
|
+
|
11
|
+
First, install the library with:
|
12
|
+
[sudo] gem install rack-recaptcha
|
13
|
+
|
14
|
+
You have to require 'rack-recaptcha' in your gemfile.
|
15
|
+
|
16
|
+
````ruby
|
17
|
+
## Gemfile
|
18
|
+
gem 'rack-recaptcha', :require => 'rack/recaptcha'
|
19
|
+
````
|
20
|
+
|
21
|
+
|
22
|
+
Available options for `Rack::Recaptcha` middleware are:
|
23
|
+
|
24
|
+
* :public_key -- your ReCaptcha API public key *(required)*
|
25
|
+
* :private_key -- your ReCaptcha API private key *(required)*
|
26
|
+
* :proxy_host -- the HTTP Proxy hostname *(optional)*
|
27
|
+
* :proxy_port -- the HTTP Proxy port *(optional)*
|
28
|
+
* :proxy_user -- the HTTP Proxy user *(optional, omit unless the proxy requires it)*
|
29
|
+
* :proxy_password -- the HTTP Proxy password *(optional, omit unless the proxy requires it)*
|
30
|
+
|
31
|
+
Now configure your app to use the middleware. This might be different across each web framework.
|
32
|
+
|
33
|
+
#### Sinatra
|
34
|
+
|
35
|
+
````ruby
|
36
|
+
## app.rb
|
37
|
+
use Rack::Recaptcha, :public_key => 'KEY', :private_key => 'SECRET'
|
38
|
+
helpers Rack::Recaptcha::Helpers
|
39
|
+
````
|
40
|
+
|
41
|
+
#### Padrino
|
42
|
+
|
43
|
+
````ruby
|
44
|
+
## app/app.rb
|
45
|
+
use Rack::Recaptcha, :public_key => 'KEY', :private_key => 'SECRET'
|
46
|
+
helpers Rack::Recaptcha::Helpers
|
47
|
+
````
|
48
|
+
|
49
|
+
|
50
|
+
#### Rails
|
51
|
+
|
52
|
+
````ruby
|
53
|
+
## application.rb:
|
54
|
+
module YourRailsAppName
|
55
|
+
class Application < Rails::Application
|
56
|
+
...
|
57
|
+
config.gem 'rack-recaptcha', :lib => 'rack/recaptcha'
|
58
|
+
config.middleware.use Rack::Recaptcha, :public_key => 'KEY', :private_key => 'SECRET'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
## application_helper.rb or whatever helper you want it in.
|
63
|
+
module ApplicationHelper
|
64
|
+
include Rack::Recaptcha::Helpers
|
65
|
+
end
|
66
|
+
|
67
|
+
## application_controller.rb or whatever controller you want it in.
|
68
|
+
class ApplicationController < ActionController::Base
|
69
|
+
...
|
70
|
+
include Rack::Recaptcha::Helpers
|
71
|
+
...
|
72
|
+
end
|
73
|
+
````
|
74
|
+
|
75
|
+
### Helpers
|
76
|
+
|
77
|
+
The `Rack::Recaptcha::Helpers` module (for Sinatra, Rails, Padrino) adds these methods to your app:
|
78
|
+
|
79
|
+
Return a javascript recaptcha form
|
80
|
+
```ruby
|
81
|
+
recaptcha_tag :challenge
|
82
|
+
```
|
83
|
+
|
84
|
+
Return a non-javascript recaptcha form
|
85
|
+
```ruby
|
86
|
+
recaptcha_tag :noscript
|
87
|
+
```
|
88
|
+
|
89
|
+
Return a ajax recaptcha form
|
90
|
+
```ruby
|
91
|
+
recaptcha_tag :ajax
|
92
|
+
```
|
93
|
+
|
94
|
+
For ajax recaptcha's, you can pass additional options like:
|
95
|
+
```ruby
|
96
|
+
recaptcha_tag :ajax, :display => { :theme => 'red'}
|
97
|
+
```
|
98
|
+
|
99
|
+
For non-ajax recaptcha's, you can pass additional options like:
|
100
|
+
```ruby
|
101
|
+
# Overrides the key set in Middleware
|
102
|
+
recaptcha_tag :challenge, :public_key => KEY
|
103
|
+
|
104
|
+
# Adjust Height and/or Width
|
105
|
+
recaptcha_tag :noscript, :height => 300, :width => 500
|
106
|
+
|
107
|
+
# Adjust the rows and/or columns
|
108
|
+
recaptcha_tag :challenge, :row => 3, :cols => 5
|
109
|
+
|
110
|
+
# Set the language
|
111
|
+
recaptcha_tag :noscript, :language => :en
|
112
|
+
```
|
113
|
+
|
114
|
+
To test whether or not the verification passed, you can use:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
recaptcha_valid?
|
118
|
+
```
|
119
|
+
|
120
|
+
The `recaptcha_valid?` helper can also be overloaded during tests. You
|
121
|
+
can set its response to either true or false by doing the following:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
# Have recaptcha_valid? return true
|
125
|
+
Rack::Recaptcha.test_mode!
|
126
|
+
|
127
|
+
# Or have it return false
|
128
|
+
Rack::Recaptcha.test_mode! :return => false
|
129
|
+
```
|
130
|
+
|
131
|
+
|
132
|
+
For additional options and resources checkout the [customization page](https://developers.google.com/recaptcha/docs/customization)
|
133
|
+
|
134
|
+
#### Example
|
135
|
+
|
136
|
+
In Padrino, here's how you would use the helpers.
|
137
|
+
|
138
|
+
````haml
|
139
|
+
## new.haml
|
140
|
+
- form_tag '/login', :class => 'some_form', :method => 'post' do
|
141
|
+
= text_field_tag :email
|
142
|
+
= password_field_tag :password
|
143
|
+
= recaptcha_tag(:challenge)
|
144
|
+
= submit_tag "Submit"
|
145
|
+
|
146
|
+
## sessions.rb
|
147
|
+
post :create, :map => '/login' do
|
148
|
+
if recaptcha_valid?
|
149
|
+
"passed!"
|
150
|
+
else
|
151
|
+
"failed!"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
````
|
155
|
+
|
156
|
+
In rails, you'll need to use also use the raw method:
|
157
|
+
|
158
|
+
````haml
|
159
|
+
## new.html.haml
|
160
|
+
- form_tag '/login' do
|
161
|
+
= raw recaptcha_tag(:challenge)
|
162
|
+
= submit_tag "Submit"
|
163
|
+
````
|
164
|
+
|
165
|
+
### Contributors
|
166
|
+
|
167
|
+
Daniel Mendler - [minad](https://github.com/minad)
|
168
|
+
|
169
|
+
* support for multiple paths and helpers clean up
|
170
|
+
|
171
|
+
Eric Anderson - [eric1234](https://github.com/eric1234)
|
172
|
+
|
173
|
+
* Make verify independently usable.
|
174
|
+
|
175
|
+
Chad Johnston - [iamthechad](https://github.com/iamthechad)
|
176
|
+
|
177
|
+
* Adding Error Message handling in recaptcha widget
|
178
|
+
|
179
|
+
Eric Hu - [eric-hu](https://github.com/eric-hu)
|
180
|
+
|
181
|
+
* Patching error message issue when no `request` is present
|
182
|
+
|
183
|
+
Tobias Begalke - [elcamino](https://github.com/elcamino)
|
184
|
+
|
185
|
+
* Added HTTP Proxy support
|
186
|
+
|
187
|
+
Julik Tarkhanov - [julik](https://github.com/julik)
|
188
|
+
|
189
|
+
* Adding rack-recaptcha to travis-ci
|
190
|
+
|
191
|
+
Rob Worley - [robworley](https://github.com/robworley)
|
192
|
+
|
193
|
+
* Adding language setting for recaptcha form
|
194
|
+
|
195
|
+
sam71 - [sam71](https://github.com/sam71)
|
196
|
+
|
197
|
+
* Fix recaptcha hanging when used in proxy environment.
|
198
|
+
|
199
|
+
#### Note on Patches/Pull Requests
|
200
|
+
|
201
|
+
* Fork the project.
|
202
|
+
* Make your feature addition or bug fix.
|
203
|
+
* Add tests for it. This is important so I don't break it in a
|
204
|
+
future version unintentionally.
|
205
|
+
* Commit, do not mess with rakefile, version, or history.
|
206
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
207
|
+
* Send me a pull request. Bonus points for topic branches.
|
208
|
+
|
209
|
+
#### Copyright
|
210
|
+
|
211
|
+
Copyright (c) 2010, 2011, 2012 Arthur Chiu. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
Rake::TestTask.new(:test) do |test|
|
6
|
+
test.libs << 'lib' << 'test'
|
7
|
+
test.pattern = 'test/**/*_test.rb'
|
8
|
+
test.warning = true
|
9
|
+
test.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => [:test]
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.expand_path '../recaptcha/helpers', __FILE__
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Rack
|
6
|
+
class Recaptcha
|
7
|
+
API_URL = 'http://www.google.com/recaptcha/api.js'
|
8
|
+
API_SECURE_URL = 'https://www.google.com/recaptcha/api.js'
|
9
|
+
VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'
|
10
|
+
RESPONSE_FIELD = 'g-recaptcha-response'
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :private_key, :public_key, :test_mode, :proxy_host, :proxy_port, :proxy_user, :proxy_password
|
14
|
+
|
15
|
+
def test_mode!(options = {})
|
16
|
+
value = options[:return]
|
17
|
+
self.test_mode = value.nil? ? true : options[:return]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Initialize the Rack Middleware. Some of the available options are:
|
22
|
+
# :public_key -- your ReCaptcha API public key *(required)*
|
23
|
+
# :private_key -- your ReCaptcha API private key *(required)*
|
24
|
+
#
|
25
|
+
def initialize(app,options = {})
|
26
|
+
@app = app
|
27
|
+
@paths = options[:paths] && [options[:paths]].flatten.compact
|
28
|
+
self.class.private_key = options[:private_key]
|
29
|
+
self.class.public_key = options[:public_key]
|
30
|
+
self.class.proxy_host = options[:proxy_host]
|
31
|
+
self.class.proxy_port = options[:proxy_port]
|
32
|
+
self.class.proxy_user = options[:proxy_user]
|
33
|
+
self.class.proxy_password = options[:proxy_password]
|
34
|
+
end
|
35
|
+
|
36
|
+
def call(env)
|
37
|
+
dup._call(env)
|
38
|
+
end
|
39
|
+
|
40
|
+
def _call(env)
|
41
|
+
request = Request.new(env)
|
42
|
+
if request.params[RESPONSE_FIELD]
|
43
|
+
value, msg = verify(
|
44
|
+
request.ip,
|
45
|
+
request.params[RESPONSE_FIELD]
|
46
|
+
)
|
47
|
+
env.merge!('recaptcha.valid' => value, 'recaptcha.msg' => msg)
|
48
|
+
end
|
49
|
+
@app.call(env)
|
50
|
+
end
|
51
|
+
|
52
|
+
def verify(ip, response)
|
53
|
+
params = {
|
54
|
+
'secret' => Rack::Recaptcha.private_key,
|
55
|
+
'remoteip' => ip,
|
56
|
+
'response' => response
|
57
|
+
}
|
58
|
+
|
59
|
+
uri = URI.parse(VERIFY_URL)
|
60
|
+
|
61
|
+
|
62
|
+
if self.class.proxy_host && self.class.proxy_port
|
63
|
+
http = Net::HTTP.Proxy(self.class.proxy_host,
|
64
|
+
self.class.proxy_port,
|
65
|
+
self.class.proxy_user,
|
66
|
+
self.class.proxy_password).start(uri.host, uri.port, :use_ssl => true)
|
67
|
+
else
|
68
|
+
http = Net::HTTP.start(uri.host, uri.port, :use_ssl => true)
|
69
|
+
end
|
70
|
+
|
71
|
+
request = Net::HTTP::Post.new(uri.path)
|
72
|
+
request.form_data = params
|
73
|
+
response = http.request(request)
|
74
|
+
|
75
|
+
parsed_response = JSON.parse(response.body)
|
76
|
+
success = parsed_response['success']
|
77
|
+
error_messages = parsed_response['error-codes'] || []
|
78
|
+
return [success, error_messages.join(',')]
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class Recaptcha
|
5
|
+
module Helpers
|
6
|
+
|
7
|
+
DEFAULT= {
|
8
|
+
:ssl => true
|
9
|
+
}
|
10
|
+
|
11
|
+
# Helper method to output a recaptcha widget.
|
12
|
+
# Available options:
|
13
|
+
#
|
14
|
+
# :public_key - Set the public key. Overrides the key set in Middleware option
|
15
|
+
def recaptcha_widget(options={})
|
16
|
+
options = DEFAULT.merge(options)
|
17
|
+
options[:public_key] ||= Rack::Recaptcha.public_key
|
18
|
+
|
19
|
+
%{<div class="g-recaptcha" data-sitekey="#{options[:public_key]}"></div>}.gsub(/^ +/, '')
|
20
|
+
end
|
21
|
+
|
22
|
+
# Helper method to output the recaptcha javascript.
|
23
|
+
# Available options:
|
24
|
+
#
|
25
|
+
# :language - Set the language
|
26
|
+
def recaptcha_javascript(options={})
|
27
|
+
options = DEFAULT.merge(options)
|
28
|
+
path = options[:ssl] ? Rack::Recaptcha::API_SECURE_URL : Rack::Recaptcha::API_URL
|
29
|
+
params = ''
|
30
|
+
params += "?hl=" + uri_parser.escape(options[:language].to_s) if options[:language]
|
31
|
+
%{<script src='#{path + params}'></script>}
|
32
|
+
end
|
33
|
+
|
34
|
+
# Helper to return whether the recaptcha was accepted.
|
35
|
+
def recaptcha_valid?
|
36
|
+
test = Rack::Recaptcha.test_mode
|
37
|
+
test.nil? ? request.env['recaptcha.valid'] : test
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def uri_parser
|
43
|
+
@uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{rack-recaptcha2}
|
3
|
+
s.version = "1.0.0"
|
4
|
+
s.required_rubygems_version = ">=1.3.6"
|
5
|
+
s.authors = ["Arthur Chiu"]
|
6
|
+
s.date = %q{2010-07-18}
|
7
|
+
s.description = %q{Rack middleware Captcha verification using Recaptcha API.}
|
8
|
+
s.email = %q{mr.arthur.chiu@gmail.com}
|
9
|
+
s.extra_rdoc_files = ["LICENSE", "README.md"]
|
10
|
+
s.files = %w{.document .gitignore LICENSE README.md Rakefile rack-recaptcha.gemspec} + Dir.glob("{lib,test}/**/*")
|
11
|
+
s.homepage = %q{http://github.com/achiu/rack-recaptcha}
|
12
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
s.rubygems_version = %q{1.3.7}
|
15
|
+
s.summary = %q{Rack middleware for Recaptcha}
|
16
|
+
s.test_files = Dir.glob("test/**/*")
|
17
|
+
s.add_runtime_dependency "json", ">= 0"
|
18
|
+
s.add_development_dependency "rake", "~> 0.9.2"
|
19
|
+
s.add_development_dependency "minitest", "~> 4.6"
|
20
|
+
s.add_development_dependency "rack-test", "~> 0.5.7"
|
21
|
+
s.add_development_dependency "fakeweb", "~> 1.3.0"
|
22
|
+
s.add_development_dependency "rr", "~> 1.0.2"
|
23
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require File.expand_path '../test_helper', __FILE__
|
2
|
+
|
3
|
+
class HelperTest
|
4
|
+
attr_accessor :request
|
5
|
+
include Rack::Recaptcha::Helpers
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@request = HelperTest::Request.new
|
9
|
+
end
|
10
|
+
|
11
|
+
class Request
|
12
|
+
attr_accessor :env
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# With "attr_accessor :request" HelperTest has "request" defined as a method
|
17
|
+
# even when @request is set to nil
|
18
|
+
#
|
19
|
+
# defined?(request)
|
20
|
+
# => method
|
21
|
+
# request
|
22
|
+
# => nil
|
23
|
+
# self
|
24
|
+
# => #<HelperTest:0x00000002125000 @request=nil>
|
25
|
+
class HelperTestWithoutRequest
|
26
|
+
include Rack::Recaptcha::Helpers
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Rack::Recaptcha::Helpers do
|
30
|
+
|
31
|
+
def helper_test
|
32
|
+
HelperTest.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def helper_test_without_request
|
36
|
+
HelperTestWithoutRequest.new
|
37
|
+
end
|
38
|
+
|
39
|
+
before do
|
40
|
+
Rack::Recaptcha.public_key = ::PUBLIC_KEY
|
41
|
+
end
|
42
|
+
|
43
|
+
describe ".recaptcha_widget" do
|
44
|
+
|
45
|
+
it "should render recaptcha div" do
|
46
|
+
topic = helper_test.recaptcha_widget()
|
47
|
+
|
48
|
+
assert_match %r{<div class="g-recaptcha" data-sitekey=".*"></div>}, topic
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should use given public key" do
|
52
|
+
topic = helper_test.recaptcha_widget(:public_key => 'test')
|
53
|
+
|
54
|
+
assert_match %r{<div class="g-recaptcha" data-sitekey="test"></div>}, topic
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe ".recaptcha_valid?" do
|
59
|
+
it "should assert that it passes when recaptcha.valid is true" do
|
60
|
+
Rack::Recaptcha.test_mode = nil
|
61
|
+
mock(helper_test.request.env).[]('recaptcha.valid').returns(true)
|
62
|
+
assert helper_test.recaptcha_valid?
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should refute that it passes when recaptcha.valid is false" do
|
66
|
+
Rack::Recaptcha.test_mode = nil
|
67
|
+
mock(helper_test.request.env).[]('recaptcha.valid').returns(false)
|
68
|
+
refute helper_test.recaptcha_valid?
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should assert that it passes when test mode set to pass" do
|
72
|
+
Rack::Recaptcha.test_mode!
|
73
|
+
assert helper_test.recaptcha_valid?
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should assert that it passes when test mode set to fail" do
|
77
|
+
Rack::Recaptcha.test_mode! :return => false
|
78
|
+
refute helper_test.recaptcha_valid?
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe ".recaptcha_widget without request object" do
|
83
|
+
|
84
|
+
it "should work without request object" do
|
85
|
+
topic = helper_test_without_request.recaptcha_widget()
|
86
|
+
|
87
|
+
assert_match %r{<div class="g-recaptcha" data-sitekey=".*"></div>}, topic
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path '../test_helper', __FILE__
|
2
|
+
|
3
|
+
describe Rack::Recaptcha do
|
4
|
+
before do
|
5
|
+
FakeWeb.allow_net_connect = false
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#test_mode!" do
|
9
|
+
it "should set test_mode to true" do
|
10
|
+
Rack::Recaptcha.test_mode!
|
11
|
+
assert Rack::Recaptcha.test_mode
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should set test_mode to true" do
|
15
|
+
Rack::Recaptcha.test_mode! :return => false
|
16
|
+
refute Rack::Recaptcha.test_mode
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should hit the login and pass" do
|
21
|
+
FakeWeb.register_uri(:post, Rack::Recaptcha::VERIFY_URL, :body => '{"success":true}')
|
22
|
+
post("/login", 'g-recaptcha-response' => 'response')
|
23
|
+
assert_equal 'post login', last_response.body
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should hit the login and fail" do
|
27
|
+
FakeWeb.register_uri(:post, Rack::Recaptcha::VERIFY_URL, :body => '{"success":false,"error-codes":["invalid-input-response"]}')
|
28
|
+
post("/login", 'g-recaptcha-response' => 'response')
|
29
|
+
assert_equal 'post fail', last_response.body
|
30
|
+
end
|
31
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'minitest'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'rack/test'
|
5
|
+
require 'rack/mock'
|
6
|
+
require 'rack/utils'
|
7
|
+
require 'rack/session/cookie'
|
8
|
+
require 'rack/builder'
|
9
|
+
require 'fakeweb'
|
10
|
+
require 'rr'
|
11
|
+
require File.expand_path '../../lib/rack/recaptcha', __FILE__
|
12
|
+
|
13
|
+
PUBLIC_KEY = '0'*40
|
14
|
+
PRIVATE_KEY = 'X'*40
|
15
|
+
|
16
|
+
class MiniTest::Spec
|
17
|
+
include Rack::Test::Methods
|
18
|
+
include RR::Adapters::MiniTest
|
19
|
+
|
20
|
+
def app
|
21
|
+
main_app = lambda { |env|
|
22
|
+
request = Rack::Request.new(env)
|
23
|
+
return_code, body_text =
|
24
|
+
case request.path
|
25
|
+
when '/' then [200,'Hello world']
|
26
|
+
when '/login'
|
27
|
+
if request.post?
|
28
|
+
env['recaptcha.valid'] ? [200, 'post login'] : [200, 'post fail']
|
29
|
+
else
|
30
|
+
[200,'login']
|
31
|
+
end
|
32
|
+
else
|
33
|
+
[404,'Nothing here']
|
34
|
+
end
|
35
|
+
[return_code,{'Content-type' => 'text/plain'}, [body_text]]
|
36
|
+
}
|
37
|
+
|
38
|
+
builder = Rack::Builder.new
|
39
|
+
builder.use Rack::Recaptcha, :private_key => PRIVATE_KEY, :public_key => PUBLIC_KEY
|
40
|
+
builder.run main_app
|
41
|
+
builder.to_app
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-recaptcha2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arthur Chiu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2010-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.9.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack-test
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.5.7
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.5.7
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: fakeweb
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.3.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.0.2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.0.2
|
97
|
+
description: Rack middleware Captcha verification using Recaptcha API.
|
98
|
+
email: mr.arthur.chiu@gmail.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files:
|
102
|
+
- LICENSE
|
103
|
+
- README.md
|
104
|
+
files:
|
105
|
+
- .document
|
106
|
+
- .gitignore
|
107
|
+
- LICENSE
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- rack-recaptcha.gemspec
|
111
|
+
- lib/rack/recaptcha.rb
|
112
|
+
- lib/rack/recaptcha/helpers.rb
|
113
|
+
- test/helpers_test.rb
|
114
|
+
- test/test_helper.rb
|
115
|
+
- test/recaptcha_test.rb
|
116
|
+
homepage: http://github.com/achiu/rack-recaptcha
|
117
|
+
licenses: []
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options:
|
121
|
+
- --charset=UTF-8
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 1.3.6
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.0.3
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Rack middleware for Recaptcha
|
140
|
+
test_files:
|
141
|
+
- test/helpers_test.rb
|
142
|
+
- test/test_helper.rb
|
143
|
+
- test/recaptcha_test.rb
|