validates_captcha 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,103 @@
1
+ require 'test_helper'
2
+
3
+ IMAGE = ValidatesCaptcha::Provider::Image
4
+
5
+ class ImageTest < 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 #reversible_encrypter method" do
24
+ assert_respond_to IMAGE, :reversible_encrypter
25
+ end
26
+
27
+ test "defines a class level #reversible_encrypter= method" do
28
+ assert_respond_to IMAGE, :reversible_encrypter=
29
+ end
30
+
31
+ test "#reversible_encrypter method's return value should equal the value set using the #reversible_encrypter= method" do
32
+ old_reversible_encrypter = IMAGE.reversible_encrypter
33
+
34
+ IMAGE.reversible_encrypter = 'abc'
35
+ assert_equal 'abc', IMAGE.reversible_encrypter
36
+
37
+ IMAGE.reversible_encrypter = old_reversible_encrypter
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
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
-
2
+
3
3
  RE = ValidatesCaptcha::ReversibleEncrypter::Simple
4
-
4
+
5
5
  class ReversibleEncrypterTest < ValidatesCaptcha::TestCase
6
6
  test "defines an instance level #encrypt method" do
7
7
  assert_respond_to RE.new, :encrypt
@@ -25,3 +25,4 @@ class ReversibleEncrypterTest < ValidatesCaptcha::TestCase
25
25
  end
26
26
  end
27
27
  end
28
+
@@ -112,3 +112,4 @@ class StringGeneratorTest < ValidatesCaptcha::TestCase
112
112
  SG.alphabet = old_alphabet
113
113
  end
114
114
  end
115
+
@@ -7,129 +7,22 @@ class ValidatesCaptchaTest < ValidatesCaptcha::TestCase
7
7
 
8
8
  test "class level #version method returns a valid version" do
9
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
10
  end
81
11
 
82
- test "defines a class level #encrypt_captcha_code method" do
83
- assert_respond_to ValidatesCaptcha, :encrypt_captcha_code
12
+ test "defines a class level #provider method" do
13
+ assert_respond_to ValidatesCaptcha, :provider
84
14
  end
85
15
 
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
16
+ test "defines a class level #provider= method" do
17
+ assert_respond_to ValidatesCaptcha, :provider=
97
18
  end
98
19
 
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)
20
+ test "#provider method's return value should equal the value set using the #provider= method" do
21
+ old_provider = ValidatesCaptcha.provider
104
22
 
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
23
+ ValidatesCaptcha.provider = 'abc'
24
+ assert_equal 'abc', ValidatesCaptcha.provider
130
25
 
131
- assert_not_nil result1
132
- assert_not_nil result2
133
- assert_equal result1, result2
26
+ ValidatesCaptcha.provider = old_provider
134
27
  end
135
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Andert
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-28 00:00:00 +02:00
12
+ date: 2009-09-29 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 2.3.2
34
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."
35
+ description: "A captcha verification approach for Rails apps, directly integrated into ActiveRecord\xE2\x80\x99s validation mechanism and providing helpers for ActionController and ActionView."
36
36
  email: martin@mehringen.de
37
37
  executables: []
38
38
 
@@ -52,19 +52,21 @@ files:
52
52
  - lib/validates_captcha/form_builder.rb
53
53
  - lib/validates_captcha/form_helper.rb
54
54
  - lib/validates_captcha/image_generator/simple.rb
55
- - lib/validates_captcha/middleware/simple.rb
56
55
  - lib/validates_captcha/model_validation.rb
56
+ - lib/validates_captcha/provider/image.rb
57
+ - lib/validates_captcha/provider/question.rb
57
58
  - lib/validates_captcha/reversible_encrypter/simple.rb
58
59
  - lib/validates_captcha/string_generator/simple.rb
59
60
  - lib/validates_captcha/test_case.rb
60
61
  - lib/validates_captcha/version.rb
61
62
  - rails/init.rb
62
63
  - test/cases/controller_validation_test.rb
63
- - test/cases/image_generator_test.rb
64
- - test/cases/middleware_test.rb
64
+ - test/cases/image_generator/simple_test.rb
65
65
  - test/cases/model_validation_test.rb
66
- - test/cases/reversible_encrypter_test.rb
67
- - test/cases/string_generator_test.rb
66
+ - test/cases/provider/image_test.rb
67
+ - test/cases/provider/question_test.rb
68
+ - test/cases/reversible_encrypter/simple_test.rb
69
+ - test/cases/string_generator/simple_test.rb
68
70
  - test/cases/validates_captcha_test.rb
69
71
  - test/test_helper.rb
70
72
  has_rdoc: true
@@ -74,7 +76,7 @@ licenses: []
74
76
  post_install_message:
75
77
  rdoc_options:
76
78
  - --title
77
- - Validates Captcha 0.9.2
79
+ - Validates Captcha 0.9.3
78
80
  - --main
79
81
  - README.rdoc
80
82
  - --line-numbers
@@ -101,13 +103,14 @@ rubyforge_project: validatecaptcha
101
103
  rubygems_version: 1.3.5
102
104
  signing_key:
103
105
  specification_version: 3
104
- summary: Image captcha verification for Rails using ActiveRecord's validation mechanism
106
+ summary: Captcha verification for Rails using ActiveRecord's validation mechanism
105
107
  test_files:
106
108
  - test/cases/controller_validation_test.rb
