kandr-easy_captcha 0.9.2 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a9098d3c82557be66d9f3769ef7a815d1bea207b9b0de7931cde92c5289766e
4
- data.tar.gz: 4df8b4194f3e77b338e1193f8f0973da28cdcf52c332beee4dfbca1338a21bbe
3
+ metadata.gz: 4f480d073b0f9492cf7ea45423c41c996b8d9d7410843fbbac6ed16409b45754
4
+ data.tar.gz: c96d490a4ef8726c2a3cf063995170763d13a5139b5dea9d7e7bcd06b07dae08
5
5
  SHA512:
6
- metadata.gz: 1f103d10c86ebd21a5e7c2e364d63c3a58d84276e225b03d499498f929d742ca304ea6b53af277de01aa8ffb56b1af7d2ceb8defbec730b32cd3b11d88add2ae
7
- data.tar.gz: 9050a9ba91f71fc3a4990b84ba013604539add8a3ad291e5a9ef73a8e21d08d4e68f6b9c289e75bb531ddc56037fa60338c743019f1e57f3cb77eec46a150c62
6
+ metadata.gz: 86c45902d40052711b77645989d0e5a976960ef893887464623bf19ca9c78594172799f67b2350f8cfbc4d48433f705fc7005087f670d31b82f205f2c1765053
7
+ data.tar.gz: 82253e91a9e2227ca6a0d54c0f125efdcd4bc09adefc69320e036b13da56edbe3f3074a5c3b556b7e59a83a746a66d25f2825e209462357307cefb97ed3ccb38
data/LICENSE.txt CHANGED
@@ -1,4 +1,5 @@
1
1
  Copyright (c) 2010 Marco Scholl
2
+ Copyright (c) 2020-2021 Karl Wilbur; K&R Software, LLC.; and EasyCAPTCHA Contributors
2
3
 
3
4
  Permission is hereby granted, free of charge, to any person obtaining
4
5
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # EasyCAPTCHA
2
2
 
