karsthammer-validates_captcha 0.9.5a
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +22 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +320 -0
- data/Rakefile +102 -0
- data/lib/validates_captcha.rb +76 -0
- data/lib/validates_captcha/controller_validation.rb +63 -0
- data/lib/validates_captcha/form_builder.rb +16 -0
- data/lib/validates_captcha/form_helper.rb +39 -0
- data/lib/validates_captcha/image_generator/simple.rb +94 -0
- data/lib/validates_captcha/model_validation.rb +65 -0
- data/lib/validates_captcha/provider/dynamic_image.rb +240 -0
- data/lib/validates_captcha/provider/question.rb +110 -0
- data/lib/validates_captcha/provider/static_image.rb +224 -0
- data/lib/validates_captcha/string_generator/simple.rb +81 -0
- data/lib/validates_captcha/symmetric_encryptor/simple.rb +52 -0
- data/lib/validates_captcha/test_case.rb +12 -0
- data/lib/validates_captcha/version.rb +9 -0
- data/rails/init.rb +29 -0
- data/tasks/static_image_tasks.rake +33 -0
- data/test/cases/controller_validation_test.rb +222 -0
- data/test/cases/image_generator/simple_test.rb +34 -0
- data/test/cases/model_validation_test.rb +258 -0
- data/test/cases/provider/dynamic_image_test.rb +103 -0
- data/test/cases/provider/question_test.rb +41 -0
- data/test/cases/provider/static_image_test.rb +148 -0
- data/test/cases/string_generator/simple_test.rb +115 -0
- data/test/cases/symmetric_encryptor/simple_test.rb +28 -0
- data/test/cases/validates_captcha_test.rb +28 -0
- data/test/test_helper.rb +26 -0
- metadata +120 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
IMAGE = ValidatesCaptcha::Provider::DynamicImage
|
4
|
+
|
5
|
+
class DynamicImageTest < ValidatesCaptcha::TestCase
|
6
|
+
test "defines a class level #string_generator method" do
|
7
|
+
assert_respond_to IMAGE, :string_generator
|
8
|
+
end
|
9
|
+
|
10
|
+
test "defines a class level #string_generator= method" do
|
11
|
+
assert_respond_to IMAGE, :string_generator=
|
12
|
+
end
|
13
|
+
|
14
|
+
test "#string_generator method's return value should equal the value set using the #string_generator= method" do
|
15
|
+
old_string_generator = IMAGE.string_generator
|
16
|
+
|
17
|
+
IMAGE.string_generator = 'abc'
|
18
|
+
assert_equal 'abc', IMAGE.string_generator
|
19
|
+
|
20
|
+
IMAGE.string_generator = old_string_generator
|
21
|
+
end
|
22
|
+
|
23
|
+
test "defines a class level #symmetric_encryptor method" do
|
24
|
+
assert_respond_to IMAGE, :symmetric_encryptor
|
25
|
+
end
|
26
|
+
|
27
|
+
test "defines a class level #symmetric_encryptor= method" do
|
28
|
+
assert_respond_to IMAGE, :symmetric_encryptor=
|
29
|
+
end
|
30
|
+
|
31
|
+
test "#symmetric_encryptor method's return value should equal the value set using the #symmetric_encryptor= method" do
|
32
|
+
old_symmetric_encryptor = IMAGE.symmetric_encryptor
|
33
|
+
|
34
|
+
IMAGE.symmetric_encryptor = 'abc'
|
35
|
+
assert_equal 'abc', IMAGE.symmetric_encryptor
|
36
|
+
|
37
|
+
IMAGE.symmetric_encryptor = old_symmetric_encryptor
|
38
|
+
end
|
39
|
+
|
40
|
+
test "defines a class level #image_generator method" do
|
41
|
+
assert_respond_to IMAGE, :image_generator
|
42
|
+
end
|
43
|
+
|
44
|
+
test "defines a class level #image_generator= method" do
|
45
|
+
assert_respond_to IMAGE, :image_generator=
|
46
|
+
end
|
47
|
+
|
48
|
+
test "#image_generator method's return value should equal the value set using the #image_generator= method" do
|
49
|
+
old_image_generator = IMAGE.image_generator
|
50
|
+
|
51
|
+
IMAGE.image_generator = 'abc'
|
52
|
+
assert_equal 'abc', IMAGE.image_generator
|
53
|
+
|
54
|
+
IMAGE.image_generator = old_image_generator
|
55
|
+
end
|
56
|
+
|
57
|
+
test "calling #call with unrecognized path should have response status 404" do
|
58
|
+
result = IMAGE.new.call 'PATH_INFO' => '/unrecognized'
|
59
|
+
|
60
|
+
assert_equal 404, result.first
|
61
|
+
end
|
62
|
+
|
63
|
+
test "calling #call with recognized path should not have response status 404" do
|
64
|
+
result = IMAGE.new.call 'PATH_INFO' => IMAGE.new.send(:image_path, 'abc123')
|
65
|
+
|
66
|
+
assert_not_equal 404, result.first
|
67
|
+
end
|
68
|
+
|
69
|
+
test "calling #call with valid encrypted captcha code should have response status 200" do
|
70
|
+
encrypted_code = IMAGE.new.generate_challenge
|
71
|
+
result = IMAGE.new.call 'PATH_INFO' => "/captchas/#{encrypted_code}"
|
72
|
+
|
73
|
+
assert_equal 200, result.first
|
74
|
+
end
|
75
|
+
|
76
|
+
test "calling #call with valid encrypted captcha code should have expected content type response header" do
|
77
|
+
encrypted_code = IMAGE.new.generate_challenge
|
78
|
+
result = IMAGE.new.call 'PATH_INFO' => "/captchas/#{encrypted_code}"
|
79
|
+
|
80
|
+
assert result.second.key?('Content-Type')
|
81
|
+
assert_equal IMAGE.image_generator.mime_type, result.second['Content-Type']
|
82
|
+
end
|
83
|
+
|
84
|
+
test "calling #call with invalid encrypted captcha code should have response status 422" do
|
85
|
+
encrypted_code = IMAGE.new.generate_challenge.reverse
|
86
|
+
result = IMAGE.new.call 'PATH_INFO' => "/captchas/#{encrypted_code}"
|
87
|
+
|
88
|
+
assert_equal 422, result.first
|
89
|
+
end
|
90
|
+
|
91
|
+
test "calling #call with regenerate path should have response status 200" do
|
92
|
+
result = IMAGE.new.call 'PATH_INFO' => IMAGE.new.send(:regenerate_path)
|
93
|
+
|
94
|
+
assert_equal 200, result.first
|
95
|
+
end
|
96
|
+
|
97
|
+
test "calling #call with regenerate path should have content type response header set to application/json" do
|
98
|
+
result = IMAGE.new.call 'PATH_INFO' => IMAGE.new.send(:regenerate_path)
|
99
|
+
|
100
|
+
assert result.second.key?('Content-Type')
|
101
|
+
assert_equal 'application/json', result.second['Content-Type']
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
QUESTION = ValidatesCaptcha::Provider::Question
|
4
|
+
|
5
|
+
class QuestionTest < ValidatesCaptcha::TestCase
|
6
|
+
test "defines a class level #questions_and_answers method" do
|
7
|
+
assert_respond_to QUESTION, :questions_and_answers
|
8
|
+
end
|
9
|
+
|
10
|
+
test "defines a class level #questions_and_answers= method" do
|
11
|
+
assert_respond_to QUESTION, :questions_and_answers=
|
12
|
+
end
|
13
|
+
|
14
|
+
test "#questions_and_answers method's return value should equal the value set using the #questions_and_answers= method" do
|
15
|
+
old_questions_and_answers = QUESTION.questions_and_answers
|
16
|
+
|
17
|
+
QUESTION.questions_and_answers = 'abc'
|
18
|
+
assert_equal 'abc', QUESTION.questions_and_answers
|
19
|
+
|
20
|
+
QUESTION.questions_and_answers = old_questions_and_answers
|
21
|
+
end
|
22
|
+
|
23
|
+
test "calling #call with unrecognized path should have response status 404" do
|
24
|
+
result = QUESTION.new.call 'PATH_INFO' => '/unrecognized'
|
25
|
+
|
26
|
+
assert_equal 404, result.first
|
27
|
+
end
|
28
|
+
|
29
|
+
test "calling #call with regenerate path should have response status 200" do
|
30
|
+
result = QUESTION.new.call 'PATH_INFO' => QUESTION.new.send(:regenerate_path)
|
31
|
+
|
32
|
+
assert_equal 200, result.first
|
33
|
+
end
|
34
|
+
|
35
|
+
test "calling #call with regenerate path should have content type response header set to application/json" do
|
36
|
+
result = QUESTION.new.call 'PATH_INFO' => QUESTION.new.send(:regenerate_path)
|
37
|
+
|
38
|
+
assert result.second.key?('Content-Type')
|
39
|
+
assert_equal 'application/json', result.second['Content-Type']
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
STATIC_IMAGE = ValidatesCaptcha::Provider::StaticImage
|
4
|
+
|
5
|
+
unless defined?(::Rails)
|
6
|
+
module Rails
|
7
|
+
def self.public_path
|
8
|
+
'public'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class StaticImageTest < ValidatesCaptcha::TestCase
|
14
|
+
test "defines a class level #string_generator method" do
|
15
|
+
assert_respond_to STATIC_IMAGE, :string_generator
|
16
|
+
end
|
17
|
+
|
18
|
+
test "defines a class level #string_generator= method" do
|
19
|
+
assert_respond_to STATIC_IMAGE, :string_generator=
|
20
|
+
end
|
21
|
+
|
22
|
+
test "#string_generator method's return value should equal the value set using the #string_generator= method" do
|
23
|
+
old_string_generator = STATIC_IMAGE.string_generator
|
24
|
+
|
25
|
+
STATIC_IMAGE.string_generator = 'abc'
|
26
|
+
assert_equal 'abc', STATIC_IMAGE.string_generator
|
27
|
+
|
28
|
+
STATIC_IMAGE.string_generator = old_string_generator
|
29
|
+
end
|
30
|
+
|
31
|
+
test "defines a class level #image_generator method" do
|
32
|
+
assert_respond_to STATIC_IMAGE, :image_generator
|
33
|
+
end
|
34
|
+
|
35
|
+
test "defines a class level #image_generator= method" do
|
36
|
+
assert_respond_to STATIC_IMAGE, :image_generator=
|
37
|
+
end
|
38
|
+
|
39
|
+
test "#image_generator method's return value should equal the value set using the #image_generator= method" do
|
40
|
+
old_image_generator = STATIC_IMAGE.image_generator
|
41
|
+
|
42
|
+
STATIC_IMAGE.image_generator = 'abc'
|
43
|
+
assert_equal 'abc', STATIC_IMAGE.image_generator
|
44
|
+
|
45
|
+
STATIC_IMAGE.image_generator = old_image_generator
|
46
|
+
end
|
47
|
+
|
48
|
+
test "defines a class level #salt method" do
|
49
|
+
assert_respond_to STATIC_IMAGE, :salt
|
50
|
+
end
|
51
|
+
|
52
|
+
test "defines a class level #salt= method" do
|
53
|
+
assert_respond_to STATIC_IMAGE, :salt=
|
54
|
+
end
|
55
|
+
|
56
|
+
test "#salt method's return value should equal the value set using the #salt= method" do
|
57
|
+
old_salt = STATIC_IMAGE.salt
|
58
|
+
|
59
|
+
STATIC_IMAGE.salt = 'abc'
|
60
|
+
assert_equal 'abc', STATIC_IMAGE.salt
|
61
|
+
|
62
|
+
STATIC_IMAGE.salt = old_salt
|
63
|
+
end
|
64
|
+
|
65
|
+
test "defines a class level #filesystem_dir method" do
|
66
|
+
assert_respond_to STATIC_IMAGE, :filesystem_dir
|
67
|
+
end
|
68
|
+
|
69
|
+
test "defines a class level #filesystem_dir= method" do
|
70
|
+
assert_respond_to STATIC_IMAGE, :filesystem_dir=
|
71
|
+
end
|
72
|
+
|
73
|
+
test "#filesystem_dir method's return value should equal the value set using the #filesystem_dir= method" do
|
74
|
+
old_filesystem_dir = STATIC_IMAGE.filesystem_dir
|
75
|
+
|
76
|
+
STATIC_IMAGE.filesystem_dir = 'abc'
|
77
|
+
assert_equal 'abc', STATIC_IMAGE.filesystem_dir
|
78
|
+
|
79
|
+
STATIC_IMAGE.filesystem_dir = old_filesystem_dir
|
80
|
+
end
|
81
|
+
|
82
|
+
test "defines a class level #web_dir method" do
|
83
|
+
assert_respond_to STATIC_IMAGE, :web_dir
|
84
|
+
end
|
85
|
+
|
86
|
+
test "defines a class level #web_dir= method" do
|
87
|
+
assert_respond_to STATIC_IMAGE, :web_dir=
|
88
|
+
end
|
89
|
+
|
90
|
+
test "#web_dir method's return value should equal the value set using the #web_dir= method" do
|
91
|
+
old_web_dir = STATIC_IMAGE.web_dir
|
92
|
+
|
93
|
+
STATIC_IMAGE.web_dir = 'abc'
|
94
|
+
assert_equal 'abc', STATIC_IMAGE.web_dir
|
95
|
+
|
96
|
+
STATIC_IMAGE.web_dir = old_web_dir
|
97
|
+
end
|
98
|
+
|
99
|
+
test "calling #call with unrecognized path should have response status 404" do
|
100
|
+
result = STATIC_IMAGE.new.call 'PATH_INFO' => '/unrecognized'
|
101
|
+
|
102
|
+
assert_equal 404, result.first
|
103
|
+
end
|
104
|
+
|
105
|
+
test "calling #call with regenerate path should have response status 200" do
|
106
|
+
si = STATIC_IMAGE.new
|
107
|
+
si.instance_variable_set "@challenges", ['abc']
|
108
|
+
|
109
|
+
result = si.call 'PATH_INFO' => si.send(:regenerate_path)
|
110
|
+
|
111
|
+
assert_equal 200, result.first
|
112
|
+
end
|
113
|
+
|
114
|
+
test "calling #call with regenerate path should have content type response header set to application/json" do
|
115
|
+
si = STATIC_IMAGE.new
|
116
|
+
si.instance_variable_set "@challenges", ['abc']
|
117
|
+
|
118
|
+
result = si.call 'PATH_INFO' => si.send(:regenerate_path)
|
119
|
+
|
120
|
+
assert result.second.key?('Content-Type')
|
121
|
+
assert_equal 'application/json', result.second['Content-Type']
|
122
|
+
end
|
123
|
+
|
124
|
+
test "calling #generate_challenge should raise runtime error if no images are available" do
|
125
|
+
si = STATIC_IMAGE.new
|
126
|
+
si.instance_variable_set "@images", []
|
127
|
+
|
128
|
+
assert_raises RuntimeError do
|
129
|
+
si.generate_challenge
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
test "calling #generate_challenge should not raise runtime error if an images is available" do
|
134
|
+
si = STATIC_IMAGE.new
|
135
|
+
si.instance_variable_set "@images", ['/path/to/an/image.gif']
|
136
|
+
|
137
|
+
assert_nothing_raised do
|
138
|
+
si.generate_challenge
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
test "calling #generate_challenge should return the image file basename without extension" do
|
143
|
+
si = STATIC_IMAGE.new
|
144
|
+
si.instance_variable_set "@images", ["/path/to/an/captcha-image#{si.send(:image_file_extension)}"]
|
145
|
+
|
146
|
+
assert_equal 'captcha-image', si.generate_challenge
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,115 @@
|
|
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
|
115
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
SE = ValidatesCaptcha::SymmetricEncryptor::Simple
|
4
|
+
|
5
|
+
class SymmetricEncryptorTest < ValidatesCaptcha::TestCase
|
6
|
+
test "defines an instance level #encrypt method" do
|
7
|
+
assert_respond_to SE.new, :encrypt
|
8
|
+
end
|
9
|
+
|
10
|
+
test "instance level #encrypt method returns a string" do
|
11
|
+
assert_kind_of String, SE.new.encrypt('abc')
|
12
|
+
end
|
13
|
+
|
14
|
+
test "defines an instance level #decrypt method" do
|
15
|
+
assert_respond_to SE.new, :decrypt
|
16
|
+
end
|
17
|
+
|
18
|
+
test "instance level #decrypt method returns nil if decryption failes" do
|
19
|
+
assert_nil SE.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, SE.new.decrypt(SE.new.encrypt(captcha))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,28 @@
|
|
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 #provider method" do
|
13
|
+
assert_respond_to ValidatesCaptcha, :provider
|
14
|
+
end
|
15
|
+
|
16
|
+
test "defines a class level #provider= method" do
|
17
|
+
assert_respond_to ValidatesCaptcha, :provider=
|
18
|
+
end
|
19
|
+
|
20
|
+
test "#provider method's return value should equal the value set using the #provider= method" do
|
21
|
+
old_provider = ValidatesCaptcha.provider
|
22
|
+
|
23
|
+
ValidatesCaptcha.provider = 'abc'
|
24
|
+
assert_equal 'abc', ValidatesCaptcha.provider
|
25
|
+
|
26
|
+
ValidatesCaptcha.provider = old_provider
|
27
|
+
end
|
28
|
+
end
|