Skip to content

Commit ce6f152

Browse files
authored
Merge pull request #297 from iainbeeston/validate-video-attachments
Added a test to validate video attachments
2 parents ecb3de2 + cf953b3 commit ce6f152

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

test/active_storage_validations_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
2929
u.proc_image_regex.attach(image_150x150_file)
3030
u.photos.attach(bad_dummy_file)
3131
u.proc_photos.attach(bad_dummy_file)
32+
u.video.attach(video_file)
3233
assert !u.valid?
3334
assert_equal u.errors.full_messages, ['Photos has an invalid content type', 'Proc photos has an invalid content type']
3435

@@ -39,6 +40,7 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
3940
u.proc_image_regex.attach(image_150x150_file)
4041
u.photos.attach(image_150x150_file)
4142
u.proc_photos.attach(image_150x150_file)
43+
u.video.attach(video_file)
4244
assert !u.valid?
4345
assert_equal u.errors.full_messages, ['Avatar has an invalid content type', 'Proc avatar has an invalid content type']
4446
assert_equal u.errors.details, avatar: [
@@ -68,6 +70,7 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
6870
u.proc_image_regex.attach(image_150x150_file)
6971
u.photos.attach(pdf_file) # Should be handled by regex match.
7072
u.proc_photos.attach(pdf_file) # Should be handled by regex match.
73+
u.video.attach(video_file)
7174
assert u.valid?
7275

7376
u = User.new(name: 'John Smith')
@@ -77,6 +80,7 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
7780
u.proc_image_regex.attach(bad_dummy_file)
7881
u.photos.attach(image_150x150_file)
7982
u.proc_photos.attach(image_150x150_file)
83+
u.video.attach(video_file)
8084
assert !u.valid?
8185
assert_equal u.errors.full_messages, ['Image regex has an invalid content type', 'Proc image regex has an invalid content type']
8286

@@ -87,6 +91,7 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
8791
u.proc_image_regex.attach(bad_dummy_file)
8892
u.photos.attach(bad_dummy_file)
8993
u.proc_photos.attach(bad_dummy_file)
94+
u.video.attach(video_file)
9095
assert !u.valid?
9196
assert_equal u.errors.full_messages, ['Avatar has an invalid content type', 'Photos has an invalid content type', 'Image regex has an invalid content type', 'Proc avatar has an invalid content type', 'Proc photos has an invalid content type', 'Proc image regex has an invalid content type']
9297

@@ -96,6 +101,7 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
96101
u.photos.attach(image_150x150_file)
97102
u.proc_photos.attach(image_150x150_file)
98103
u.conditional_image_2.attach(image_150x150_file)
104+
u.video.attach(video_file)
99105
assert u.valid?
100106
assert_equal u.errors.full_messages, []
101107

@@ -105,6 +111,7 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
105111
u.photos.attach(bad_dummy_file)
106112
u.proc_photos.attach(image_150x150_file)
107113
u.conditional_image_2.attach(bad_dummy_file)
114+
u.video.attach(video_file)
108115
assert !u.valid?
109116
assert_equal u.errors.full_messages, ["Avatar has an invalid content type", "Photos has an invalid content type", "Conditional image 2 has an invalid content type", "Proc avatar has an invalid content type"]
110117
end

test/dummy/app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class User < ApplicationRecord
2020
has_many_attached :proc_photos
2121
has_many_attached :proc_photo_with_messages
2222
has_one_attached :proc_image_regex
23+
has_one_attached :video
2324

2425
validates :name, presence: true
2526

@@ -29,6 +30,7 @@ class User < ApplicationRecord
2930
validates :image_regex, content_type: /\Aimage\/.*\z/
3031
validates :conditional_image, attached: true, if: -> { name == 'Foo' }
3132
validates :conditional_image_2, attached: true, content_type: -> (record) {[/\Aimage\/.*\z/]}, size: { less_than: 10.megabytes }, if: -> { name == 'Peter Griffin' }
33+
validates :video, content_type: [:mp4, :mpeg, :webm]
3234

3335
validates :moon_picture, content_type: ['image/png'], size: { greater_than: 0 }
3436

test/dummy/public/video.mp4

59.6 KB
Binary file not shown.

test/support/files.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ def not_identifiable_io_file
193193
}
194194
end
195195

196+
def video_file
197+
{
198+
io: File.open(Rails.root.join('public', 'video.mp4')),
199+
filename: 'video',
200+
content_type: 'video/mp4'
201+
}
202+
end
203+
196204
def create_blob_from_file(file)
197205
ActiveStorage::Blob.create_and_upload!(
198206
io: file[:io],

0 commit comments

Comments
 (0)