file_validators 1.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +11 -0
- data/Appraisals +19 -0
- data/Gemfile +15 -0
- data/MIT-LICENSE +20 -0
- data/README.md +216 -0
- data/README.rdoc +3 -0
- data/Rakefile +31 -0
- data/file_validators.gemspec +26 -0
- data/gemfiles/activemodel_3.0.gemfile +8 -0
- data/gemfiles/activemodel_3.1.gemfile +8 -0
- data/gemfiles/activemodel_3.2.gemfile +8 -0
- data/gemfiles/activemodel_4.0.gemfile +8 -0
- data/gemfiles/activemodel_4.1.gemfile +8 -0
- data/lib/file_validators/validators/file_content_type_validator.rb +73 -0
- data/lib/file_validators/validators/file_size_validator.rb +89 -0
- data/lib/file_validators/version.rb +3 -0
- data/lib/file_validators.rb +3 -0
- data/spec/fixtures/chubby_bubble.jpg +0 -0
- data/spec/fixtures/chubby_cute.png +0 -0
- data/spec/fixtures/cute.jpg +0 -0
- data/spec/fixtures/sample.txt +1 -0
- data/spec/integration/combined_validators_integration_spec.rb +84 -0
- data/spec/integration/file_content_type_validation_integration_spec.rb +195 -0
- data/spec/integration/file_size_validator_integration_spec.rb +152 -0
- data/spec/lib/file_validators/validators/file_content_type_validator_spec.rb +128 -0
- data/spec/lib/file_validators/validators/file_size_validator_spec.rb +108 -0
- data/spec/locale/en.yml +13 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/matchers/allow_content_type.rb +13 -0
- data/spec/support/matchers/allow_file_size.rb +13 -0
- metadata +159 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack/test/uploaded_file'
|
3
|
+
|
4
|
+
describe 'Combined File Validators integration with ActiveModel' do
|
5
|
+
class Person
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :avatar
|
8
|
+
end
|
9
|
+
|
10
|
+
before :all do
|
11
|
+
@cute_path = File.join(File.dirname(__FILE__), '../fixtures/cute.jpg')
|
12
|
+
@chubby_bubble_path = File.join(File.dirname(__FILE__), '../fixtures/chubby_bubble.jpg')
|
13
|
+
@chubby_cute_path = File.join(File.dirname(__FILE__), '../fixtures/chubby_cute.png')
|
14
|
+
@sample_text_path = File.join(File.dirname(__FILE__), '../fixtures/sample.txt')
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'without helpers' do
|
18
|
+
before :all do
|
19
|
+
Person.class_eval do
|
20
|
+
Person.reset_callbacks(:validate)
|
21
|
+
validates :avatar, file_size: { less_than: 20.kilobytes },
|
22
|
+
file_content_type: { allow: 'image/jpeg' }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
subject { Person.new }
|
27
|
+
|
28
|
+
context 'with an allowed type' do
|
29
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg') }
|
30
|
+
it { is_expected.to be_valid }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with a disallowed type' do
|
34
|
+
it 'invalidates jpeg image file having size bigger than the allowed size' do
|
35
|
+
subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path, 'image/jpeg')
|
36
|
+
expect(subject).not_to be_valid
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'invalidates png image file' do
|
40
|
+
subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png')
|
41
|
+
expect(subject).not_to be_valid
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'invalidates text file' do
|
45
|
+
subject.avatar = Rack::Test::UploadedFile.new(@sample_text_path, 'text/plain')
|
46
|
+
expect(subject).not_to be_valid
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with helpers' do
|
52
|
+
before :all do
|
53
|
+
Person.class_eval do
|
54
|
+
Person.reset_callbacks(:validate)
|
55
|
+
validates_file_size :avatar, { less_than: 20.kilobytes }
|
56
|
+
validates_file_content_type :avatar, allow: 'image/jpeg'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
subject { Person.new }
|
61
|
+
|
62
|
+
context 'with an allowed type' do
|
63
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg') }
|
64
|
+
it { is_expected.to be_valid }
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'with a disallowed type' do
|
68
|
+
it 'invalidates jpeg image file having size bigger than the allowed size' do
|
69
|
+
subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path, 'image/jpeg')
|
70
|
+
expect(subject).not_to be_valid
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'invalidates png image file' do
|
74
|
+
subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png')
|
75
|
+
expect(subject).not_to be_valid
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'invalidates text file' do
|
79
|
+
subject.avatar = Rack::Test::UploadedFile.new(@sample_text_path, 'text/plain')
|
80
|
+
expect(subject).not_to be_valid
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack/test/uploaded_file'
|
3
|
+
|
4
|
+
describe 'File Content Type integration with ActiveModel' do
|
5
|
+
class Person
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :avatar
|
8
|
+
end
|
9
|
+
|
10
|
+
before :all do
|
11
|
+
@cute_path = File.join(File.dirname(__FILE__), '../fixtures/cute.jpg')
|
12
|
+
@chubby_bubble_path = File.join(File.dirname(__FILE__), '../fixtures/chubby_bubble.jpg')
|
13
|
+
@chubby_cute_path = File.join(File.dirname(__FILE__), '../fixtures/chubby_cute.png')
|
14
|
+
@sample_text_path = File.join(File.dirname(__FILE__), '../fixtures/sample.txt')
|
15
|
+
end
|
16
|
+
|
17
|
+
context ':allow option' do
|
18
|
+
context 'a string' do
|
19
|
+
before :all do
|
20
|
+
Person.class_eval do
|
21
|
+
Person.reset_callbacks(:validate)
|
22
|
+
validates :avatar, file_content_type: { allow: 'image/jpeg' }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
subject { Person.new }
|
27
|
+
|
28
|
+
context 'with an allowed type' do
|
29
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg') }
|
30
|
+
it { is_expected.to be_valid }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with a disallowed type' do
|
34
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png') }
|
35
|
+
it { is_expected.not_to be_valid }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'as a regex' do
|
40
|
+
before :all do
|
41
|
+
Person.class_eval do
|
42
|
+
Person.reset_callbacks(:validate)
|
43
|
+
validates :avatar, file_content_type: { allow: /^image\/.*/ }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
subject { Person.new }
|
48
|
+
|
49
|
+
context 'with an allowed types' do
|
50
|
+
it 'validates jpeg image file' do
|
51
|
+
subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg')
|
52
|
+
expect(subject).to be_valid
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'validates png image file' do
|
56
|
+
subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png')
|
57
|
+
expect(subject).to be_valid
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with a disallowed type' do
|
62
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@sample_text_path, 'text/plain') }
|
63
|
+
it { is_expected.not_to be_valid }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'as a list' do
|
68
|
+
before :all do
|
69
|
+
Person.class_eval do
|
70
|
+
Person.reset_callbacks(:validate)
|
71
|
+
validates :avatar, file_content_type: { allow: ['image/jpeg', 'text/plain'] }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
subject { Person.new }
|
76
|
+
|
77
|
+
context 'with allowed types' do
|
78
|
+
it 'validates jpeg' do
|
79
|
+
subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg')
|
80
|
+
expect(subject).to be_valid
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'validates text file' do
|
84
|
+
subject.avatar = Rack::Test::UploadedFile.new(@sample_text_path, 'text/plain')
|
85
|
+
expect(subject).to be_valid
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'with a disallowed type' do
|
90
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png') }
|
91
|
+
it { is_expected.not_to be_valid }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context ':exclude option' do
|
97
|
+
context 'a string' do
|
98
|
+
before :all do
|
99
|
+
Person.class_eval do
|
100
|
+
Person.reset_callbacks(:validate)
|
101
|
+
validates :avatar, file_content_type: { exclude: 'image/jpeg' }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
subject { Person.new }
|
106
|
+
|
107
|
+
context 'with an allowed type' do
|
108
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@sample_text_path, 'text/plain') }
|
109
|
+
it { is_expected.to be_valid }
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'with a disallowed type' do
|
113
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg') }
|
114
|
+
it { is_expected.not_to be_valid }
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'as a regex' do
|
119
|
+
before :all do
|
120
|
+
Person.class_eval do
|
121
|
+
Person.reset_callbacks(:validate)
|
122
|
+
validates :avatar, file_content_type: { exclude: /^image\/.*/ }
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
subject { Person.new }
|
127
|
+
|
128
|
+
context 'with an allowed type' do
|
129
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@sample_text_path, 'text/plain') }
|
130
|
+
it { is_expected.to be_valid }
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'with a disallowed types' do
|
134
|
+
it 'invalidates jpeg image file' do
|
135
|
+
subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg')
|
136
|
+
expect(subject).not_to be_valid
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'invalidates png image file' do
|
140
|
+
subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png')
|
141
|
+
expect(subject).not_to be_valid
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'as a list' do
|
147
|
+
before :all do
|
148
|
+
Person.class_eval do
|
149
|
+
Person.reset_callbacks(:validate)
|
150
|
+
validates :avatar, file_content_type: { exclude: ['image/jpeg', 'text/plain'] }
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
subject { Person.new }
|
155
|
+
|
156
|
+
context 'with an allowed type' do
|
157
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png') }
|
158
|
+
it { is_expected.to be_valid }
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'with a disallowed types' do
|
162
|
+
it 'invalidates jpeg' do
|
163
|
+
subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg')
|
164
|
+
expect(subject).not_to be_valid
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'invalidates text file' do
|
168
|
+
subject.avatar = Rack::Test::UploadedFile.new(@sample_text_path, 'text/plain')
|
169
|
+
expect(subject).not_to be_valid
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
context ':allow and :exclude combined' do
|
176
|
+
before :all do
|
177
|
+
Person.class_eval do
|
178
|
+
Person.reset_callbacks(:validate)
|
179
|
+
validates :avatar, file_content_type: { allow: /^image\/.*/, exclude: 'image/png' }
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
subject { Person.new }
|
184
|
+
|
185
|
+
context 'with an allowed type' do
|
186
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path, 'image/jpeg') }
|
187
|
+
it { is_expected.to be_valid }
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'with a disallowed type' do
|
191
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path, 'image/png') }
|
192
|
+
it { is_expected.not_to be_valid }
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack/test/uploaded_file'
|
3
|
+
|
4
|
+
describe 'File Size Validator integration with ActiveModel' do
|
5
|
+
class Person
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :avatar
|
8
|
+
end
|
9
|
+
|
10
|
+
before :all do
|
11
|
+
@cute_path = File.join(File.dirname(__FILE__), '../fixtures/cute.jpg')
|
12
|
+
@chubby_bubble_path = File.join(File.dirname(__FILE__), '../fixtures/chubby_bubble.jpg')
|
13
|
+
@chubby_cute_path = File.join(File.dirname(__FILE__), '../fixtures/chubby_cute.png')
|
14
|
+
end
|
15
|
+
|
16
|
+
context ':in option' do
|
17
|
+
before :all do
|
18
|
+
Person.class_eval do
|
19
|
+
Person.reset_callbacks(:validate)
|
20
|
+
validates :avatar, file_size: { in: 20.kilobytes..40.kilobytes }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
subject { Person.new }
|
25
|
+
|
26
|
+
context 'when file size is out of range' do
|
27
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path) }
|
28
|
+
it { is_expected.not_to be_valid }
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when file size is out of range' do
|
32
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path) }
|
33
|
+
it { is_expected.not_to be_valid }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when file size within range' do
|
37
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path) }
|
38
|
+
it { is_expected.to be_valid }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context ':greater_than and :less_than option' do
|
43
|
+
before :all do
|
44
|
+
Person.class_eval do
|
45
|
+
Person.reset_callbacks(:validate)
|
46
|
+
validates :avatar, file_size: { greater_than: 20.kilobytes,
|
47
|
+
less_than: 40.kilobytes }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
subject { Person.new }
|
52
|
+
|
53
|
+
context 'when file size is out of range' do
|
54
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path) }
|
55
|
+
it { is_expected.not_to be_valid }
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when file size is out of range' do
|
59
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_cute_path) }
|
60
|
+
it { is_expected.not_to be_valid }
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'when file size within range' do
|
64
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path) }
|
65
|
+
it { is_expected.to be_valid }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context ':less_than_or_equal_to option' do
|
70
|
+
before :all do
|
71
|
+
Person.class_eval do
|
72
|
+
Person.reset_callbacks(:validate)
|
73
|
+
validates :avatar, file_size: { less_than_or_equal_to: 20.kilobytes }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
subject { Person.new }
|
78
|
+
|
79
|
+
context 'when file size is greater than the specified size' do
|
80
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path) }
|
81
|
+
it { is_expected.not_to be_valid }
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'when file size within the specified size' do
|
85
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path) }
|
86
|
+
it { is_expected.to be_valid }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context ':greater_than_or_equal_to option' do
|
91
|
+
before :all do
|
92
|
+
Person.class_eval do
|
93
|
+
Person.reset_callbacks(:validate)
|
94
|
+
validates :avatar, file_size: { greater_than_or_equal_to: 20.kilobytes }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
subject { Person.new }
|
99
|
+
|
100
|
+
context 'when file size is less than the specified size' do
|
101
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path) }
|
102
|
+
it { is_expected.not_to be_valid }
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'when file size within the specified size' do
|
106
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path) }
|
107
|
+
it { is_expected.to be_valid }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context ':less_than option' do
|
112
|
+
before :all do
|
113
|
+
Person.class_eval do
|
114
|
+
Person.reset_callbacks(:validate)
|
115
|
+
validates :avatar, file_size: { less_than: 20.kilobytes }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
subject { Person.new }
|
120
|
+
|
121
|
+
context 'when file size is greater than the specified size' do
|
122
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path) }
|
123
|
+
it { is_expected.not_to be_valid }
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'when file size within the specified size' do
|
127
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path) }
|
128
|
+
it { is_expected.to be_valid }
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context ':greater_than option' do
|
133
|
+
before :all do
|
134
|
+
Person.class_eval do
|
135
|
+
Person.reset_callbacks(:validate)
|
136
|
+
validates :avatar, file_size: { greater_than: 20.kilobytes }
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
subject { Person.new }
|
141
|
+
|
142
|
+
context 'when file size is less than the specified size' do
|
143
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@cute_path) }
|
144
|
+
it { is_expected.not_to be_valid }
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'when file size within the specified size' do
|
148
|
+
before { subject.avatar = Rack::Test::UploadedFile.new(@chubby_bubble_path) }
|
149
|
+
it { is_expected.to be_valid }
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'file_validators/validators/file_content_type_validator'
|
3
|
+
|
4
|
+
describe ActiveModel::Validations::FileContentTypeValidator do
|
5
|
+
class Dummy
|
6
|
+
include ActiveModel::Validations
|
7
|
+
end
|
8
|
+
|
9
|
+
subject { Dummy }
|
10
|
+
|
11
|
+
def build_validator(options)
|
12
|
+
@validator = ActiveModel::Validations::FileContentTypeValidator.new(options.merge(attributes: :avatar))
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'whitelist format' do
|
16
|
+
context 'with an allowed type' do
|
17
|
+
context 'as a string' do
|
18
|
+
before { build_validator allow: 'image/jpg' }
|
19
|
+
it { is_expected.to allow_file_content_type('image/jpg', @validator) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'as an regexp' do
|
23
|
+
before { build_validator allow: /^image\/.*/ }
|
24
|
+
it { is_expected.to allow_file_content_type('image/png', @validator) }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'as a list' do
|
28
|
+
before { build_validator allow: ['image/png', 'image/jpg', 'image/jpeg'] }
|
29
|
+
it { is_expected.to allow_file_content_type('image/png', @validator) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with a disallowed type' do
|
34
|
+
context 'as a string' do
|
35
|
+
before { build_validator allow: 'image/png' }
|
36
|
+
it { is_expected.not_to allow_file_content_type('image/jpeg', @validator) }
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'as a regexp' do
|
40
|
+
before { build_validator allow: /^text\/.*/ }
|
41
|
+
it { is_expected.not_to allow_file_content_type('image/png', @validator) }
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'with :message option' do
|
45
|
+
context 'without interpolation' do
|
46
|
+
before { build_validator allow: 'image/png', message: 'should be a PNG image' }
|
47
|
+
it { is_expected.not_to allow_file_content_type('image/jpeg', @validator, message: 'Avatar should be a PNG image') }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'with interpolation' do
|
51
|
+
before { build_validator allow: 'image/png', message: 'should have content type %{types}' }
|
52
|
+
it { is_expected.not_to allow_file_content_type('image/jpeg', @validator,
|
53
|
+
message: 'Avatar should have content type image/png') }
|
54
|
+
it { is_expected.to allow_file_content_type('image/png', @validator,
|
55
|
+
message: 'Avatar should have content type image/png') }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'blacklist format' do
|
62
|
+
context 'with an allowed type' do
|
63
|
+
context 'as a string' do
|
64
|
+
before { build_validator exclude: 'image/gif' }
|
65
|
+
it { is_expected.to allow_file_content_type('image/png', @validator) }
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'as an regexp' do
|
69
|
+
before { build_validator exclude: /^text\/.*/ }
|
70
|
+
it { is_expected.to allow_file_content_type('image/png', @validator) }
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'as a list' do
|
74
|
+
before { build_validator exclude: ['image/png', 'image/jpg', 'image/jpeg'] }
|
75
|
+
it { is_expected.to allow_file_content_type('image/gif', @validator) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with a disallowed type' do
|
80
|
+
context 'as a string' do
|
81
|
+
before { build_validator exclude: 'image/gif' }
|
82
|
+
it { is_expected.not_to allow_file_content_type('image/gif', @validator) }
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'as an regexp' do
|
86
|
+
before { build_validator exclude: /^text\/.*/ }
|
87
|
+
it { is_expected.not_to allow_file_content_type('text/plain', @validator) }
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'with :message option' do
|
91
|
+
context 'without interpolation' do
|
92
|
+
before { build_validator exclude: 'image/png', message: 'should not be a PNG image' }
|
93
|
+
it { is_expected.not_to allow_file_content_type('image/png', @validator, message: 'Avatar should not be a PNG image') }
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'with interpolation' do
|
97
|
+
before { build_validator exclude: 'image/png', message: 'should not have content type %{types}' }
|
98
|
+
it { is_expected.not_to allow_file_content_type('image/png', @validator,
|
99
|
+
message: 'Avatar should not have content type image/png') }
|
100
|
+
it { is_expected.to allow_file_content_type('image/jpeg', @validator,
|
101
|
+
message: 'Avatar should not have content type image/jpeg') }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'using the helper' do
|
108
|
+
before { Dummy.validates_file_content_type :avatar, allow: 'image/jpg' }
|
109
|
+
|
110
|
+
it 'adds the validator to the class' do
|
111
|
+
expect(Dummy.validators_on(:avatar)).to include(ActiveModel::Validations::FileContentTypeValidator)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'given options' do
|
116
|
+
it 'raises argument error if no required argument was given' do
|
117
|
+
expect { build_validator message: 'Some message' }.to raise_error(ArgumentError)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'does not raise argument error if :in was given' do
|
121
|
+
expect { build_validator allow: 'image/jpg' }.not_to raise_error
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'does not raise argument error if :exclude was given' do
|
125
|
+
expect { build_validator exclude: 'image/jpg' }.not_to raise_error
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'file_validators/validators/file_size_validator'
|
3
|
+
|
4
|
+
describe ActiveModel::Validations::FileSizeValidator do
|
5
|
+
class Dummy
|
6
|
+
include ActiveModel::Validations
|
7
|
+
end
|
8
|
+
|
9
|
+
def storage_units
|
10
|
+
if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
|
11
|
+
{ 5120 => '5 KB', 10240 => '10 KB' }
|
12
|
+
else
|
13
|
+
{ 5120 => '5120 Bytes', 10240 => '10240 Bytes' }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
before :all do
|
18
|
+
@storage_units = storage_units
|
19
|
+
end
|
20
|
+
|
21
|
+
subject { Dummy }
|
22
|
+
|
23
|
+
def build_validator(options)
|
24
|
+
@validator = ActiveModel::Validations::FileSizeValidator.new(options.merge(attributes: :avatar))
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with :in option' do
|
28
|
+
before { build_validator in: (5.kilobytes..10.kilobytes) }
|
29
|
+
|
30
|
+
it { is_expected.to allow_file_size(7.kilobytes, @validator) }
|
31
|
+
it { is_expected.not_to allow_file_size(4.kilobytes, @validator) }
|
32
|
+
it { is_expected.not_to allow_file_size(11.kilobytes, @validator) }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'with :greater_than_or_equal_to option' do
|
36
|
+
before { build_validator greater_than_or_equal_to: 10.kilobytes }
|
37
|
+
|
38
|
+
it { is_expected.to allow_file_size(11.kilobytes, @validator) }
|
39
|
+
it { is_expected.to allow_file_size(10.kilobytes, @validator) }
|
40
|
+
it { is_expected.not_to allow_file_size(9.kilobytes, @validator) }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with :less_than_or_equal_to option' do
|
44
|
+
before { build_validator less_than_or_equal_to: 10.kilobytes }
|
45
|
+
|
46
|
+
it { is_expected.to allow_file_size(9.kilobytes, @validator) }
|
47
|
+
it { is_expected.to allow_file_size(10.kilobytes, @validator) }
|
48
|
+
it { is_expected.not_to allow_file_size(11.kilobytes, @validator) }
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with :greater_than option' do
|
52
|
+
before { build_validator greater_than: 10.kilobytes }
|
53
|
+
|
54
|
+
it { is_expected.to allow_file_size(11.kilobytes, @validator) }
|
55
|
+
it { is_expected.not_to allow_file_size(10.kilobytes, @validator) }
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'with :less_than option' do
|
59
|
+
before { build_validator less_than: 10.kilobytes }
|
60
|
+
|
61
|
+
it { is_expected.to allow_file_size(9.kilobytes, @validator) }
|
62
|
+
it { is_expected.not_to allow_file_size(10.kilobytes, @validator) }
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with :greater_than and :less_than option' do
|
66
|
+
before { build_validator greater_than: 5.kilobytes, less_than: 10.kilobytes }
|
67
|
+
|
68
|
+
it { is_expected.to allow_file_size(7.kilobytes, @validator) }
|
69
|
+
it { is_expected.not_to allow_file_size(5.kilobytes, @validator) }
|
70
|
+
it { is_expected.not_to allow_file_size(10.kilobytes, @validator) }
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with :message option' do
|
74
|
+
before { build_validator in: (5.kilobytes..10.kilobytes),
|
75
|
+
message: 'is invalid. (Between %{min} and %{max} please.)' }
|
76
|
+
|
77
|
+
it { is_expected.not_to allow_file_size(11.kilobytes, @validator,
|
78
|
+
message: "Avatar is invalid. (Between #{@storage_units[5120]} and #{@storage_units[10240]} please.)") }
|
79
|
+
|
80
|
+
it { is_expected.to allow_file_size(7.kilobytes, @validator,
|
81
|
+
message: "Avatar is invalid. (Between #{@storage_units[5120]} and #{@storage_units[10240]} please.)") }
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
context 'using the helper' do
|
86
|
+
before { Dummy.validates_file_size :avatar, in: (5.kilobytes..10.kilobytes) }
|
87
|
+
|
88
|
+
it 'adds the validator to the class' do
|
89
|
+
expect(Dummy.validators_on(:avatar)).to include(ActiveModel::Validations::FileSizeValidator)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'given options' do
|
94
|
+
it 'raises argument error if no required argument was given' do
|
95
|
+
expect { build_validator message: 'Some message' }.to raise_error(ArgumentError)
|
96
|
+
end
|
97
|
+
|
98
|
+
(ActiveModel::Validations::FileSizeValidator::AVAILABLE_CHECKS).each do |argument|
|
99
|
+
it "does not raise argument error if #{argument} was given" do
|
100
|
+
expect { build_validator argument => 5.kilobytes }.not_to raise_error
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'does not raise argument error if :in was given' do
|
105
|
+
expect { build_validator in: (5.kilobytes..10.kilobytes) }.not_to raise_error
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/spec/locale/en.yml
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
2
|
+
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require_relative '../lib/file_validators'
|
5
|
+
require 'rspec'
|
6
|
+
require 'coveralls'
|
7
|
+
|
8
|
+
Coveralls.wear!
|
9
|
+
|
10
|
+
locale_path = Dir.glob(File.dirname(__FILE__) + '/locale/*.yml')
|
11
|
+
I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
|
12
|
+
|
13
|
+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
end
|