107
- - test/cases/image_generator_test.rb
108
- - test/cases/middleware_test.rb
109
+ - test/cases/image_generator/simple_test.rb
109
110
  - test/cases/model_validation_test.rb
110
- - test/cases/reversible_encrypter_test.rb
111
- - test/cases/string_generator_test.rb
111
+ - test/cases/provider/image_test.rb
112
+ - test/cases/provider/question_test.rb
113
+ - test/cases/reversible_encrypter/simple_test.rb
114
+ - test/cases/string_generator/simple_test.rb
112
115
  - test/cases/validates_captcha_test.rb
113
116
  - test/test_helper.rb
@@ -1,108 +0,0 @@
1
- require 'active_support/core_ext/string/bytesize'
2
-
3
- module ValidatesCaptcha
4
- module Middleware
5
- # This class acts as the Rack middleware that serves the captcha images.
6
- #
7
- # You can implement your own middleware by creating a
8
- # class that conforms to the method definitions of the example below and
9
- # assign an instance of it to ValidatesCaptcha#middleware=.
10
- #
11
- # Example for a custom middleware:
12
- #
13
- # class MyMiddleware
14
- # def image_path(encrypted_code)
15
- # '/captchas/image/' + encrypted_code + ValidatesCaptcha.captcha_image_file_extension
16
- # end
17
- #
18
- # def regenerate_path
19
- # '/captchas/regenerate'
20
- # end
21
- #
22
- # def call(env)
23
- # if env['PATH_INFO'] =~ /^\/captchas\/image\/([^\.]+)/
24
- # decrypted_code = ValidatesCaptcha.decrypt_captcha_code($1)
25
- #
26
- # if decrypted_code.nil?
27
- # [422, { 'Content-Type' => 'text/html' }, ['Unprocessable Entity']]
28
- # else
29
- # image_data = ValidatesCaptcha.generate_captcha_image(decrypted_code)
30
- #
31
- # headers = {
32
- # 'Content-Length' => image_data.bytesize.to_s,
33
- # 'Content-Type' => ValidatesCaptcha.captcha_image_mime_type,
34
- # 'Content-Disposition' => 'inline',
35
- # 'Content-Transfer-Encoding' => 'binary'
36
- # }
37
- #
38
- # [200, headers, [image_data]]
39
- # end
40
- # elsif env['PATH_INFO'] == regenerate_path
41
- # encrypted_code = ValidatesCaptcha.encrypt_captcha_code(ValidatesCaptcha.generate_captcha_code)
42
- # xml = { :encrypted_captcha_code => encrypted_code, :captcha_image_path => image_path(encrypted_code) }.to_xml
43
- #
44
- # [200, { 'Content-Type' => 'application/xml' }, [xml]]
45
- # else
46
- # [404, { 'Content-Type' => 'text/html' }, ['Not Found']]
47
- # end
48
- # end
49
- # end
50
- #
51
- # ValidatesCaptcha.middleware = MyMiddleware.new
52
- #
53
- class Simple
54
- # Returns the captcha image path for a given encrypted code.
55
- #
56
- # This is used by the +captcha_image+ form helper.
57
- def image_path(encrypted_code)
58
- "/captchas/#{encrypted_code}#{ValidatesCaptcha.captcha_image_file_extension}"
59
- end
60
-
61
- # Returns the path that is used when requesting the regeneration
62
- # of a captcha image. Defaults to '/captchas/regenerate'.
63
- #
64
- # This is used by the +regenerate_captcha_link+ form helper.
65
- def regenerate_path
66
- '/captchas/regenerate'
67
- end
68
-
69
- # This method is the one called by Rack.
70
- #
71
- # It returns HTTP status 404 if the path is not recognized. If the path is
72
- # recognized, it returns HTTP status 200 and delivers the image if it could
73
- # successfully decrypt the captcha code, otherwise HTTP status 422.
74
- #
75
- # Please take a look at the source code if you want to learn more.
76
- def call(env)
77
- if env['PATH_INFO'] =~ /^\/captchas\/([^\.]+)/
78
- if $1 == 'regenerate'
79
- encrypted_code = ValidatesCaptcha.encrypt_captcha_code(ValidatesCaptcha.generate_captcha_code)
80
- json = { :encrypted_captcha_code => encrypted_code, :captcha_image_path => image_path(encrypted_code) }.to_json
81
-
82
- [200, { 'Content-Type' => 'application/json' }, [json]]
83
- else
84
- decrypted_code = ValidatesCaptcha.decrypt_captcha_code($1)
85
-
86
- if decrypted_code.nil?
87
- [422, { 'Content-Type' => 'text/html' }, ['Unprocessable Entity']]
88
- else
89
- image_data = ValidatesCaptcha.generate_captcha_image(decrypted_code)
90
-
91
- response_headers = {
92
- 'Content-Length' => image_data.bytesize.to_s,
93
- 'Content-Type' => ValidatesCaptcha.captcha_image_mime_type,
94
- 'Content-Disposition' => 'inline',
95
- 'Content-Transfer-Encoding' => 'binary',
96
- 'Cache-Control' => 'private'
97
- }
98
-
99
- [200, response_headers, [image_data]]
100
- end
101
- end
102
- else
103
- [404, { 'Content-Type' => 'text/html' }, ['Not Found']]
104
- end
105
- end
106
- end
107
- end
108
- end