validates_captcha 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ MW = ValidatesCaptcha::Middleware::Simple
4
+
5
+ class MiddlewareTest < ValidatesCaptcha::TestCase
6
+ test "defines an instance level #image_path method" do
7
+ assert_respond_to MW.new, :image_path
8
+ end
9
+
10
+ test "instance level #image_path method accepts an argument" do
11
+ assert_nothing_raised { MW.new.image_path('test') }
12
+ end
13
+
14
+ test "instance level #image_path method returns a string" do
15
+ assert_kind_of String, MW.new.image_path('test')
16
+ end
17
+
18
+ test "defines an instance level #regenerate_path method" do
19
+ assert_respond_to MW.new, :image_path
20
+ end
21
+
22
+ test "instance level #regenerate_path method returns a string" do
23
+ assert_kind_of String, MW.new.regenerate_path
24
+ end
25
+
26
+ test "calling middleware with unrecognized path should have response status 404" do
27
+ result = MW.new.call 'PATH_INFO' => '/unrecognized'
28
+
29
+ assert_equal 404, result.first
30
+ end
31
+
32
+ test "calling middleware with recognized path should not have response status 404" do
33
+ result = MW.new.call 'PATH_INFO' => ValidatesCaptcha.captcha_image_path('abc123')
34
+
35
+ assert_not_equal 404, result.first
36
+ end
37
+
38
+ test "calling middleware with valid encrypted captcha code should have response status 200" do
39
+ encrypted_code = ValidatesCaptcha.encrypt_captcha_code('hello')
40
+ result = MW.new.call 'PATH_INFO' => "/captchas/#{encrypted_code}"
41
+
42
+ assert_equal 200, result.first
43
+ end
44
+
45
+ test "calling middleware with valid encrypted captcha code should have expected content type response header" do
46
+ encrypted_code = ValidatesCaptcha.encrypt_captcha_code('hello')
47
+ result = MW.new.call 'PATH_INFO' => "/captchas/#{encrypted_code}"
48
+
49
+ assert result.second.key?('Content-Type')
50
+ assert_equal ValidatesCaptcha.captcha_image_mime_type, result.second['Content-Type']
51
+ end
52
+
53
+ test "calling middleware with invalid encrypted captcha code should have response status 422" do
54
+ result = MW.new.call 'PATH_INFO' => "/captchas/invalid123"
55
+
56
+ assert_equal 422, result.first
57
+ end
58
+
59
+ test "calling middleware with regenerate path should have response status 200" do
60
+ result = MW.new.call 'PATH_INFO' => ValidatesCaptcha.regenerate_captcha_path
61
+
62
+ assert_equal 200, result.first
63
+ end
64
+
65
+ test "calling middleware with regenerate path should have content type response header set to application/json" do
66
+ result = MW.new.call 'PATH_INFO' => ValidatesCaptcha.regenerate_captcha_path
67
+
68
+ assert result.second.key?('Content-Type')
69
+ assert_equal 'application/json', result.second['Content-Type']
70
+ end
71
+ end
@@ -0,0 +1,130 @@
1
+ require 'test_helper'
2
+
3
+ class ModelValidationTest < ValidatesCaptcha::TestCase
4
+ test "defines an accessible attribute named +captcha+" do
5
+ assert Widget.accessible_attributes.include?('captcha')
6
+ end
7
+
8
+ test "defines an instance level #captcha method" do
9
+ assert_respond_to Widget.new, :captcha
10
+ end
11
+
12
+ test "defines a instance level #captcha= method" do
13
+ assert_respond_to Widget.new, :captcha=
14
+ end
15
+
16
+ test "assigned value to #captcha= should equal return value of #captcha" do
17
+ widget = Widget.new
18
+ widget.captcha = 'abc123'
19
+
20
+ assert_equal 'abc123', widget.captcha
21
+ end
22
+
23
+ test "defines an accessible attribute named +encrypted_captcha+" do
24
+ assert Widget.accessible_attributes.include?('encrypted_captcha')
25
+ end
26
+
27
+ test "defines an instance level #encrypted_captcha method" do
28
+ assert_respond_to Widget.new, :encrypted_captcha
29
+ end
30
+
31
+ test "defines an instance level #encrypted_captcha= method" do
32
+ assert_respond_to Widget.new, :encrypted_captcha=
33
+ end
34
+
35
+ test "value assigned to #encrypted_captcha= should equal return value of #encrypted_captcha" do
36
+ widget = Widget.new
37
+ widget.encrypted_captcha = 'asdfghjk3456789'
38
+
39
+ assert_equal 'asdfghjk3456789', widget.encrypted_captcha
40
+ end
41
+
42
+ test "defines #validate_captcha method callback of kind +validate+" do
43
+ assert Widget.validate_callback_chain.any? { |callback| callback.method == :validate_captcha && callback.kind == :validate }
44
+ end
45
+
46
+ test "defines a class level with_captcha_validation method" do
47
+ assert_respond_to Widget, :with_captcha_validation
48
+ end
49
+
50
+ test "not within a #with_captcha_validation block, calling valid? should return true if no captcha is set" do
51
+ widget = Widget.new
52
+
53
+ assert widget.valid?
54
+ end
55
+
56
+ test "not within a #with_captcha_validation block, calling valid? should return true if an empty captcha is set" do
57
+ widget = Widget.new
58
+ widget.captcha = ' '
59
+
60
+ assert widget.valid?
61
+ end
62
+
63
+ test "not within a #with_captcha_validation block, calling valid? should return true if an invalid captcha is set" do
64
+ widget = Widget.new
65
+ widget.captcha = 'J§$%ZT&/ÖGHJ'
66
+
67
+ assert widget.valid?
68
+ end
69
+
70
+ test "not within a #with_captcha_validation block, calling valid? should return true if a valid captcha is set" do
71
+ widget = Widget.new
72
+ widget.captcha = ValidatesCaptcha.decrypt_captcha_code(widget.encrypted_captcha)
73
+
74
+ assert widget.valid?
75
+ end
76
+
77
+ test "within a #with_captcha_validation block, calling valid? should return false if no captcha is set" do
78
+ Widget.with_captcha_validation do
79
+ widget = Widget.new
80
+
81
+ assert !widget.valid?
82
+ assert_equal 1, Array.wrap(widget.errors[:captcha]).size
83
+ assert Array.wrap(widget.errors[:captcha]).first.include?('blank')
84
+ end
85
+ end
86
+
87
+ test "within a #with_captcha_validation block, calling valid? should return false if an empty captcha is set" do
88
+ Widget.with_captcha_validation do
89
+ widget = Widget.new
90
+ widget.captcha = ' '
91
+
92
+ assert !widget.valid?
93
+ assert_equal 1, Array.wrap(widget.errors[:captcha]).size
94
+ assert Array.wrap(widget.errors[:captcha]).first.include?('blank')
95
+ end
96
+ end
97
+
98
+ test "within a #with_captcha_validation block, calling valid? should return false if an invalid captcha is set" do
99
+ Widget.with_captcha_validation do
100
+ widget = Widget.new
101
+ widget.captcha = 'J§$%ZT&/ÖGHJ'
102
+
103
+ assert !widget.valid?
104
+ assert_equal 1, Array.wrap(widget.errors[:captcha]).size
105
+ assert Array.wrap(widget.errors[:captcha]).first.include?('invalid')
106
+ end
107
+ end
108
+
109
+ test "within a #with_captcha_validation block, calling valid? should return true if a valid captcha is set" do
110
+ Widget.with_captcha_validation do
111
+ widget = Widget.new
112
+ widget.captcha = ValidatesCaptcha.decrypt_captcha_code(widget.encrypted_captcha)
113
+
114
+ assert widget.valid?
115
+ end
116
+ end
117
+
118
+ test "with #with_captcha_validation block, calling valid? before and after the block should return true if valid? returned false within block" do
119
+ widget = Widget.new
120
+ widget.captcha = 'J§$%ZT&/ÖGHJ'
121
+
122
+ assert widget.valid?
123
+
124
+ Widget.with_captcha_validation do
125
+ assert !widget.valid?
126
+ end
127
+
128
+ assert widget.valid?
129
+ end
130
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ RE = ValidatesCaptcha::ReversibleEncrypter::Simple
4
+
5
+ class ReversibleEncrypterTest < ValidatesCaptcha::TestCase
6
+ test "defines an instance level #encrypt method" do
7
+ assert_respond_to RE.new, :encrypt
8
+ end
9
+
10
+ test "instance level #encrypt method returns a string" do
11
+ assert_kind_of String, RE.new.encrypt('abc')
12
+ end
13
+
14
+ test "defines an instance level #decrypt method" do
15
+ assert_respond_to RE.new, :decrypt
16
+ end
17
+
18
+ test "instance level #decrypt method returns nil if decryption failes" do
19
+ assert_nil RE.new.decrypt('invalid')
20
+ end
21
+
22
+ test "decryption of encryption of string should equal the string" do
23
+ %w(d3crypti0n 3ncrypt3d 5trin9 sh0u1d equ41 th3 c4ptch4).each do |captcha|
24
+ assert_equal captcha, RE.new.decrypt(RE.new.encrypt(captcha))
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,114 @@
1
+ require 'test_helper'
2
+
3
+ SG = ValidatesCaptcha::StringGenerator::Simple
4
+
5
+ class StringGeneratorTest < ValidatesCaptcha::TestCase
6
+ test "defines a class level #alphabet method" do
7
+ assert_respond_to SG, :alphabet
8
+ end
9
+
10
+ test "class level #alphabet method returns a string" do
11
+ assert_kind_of String, SG.alphabet
12
+ assert_greater_than 0, SG.alphabet.length
13
+ end
14
+
15
+ test "defines a class level #alphabet= method" do
16
+ assert_respond_to SG, :alphabet=
17
+ end
18
+
19
+ test "#alphabet method's return value should equal the value set using the #alphabet= method" do
20
+ old_alphabet = SG.alphabet
21
+
22
+ SG.alphabet = 'abc'
23
+ assert_equal 'abc', SG.alphabet
24
+
25
+ SG.alphabet = old_alphabet
26
+ end
27
+
28
+ test "#alphabet= method converts supplied value to a string" do
29
+ old_alphabet = SG.alphabet
30
+
31
+ [:abc, 123, %w(x y z)].each do |value|
32
+ SG.alphabet = value
33
+ assert_equal value.to_s, SG.alphabet
34
+ end
35
+
36
+ SG.alphabet = old_alphabet
37
+ end
38
+
39
+ test "#alphabet= method removes whitespace from supplied value" do
40
+ old_alphabet = SG.alphabet
41
+
42
+ SG.alphabet = " abc d ef\n"
43
+ assert_equal "abcdef", SG.alphabet
44
+
45
+ SG.alphabet = old_alphabet
46
+ end
47
+
48
+ test "#alphabet= method raises error if supplied value is blank" do
49
+ assert_raise RuntimeError do
50
+ SG.alphabet = ''
51
+ end
52
+ end
53
+
54
+ test "defines a class level #length method" do
55
+ assert_respond_to SG, :length
56
+ end
57
+
58
+ test "class level #length method returns a number" do
59
+ assert_kind_of Fixnum, SG.length
60
+ end
61
+
62
+ test "defines a class level #length= method" do
63
+ assert_respond_to SG, :length=
64
+ end
65
+
66
+ test "#length method's return value should equal the value set using the #length= method" do
67
+ old_length = SG.length
68
+
69
+ SG.length = 42
70
+ assert_equal 42, SG.length
71
+
72
+ SG.length = old_length
73
+ end
74
+
75
+ test "defines an instance level #generate method" do
76
+ assert_respond_to SG.new, :generate
77
+ end
78
+
79
+ test "instance level #generate method returns a string" do
80
+ assert_kind_of String, SG.new.generate
81
+ end
82
+
83
+ test "calling #generate should return a string of #length size" do
84
+ 10.times do
85
+ assert_equal SG.length, SG.new.generate.length
86
+ end
87
+ end
88
+
89
+ test "calling #generate with custom set #length should return a string of #length size" do
90
+ old_length = SG.length
91
+ SG.length = 42
92
+
93
+ 10.times do
94
+ assert_equal 42, SG.new.generate.length
95
+ end
96
+
97
+ SG.length = old_length
98
+ end
99
+
100
+ test "string returned from #generate should only contain chars from #alphabet" do
101
+ old_alphabet = SG.alphabet
102
+ old_length = SG.length
103
+
104
+ SG.alphabet = 'Abc123'
105
+ SG.length = 1000
106
+ generated = SG.new.generate
107
+
108
+ assert generated.tr('Abc123', ' ').blank?
109
+ assert !generated.tr('abc123', ' ').blank?
110
+
111
+ SG.length = old_length
112
+ SG.alphabet = old_alphabet
113
+ end
114
+ end
@@ -0,0 +1,135 @@
1
+ require 'test_helper'
2
+
3
+ class ValidatesCaptchaTest < ValidatesCaptcha::TestCase
4
+ test "defines a class level #version method" do
5
+ assert_respond_to ValidatesCaptcha, :version
6
+ end
7
+
8
+ test "class level #version method returns a valid version" do
9
+ assert_match /^\d+\.\d+\.\w+$/, ValidatesCaptcha.version
10
+ end
11
+
12
+ test "defines a class level #image_generator method" do
13
+ assert_respond_to ValidatesCaptcha, :image_generator
14
+ end
15
+
16
+ test "defines a class level #image_generator= method" do
17
+ assert_respond_to ValidatesCaptcha, :image_generator=
18
+ end
19
+
20
+ test "defines a class level #string_generator method" do
21
+ assert_respond_to ValidatesCaptcha, :string_generator
22
+ end
23
+
24
+ test "defines a class level #string_generator= method" do
25
+ assert_respond_to ValidatesCaptcha, :string_generator=
26
+ end
27
+
28
+ test "defines a class level #reversible_encrypter method" do
29
+ assert_respond_to ValidatesCaptcha, :reversible_encrypter
30
+ end
31
+
32
+ test "defines a class level #reversible_encrypter= method" do
33
+ assert_respond_to ValidatesCaptcha, :reversible_encrypter=
34
+ end
35
+
36
+ test "defines a class level #middleware method" do
37
+ assert_respond_to ValidatesCaptcha, :middleware
38
+ end
39
+
40
+ test "defines a class level #middleware= method" do
41
+ assert_respond_to ValidatesCaptcha, :middleware=
42
+ end
43
+
44
+ test "defines a class level #generate_captcha_code method" do
45
+ assert_respond_to ValidatesCaptcha, :generate_captcha_code
46
+ end
47
+
48
+ test "class level #generate_captcha_code returns a string" do
49
+ assert_kind_of String, ValidatesCaptcha.generate_captcha_code
50
+ end
51
+
52
+ test "defines a class level #generate_captcha_image method" do
53
+ assert_respond_to ValidatesCaptcha, :generate_captcha_image
54
+ end
55
+
56
+ test "defines a class level #captcha_image_file_extension method" do
57
+ assert_respond_to ValidatesCaptcha, :captcha_image_file_extension
58
+ end
59
+
60
+ test "result of #captcha_image_file_extension should equal result of #image_generator.image_file_extension" do
61
+ result1 = ValidatesCaptcha.captcha_image_file_extension
62
+ result2 = ValidatesCaptcha.image_generator.image_file_extension
63
+
64
+ assert_not_nil result1
65
+ assert_not_nil result2
66
+ assert_equal result1, result2
67
+ end
68
+
69
+ test "defines a class level #captcha_image_mime_type method" do
70
+ assert_respond_to ValidatesCaptcha, :captcha_image_mime_type
71
+ end
72
+
73
+ test "result of #captcha_image_mime_type should equal result of #image_generator.image_mime_type" do
74
+ result1 = ValidatesCaptcha.captcha_image_mime_type
75
+ result2 = ValidatesCaptcha.image_generator.image_mime_type
76
+
77
+ assert_not_nil result1
78
+ assert_not_nil result2
79
+ assert_equal result1, result2
80
+ end
81
+
82
+ test "defines a class level #encrypt_captcha_code method" do
83
+ assert_respond_to ValidatesCaptcha, :encrypt_captcha_code
84
+ end
85
+
86
+ test "given the same argument, result of #encrypt_captcha_code should equal result of #reversible_encrypter.encrypt" do
87
+ result1 = ValidatesCaptcha.encrypt_captcha_code('123456')
88
+ result2 = ValidatesCaptcha.reversible_encrypter.encrypt('123456')
89
+
90
+ assert_not_nil result1
91
+ assert_not_nil result2
92
+ assert_equal result1, result2
93
+ end
94
+
95
+ test "defines a class level #decrypt_captcha_code method" do
96
+ assert_respond_to ValidatesCaptcha, :decrypt_captcha_code
97
+ end
98
+
99
+ test "given the same argument, result of #decrypt_captcha_code should equal result of #reversible_encrypter.decrypt" do
100
+ encrypted_code = ValidatesCaptcha.encrypt_captcha_code('123456')
101
+
102
+ result1 = ValidatesCaptcha.decrypt_captcha_code(encrypted_code)
103
+ result2 = ValidatesCaptcha.reversible_encrypter.decrypt(encrypted_code)
104
+
105
+ assert_not_nil result1
106
+ assert_not_nil result2
107
+ assert_equal result1, result2
108
+ end
109
+
110
+ test "defines a class level #captcha_image_path method" do
111
+ assert_respond_to ValidatesCaptcha, :captcha_image_path
112
+ end
113
+
114
+ test "given the same argument, result of #captcha_image_path should equal result of #middleware.image_path" do
115
+ result1 = ValidatesCaptcha.captcha_image_path('123456')
116
+ result2 = ValidatesCaptcha.middleware.image_path('123456')
117
+
118
+ assert_not_nil result1
119
+ assert_not_nil result2
120
+ assert_equal result1, result2
121
+ end
122
+
123
+ test "defines a class level #regenerate_captcha_path method" do
124
+ assert_respond_to ValidatesCaptcha, :regenerate_captcha_path
125
+ end
126
+
127
+ test "result of #regenerate_captcha_path should equal result of #middleware.regenerate_path" do
128
+ result1 = ValidatesCaptcha.regenerate_captcha_path
129
+ result2 = ValidatesCaptcha.middleware.regenerate_path
130
+
131
+ assert_not_nil result1
132
+ assert_not_nil result2
133
+ assert_equal result1, result2
134
+ end
135
+ end
@@ -0,0 +1,26 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../../lib'
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+
6
+ require 'validates_captcha'
7
+
8
+ begin
9
+ require 'ruby-debug'
10
+ Debugger.start
11
+ rescue LoadError
12
+ end
13
+
14
+ require 'active_record'
15
+
16
+ ActiveRecord::Schema.verbose = false
17
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
18
+ ActiveRecord::Schema.define :version => 1 do
19
+ create_table :widgets do |t|
20
+ end
21
+ end
22
+
23
+ class Widget < ActiveRecord::Base
24
+ include ValidatesCaptcha::ModelValidation
25
+ end
26
+
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_captcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Andert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-26 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.2
34
+ version:
35
+ description: "An image captcha verification approach for Rails apps, directly integrated into ActiveRecord\xE2\x80\x99s validation mechanism and providing helpers for ActionController and ActionView."
36
+ email: martin@mehringen.de
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - CHANGELOG.rdoc
43
+ - MIT-LICENSE
44
+ - README.rdoc
45
+ files:
46
+ - README.rdoc
47
+ - CHANGELOG.rdoc
48
+ - Rakefile
49
+ - MIT-LICENSE
50
+ - lib/validates_captcha/version.rb
51
+ - lib/validates_captcha/test_case.rb
52
+ - lib/validates_captcha/controller_validation.rb
53
+ - lib/validates_captcha/reversible_encrypter/simple.rb
54
+ - lib/validates_captcha/model_validation.rb
55
+ - lib/validates_captcha/string_generator/simple.rb
56
+ - lib/validates_captcha/form_helper.rb
57
+ - lib/validates_captcha/image_generator/simple.rb
58
+ - lib/validates_captcha/middleware/simple.rb
59
+ - lib/validates_captcha/form_builder.rb
60
+ - lib/validates_captcha.rb
61
+ - test/cases/string_generator_test.rb
62
+ - test/cases/reversible_encrypter_test.rb
63
+ - test/cases/controller_validation_test.rb
64
+ - test/cases/middleware_test.rb
65
+ - test/cases/validates_captcha_test.rb
66
+ - test/cases/image_generator_test.rb
67
+ - test/cases/model_validation_test.rb
68
+ - test/test_helper.rb
69
+ - rails/init.rb
70
+ has_rdoc: true
71
+ homepage: http://m4n.github.com/validates_captcha
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --title
77
+ - Validates Captcha 0.9.0
78
+ - --main
79
+ - README.rdoc
80
+ - --line-numbers
81
+ - --inline-source
82
+ - --charset
83
+ - utf-8
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ version:
98
+ requirements: []
99
+
100
+ rubyforge_project: validatecaptcha
101
+ rubygems_version: 1.3.5
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Image captcha verification for Rails using ActiveRecord's validation mechanism
105
+ test_files:
106
+ - test/cases/string_generator_test.rb
107
+ - test/cases/reversible_encrypter_test.rb
108
+ - test/cases/controller_validation_test.rb
109
+ - test/cases/middleware_test.rb
110
+ - test/cases/validates_captcha_test.rb
111
+ - test/cases/image_generator_test.rb
112
+ - test/cases/model_validation_test.rb
113
+ - test/test_helper.rb