aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/test/models
diff options
context:
space:
mode:
authorGeorge Claghorn <george.claghorn@gmail.com>2018-01-15 13:06:17 -0500
committerGitHub <noreply@github.com>2018-01-15 13:06:17 -0500
commitc2ba530c43244b5b60fd629f61cd8b44c43ecda9 (patch)
tree76c572f419a8e30b4ba2d2ae22570844ba28361e /activestorage/test/models
parentd8e7d6b03c50f4f01c333461c46666d8210e2c17 (diff)
downloadrails-c2ba530c43244b5b60fd629f61cd8b44c43ecda9.tar.gz
rails-c2ba530c43244b5b60fd629f61cd8b44c43ecda9.tar.bz2
rails-c2ba530c43244b5b60fd629f61cd8b44c43ecda9.zip
Extract content types from blob data
Diffstat (limited to 'activestorage/test/models')
-rw-r--r--activestorage/test/models/attachments_test.rb25
-rw-r--r--activestorage/test/models/blob_test.rb12
-rw-r--r--activestorage/test/models/variant_test.rb2
3 files changed, 36 insertions, 3 deletions
diff --git a/activestorage/test/models/attachments_test.rb b/activestorage/test/models/attachments_test.rb
index 20eec3c220..f0aa96b411 100644
--- a/activestorage/test/models/attachments_test.rb
+++ b/activestorage/test/models/attachments_test.rb
@@ -97,6 +97,29 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
assert_equal "funky.jpg", @user.avatar_attachment.blob.filename.to_s
end
+ test "identify newly-attached, directly-uploaded blob" do
+ # Simulate a direct upload.
+ blob = create_blob_before_direct_upload(filename: "racecar.jpg", content_type: "application/octet-stream", byte_size: 1124062, checksum: "7GjDDNEQb4mzMzsW+MS0JQ==")
+ ActiveStorage::Blob.service.upload(blob.key, file_fixture("racecar.jpg").open)
+
+ stub_request(:get, %r{localhost:3000/rails/active_storage/disk/.*}).to_return(body: file_fixture("racecar.jpg"))
+ @user.avatar.attach(blob)
+
+ assert_equal "image/jpeg", @user.avatar.reload.content_type
+ assert @user.avatar.identified?
+ end
+
+ test "identify newly-attached blob only once" do
+ blob = create_file_blob
+ assert blob.identified?
+
+ # The blob's backing file is a PNG image. Fudge its content type so we can tell if it's identified when we attach it.
+ blob.update! content_type: "application/octet-stream"
+
+ @user.avatar.attach blob
+ assert_equal "application/octet-stream", blob.content_type
+ end
+
test "analyze newly-attached blob" do
perform_enqueued_jobs do
@user.avatar.attach create_file_blob
@@ -115,7 +138,7 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
assert blob.reload.analyzed?
- @user.avatar.attachment.destroy
+ @user.avatar.detach
assert_no_enqueued_jobs do
@user.reload.avatar.attach blob
diff --git a/activestorage/test/models/blob_test.rb b/activestorage/test/models/blob_test.rb
index 2db516c088..b5daee2b57 100644
--- a/activestorage/test/models/blob_test.rb
+++ b/activestorage/test/models/blob_test.rb
@@ -13,6 +13,16 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
assert_equal Digest::MD5.base64digest(data), blob.checksum
end
+ test "create after upload extracts content type from data" do
+ blob = create_file_blob content_type: "application/octet-stream"
+ assert_equal "image/jpeg", blob.content_type
+ end
+
+ test "create after upload extracts content type from filename" do
+ blob = create_blob content_type: "application/octet-stream"
+ assert_equal "text/plain", blob.content_type
+ end
+
test "text?" do
blob = create_blob data: "Hello world!"
assert blob.text?
@@ -79,6 +89,6 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
def expected_url_for(blob, disposition: :inline, filename: nil)
filename ||= blob.filename
query_string = { content_type: blob.content_type, disposition: "#{disposition}; #{filename.parameters}" }.to_param
- "/rails/active_storage/disk/#{ActiveStorage.verifier.generate(blob.key, expires_in: 5.minutes, purpose: :blob_key)}/#{filename}?#{query_string}"
+ "http://localhost:3000/rails/active_storage/disk/#{ActiveStorage.verifier.generate(blob.key, expires_in: 5.minutes, purpose: :blob_key)}/#{filename}?#{query_string}"
end
end
diff --git a/activestorage/test/models/variant_test.rb b/activestorage/test/models/variant_test.rb
index 49660e880a..0cf8a583bd 100644
--- a/activestorage/test/models/variant_test.rb
+++ b/activestorage/test/models/variant_test.rb
@@ -67,6 +67,6 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
test "service_url doesn't grow in length despite long variant options" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(font: "a" * 10_000).processed
- assert_operator variant.service_url.length, :<, 500
+ assert_operator variant.service_url.length, :<, 525
end
end