reverse_captcha 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Daniel Lopes
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.
@@ -0,0 +1,40 @@
1
+ = reverse_captcha
2
+
3
+ Simple reverse captcha solution that consist in create a hidden field that should be always empty. If you don't know reverse
4
+ captcha aproach search on the internet.
5
+
6
+ == Usage
7
+
8
+ The gem is only compatible with Rails 3.0 (if you need a Rails 2.+ version send a pull request). Add it to your Gemfile:
9
+
10
+ gem 'reverse_captcha'
11
+
12
+ After that you will be able to create a captcha attribute in your model (ActiveRecord only)
13
+
14
+
15
+ class Comment < ActiveRecord::Base
16
+ captcha :nickname
17
+ end
18
+
19
+ Now in your view you should add a captcha field like below:
20
+
21
+ form_for @comment do |f|
22
+ ...
23
+ <%= f.captcha %>
24
+ end
25
+
26
+ And that's it. If the nickname attribute is filled Rails will fail to save with a validation error.
27
+
28
+ == Note on Patches/Pull Requests
29
+
30
+ * Fork the project.
31
+ * Make your feature addition or bug fix.
32
+ * Add tests for it. This is important so I don't break it in a
33
+ future version unintentionally.
34
+ * Commit, do not mess with rakefile, version, or history.
35
+ (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)
36
+ * Send me a pull request. Bonus points for topic branches.
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2010 Daniel Lopes. See LICENSE for details.
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "reverse_captcha"
8
+ gem.summary = %Q{simple solution for reverse captcha in Rails apps}
9
+ gem.description = %Q{Simple reverse captcha solution that consist in create a hidden field that should be always empty.}
10
+ gem.email = "danielvlopes@gmail.com"
11
+ gem.homepage = "http://github.com/danielvlopes/reverse_captcha"
12
+ gem.authors = ["Daniel Lopes"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/test_*.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+ task :test => :check_dependencies
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "reverse_captcha #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,7 @@
1
+ module ReverseCaptcha
2
+ autoload :Model, 'reverse_captcha/model'
3
+ autoload :View, 'reverse_captcha/view'
4
+ end
5
+
6
+ ActiveRecord::Base.send :include, ReverseCaptcha::Model
7
+ ActionView::Helpers::FormBuilder.send :include, ReverseCaptcha::View
@@ -0,0 +1,31 @@
1
+ module ReverseCaptcha
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :captcha_attribute
7
+ end
8
+
9
+ module ClassMethods
10
+ def captcha(attribute)
11
+ self.captcha_attribute = attribute
12
+ attr_accessor attribute
13
+ validate :humanity
14
+ end
15
+ end
16
+
17
+ def captcha_attribute
18
+ self.class.captcha_attribute
19
+ end
20
+
21
+ def spam?
22
+ true unless send(captcha_attribute).blank?
23
+ end
24
+
25
+ protected
26
+ def humanity
27
+ errors.add(captcha_attribute, 'need to be blank') if spam?
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ module ReverseCaptcha
2
+ module View
3
+
4
+ def captcha(options={})
5
+ text_field object.captcha_attribute, options.reverse_merge(:style => "display:none;")
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,63 @@
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{reverse_captcha}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Daniel Lopes"]
12
+ s.date = %q{2010-08-13}
13
+ s.description = %q{Simple reverse captcha solution that consist in create a hidden field that should be always empty.}
14
+ s.email = %q{danielvlopes@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/reverse_captcha.rb",
27
+ "lib/reverse_captcha/model.rb",
28
+ "lib/reverse_captcha/view.rb",
29
+ "reverse_captcha.gemspec",
30
+ "test/schema.rb",
31
+ "test/support/comment.rb",
32
+ "test/support/mock_controller.rb",
33
+ "test/support/mock_response.rb",
34
+ "test/test_helper.rb",
35
+ "test/test_model.rb",
36
+ "test/test_view.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/danielvlopes/reverse_captcha}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{simple solution for reverse captcha in Rails apps}
43
+ s.test_files = [
44
+ "test/schema.rb",
45
+ "test/support/comment.rb",
46
+ "test/support/mock_controller.rb",
47
+ "test/support/mock_response.rb",
48
+ "test/test_helper.rb",
49
+ "test/test_model.rb",
50
+ "test/test_view.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ else
59
+ end
60
+ else
61
+ end
62
+ end
63
+
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :comments do |t|
3
+ t.string :author
4
+ t.string :email
5
+ t.text :body
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ class Comment < ActiveRecord::Base; end
@@ -0,0 +1,15 @@
1
+ class MockController
2
+ attr_accessor :action_name
3
+
4
+ def _routes
5
+ self
6
+ end
7
+
8
+ def action_name
9
+ @action_name || "edit"
10
+ end
11
+
12
+ def url_for(*args)
13
+ "http://example.com"
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ class MockResponse
2
+
3
+ def initialize(test_case)
4
+ @test_case = test_case
5
+ end
6
+
7
+ def content_type
8
+ 'text/html'
9
+ end
10
+
11
+ def body
12
+ @test_case.send :output_buffer
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require "active_record"
4
+ require 'action_view'
5
+ require 'action_view/template'
6
+ require 'action_view/test_case'
7
+
8
+ require 'reverse_captcha'
9
+
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
11
+
12
+ Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
13
+
14
+ load "schema.rb"
15
+
16
+ class ActionView::TestCase
17
+ setup :set_controller
18
+ setup :set_response
19
+ setup :setup_new_comment
20
+
21
+ def set_controller
22
+ @controller = MockController.new
23
+ end
24
+
25
+ def set_response
26
+ @response = MockResponse.new(self)
27
+ end
28
+
29
+ def setup_new_comment(options={})
30
+ @comment = Comment.new({
31
+ :id => 1,
32
+ :author => "Daniel",
33
+ :email => "daniel@google.com",
34
+ :body => "bla bla bla"
35
+ }.merge(options))
36
+ end
37
+
38
+ def protect_against_forgery?
39
+ false
40
+ end
41
+
42
+ def comment_path(*args)
43
+ '/comments'
44
+ end
45
+ alias :comments_path :comment_path
46
+ end
@@ -0,0 +1,37 @@
1
+ require "test_helper"
2
+
3
+ class TestModel < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @comment = Comment.new
7
+ Comment.captcha :nickname
8
+ end
9
+
10
+ def test_respond_to_captcha
11
+ assert Comment.respond_to?(:captcha)
12
+ end
13
+
14
+ def test_error_message_on_attribute
15
+ @comment.update_attributes :email => "jonh@gmail", :nickname => "teste"
16
+ assert @comment.errors[:nickname], 'should be blank'
17
+ end
18
+
19
+ def test_not_save_in_database
20
+ @comment.nickname = "teste"
21
+ assert !@comment.save
22
+ end
23
+
24
+ def test_spam
25
+ @comment.nickname = "jonh doe"
26
+ assert @comment.spam?
27
+ end
28
+
29
+ def test_instance_level_captcha_attribute
30
+ assert @comment.captcha_attribute, :nickname
31
+ end
32
+
33
+ def test_class_level_captcha_attribute
34
+ assert @comment.captcha_attribute, :nickname
35
+ end
36
+
37
+ end
@@ -0,0 +1,26 @@
1
+ require "test_helper"
2
+
3
+ class TestView < ActionView::TestCase
4
+ tests ReverseCaptcha::View
5
+
6
+ def setup
7
+ Comment.captcha :nickname
8
+ end
9
+
10
+ test "captcha field map to object captcha attribute" do
11
+ concat(form_for @comment do |f|
12
+ concat f.captcha
13
+ end)
14
+
15
+ assert_select "form input#comment_nickname"
16
+ end
17
+
18
+ test "captcha field is hidden by css" do
19
+ concat(form_for @comment do |f|
20
+ concat f.captcha
21
+ end)
22
+
23
+ assert_select "form input#comment_nickname[style='display:none;']"
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reverse_captcha
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Daniel Lopes
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-13 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simple reverse captcha solution that consist in create a hidden field that should be always empty.
23
+ email: danielvlopes@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - .document
33
+ - .gitignore
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - VERSION
38
+ - lib/reverse_captcha.rb
39
+ - lib/reverse_captcha/model.rb
40
+ - lib/reverse_captcha/view.rb
41
+ - reverse_captcha.gemspec
42
+ - test/schema.rb
43
+ - test/support/comment.rb
44
+ - test/support/mock_controller.rb
45
+ - test/support/mock_response.rb
46
+ - test/test_helper.rb
47
+ - test/test_model.rb
48
+ - test/test_view.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/danielvlopes/reverse_captcha
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: simple solution for reverse captcha in Rails apps
83
+ test_files:
84
+ - test/schema.rb
85
+ - test/support/comment.rb
86
+ - test/support/mock_controller.rb
87
+ - test/support/mock_response.rb
88
+ - test/test_helper.rb
89
+ - test/test_model.rb
90
+ - test/test_view.rb