From 1cd1df6193a891c3ecb2ee09eae9cf87f88d2718 Mon Sep 17 00:00:00 2001 From: Nataliya Terskaya Date: Mon, 23 May 2022 14:38:08 +0200 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82=D0=B8=D0=BF=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2=20=D0=B1=D0=B5=D0=B7=20=D0=BA=D1=8D=D1=88?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/storage/no_cache_s3_test.rb | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 test/storage/no_cache_s3_test.rb diff --git a/test/storage/no_cache_s3_test.rb b/test/storage/no_cache_s3_test.rb new file mode 100644 index 0000000..1e815a7 --- /dev/null +++ b/test/storage/no_cache_s3_test.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'fog/local' +require 'sidekiq' +require 'sidekiq/testing' + +require 'delayed_paperclip' +DelayedPaperclip::Railtie.insert + +# rubocop:disable Naming/VariableNumber + +class FakeModel + attr_accessor :synced_to_store_1, :synced_to_store_2 +end + +class NoCachedS3Test < Test::Unit::TestCase + TEST_ROOT = Pathname(__dir__).join('test') + + def fog_directory(suffix) + Fog::Storage.new(provider: 'Local', local_root: TEST_ROOT.join(suffix.to_s)) + .directories.new(key: '', public: true) + end + + def stub_file(name, content) + StringIO.new(content).tap { |string_io| string_io.stubs(:original_filename).returns(name) } + end + + setup do + rebuild_model( + storage: :no_cache_s3, + key: ':filename', + url: 'http://store.local/:key', + stores: { + store_1: fog_directory(:store_1), + store_2: fog_directory(:store_2) + } + ) + modify_table(:dummies) do |table| + table.boolean :avatar_synced_to_store_1, null: false, default: false + table.boolean :avatar_synced_to_store_2, null: false, default: false + end + @instance = Dummy.create + end + + teardown { TEST_ROOT.rmtree if TEST_ROOT.exist? } + + context 'assigning file' do + setup { Sidekiq::Testing.fake! } + + should 'write to main store and enqueue jobs to copy to others' do + @instance.update!(avatar: stub_file('test.txt', 'qwe')) + @instance.reload + attachment = @instance.avatar + key = attachment.key + assert_equal false, attachment.class.store_by(:store_1).files.head(key).nil? + assert_equal true, attachment.class.store_by(:store_2).files.head(key).nil? + assert_equal 'http://store.local/test.txt', attachment.url(:original, false) + end + + context 'with inline jobs' do + setup { Sidekiq::Testing.inline! } + teardown { Sidekiq::Testing.fake! } + + should 'write to all permanent stores' do + @instance.update!(avatar: stub_file('test.txt', 'qwe')) + @instance.reload + attachment = @instance.avatar + key = attachment.key + assert_equal false, attachment.class.store_by(:store_1).files.head(key).nil? + assert_equal false, attachment.class.store_by(:store_2).files.head(key).nil? + assert_equal 'http://store.local/test.txt', attachment.url(:original, false) + end + end + end +end + +# rubocop:enable Naming/VariableNumber