file_validators 2.3.0 → 3.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +32 -0
- data/Appraisals +2 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile +2 -0
- data/README.md +10 -9
- data/Rakefile +3 -1
- data/file_validators.gemspec +9 -6
- data/gemfiles/activemodel_3.2.gemfile +7 -5
- data/gemfiles/activemodel_4.0.gemfile +7 -5
- data/gemfiles/activemodel_4.1.gemfile +7 -5
- data/gemfiles/activemodel_4.2.gemfile +7 -5
- data/gemfiles/activemodel_5.0.gemfile +6 -4
- data/lib/file_validators.rb +3 -1
- data/lib/file_validators/utils/content_type_detector.rb +9 -10
- data/lib/file_validators/utils/media_type_spoof_detector.rb +4 -4
- data/lib/file_validators/validators/file_content_type_validator.rb +15 -15
- data/lib/file_validators/validators/file_size_validator.rb +36 -18
- data/lib/file_validators/version.rb +3 -1
- data/spec/integration/combined_validators_integration_spec.rb +3 -1
- data/spec/integration/file_content_type_validation_integration_spec.rb +43 -17
- data/spec/integration/file_size_validator_integration_spec.rb +37 -15
- data/spec/lib/file_validators/utils/content_type_detector_spec.rb +2 -0
- data/spec/lib/file_validators/utils/media_type_spoof_detector_spec.rb +2 -0
- data/spec/lib/file_validators/validators/file_content_type_validator_spec.rb +90 -32
- data/spec/lib/file_validators/validators/file_size_validator_spec.rb +78 -30
- data/spec/spec_helper.rb +2 -0
- data/spec/support/matchers/allow_content_type.rb +2 -0
- data/spec/support/matchers/allow_file_size.rb +2 -0
- metadata +34 -19
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe ActiveModel::Validations::FileSizeValidator do
|
@@ -7,9 +9,9 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
7
9
|
|
8
10
|
def storage_units
|
9
11
|
if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
|
10
|
-
{ 5120 => '5 KB',
|
12
|
+
{ 5120 => '5 KB', 10_240 => '10 KB' }
|
11
13
|
else
|
12
|
-
{ 5120 => '5120 Bytes',
|
14
|
+
{ 5120 => '5120 Bytes', 10_240 => '10240 Bytes' }
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
@@ -33,7 +35,7 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
33
35
|
end
|
34
36
|
|
35
37
|
context 'as a proc' do
|
36
|
-
before { build_validator in:
|
38
|
+
before { build_validator in: ->(_record) { (5.kilobytes..10.kilobytes) } }
|
37
39
|
|
38
40
|
it { is_expected.to allow_file_size(7.kilobytes, @validator) }
|
39
41
|
it { is_expected.not_to allow_file_size(4.kilobytes, @validator) }
|
@@ -51,7 +53,7 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
51
53
|
end
|
52
54
|
|
53
55
|
context 'as a proc' do
|
54
|
-
before { build_validator greater_than_or_equal_to:
|
56
|
+
before { build_validator greater_than_or_equal_to: ->(_record) { 10.kilobytes } }
|
55
57
|
|
56
58
|
it { is_expected.to allow_file_size(11.kilobytes, @validator) }
|
57
59
|
it { is_expected.to allow_file_size(10.kilobytes, @validator) }
|
@@ -69,7 +71,7 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
69
71
|
end
|
70
72
|
|
71
73
|
context 'as a proc' do
|
72
|
-
before { build_validator less_than_or_equal_to:
|
74
|
+
before { build_validator less_than_or_equal_to: ->(_record) { 10.kilobytes } }
|
73
75
|
|
74
76
|
it { is_expected.to allow_file_size(9.kilobytes, @validator) }
|
75
77
|
it { is_expected.to allow_file_size(10.kilobytes, @validator) }
|
@@ -86,7 +88,7 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
86
88
|
end
|
87
89
|
|
88
90
|
context 'as a proc' do
|
89
|
-
before { build_validator greater_than:
|
91
|
+
before { build_validator greater_than: ->(_record) { 10.kilobytes } }
|
90
92
|
|
91
93
|
it { is_expected.to allow_file_size(11.kilobytes, @validator) }
|
92
94
|
it { is_expected.not_to allow_file_size(10.kilobytes, @validator) }
|
@@ -102,7 +104,7 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
102
104
|
end
|
103
105
|
|
104
106
|
context 'as a proc' do
|
105
|
-
before { build_validator less_than:
|
107
|
+
before { build_validator less_than: ->(_record) { 10.kilobytes } }
|
106
108
|
|
107
109
|
it { is_expected.to allow_file_size(9.kilobytes, @validator) }
|
108
110
|
it { is_expected.not_to allow_file_size(10.kilobytes, @validator) }
|
@@ -119,8 +121,10 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
119
121
|
end
|
120
122
|
|
121
123
|
context 'as a proc' do
|
122
|
-
before
|
123
|
-
|
124
|
+
before do
|
125
|
+
build_validator greater_than: ->(_record) { 5.kilobytes },
|
126
|
+
less_than: ->(_record) { 10.kilobytes }
|
127
|
+
end
|
124
128
|
|
125
129
|
it { is_expected.to allow_file_size(7.kilobytes, @validator) }
|
126
130
|
it { is_expected.not_to allow_file_size(5.kilobytes, @validator) }
|
@@ -129,42 +133,86 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
129
133
|
end
|
130
134
|
|
131
135
|
context 'with :message option' do
|
132
|
-
before
|
133
|
-
|
136
|
+
before do
|
137
|
+
build_validator in: (5.kilobytes..10.kilobytes),
|
138
|
+
message: 'is invalid. (Between %{min} and %{max} please.)'
|
139
|
+
end
|
134
140
|
|
135
|
-
it
|
136
|
-
|
141
|
+
it do
|
142
|
+
is_expected.not_to allow_file_size(
|
143
|
+
11.kilobytes, @validator,
|
144
|
+
message: "Avatar is invalid. (Between #{@storage_units[5120]}" \
|
145
|
+
" and #{@storage_units[10_240]} please.)"
|
146
|
+
)
|
147
|
+
end
|
137
148
|
|
138
|
-
it
|
139
|
-
|
149
|
+
it do
|
150
|
+
is_expected.to allow_file_size(
|
151
|
+
7.kilobytes, @validator,
|
152
|
+
message: "Avatar is invalid. (Between #{@storage_units[5120]}" \
|
153
|
+
" and #{@storage_units[10_240]} please.)"
|
154
|
+
)
|
155
|
+
end
|
140
156
|
end
|
141
157
|
|
142
158
|
context 'default error message' do
|
143
159
|
context 'given :in options' do
|
144
160
|
before { build_validator in: 5.kilobytes..10.kilobytes }
|
145
161
|
|
146
|
-
it
|
147
|
-
|
148
|
-
|
149
|
-
|
162
|
+
it do
|
163
|
+
is_expected.not_to allow_file_size(
|
164
|
+
11.kilobytes, @validator,
|
165
|
+
message: "Avatar file size must be between #{@storage_units[5120]}" \
|
166
|
+
" and #{@storage_units[10_240]}"
|
167
|
+
)
|
168
|
+
end
|
169
|
+
|
170
|
+
it do
|
171
|
+
is_expected.not_to allow_file_size(
|
172
|
+
4.kilobytes, @validator,
|
173
|
+
message: "Avatar file size must be between #{@storage_units[5120]}" \
|
174
|
+
" and #{@storage_units[10_240]}"
|
175
|
+
)
|
176
|
+
end
|
150
177
|
end
|
151
178
|
|
152
179
|
context 'given :greater_than and :less_than options' do
|
153
180
|
before { build_validator greater_than: 5.kilobytes, less_than: 10.kilobytes }
|
154
181
|
|
155
|
-
it
|
156
|
-
|
157
|
-
|
158
|
-
|
182
|
+
it do
|
183
|
+
is_expected.not_to allow_file_size(
|
184
|
+
11.kilobytes, @validator,
|
185
|
+
message: "Avatar file size must be less than #{@storage_units[10_240]}"
|
186
|
+
)
|
187
|
+
end
|
188
|
+
|
189
|
+
it do
|
190
|
+
is_expected.not_to allow_file_size(
|
191
|
+
4.kilobytes, @validator,
|
192
|
+
message: "Avatar file size must be greater than #{@storage_units[5120]}"
|
193
|
+
)
|
194
|
+
end
|
159
195
|
end
|
160
196
|
|
161
197
|
context 'given :greater_than_or_equal_to and :less_than_or_equal_to options' do
|
162
|
-
before
|
198
|
+
before do
|
199
|
+
build_validator greater_than_or_equal_to: 5.kilobytes,
|
200
|
+
less_than_or_equal_to: 10.kilobytes
|
201
|
+
end
|
163
202
|
|
164
|
-
it
|
165
|
-
|
166
|
-
|
167
|
-
|
203
|
+
it do
|
204
|
+
is_expected.not_to allow_file_size(
|
205
|
+
11.kilobytes, @validator,
|
206
|
+
message: "Avatar file size must be less than or equal to #{@storage_units[10_240]}"
|
207
|
+
)
|
208
|
+
end
|
209
|
+
|
210
|
+
it do
|
211
|
+
is_expected.not_to allow_file_size(
|
212
|
+
4.kilobytes, @validator,
|
213
|
+
message: "Avatar file size must be greater than or equal to #{@storage_units[5120]}"
|
214
|
+
)
|
215
|
+
end
|
168
216
|
end
|
169
217
|
end
|
170
218
|
|
@@ -191,7 +239,7 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
191
239
|
(described_class::CHECKS.keys - [:in]).each do |argument|
|
192
240
|
it "does not raise argument error if :#{argument} is numeric or a proc" do
|
193
241
|
expect { build_validator argument => 5.kilobytes }.not_to raise_error
|
194
|
-
expect { build_validator argument =>
|
242
|
+
expect { build_validator argument => ->(_record) { 5.kilobytes } }.not_to raise_error
|
195
243
|
end
|
196
244
|
|
197
245
|
it "raises error if :#{argument} is neither a number nor a proc" do
|
@@ -201,7 +249,7 @@ describe ActiveModel::Validations::FileSizeValidator do
|
|
201
249
|
|
202
250
|
it 'does not raise argument error if :in is a range or a proc' do
|
203
251
|
expect { build_validator in: 5.kilobytes..10.kilobytes }.not_to raise_error
|
204
|
-
expect { build_validator in:
|
252
|
+
expect { build_validator in: ->(_record) { 5.kilobytes..10.kilobytes } }.not_to raise_error
|
205
253
|
end
|
206
254
|
|
207
255
|
it 'raises error if :in is neither a range nor a proc' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ahmad Musaffa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -39,19 +39,33 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: coveralls
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack-test
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rake
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,33 +95,33 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: 3.5.0
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: rubocop
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- - "
|
101
|
+
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
103
|
+
version: 0.58.2
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
|
-
- - "
|
108
|
+
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
110
|
+
version: 0.58.2
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
112
|
+
name: terrapin
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
|
-
- - "
|
115
|
+
- - "~>"
|
102
116
|
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
117
|
+
version: '0.6'
|
104
118
|
type: :development
|
105
119
|
prerelease: false
|
106
120
|
version_requirements: !ruby/object:Gem::Requirement
|
107
121
|
requirements:
|
108
|
-
- - "
|
122
|
+
- - "~>"
|
109
123
|
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
124
|
+
version: '0.6'
|
111
125
|
description: Adds file validators to ActiveModel
|
112
126
|
email:
|
113
127
|
- musaffa_csemm@yahoo.com
|
@@ -117,6 +131,7 @@ extra_rdoc_files: []
|
|
117
131
|
files:
|
118
132
|
- ".gitignore"
|
119
133
|
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
120
135
|
- ".travis.yml"
|
121
136
|
- Appraisals
|
122
137
|
- CHANGELOG.md
|
@@ -169,9 +184,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
169
184
|
version: '0'
|
170
185
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
186
|
requirements:
|
172
|
-
- - "
|
187
|
+
- - ">"
|
173
188
|
- !ruby/object:Gem::Version
|
174
|
-
version:
|
189
|
+
version: 1.3.1
|
175
190
|
requirements: []
|
176
191
|
rubyforge_project:
|
177
192
|
rubygems_version: 2.5.1
|