3
- [![Build Status](https://travis-ci.org/K-and-R/easy_captcha.svg?branch=master)](https://travis-ci.org/K-and-R/easy_captcha)
3
+ [![Build Status](https://travis-ci.com/K-and-R/easy_captcha.svg?branch=master)](https://travis-ci.org/K-and-R/easy_captcha)
4
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/0505012081a472de86e2/maintainability)](https://codeclimate.com/github/K-and-R/easy_captcha/maintainability)
5
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/0505012081a472de86e2/test_coverage)](https://codeclimate.com/github/K-and-R/easy_captcha/test_coverage)
6
6
 
@@ -48,73 +48,92 @@ you want to customize the default configuration
48
48
 
49
49
  ```ruby
50
50
  EasyCaptcha.setup do |config|
51
- # Cache
52
- # config.cache = true
51
+ # #####
52
+ # # Cache
53
+ # config.cache = true
53
54
  # Cache temp dir from Rails.root
54
- # config.cache_temp_dir = Rails.root.join('tmp', 'captchas')
55
+ # config.cache_temp_dir = Rails.root.join('tmp', 'captchas')
55
56
  # Cache size
56
- # config.cache_size = 500
57
+ # config.cache_size = 500
57
58
  # Cache expire
58
- # config.cache_expire = 1.day
59
-
60
- # Chars
61
- # config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
62
-
63
- # Length
64
- # config.length = 6
65
-
59
+ # config.cache_expire = 1.day
60
+
61
+ # #####
62
+ # # CAPTCHA
63
+ # # Chars available for CAPTCHA
64
+ # config.captcha_character_pool = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
65
+ #
66
+ # # Length of CAPTCHA string
67
+ # config.captcha_character_count = 6
68
+ # config.captcha_character_count_max = 6
69
+ # config.captcha_character_count_min = 6
70
+ #
71
+ # # CAPTCHA Image dimensions
72
+ # config.captcha_image_height = 40
73
+
74
+ #####
66
75
  # Image
67
- # config.image_height = 40
68
- # config.image_width = 140
76
+ # config.captcha_image_height = 40
77
+ # config.captcha_image_width = 140
78
+
79
+ # #####
80
+ # # eSpeak
81
+ # # Enable eSpeak using all defaults:
82
+ # config.espeak = true
69
83
 
70
- # eSpeak (default disabled)
84
+ # # Enable eSpeak using custom config:
71
85
  # config.espeak do |espeak|
72
- # Amplitude, 0 to 200
73
- # espeak.amplitude = 80..120
86
+ # Amplitude, 0 to 200
87
+ # espeak.amplitude = 80..120
74
88
 
75
- # Word gap. Pause between words
76
- # espeak.gap = 80
89
+ # Word gap. Pause between words
90
+ # espeak.gap = 80
77
91
 
78
- # Pitch adjustment, 0 to 99
79
- # espeak.pitch = 30..70
92
+ # Pitch adjustment, 0 to 99
93
+ # espeak.pitch = 30..70
80
94
 
81
- # Use voice file of this name from espeak-data/voices
82
- # espeak.voice = nil
95
+ # Use voice file of this name from espeak-data/voices
96
+ # espeak.voice = nil
83
97
  # end
84
98
 
85
- # configure generator
99
+ # #####
100
+ # # Generator
86
101
  # config.generator :default do |generator|
87
-
88
- # Font
89
- # generator.font_size = 24
90
- # generator.font_fill_color = '#333333'
91
- # generator.font_stroke_color = '#000000'
92
- # generator.font_stroke = 0
93
- # generator.font = File.expand_path('../../resources/afont.ttf', __FILE__)
94
-
95
-
96
- # Background color
97
- # generator.image_background_color = "#FFFFFF"
98
- # Or background image (e.g. transparent png)
99
- # generator.background_image = File.expand_path('../../resources/captcha_bg.png', __FILE__)
100
-
101
- # Wave
102
- # generator.wave = true
103
- # generator.wave_length = (60..100)
104
- # generator.wave_amplitude = (3..5)
105
-
106
- # Sketch
107
- # generator.sketch = true
108
- # generator.sketch_radius = 3
109
- # generator.sketch_sigma = 1
110
-
111
- # Implode
112
- # generator.implode = 0.1
113
-
114
- # Blur
115
- # generator.blur = true
116
- # generator.blur_radius = 1
117
- # generator.blur_sigma = 2
102
+ # # # Backgound
103
+ # # # Ignored (set as `none`) when using custom background image or fill
104
+ # # generator.background_color = '#FFFFFF'
105
+ # # An instance of a `Fill` class. RMagick supplies three `Fill` classes:
106
+ # # - `HatchFill`
107
+ # # - `GradientFill`
108
+ # # - `TextureFill`.
109
+ # generator.background_fill = nil
110
+ # # # Custom image to be used for CAPTCHA background
111
+ # # generator.background_image = nil
112
+
113
+ # # Blur
114
+ # # generator.blur = true
115
+ # # generator.blur_radius = 1
116
+ # # generator.blur_sigma = 2
117
+
118
+ # # # Font
119
+ # # generator.font_size = 24
120
+ # # generator.font_fill_color = '#333333'
121
+ # # generator.font_stroke_color = '#000000'
122
+ # # generator.font_stroke = 0
123
+ # # generator.font_family = File.expand_path('../../resources/afont.ttf', __FILE__)
124
+
125
+ # # # Implode (the "funhouse mirror" effect; negative values allowed)
126
+ # # generator.implode = 0.1
127
+
128
+ # # # Sketch
129
+ # # generator.sketch = true
130
+ # # generator.sketch_radius = 0.0
131
+ # # generator.sketch_sigma = 1.0
132
+
133
+ # # Wave
134
+ # # generator.wave = true
135
+ # # generator.wave_length = (60..100)
136
+ # # generator.wave_amplitude = (3..5)
118
137
  # end
119
138
  end
120
139
  ```
@@ -127,11 +146,11 @@ file below.
127
146
  ```ruby
128
147
  EasyCaptcha.setup do |config|
129
148
  # Cache
130
- config.cache = true
149
+ config.cache = true
131
150
  # Cache temp dir from Rails.root
132
- config.cache_temp_dir = Rails.root.join('tmp', 'captchas')
151
+ config.cache_temp_dir = Rails.root.join('tmp', 'captchas')
133
152
  # Cache expire
134
- config.cache_expire = 1.day
153
+ config.cache_expire = 1.day
135
154
  # Cache size
136
155
  # config.cache_size = 500
137
156
  end
@@ -174,4 +193,7 @@ See the [CHANGELOG.md](./CHANGELOG.md)
174
193
 
175
194
  ## Copyright
176
195
 
177
- Copyright (c) 2010 Marco Scholl. See LICENSE.txt for further details.
196
+ EasyCAPTCHA is licensed under The MIT License.
197
+
198
+ Copyright (c) 2010-2021 Marco Scholl; K&R Software, LLC.; and EasyCAPTCHA Contributors.
199
+ See [`LICENSE.txt`](./LICENSE.txt) and [`CHANGELOG.md`](./CHANGELOG.md) for further details.
data/lib/easy_captcha.rb CHANGED
@@ -22,6 +22,8 @@ module EasyCaptcha
22
22
  cache_size: 500,
23
23
  captcha_character_pool: %w[2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z],
24
24
  captcha_character_count: 6,
25
+ captcha_character_count_max: 6,
26
+ captcha_character_count_min: 6,
25
27
  captcha_image_height: 40,
26
28
  captcha_image_width: 140
27
29
  }.freeze
@@ -42,7 +44,7 @@ module EasyCaptcha
42
44
  mattr_accessor :captcha_character_pool
43
45
 
44
46
  # Length
45
- mattr_accessor :captcha_character_count
47
+ mattr_accessor :captcha_character_count, :captcha_character_count_max, :captcha_character_count_min
46
48
 
47
49
  # Image dimensions
48
50
  mattr_accessor :captcha_image_height, :captcha_image_width
@@ -61,6 +63,13 @@ module EasyCaptcha
61
63
  cache
62
64
  end
63
65
 
66
+ def captcha_code_length
67
+ max = [captcha_character_count_min, captcha_character_count_max].max
68
+ min = [captcha_character_count_min, captcha_character_count_max].min
69
+ return (min..max).to_a.sample if captcha_character_count == :range
70
+ captcha_character_count
71
+ end
72
+
64
73
  # select generator and configure this
65
74
  def generator(generator = nil, &block)
66
75
  resolve_generator(generator, &block) unless generator.nil?
@@ -7,6 +7,13 @@ module EasyCaptcha
7
7
 
8
8
  # send the generated image to browser
9
9
  def captcha
10
+ # Reset the CAPTCHA code on request
11
+ session.delete(:captcha)
12
+
13
+ # Generate the new CAPTCHA code
14
+ generate_captcha_code
15
+
16
+ # Generate and output the CAPTCHA image/audio file
10
17
  if (params[:format] == 'wav') && EasyCaptcha.espeak?
11
18
  send_data(generate_speech_captcha, disposition: 'inline', type: 'audio/wav')
12
19
  else
@@ -50,7 +50,7 @@ module EasyCaptcha
50
50
  # rubocop:disable Metrics/AbcSize, Naming/MemoizedInstanceVariableName
51
51
  def generate_captcha_code
52
52
  @captcha_code ||= begin
53
- length = EasyCaptcha.captcha_character_count
53
+ length = EasyCaptcha.captcha_code_length
54
54
  # overwrite `current_captcha_code`
55
55
  session[:captcha] = Array.new(length) { EasyCaptcha.captcha_character_pool.sample }.join
56
56
  Rails.logger.info(
@@ -10,6 +10,8 @@ module EasyCaptcha
10
10
  # default generator
11
11
  class Default < Base
12
12
  DEFAULT_CONFIG = {
13
+ background_color: '#FFFFFF',
14
+ background_fill: nil,
13
15
  background_image: nil,
14
16
  blur: true,
15
17
  blur_radius: 1,
@@ -19,7 +21,6 @@ module EasyCaptcha
19
21
  font_size: 24,
20
22
  font_stroke: 0,
21
23
  font_stroke_color: '#000000',
22
- image_background_color: '#FFFFFF',
23
24
  implode: 0.05,
24
25
  sketch: true,
25
26
  sketch_radius: 3,
@@ -36,15 +37,15 @@ module EasyCaptcha
36
37
  end
37
38
  end
38
39
 
39
- # Gaussian Blur
40
+ # Background
41
+ attr_accessor :background_color, :background_fill, :background_image
42
+
43
+ # Blur
40
44
  attr_accessor :blur, :blur_radius, :blur_sigma
41
45
 
42
46
  # Font
43
47
  attr_accessor :font_size, :font_fill_color, :font, :font_family, :font_stroke, :font_stroke_color
44
48
 
45
- # Background
46
- attr_accessor :image_background_color, :background_image
47
-
48
49
  # Implode
49
50
  attr_accessor :implode
50
51
 
@@ -60,6 +61,18 @@ module EasyCaptcha
60
61
  # The CAPTCHA image
61
62
  attr_reader :image
62
63
 
64
+ def image_background_color=(val)
65
+ warn '[DEPRECATION] EasyCaptcha configuration option `image_background_color` is deprecated. ' \
66
+ 'Please use `background_color` instead.'
67
+ self.background_color = val
68
+ end
69
+
70
+ def image_background_color
71
+ warn '[DEPRECATION] EasyCaptcha configuration option `image_background_color` is deprecated. ' \
72
+ 'Please use `background_color` instead.'
73
+ background_color
74
+ end
75
+
63
76
  def blur? #:nodoc:
64
77
  @blur
65
78
  end
@@ -76,25 +89,27 @@ module EasyCaptcha
76
89
  def generate(code)
77
90
  @code = code
78
91
  render_code_in_image
79
- apply_blur
80
- apply_wave
81
- apply_sketch
82
- apply_implode
83
- apply_crop
92
+ apply_effects
84
93
  create_blob
85
94
  set_image_encoding
86
95
  free_canvas
87
96
  @image
88
97
  end
89
98
 
99
+ def apply_effects
100
+ apply_sketch
101
+ apply_implode
102
+ apply_blur
103
+ apply_wave
104
+ apply_crop
105
+ end
106
+
90
107
  def create_blob
91
- @image = begin
92
- if generator_config.background_image.present?
93
- create_composite_blob
94
- else
95
- canvas.to_blob { self.format = 'PNG' }
96
- end
97
- end
108
+ @image = if generator_config.background_image.present?
109
+ create_composite_blob
110
+ else
111
+ canvas.to_blob { self.format = 'PNG' }
112
+ end
98
113
  end
99
114
 
100
115
  def create_composite_blob
@@ -128,37 +143,101 @@ module EasyCaptcha
128
143
  private
129
144
 
130
145
  def apply_blur
131
- return unless blur?
132
- @canvas = canvas.blur_image(generator_config.blur_radius, generator_config.blur_sigma)
146
+ return @canvas unless blur?
147
+ apply_image_blur
148
+ apply_motion_blur
149
+ end
150
+
151
+ def apply_image_blur
152
+ return @canvas unless canvas.respond_to?(:blur_image)
153
+ ###
154
+ # https://rmagick.github.io/image1.html#blur_image
155
+ # Parameters:
156
+ # radius (Float) (defaults to: 0.0) - The radius of the Gaussian operator.
157
+ # sigma (Float) (defaults to: 1.0) - The standard deviation of the Gaussian operator. Must be non-zero.
158
+ @canvas = canvas.blur_image(
159
+ generator_config.blur_radius,
160
+ generator_config.blur_sigma
161
+ )
162
+ end
163
+
164
+ def apply_motion_blur
165
+ return @canvas unless canvas.respond_to?(:motion_blur)
166
+ ###
167
+ # https://rmagick.github.io/image2.html#motion_blur
168
+ # Parameters:
169
+ # radius (Float) (defaults to: 0.0) - The radius of the Gaussian operator.
170
+ # sigma (Float) (defaults to: 1.0) - The standard deviation of the Gaussian operator. Must be non-zero.
171
+ # angle (Float) (defaults to: 0.0) - The angle (in degrees) of the blurring motion.
172
+ @canvas = canvas.motion_blur(
173
+ generator_config.blur_radius,
174
+ generator_config.blur_sigma,
175
+ rand(180)
176
+ )
133
177
  end
134
178
 
135
179
  def apply_crop
180
+ return @canvas unless canvas.respond_to?(:crop)
136
181
  # Crop image because to big after waveing
182
+ ###
183
+ # https://rmagick.github.io/image1.html#crop
184
+ # Parameters:
185
+ # gravity (Magick::GravityType) - the gravity type
186
+ # width (Numeric) - width of region
187
+ # height (Numeric) - height of region
137
188
  @canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.captcha_image_width, EasyCaptcha.captcha_image_height)
138
189
  end
139
190
 
140
191
  def apply_implode
192
+ ###
193
+ # https://rmagick.github.io/image2.html#implode
194
+ # Parameters:
195
+ # amount (Float) (defaults to: 0.50) - The precentage to implode the image
141
196
  @canvas = canvas.implode(generator_config.implode.to_f) if generator_config.implode.is_a? Float
142
197
  end
143
198
 
144
199
  def apply_sketch
145
- return unless sketch? && canvas.respond_to?(:sketch)
200
+ return @canvas unless sketch? && canvas.respond_to?(:sketch)
201
+ ###
202
+ # https://rmagick.github.io/image3.html#sketch
203
+ # Parameters:
204
+ # radius (Float) (defaults to: 0.0) - The radius of the Gaussian operator.
205
+ # sigma (Float) (defaults to: 1.0) - The standard deviation of the Gaussian operator.
206
+ # angle (Float) (defaults to: 0.0) - The angle (in degrees) of the sketch lines.
146
207
  @canvas = canvas.sketch(generator_config.sketch_radius, generator_config.sketch_sigma, rand(180))
147
208
  end
148
209
 
149
210
  def apply_wave
150
- return unless wave?
211
+ return @canvas unless wave? && canvas.respond_to?(:wave)
212
+ ###
213
+ # https://rmagick.github.io/image3.html#wave
214
+ # Parameters:
215
+ # amplitude (Float) (defaults to: 25.0) - the amplitude
216
+ # wavelength (Float) (defaults to: 150.0) - the wave length
151
217
  @canvas = canvas.wave(random_wave_amplitude, random_wave_length)
152
218
  end
153
219
 
220
+ # rubocop:disable Metrics/AbcSize
154
221
  def canvas
155
222
  config = generator_config
156
223
  @canvas = nil if @canvas.respond_to?('destroyed?') && @canvas.destroyed?
157
- @canvas ||= Magick::Image.new(EasyCaptcha.captcha_image_width, EasyCaptcha.captcha_image_height) do |_variable|
158
- self.background_color = config.image_background_color unless config.image_background_color.nil?
159
- self.background_color = 'none' if config.background_image.present?
224
+ ###
225
+ # https://rmagick.github.io/image1.html#new
226
+ # Parameters:
227
+ # cols (Numeric) - the image width
228
+ # rows (Numeric) - the image height
229
+ # fill (Magick::GradientFill, Magick::HatchFill, Magick::TextureFill) (defaults to: nil)
230
+ # - if object is given as fill argument, background color will be filled using it.
231
+ @canvas ||= Magick::Image.new(
232
+ EasyCaptcha.captcha_image_width,
233
+ EasyCaptcha.captcha_image_height,
234
+ config.background_fill
235
+ ) do |image|
236
+ image.background_color = config.background_color unless config.background_color.nil?
237
+ image.background_color = 'none' if config.background_image.present? || config.background_fill.present?
160
238
  end
161
239
  end
240
+ # rubocop:enable Metrics/AbcSize
162
241
 
163
242
  def generator_config
164
243
  @generator_config ||= self
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyCaptcha
4
- VERSION = '0.9.2'
4
+ VERSION = '0.11.0'
5
5
  end
@@ -46,6 +46,17 @@ EasyCaptcha.setup do |config|
46
46
  # #####
47
47
  # # Generator
48
48
  # config.generator :default do |generator|
49
+ # # Backgound
50
+ # # Ignored (set as `none`) when using custom background image or fill
51
+ # generator.background_color = '#FFFFFF'
52
+ # # An instance of a `Fill` class. RMagick supplies three `Fill` classes:
53
+ # # - `HatchFill`
54
+ # # - `GradientFill`
55
+ # # - `TextureFill`.
56
+ # generator.background_fill = nil
57
+ # # Custom image to be used for CAPTCHA background
58
+ # generator.background_image = nil
59
+ #
49
60
  # # Blur
50
61
  # generator.blur = true
51
62
  # generator.blur_radius = 1
@@ -58,10 +69,7 @@ EasyCaptcha.setup do |config|
58
69
  # generator.font_stroke = 0
59
70
  # generator.font_stroke_color = '#000000'
60
71
  #
61
- # # Image
62
- # generator.image_background_color = "#FFFFFF"
63
- #
64
- # # Implode
72
+ # # Implode (the "funhouse mirror" effect; negative values allowed)
65
73
  # generator.implode = 0.1
66
74
  #
67
75
  # # Sketch
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'spec_helper'
5
+
6
+ require 'easy_captcha'
7
+
8
+ custom_generator_config_options = {
9
+ background_color: '#abc123',
10
+ background_fill: nil,
11
+ background_image: nil,
12
+ blur: true,
13
+ blur_radius: 2,
14
+ blur_sigma: 5,
15
+ font_size: 20,
16
+ font_fill_color: '#123456',
17
+ font_stroke_color: '#fedcba',
18
+ font_stroke: 2,
19
+ font_family: File.expand_path('../resources/afont.ttf', __dir__),
20
+ implode: 0.1,
21
+ sketch: true,
22
+ sketch_radius: 5,
23
+ sketch_sigma: 2,
24
+ wave: true,
25
+ wave_length: (50..80),
26
+ wave_amplitude: (2..8)
27
+ }
28
+
29
+ RSpec.describe EasyCaptcha::Generator::Default do
30
+ let(:default_config) { described_class::DEFAULT_CONFIG }
31
+ let(:length) { EasyCaptcha.captcha_code_length }
32
+ let(:code) { Array.new(length) { EasyCaptcha.captcha_character_pool.sample }.join }
33
+
34
+ before do
35
+ EasyCaptcha.setup
36
+ end
37
+
38
+ context 'when using defaults' do
39
+ let(:generator) { EasyCaptcha.generator }
40
+
41
+ described_class::DEFAULT_CONFIG.map do |k, v|
42
+ it "has #{k} value that matches the default config" do
43
+ expect(generator.send(k)).to eq v
44
+ end
45
+ end
46
+
47
+ context 'when generating an image' do
48
+ it 'generates an image' do
49
+ expect(generator.generate(code)).not_to be_empty
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'when overriding defaults' do
55
+ let(:generator) do
56
+ described_class.new do |g|
57
+ custom_generator_config_options.each { |k, v| g.send("#{k}=", v) }
58
+ end
59
+ end
60
+
61
+ custom_generator_config_options.map do |k, v|
62
+ it "uses the `#{k}` value from the custom config" do
63
+ expect(generator.send(k)).to eq v
64
+ end
65
+ end
66
+
67
+ context 'when generating an image' do
68
+ it 'generates an image' do
69
+ expect(generator.generate(code)).not_to be_empty
70
+ end
71
+
72
+ describe 'canvas' do
73
+ let(:canvas) { generator.send(:canvas) }
74
+
75
+ it 'has a row count that matches the defined captcha_image_height' do
76
+ expect(canvas.rows).to eq EasyCaptcha.captcha_image_height
77
+ end
78
+
79
+ it 'has a columns count that matches the defined captcha_image_width' do
80
+ expect(canvas.columns).to eq EasyCaptcha.captcha_image_width
81
+ end
82
+
83
+ it 'has a background color that matches the defined background_color code' do
84
+ # RGB 8-bit hex codes (like `#abc123`) are converted to 16-bit (`#ababc1c12323`)
85
+ # I would expect `#abc123` to become `#00ab00c10023`, but that's not what happens
86
+ color_code = '#abc123'
87
+ generator.background_color = color_code
88
+ color_code_alnum = color_code[1..6]
89
+ sixteen_bit_color_code = '#' + color_code_alnum.scan(/../).map { |color| color * 2 }.join.upcase # rubocop:disable Style/StringConcatenation
90
+ expect(generator.send(:canvas).background_color).to eq sixteen_bit_color_code
91
+ end
92
+
93
+ it 'has a background color that matches the defined background_color name' do
94
+ # Using a color name string like "red"
95
+ color_name = 'red'
96
+ generator.background_color = color_name
97
+ expect(generator.send(:canvas).background_color).to eq color_name
98
+ end
99
+
100
+ it 'ignores the background_color value with using a `Fill` object' do
101
+ generator.background_fill = Magick::GradientFill.new(0, 0, 0, 1000, '#990000', '#000000')
102
+ # `nil` or `'none'` gets converted to `'black'`
103
+ expect(generator.send(:canvas).background_color).to eq 'black'
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -22,7 +22,7 @@ RSpec.describe EasyCaptcha::Generators::InstallGenerator, type: :generator do
22
22
  File.open "#{temp_dir}/app/controllers/application_controller.rb", 'w' do |f|
23
23
  f.write "class ApplicationController \nend"
24
24
  end
25
- # Ensure taht `ApplicationController` is already defined.
25
+ # Ensure that `ApplicationController` is already defined.
26
26
  require "#{temp_dir}/app/controllers/application_controller.rb"
27
27
  run_generator
28
28
  end
@@ -33,39 +33,40 @@ RSpec.describe EasyCaptcha::Generators::InstallGenerator, type: :generator do
33
33
  end
34
34
  # rubocop:enable RSpec/BeforeAfterAll
35
35
 
36
- # rubocop:disable Lint/AmbiguousBlockAssociation
37
- specify do
38
- expect(destination_root).to have_structure {
39
- directory('config') do
40
- directory('initializers') do
41
- file('easy_captcha.rb') do
42
- contains('EasyCaptcha.setup')
43
- end
44
- end
45
- end
46
- }
36
+ specify 'creates proper initializer' do
37
+ expect(destination_root).to(have_structure do
38
+ directory('config') do
39
+ directory('initializers') do
40
+ file('easy_captcha.rb') do
41
+ contains('EasyCaptcha.setup')
42
+ end
43
+ end
44
+ end
45
+ end
46
+ ) # editorconfig-checker-disable-line
47
47
  end
48
48
 
49
- specify do
50
- expect(destination_root).to have_structure {
51
- directory('app') do
52
- directory('controllers') do
53
- file('application_controller.rb') do
54
- contains('reset_last_captcha_code!')
55
- end
56
- end
57
- end
58
- }
49
+ specify 'adds `reset_last_captcha_code!` to `application_controller`' do
50
+ expect(destination_root).to(have_structure do
51
+ directory('app') do
52
+ directory('controllers') do
53
+ file('application_controller.rb') do
54
+ contains('reset_last_captcha_code!')
55
+ end
56
+ end
57
+ end
58
+ end
59
+ ) # editorconfig-checker-disable-line
59
60
  end
60
61
 
61
- specify do
62
- expect(destination_root).to have_structure {
63
- directory('config') do
64
- file('routes.rb') do
65
- contains('captcha_route')
66
- end
67
- end
68
- }
62
+ specify 'adds `captcha_route`' do
63
+ expect(destination_root).to(have_structure do
64
+ directory('config') do
65
+ file('routes.rb') do
66
+ contains('captcha_route')
67
+ end
68
+ end
69
+ end
70
+ ) # editorconfig-checker-disable-line
69
71
  end
70
- # rubocop:enable Lint/AmbiguousBlockAssociation
71
72
  end
@@ -2,6 +2,14 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
+ custom_captcha_config_options = {
6
+ cache: false,
7
+ captcha_character_pool: %w[2 4 6 8 A E I O U],
8
+ captcha_image_height: 6,
9
+ captcha_image_width: 9,
10
+ captcha_character_count: 6
11
+ }
12
+
5
13
  describe EasyCaptcha do
6
14
  it 'has a VERSION' do
7
15
  expect(described_class::VERSION).not_to be_blank
@@ -13,57 +21,39 @@ describe EasyCaptcha do
13
21
  expect { described_class.setup }.not_to raise_error
14
22
  end
15
23
 
16
- it 'will use defaults if no block given' do
17
- described_class.setup
18
- end
19
-
20
24
  it 'has default generator' do
21
25
  expect(described_class.generator).to be_an(EasyCaptcha::Generator::Default)
22
26
  end
23
- end
24
27
 
25
- context 'when chaging defaults' do
26
- let(:captcha_config_options) do
27
- {
28
- cache: false,
29
- captcha_character_pool: %w[2 4 6 8 A E I O U],
30
- captcha_image_height: 6,
31
- captcha_image_width: 9,
32
- captcha_character_count: 6
33
- }
34
- end
28
+ context 'with default options' do
29
+ before do
30
+ described_class.setup
31
+ end
35
32
 
36
- let(:generator_config_options) do
37
- {
38
- blur: true,
39
- blur_radius: 1,
40
- blur_sigma: 2,
41
- font_size: 24,
42
- font_fill_color: '#333333',
43
- font_stroke_color: '#000000',
44
- font_stroke: 0,
45
- font_family: File.expand_path('../resources/afont.ttf', __dir__),
46
- image_background_color: '#FFFFFF',
47
- implode: 0.1,
48
- sketch: true,
49
- sketch_radius: 3,
50
- sketch_sigma: 1,
51
- wave: true,
52
- wave_length: (60..100),
53
- wave_amplitude: (3..5)
54
- }
33
+ described_class::DEFAULT_CONFIG.map do |k, v|
34
+ it "has #{k} value that matches the default config" do
35
+ expect(described_class.send(k)).to eq v
36
+ end
37
+ end
55
38
  end
39
+ end
56
40
 
41
+ context 'when changing defaults' do
57
42
  before do
58
43
  described_class.setup do |config|
59
- captcha_config_options.each { |k, v| config.send("#{k}=", v) }
60
- config.generator(:default) { |generator| generator_config_options.each { |k, v| generator.send("#{k}=", v) } }
44
+ custom_captcha_config_options.each { |k, v| config.send("#{k}=", v) }
61
45
  end
62
46
  end
63
47
 
64
48
  it 'does not cache' do
65
49
  expect(described_class).not_to be_cache
66
50
  end
51
+
52
+ custom_captcha_config_options.map do |k, v|
53
+ it "uses the `#{k}` value from the custom config" do
54
+ expect(described_class.send(k)).to eq v
55
+ end
56
+ end
67
57
  end
68
58
  end
69
59
  end
data/spec/spec_helper.rb CHANGED
@@ -21,7 +21,3 @@ require 'action_dispatch'
21
21
  require 'action_view'
22
22
  require 'rspec/rails'
23
23
  require 'easy_captcha'
24
-
25
- # Requires supporting files with custom matchers and macros, etc,
26
- # in ./support/ and its subdirectories.
27
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kandr-easy_captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Scholl
@@ -69,6 +69,7 @@ files:
69
69
  - spec/easy_captcha/captcha_spec.rb
70
70
  - spec/easy_captcha/controller_helpers_spec.rb
71
71
  - spec/easy_captcha/espeak_spec.rb
72
+ - spec/easy_captcha/generator/default_generator_spec.rb
72
73
  - spec/easy_captcha/generators/install_generator_spec.rb
73
74
  - spec/easy_captcha/model_helpers_spec.rb
74
75
  - spec/easy_captcha/routing_spec.rb
@@ -77,7 +78,6 @@ files:
77
78
  - spec/fixtures/application.rb
78
79
  - spec/fixtures/controller.rb
79
80
  - spec/fixtures/model.rb
80
- - spec/spec.opts
81
81
  - spec/spec_helper.rb
82
82
  homepage: http://github.com/K-and-R/easy_captcha
83
83
  licenses:
@@ -107,6 +107,7 @@ test_files:
107
107
  - spec/easy_captcha/captcha_spec.rb
108
108
  - spec/easy_captcha/controller_helpers_spec.rb
109
109
  - spec/easy_captcha/espeak_spec.rb
110
+ - spec/easy_captcha/generator/default_generator_spec.rb
110
111
  - spec/easy_captcha/generators/install_generator_spec.rb
111
112
  - spec/easy_captcha/model_helpers_spec.rb
112
113
  - spec/easy_captcha/routing_spec.rb
@@ -115,5 +116,4 @@ test_files:
115
116
  - spec/fixtures/application.rb
116
117
  - spec/fixtures/controller.rb
117
118
  - spec/fixtures/model.rb
118
- - spec/spec.opts
119
119
  - spec/spec_helper.rb
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --color