validates_captcha 0.9.5 → 0.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +6 -0
- data/Rakefile +9 -9
- data/lib/validates_captcha.rb +12 -12
- data/lib/validates_captcha/controller_validation.rb +11 -10
- data/lib/validates_captcha/form_builder.rb +2 -2
- data/lib/validates_captcha/form_helper.rb +11 -11
- data/lib/validates_captcha/image_generator/simple.rb +24 -23
- data/lib/validates_captcha/model_validation.rb +29 -13
- data/lib/validates_captcha/provider/dynamic_image.rb +50 -50
- data/lib/validates_captcha/provider/question.rb +28 -27
- data/lib/validates_captcha/provider/static_image.rb +62 -62
- data/lib/validates_captcha/string_generator/simple.rb +18 -17
- data/lib/validates_captcha/symmetric_encryptor/simple.rb +11 -10
- data/lib/validates_captcha/test_case.rb +1 -0
- data/lib/validates_captcha/version.rb +3 -2
- data/rails/init.rb +4 -4
- data/tasks/static_image_tasks.rake +7 -9
- data/test/cases/controller_validation_test.rb +42 -41
- data/test/cases/image_generator/simple_test.rb +8 -8
- data/test/cases/model_validation_test.rb +79 -64
- data/test/cases/provider/dynamic_image_test.rb +29 -28
- data/test/cases/provider/question_test.rb +11 -10
- data/test/cases/provider/static_image_test.rb +40 -39
- data/test/cases/string_generator/simple_test.rb +29 -29
- data/test/cases/symmetric_encryptor/simple_test.rb +6 -6
- data/test/cases/validates_captcha_test.rb +7 -6
- data/test/test_helper.rb +4 -0
- metadata +3 -3
@@ -2,13 +2,13 @@ require 'active_support'
|
|
2
2
|
|
3
3
|
module ValidatesCaptcha
|
4
4
|
module SymmetricEncryptor
|
5
|
-
# This class is responsible for encrypting and decrypting captcha codes.
|
6
|
-
# It internally uses ActiveSupport's MessageEncryptor to do the string
|
5
|
+
# This class is responsible for encrypting and decrypting captcha codes.
|
6
|
+
# It internally uses ActiveSupport's MessageEncryptor to do the string
|
7
7
|
# encryption/decryption.
|
8
8
|
#
|
9
|
-
# You can implement your own symmetric encryptor by creating a class
|
10
|
-
# that conforms to the method definitions of the example below and
|
11
|
-
# assign an instance of it to
|
9
|
+
# You can implement your own symmetric encryptor by creating a class
|
10
|
+
# that conforms to the method definitions of the example below and
|
11
|
+
# assign an instance of it to
|
12
12
|
# ValidatesCaptcha::Provider::DynamicImage#symmetric_encryptor=.
|
13
13
|
#
|
14
14
|
# Example for a custom symmetric encryptor:
|
@@ -28,19 +28,19 @@ module ValidatesCaptcha
|
|
28
28
|
# ValidatesCaptcha::Provider::DynamicImage.symmetric_encryptor = ReverseString.new
|
29
29
|
# ValidatesCaptcha.provider = ValidatesCaptcha::Provider::DynamicImage.new
|
30
30
|
#
|
31
|
-
# Please note: The #decrypt method should return +nil+ if decryption fails.
|
31
|
+
# Please note: The #decrypt method should return +nil+ if decryption fails.
|
32
32
|
class Simple
|
33
33
|
KEY = ::ActiveSupport::SecureRandom.hex(64).freeze
|
34
|
-
|
34
|
+
|
35
35
|
def initialize #:nodoc:
|
36
36
|
@symmetric_encryptor = ::ActiveSupport::MessageEncryptor.new(KEY)
|
37
37
|
end
|
38
|
-
|
39
|
-
# Encrypts a cleartext string.
|
38
|
+
|
39
|
+
# Encrypts a cleartext string.
|
40
40
|
def encrypt(code)
|
41
41
|
@symmetric_encryptor.encrypt(code).gsub('+', '%2B').gsub('/', '%2F')
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
# Decrypts an encrypted string.
|
45
45
|
def decrypt(encrypted_code)
|
46
46
|
@symmetric_encryptor.decrypt encrypted_code.gsub('%2F', '/').gsub('%2B', '+')
|
@@ -50,3 +50,4 @@ module ValidatesCaptcha
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
|
+
|
data/rails/init.rb
CHANGED
@@ -10,16 +10,16 @@ end
|
|
10
10
|
module ::ValidatesCaptcha
|
11
11
|
class MiddlewareWrapper
|
12
12
|
RECOGNIZED_RESPONSE_STATUS_CODES = [200, 422].freeze
|
13
|
-
|
13
|
+
|
14
14
|
def initialize(app)
|
15
15
|
@app = app
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def call(env)
|
19
19
|
result = ValidatesCaptcha.provider.call(env)
|
20
|
-
|
20
|
+
|
21
21
|
return @app.call(env) unless RECOGNIZED_RESPONSE_STATUS_CODES.include?(result.first)
|
22
|
-
|
22
|
+
|
23
23
|
result
|
24
24
|
end
|
25
25
|
end
|
@@ -1,29 +1,27 @@
|
|
1
|
-
require 'validates_captcha'
|
2
|
-
|
3
1
|
namespace :validates_captcha do
|
4
|
-
desc "Create 3 static captcha images in RAILS_ROOT/public/images/captchas/, specify a different number with COUNT=n"
|
5
|
-
task :create_static_images => [:create_static_image_dir, :clear_static_image_dir] do
|
2
|
+
desc "Create 3 static captcha images in RAILS_ROOT/public/images/captchas/, specify a different number with COUNT=n"
|
3
|
+
task :create_static_images => [:create_static_image_dir, :clear_static_image_dir] do
|
6
4
|
count = ENV['COUNT'] ? ENV['COUNT'].to_i : 3
|
7
|
-
|
5
|
+
|
8
6
|
count.times do
|
9
7
|
path, code = ValidatesCaptcha::Provider::StaticImage.create_image
|
10
8
|
puts "Created #{path} with code #{code}"
|
11
9
|
end
|
12
10
|
end
|
13
|
-
|
11
|
+
|
14
12
|
task :create_static_image_dir => :environment do
|
15
13
|
image_dir = ValidatesCaptcha::Provider::StaticImage.filesystem_dir
|
16
|
-
|
14
|
+
|
17
15
|
unless File.exist?(image_dir)
|
18
16
|
FileUtils.mkdir_p image_dir
|
19
17
|
puts "Created directory #{image_dir}"
|
20
18
|
end
|
21
19
|
end
|
22
|
-
|
20
|
+
|
23
21
|
task :clear_static_image_dir => :environment do
|
24
22
|
image_dir = ValidatesCaptcha::Provider::StaticImage.filesystem_dir
|
25
23
|
image_files = Dir[File.join(image_dir, '*')]
|
26
|
-
|
24
|
+
|
27
25
|
if image_files.any?
|
28
26
|
FileUtils.rm image_files
|
29
27
|
puts "Cleared directory #{image_dir}"
|
@@ -7,31 +7,31 @@ end
|
|
7
7
|
|
8
8
|
class WidgetsController < ActionController::Base
|
9
9
|
include ValidatesCaptcha::ControllerValidation
|
10
|
-
|
10
|
+
|
11
11
|
validates_captcha :except => [:update, :save, :persist, :bingo]
|
12
12
|
validates_captcha_of :widgets, :only => :update
|
13
13
|
validates_captcha_of Widget, :only => [:save, :persist]
|
14
|
-
|
14
|
+
|
15
15
|
def create
|
16
16
|
begin
|
17
17
|
Widget.new.save!
|
18
18
|
rescue ActiveRecord::RecordInvalid
|
19
19
|
@invalid = true
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
render :nothing => true
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def update
|
26
26
|
begin
|
27
27
|
Widget.new.save!
|
28
28
|
rescue ActiveRecord::RecordInvalid
|
29
29
|
@invalid = true
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
render :nothing => true
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def save
|
36
36
|
begin
|
37
37
|
Widget.create! params['widget']
|
@@ -41,7 +41,7 @@ class WidgetsController < ActionController::Base
|
|
41
41
|
|
42
42
|
render :nothing => true
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
def store
|
46
46
|
begin
|
47
47
|
Widget.create! params['widget']
|
@@ -51,7 +51,7 @@ class WidgetsController < ActionController::Base
|
|
51
51
|
|
52
52
|
render :nothing => true
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def persist
|
56
56
|
begin
|
57
57
|
Widget.create! params['widget']
|
@@ -61,28 +61,28 @@ class WidgetsController < ActionController::Base
|
|
61
61
|
|
62
62
|
render :nothing => true
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
def bingo
|
66
66
|
begin
|
67
67
|
Widget.new.save!
|
68
68
|
rescue ActiveRecord::RecordInvalid
|
69
69
|
@invalid = true
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
render :nothing => true
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
class ControllerValidationTest < ActionController::TestCase
|
77
77
|
tests WidgetsController
|
78
|
-
|
78
|
+
|
79
79
|
def with_dynamic_image_provider(&block)
|
80
80
|
old_provider = ValidatesCaptcha.provider
|
81
81
|
provider = ValidatesCaptcha.provider = ValidatesCaptcha::Provider::DynamicImage.new
|
82
82
|
yield provider
|
83
83
|
ValidatesCaptcha.provider = old_provider
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
def with_static_image_provider(&block)
|
87
87
|
old_provider = ValidatesCaptcha.provider
|
88
88
|
provider = ValidatesCaptcha.provider = ValidatesCaptcha::Provider::StaticImage.new
|
@@ -90,133 +90,134 @@ class ControllerValidationTest < ActionController::TestCase
|
|
90
90
|
yield provider
|
91
91
|
ValidatesCaptcha.provider = old_provider
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
def with_question_provider(&block)
|
95
95
|
old_provider = ValidatesCaptcha.provider
|
96
96
|
provider = ValidatesCaptcha.provider = ValidatesCaptcha::Provider::Question.new
|
97
97
|
yield provider
|
98
98
|
ValidatesCaptcha.provider = old_provider
|
99
99
|
end
|
100
|
-
|
100
|
+
|
101
101
|
test "defines a class level #validates_captcha method" do
|
102
102
|
assert_respond_to WidgetsController, :validates_captcha
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
test "defines a class level #validates_captcha_of method" do
|
106
106
|
assert_respond_to WidgetsController, :validates_captcha_of
|
107
107
|
end
|
108
|
-
|
108
|
+
|
109
109
|
test "calling #create method of controller should assign @invalid" do
|
110
110
|
post :create
|
111
111
|
assert_not_nil assigns(:invalid)
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
test "calling #update method of controller should assign @invalid" do
|
115
115
|
post :update
|
116
116
|
assert_not_nil assigns(:invalid)
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
test "calling #save method of controller should not assign @invalid" do
|
120
120
|
with_dynamic_image_provider do |provider|
|
121
121
|
challenge = provider.generate_challenge
|
122
122
|
solution = provider.send(:decrypt, challenge)
|
123
|
-
|
123
|
+
|
124
124
|
post :save, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
125
125
|
assert_nil assigns(:invalid)
|
126
126
|
end
|
127
|
-
|
127
|
+
|
128
128
|
with_static_image_provider do |provider|
|
129
129
|
challenge = provider.generate_challenge
|
130
130
|
solution = 'hello'
|
131
|
-
|
131
|
+
|
132
132
|
post :save, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
133
133
|
assert_nil assigns(:invalid)
|
134
134
|
end
|
135
|
-
|
135
|
+
|
136
136
|
with_question_provider do |provider|
|
137
137
|
challenge = provider.generate_challenge
|
138
138
|
solution = provider.send(:solve, challenge)
|
139
|
-
|
139
|
+
|
140
140
|
post :save, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
141
141
|
assert_nil assigns(:invalid)
|
142
142
|
end
|
143
143
|
end
|
144
|
-
|
144
|
+
|
145
145
|
test "calling #store method of controller should assign @invalid" do
|
146
146
|
with_dynamic_image_provider do |provider|
|
147
147
|
challenge = provider.generate_challenge
|
148
148
|
solution = provider.send(:decrypt, challenge).reverse
|
149
|
-
|
149
|
+
|
150
150
|
post :store, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
151
151
|
assert_not_nil assigns(:invalid)
|
152
152
|
end
|
153
|
-
|
153
|
+
|
154
154
|
with_static_image_provider do |provider|
|
155
155
|
challenge = provider.generate_challenge
|
156
156
|
solution = '---'
|
157
|
-
|
157
|
+
|
158
158
|
post :store, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
159
159
|
assert_not_nil assigns(:invalid)
|
160
160
|
end
|
161
|
-
|
161
|
+
|
162
162
|
with_question_provider do |provider|
|
163
163
|
challenge = provider.generate_challenge
|
164
164
|
solution = '---'
|
165
|
-
|
165
|
+
|
166
166
|
post :store, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
167
167
|
assert_not_nil assigns(:invalid)
|
168
168
|
end
|
169
169
|
end
|
170
|
-
|
170
|
+
|
171
171
|
test "calling #persist method of controller should assign @invalid" do
|
172
172
|
with_dynamic_image_provider do |provider|
|
173
173
|
challenge = provider.generate_challenge
|
174
174
|
solution = provider.send(:decrypt, challenge).reverse
|
175
|
-
|
175
|
+
|
176
176
|
post :persist, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
177
177
|
assert_not_nil assigns(:invalid)
|
178
178
|
end
|
179
|
-
|
179
|
+
|
180
180
|
with_static_image_provider do |provider|
|
181
181
|
challenge = provider.generate_challenge
|
182
182
|
solution = '---'
|
183
|
-
|
183
|
+
|
184
184
|
post :persist, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
185
185
|
assert_not_nil assigns(:invalid)
|
186
186
|
end
|
187
|
-
|
187
|
+
|
188
188
|
with_question_provider do |provider|
|
189
189
|
challenge = provider.generate_challenge
|
190
190
|
solution = '---'
|
191
|
-
|
191
|
+
|
192
192
|
post :persist, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
193
193
|
assert_not_nil assigns(:invalid)
|
194
194
|
end
|
195
195
|
end
|
196
|
-
|
196
|
+
|
197
197
|
test "calling #bingo method of controller should not assign @invalid" do
|
198
198
|
with_dynamic_image_provider do |provider|
|
199
199
|
challenge = provider.generate_challenge
|
200
200
|
solution = provider.send(:decrypt, challenge).reverse
|
201
|
-
|
201
|
+
|
202
202
|
post :bingo, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
203
203
|
assert_nil assigns(:invalid)
|
204
204
|
end
|
205
|
-
|
205
|
+
|
206
206
|
with_static_image_provider do |provider|
|
207
207
|
challenge = provider.generate_challenge
|
208
208
|
solution = '---'
|
209
|
-
|
209
|
+
|
210
210
|
post :bingo, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
211
211
|
assert_nil assigns(:invalid)
|
212
212
|
end
|
213
|
-
|
213
|
+
|
214
214
|
with_question_provider do |provider|
|
215
215
|
challenge = provider.generate_challenge
|
216
216
|
solution = '---'
|
217
|
-
|
217
|
+
|
218
218
|
post :bingo, { 'widget' => { 'captcha_challenge' => challenge, 'captcha_solution' => solution } }
|
219
219
|
assert_nil assigns(:invalid)
|
220
220
|
end
|
221
221
|
end
|
222
222
|
end
|
223
|
+
|
@@ -1,32 +1,32 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
|
2
|
+
|
3
3
|
IG = ValidatesCaptcha::ImageGenerator::Simple
|
4
|
-
|
4
|
+
|
5
5
|
class ImageGeneratorTest < ValidatesCaptcha::TestCase
|
6
6
|
test "defines an instance level #generate method" do
|
7
7
|
assert_respond_to IG.new, :generate
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
test "instance level #generate method accepts an argument" do
|
11
11
|
assert_nothing_raised { IG.new.generate('abc') }
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
test "instance level #generate method returns a string" do
|
15
15
|
assert_kind_of String, IG.new.generate('abc')
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
test "defines an instance level #file_extension method" do
|
19
19
|
assert_respond_to IG.new, :file_extension
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
test "instance level #file_extension method returns a string" do
|
23
23
|
assert_kind_of String, IG.new.file_extension
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
test "defines an instance level #mime_type method" do
|
27
27
|
assert_respond_to IG.new, :mime_type
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
test "instance level #mime_type method returns a string" do
|
31
31
|
assert_kind_of String, IG.new.mime_type
|
32
32
|
end
|
@@ -7,7 +7,7 @@ class ModelValidationTest < ValidatesCaptcha::TestCase
|
|
7
7
|
yield provider
|
8
8
|
ValidatesCaptcha.provider = old_provider
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def with_static_image_provider(&block)
|
12
12
|
old_provider = ValidatesCaptcha.provider
|
13
13
|
provider = ValidatesCaptcha.provider = ValidatesCaptcha::Provider::StaticImage.new
|
@@ -15,244 +15,259 @@ class ModelValidationTest < ValidatesCaptcha::TestCase
|
|
15
15
|
yield provider
|
16
16
|
ValidatesCaptcha.provider = old_provider
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def with_question_provider(&block)
|
20
20
|
old_provider = ValidatesCaptcha.provider
|
21
21
|
provider = ValidatesCaptcha.provider = ValidatesCaptcha::Provider::Question.new
|
22
22
|
yield provider
|
23
23
|
ValidatesCaptcha.provider = old_provider
|
24
24
|
end
|
25
|
-
|
26
|
-
test "defines an accessible attribute named +captcha_solution+" do
|
27
|
-
assert Widget.accessible_attributes.include?('captcha_solution')
|
28
|
-
end
|
29
|
-
|
25
|
+
|
30
26
|
test "defines an instance level #captcha_solution method" do
|
31
27
|
assert_respond_to Widget.new, :captcha_solution
|
32
28
|
end
|
33
|
-
|
29
|
+
|
34
30
|
test "defines a instance level #captcha_solution= method" do
|
35
31
|
assert_respond_to Widget.new, :captcha_solution=
|
36
32
|
end
|
37
|
-
|
33
|
+
|
38
34
|
test "assigned value to #captcha_solution= should equal return value of #captcha_solution" do
|
39
35
|
widget = Widget.new
|
40
36
|
widget.captcha_solution = 'abc123'
|
41
|
-
|
37
|
+
|
42
38
|
assert_equal 'abc123', widget.captcha_solution
|
43
39
|
end
|
44
|
-
|
45
|
-
test "defines an accessible attribute named +captcha_challenge+" do
|
46
|
-
assert Widget.accessible_attributes.include?('captcha_challenge')
|
47
|
-
end
|
48
|
-
|
40
|
+
|
49
41
|
test "defines an instance level #captcha_challenge method" do
|
50
42
|
assert_respond_to Widget.new, :captcha_challenge
|
51
43
|
end
|
52
|
-
|
44
|
+
|
53
45
|
test "defines an instance level #captcha_challenge= method" do
|
54
46
|
assert_respond_to Widget.new, :captcha_challenge=
|
55
47
|
end
|
56
|
-
|
48
|
+
|
57
49
|
test "value assigned to #captcha_challenge= should equal return value of #captcha_challenge" do
|
58
50
|
widget = Widget.new
|
59
51
|
widget.captcha_challenge = 'asdfghjk3456789'
|
60
|
-
|
52
|
+
|
61
53
|
assert_equal 'asdfghjk3456789', widget.captcha_challenge
|
62
54
|
end
|
63
|
-
|
55
|
+
|
56
|
+
test "should not add captcha_challenge as attr_accessible" do
|
57
|
+
assert !(Widget.accessible_attributes || []).include?('captcha_challenge')
|
58
|
+
end
|
59
|
+
|
60
|
+
test "should not add captcha_solution as attr_accessible" do
|
61
|
+
assert !(Widget.accessible_attributes || []).include?('captcha_solution')
|
62
|
+
end
|
63
|
+
|
64
|
+
test "should also assign non-attr_accessible captcha fields when mass assigning attributes" do
|
65
|
+
widget = Widget.new
|
66
|
+
|
67
|
+
widget.attributes = { :name => 'foo', :captcha_challenge => 'bar', :captcha_solution => 'baz' }
|
68
|
+
assert_equal 'foo', widget.name
|
69
|
+
assert_equal 'bar', widget.captcha_challenge
|
70
|
+
assert_equal 'baz', widget.captcha_solution
|
71
|
+
|
72
|
+
widget.attributes = { 'name' => 'bar', 'captcha_challenge' => 'baz', 'captcha_solution' => 'foo' }
|
73
|
+
assert_equal 'bar', widget.name
|
74
|
+
assert_equal 'baz', widget.captcha_challenge
|
75
|
+
assert_equal 'foo', widget.captcha_solution
|
76
|
+
end
|
77
|
+
|
64
78
|
test "defines #validate_captcha method callback of kind +validate+" do
|
65
79
|
assert Widget.validate_callback_chain.any? { |callback| callback.method == :validate_captcha && callback.kind == :validate }
|
66
80
|
end
|
67
|
-
|
81
|
+
|
68
82
|
test "defines a class level with_captcha_validation method" do
|
69
83
|
assert_respond_to Widget, :with_captcha_validation
|
70
84
|
end
|
71
|
-
|
85
|
+
|
72
86
|
test "not within a #with_captcha_validation block, calling valid? should return true if no captcha_solution is set" do
|
73
87
|
widget = Widget.new
|
74
88
|
widget.captcha_solution = nil
|
75
|
-
|
89
|
+
|
76
90
|
assert widget.valid?
|
77
91
|
end
|
78
|
-
|
92
|
+
|
79
93
|
test "not within a #with_captcha_validation block, calling valid? should return true if an empty captcha_solution is set" do
|
80
94
|
widget = Widget.new
|
81
95
|
widget.captcha_solution = ' '
|
82
|
-
|
96
|
+
|
83
97
|
assert widget.valid?
|
84
98
|
end
|
85
|
-
|
99
|
+
|
86
100
|
test "not within a #with_captcha_validation block, calling valid? should return true if an invalid captcha_solution is set" do
|
87
101
|
with_dynamic_image_provider do |provider|
|
88
102
|
widget = Widget.new
|
89
103
|
widget.captcha_solution = provider.send(:decrypt, widget.captcha_challenge).reverse
|
90
|
-
|
104
|
+
|
91
105
|
assert widget.valid?
|
92
106
|
end
|
93
|
-
|
107
|
+
|
94
108
|
with_static_image_provider do |provider|
|
95
109
|
widget = Widget.new
|
96
110
|
widget.captcha_solution = '---'
|
97
|
-
|
111
|
+
|
98
112
|
assert widget.valid?
|
99
113
|
end
|
100
|
-
|
114
|
+
|
101
115
|
with_question_provider do |provider|
|
102
116
|
widget = Widget.new
|
103
117
|
widget.captcha_solution = '---'
|
104
|
-
|
118
|
+
|
105
119
|
assert widget.valid?
|
106
120
|
end
|
107
121
|
end
|
108
|
-
|
122
|
+
|
109
123
|
test "not within a #with_captcha_validation block, calling valid? should return true if a valid captcha_solution is set" do
|
110
124
|
with_dynamic_image_provider do |provider|
|
111
125
|
widget = Widget.new
|
112
126
|
widget.captcha_solution = provider.send(:decrypt, widget.captcha_challenge)
|
113
|
-
|
127
|
+
|
114
128
|
assert widget.valid?
|
115
129
|
end
|
116
|
-
|
130
|
+
|
117
131
|
with_static_image_provider do |provider|
|
118
132
|
widget = Widget.new
|
119
133
|
widget.captcha_solution = 'hello'
|
120
|
-
|
134
|
+
|
121
135
|
assert widget.valid?
|
122
136
|
end
|
123
|
-
|
137
|
+
|
124
138
|
with_question_provider do |provider|
|
125
139
|
widget = Widget.new
|
126
140
|
widget.captcha_solution = provider.send(:solve, widget.captcha_challenge)
|
127
|
-
|
141
|
+
|
128
142
|
assert widget.valid?
|
129
143
|
end
|
130
144
|
end
|
131
|
-
|
145
|
+
|
132
146
|
test "within a #with_captcha_validation block, calling valid? should return false if no captcha_solution is set" do
|
133
147
|
Widget.with_captcha_validation do
|
134
148
|
widget = Widget.new
|
135
149
|
widget.captcha_solution = nil
|
136
|
-
|
150
|
+
|
137
151
|
assert !widget.valid?
|
138
152
|
assert_equal 1, Array.wrap(widget.errors[:captcha_solution]).size
|
139
153
|
assert Array.wrap(widget.errors[:captcha_solution]).first.include?('blank')
|
140
154
|
end
|
141
155
|
end
|
142
|
-
|
156
|
+
|
143
157
|
test "within a #with_captcha_validation block, calling valid? should return false if an empty captcha_solution is set" do
|
144
158
|
Widget.with_captcha_validation do
|
145
159
|
widget = Widget.new
|
146
160
|
widget.captcha_solution = ' '
|
147
|
-
|
161
|
+
|
148
162
|
assert !widget.valid?
|
149
163
|
assert_equal 1, Array.wrap(widget.errors[:captcha_solution]).size
|
150
164
|
assert Array.wrap(widget.errors[:captcha_solution]).first.include?('blank')
|
151
165
|
end
|
152
166
|
end
|
153
|
-
|
167
|
+
|
154
168
|
test "within a #with_captcha_validation block, calling valid? should return false if an invalid captcha_solution is set" do
|
155
169
|
with_dynamic_image_provider do |provider|
|
156
170
|
Widget.with_captcha_validation do
|
157
171
|
widget = Widget.new
|
158
172
|
widget.captcha_solution = provider.send(:decrypt, widget.captcha_challenge).reverse
|
159
|
-
|
173
|
+
|
160
174
|
assert !widget.valid?
|
161
175
|
assert_equal 1, Array.wrap(widget.errors[:captcha_solution]).size
|
162
176
|
assert Array.wrap(widget.errors[:captcha_solution]).first.include?('invalid')
|
163
177
|
end
|
164
178
|
end
|
165
|
-
|
179
|
+
|
166
180
|
with_static_image_provider do |provider|
|
167
181
|
Widget.with_captcha_validation do
|
168
182
|
widget = Widget.new
|
169
183
|
widget.captcha_solution = '---'
|
170
|
-
|
184
|
+
|
171
185
|
assert !widget.valid?
|
172
186
|
assert_equal 1, Array.wrap(widget.errors[:captcha_solution]).size
|
173
187
|
assert Array.wrap(widget.errors[:captcha_solution]).first.include?('invalid')
|
174
188
|
end
|
175
189
|
end
|
176
|
-
|
190
|
+
|
177
191
|
with_question_provider do |provider|
|
178
192
|
Widget.with_captcha_validation do
|
179
193
|
widget = Widget.new
|
180
194
|
widget.captcha_solution = '---'
|
181
|
-
|
195
|
+
|
182
196
|
assert !widget.valid?
|
183
197
|
assert_equal 1, Array.wrap(widget.errors[:captcha_solution]).size
|
184
198
|
assert Array.wrap(widget.errors[:captcha_solution]).first.include?('invalid')
|
185
199
|
end
|
186
200
|
end
|
187
201
|
end
|
188
|
-
|
202
|
+
|
189
203
|
test "within a #with_captcha_validation block, calling valid? should return true if a valid captcha_solution is set" do
|
190
204
|
with_dynamic_image_provider do |provider|
|
191
205
|
Widget.with_captcha_validation do
|
192
206
|
widget = Widget.new
|
193
207
|
widget.captcha_solution = provider.send(:decrypt, widget.captcha_challenge)
|
194
|
-
|
208
|
+
|
195
209
|
assert widget.valid?
|
196
210
|
end
|
197
211
|
end
|
198
|
-
|
212
|
+
|
199
213
|
with_static_image_provider do |provider|
|
200
214
|
Widget.with_captcha_validation do
|
201
215
|
widget = Widget.new
|
202
216
|
widget.captcha_solution = 'hello'
|
203
|
-
|
217
|
+
|
204
218
|
assert widget.valid?
|
205
219
|
end
|
206
220
|
end
|
207
|
-
|
221
|
+
|
208
222
|
with_question_provider do |provider|
|
209
223
|
Widget.with_captcha_validation do
|
210
224
|
widget = Widget.new
|
211
225
|
widget.captcha_solution = provider.send(:solve, widget.captcha_challenge)
|
212
|
-
|
226
|
+
|
213
227
|
assert widget.valid?
|
214
228
|
end
|
215
229
|
end
|
216
230
|
end
|
217
|
-
|
231
|
+
|
218
232
|
test "with #with_captcha_validation block, calling valid? before and after the block should return true if valid? returned false within block" do
|
219
233
|
with_dynamic_image_provider do |provider|
|
220
234
|
widget = Widget.new
|
221
235
|
widget.captcha_solution = provider.send(:decrypt, widget.captcha_challenge).reverse
|
222
|
-
|
236
|
+
|
223
237
|
assert widget.valid?
|
224
|
-
|
238
|
+
|
225
239
|
Widget.with_captcha_validation do
|
226
240
|
assert !widget.valid?
|
227
241
|
end
|
228
|
-
|
242
|
+
|
229
243
|
assert widget.valid?
|
230
244
|
end
|
231
|
-
|
245
|
+
|
232
246
|
with_static_image_provider do |provider|
|
233
247
|
widget = Widget.new
|
234
248
|
widget.captcha_solution = '---'
|
235
|
-
|
249
|
+
|
236
250
|
assert widget.valid?
|
237
|
-
|
251
|
+
|
238
252
|
Widget.with_captcha_validation do
|
239
253
|
assert !widget.valid?
|
240
254
|
end
|
241
|
-
|
255
|
+
|
242
256
|
assert widget.valid?
|
243
257
|
end
|
244
|
-
|
258
|
+
|
245
259
|
with_question_provider do |provider|
|
246
260
|
widget = Widget.new
|
247
261
|
widget.captcha_solution = '---'
|
248
|
-
|
262
|
+
|
249
263
|
assert widget.valid?
|
250
|
-
|
264
|
+
|
251
265
|
Widget.with_captcha_validation do
|
252
266
|
assert !widget.valid?
|
253
267
|
end
|
254
|
-
|
268
|
+
|
255
269
|
assert widget.valid?
|
256
270
|
end
|
257
271
|
end
|
258
272
|
end
|
273
|
+
|