From e5f887236b60b207da85f44a5c2afee71db25c05 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 22 Jul 2017 09:51:39 -0500 Subject: Move model tests to models directory --- test/models/attachments_test.rb | 100 +++++++++++++++++++++++ test/models/blob_test.rb | 41 ++++++++++ test/models/variant_test.rb | 23 ++++++ test/models/verified_key_with_expiration_test.rb | 19 +++++ 4 files changed, 183 insertions(+) create mode 100644 test/models/attachments_test.rb create mode 100644 test/models/blob_test.rb create mode 100644 test/models/variant_test.rb create mode 100644 test/models/verified_key_with_expiration_test.rb (limited to 'test/models') diff --git a/test/models/attachments_test.rb b/test/models/attachments_test.rb new file mode 100644 index 0000000000..9b88b18247 --- /dev/null +++ b/test/models/attachments_test.rb @@ -0,0 +1,100 @@ +require "test_helper" +require "database/setup" +require "active_storage/blob" + +require "active_job" +ActiveJob::Base.queue_adapter = :test +ActiveJob::Base.logger = nil + +# ActiveRecord::Base.logger = Logger.new(STDOUT) + +class User < ActiveRecord::Base + has_one_attached :avatar + has_many_attached :highlights +end + +class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase + include ActiveJob::TestHelper + + setup { @user = User.create!(name: "DHH") } + + teardown { ActiveStorage::Blob.all.each(&:purge) } + + test "attach existing blob" do + @user.avatar.attach create_blob(filename: "funky.jpg") + assert_equal "funky.jpg", @user.avatar.filename.to_s + end + + test "attach existing sgid blob" do + @user.avatar.attach create_blob(filename: "funky.jpg").to_sgid.to_s + assert_equal "funky.jpg", @user.avatar.filename.to_s + end + + test "attach new blob" do + @user.avatar.attach io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" + assert_equal "town.jpg", @user.avatar.filename.to_s + end + + test "purge attached blob" do + @user.avatar.attach create_blob(filename: "funky.jpg") + avatar_key = @user.avatar.key + + @user.avatar.purge + assert_not @user.avatar.attached? + assert_not ActiveStorage::Blob.service.exist?(avatar_key) + end + + test "purge attached blob later when the record is destroyed" do + @user.avatar.attach create_blob(filename: "funky.jpg") + avatar_key = @user.avatar.key + + perform_enqueued_jobs do + @user.destroy + + assert_nil ActiveStorage::Blob.find_by(key: avatar_key) + assert_not ActiveStorage::Blob.service.exist?(avatar_key) + end + end + + + test "attach existing blobs" do + @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg") + + assert_equal "funky.jpg", @user.highlights.first.filename.to_s + assert_equal "wonky.jpg", @user.highlights.second.filename.to_s + end + + test "attach new blobs" do + @user.highlights.attach( + { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" }, + { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" }) + + assert_equal "town.jpg", @user.highlights.first.filename.to_s + assert_equal "country.jpg", @user.highlights.second.filename.to_s + end + + test "purge attached blobs" do + @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg") + highlight_keys = @user.highlights.collect(&:key) + + @user.highlights.purge + assert_not @user.highlights.attached? + assert_not ActiveStorage::Blob.service.exist?(highlight_keys.first) + assert_not ActiveStorage::Blob.service.exist?(highlight_keys.second) + end + + test "purge attached blobs later when the record is destroyed" do + @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg") + highlight_keys = @user.highlights.collect(&:key) + + perform_enqueued_jobs do + @user.destroy + + assert_nil ActiveStorage::Blob.find_by(key: highlight_keys.first) + assert_not ActiveStorage::Blob.service.exist?(highlight_keys.first) + + assert_nil ActiveStorage::Blob.find_by(key: highlight_keys.second) + assert_not ActiveStorage::Blob.service.exist?(highlight_keys.second) + end + end +end diff --git a/test/models/blob_test.rb b/test/models/blob_test.rb new file mode 100644 index 0000000000..ddc000ed51 --- /dev/null +++ b/test/models/blob_test.rb @@ -0,0 +1,41 @@ +require "test_helper" +require "database/setup" +require "active_storage/blob" + +class ActiveStorage::BlobTest < ActiveSupport::TestCase + test "create after upload sets byte size and checksum" do + data = "Hello world!" + blob = create_blob data: data + + assert_equal data, blob.download + assert_equal data.length, blob.byte_size + assert_equal Digest::MD5.base64digest(data), blob.checksum + end + + test "download yields chunks" do + blob = create_blob data: "a" * 75.kilobytes + chunks = [] + + blob.download do |chunk| + chunks << chunk + end + + assert_equal 2, chunks.size + assert_equal "a" * 64.kilobytes, chunks.first + assert_equal "a" * 11.kilobytes, chunks.second + end + + test "urls expiring in 5 minutes" do + blob = create_blob + + freeze_time do + assert_equal expected_url_for(blob), blob.url + assert_equal expected_url_for(blob, disposition: :attachment), blob.url(disposition: :attachment) + end + end + + private + def expected_url_for(blob, disposition: :inline) + "/rails/active_storage/disk/#{ActiveStorage::VerifiedKeyWithExpiration.encode(blob.key, expires_in: 5.minutes)}/#{blob.filename}?disposition=#{disposition}" + end +end diff --git a/test/models/variant_test.rb b/test/models/variant_test.rb new file mode 100644 index 0000000000..c7ff0d77e1 --- /dev/null +++ b/test/models/variant_test.rb @@ -0,0 +1,23 @@ +require "test_helper" +require "database/setup" +require "active_storage/variant" + +class ActiveStorage::VariantTest < ActiveSupport::TestCase + setup do + @blob = create_image_blob filename: "racecar.jpg" + end + + test "resized variation" do + variant = @blob.variant(resize: "100x100").processed + + assert_match /racecar.jpg/, variant.url + assert_same_image "racecar-100x100.jpg", variant + end + + test "resized and monochrome variation" do + variant = @blob.variant(resize: "100x100", monochrome: true).processed + + assert_match /racecar.jpg/, variant.url + assert_same_image "racecar-100x100-monochrome.jpg", variant + end +end diff --git a/test/models/verified_key_with_expiration_test.rb b/test/models/verified_key_with_expiration_test.rb new file mode 100644 index 0000000000..ee4dc7e02e --- /dev/null +++ b/test/models/verified_key_with_expiration_test.rb @@ -0,0 +1,19 @@ +require "test_helper" +require "active_support/core_ext/securerandom" + +class ActiveStorage::VerifiedKeyWithExpirationTest < ActiveSupport::TestCase + FIXTURE_KEY = SecureRandom.base58(24) + + test "without expiration" do + encoded_key = ActiveStorage::VerifiedKeyWithExpiration.encode(FIXTURE_KEY) + assert_equal FIXTURE_KEY, ActiveStorage::VerifiedKeyWithExpiration.decode(encoded_key) + end + + test "with expiration" do + encoded_key = ActiveStorage::VerifiedKeyWithExpiration.encode(FIXTURE_KEY, expires_in: 1.minute) + assert_equal FIXTURE_KEY, ActiveStorage::VerifiedKeyWithExpiration.decode(encoded_key) + + travel 2.minutes + assert_nil ActiveStorage::VerifiedKeyWithExpiration.decode(encoded_key) + end +end -- cgit v1.2.3