From 1526e54ac6f08431c8c833f20b4be8882355967c Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 17 Nov 2023 04:03:46 -0500 Subject: [PATCH] Add spec coverage for `workers/redownload_*` worker classes (#27892) --- spec/workers/redownload_avatar_worker_spec.rb | 45 +++++++++++++++++-- spec/workers/redownload_header_worker_spec.rb | 45 +++++++++++++++++-- spec/workers/redownload_media_worker_spec.rb | 37 +++++++++++++++ 3 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 spec/workers/redownload_media_worker_spec.rb diff --git a/spec/workers/redownload_avatar_worker_spec.rb b/spec/workers/redownload_avatar_worker_spec.rb index b44ae9f035..4ab368e12f 100644 --- a/spec/workers/redownload_avatar_worker_spec.rb +++ b/spec/workers/redownload_avatar_worker_spec.rb @@ -5,9 +5,48 @@ require 'rails_helper' describe RedownloadAvatarWorker do let(:worker) { described_class.new } - describe 'perform' do - it 'runs without error for missing record' do - expect { worker.perform(nil) }.to_not raise_error + describe '#perform' do + it 'returns nil for non-existent record' do + result = worker.perform(123_123_123) + + expect(result).to be_nil + end + + it 'returns nil for suspended account' do + account = Fabricate(:account, suspended_at: 10.days.ago) + + expect(worker.perform(account.id)).to be_nil + end + + it 'returns nil with a domain block' do + account = Fabricate(:account, domain: 'host.example') + Fabricate(:domain_block, domain: account.domain, reject_media: true) + + expect(worker.perform(account.id)).to be_nil + end + + it 'returns nil without an avatar remote url' do + account = Fabricate(:account, avatar_remote_url: '') + + expect(worker.perform(account.id)).to be_nil + end + + it 'returns nil when avatar file name is present' do + stub_request(:get, 'https://example.host/file').to_return request_fixture('avatar.txt') + account = Fabricate(:account, avatar_remote_url: 'https://example.host/file', avatar_file_name: 'test.jpg') + + expect(worker.perform(account.id)).to be_nil + end + + it 'reprocesses a remote avatar' do + stub_request(:get, 'https://example.host/file').to_return request_fixture('avatar.txt') + account = Fabricate(:account, avatar_remote_url: 'https://example.host/file') + account.update_column(:avatar_file_name, nil) # rubocop:disable Rails/SkipsModelValidations + + result = worker.perform(account.id) + + expect(result).to be(true) + expect(account.reload.avatar_file_name).to_not be_nil end end end diff --git a/spec/workers/redownload_header_worker_spec.rb b/spec/workers/redownload_header_worker_spec.rb index 767ae7a5ab..3b6f497bb8 100644 --- a/spec/workers/redownload_header_worker_spec.rb +++ b/spec/workers/redownload_header_worker_spec.rb @@ -5,9 +5,48 @@ require 'rails_helper' describe RedownloadHeaderWorker do let(:worker) { described_class.new } - describe 'perform' do - it 'runs without error for missing record' do - expect { worker.perform(nil) }.to_not raise_error + describe '#perform' do + it 'returns nil for non-existent record' do + result = worker.perform(123_123_123) + + expect(result).to be_nil + end + + it 'returns nil for suspended account' do + account = Fabricate(:account, suspended_at: 10.days.ago) + + expect(worker.perform(account.id)).to be_nil + end + + it 'returns nil with a domain block' do + account = Fabricate(:account, domain: 'host.example') + Fabricate(:domain_block, domain: account.domain, reject_media: true) + + expect(worker.perform(account.id)).to be_nil + end + + it 'returns nil without an header remote url' do + account = Fabricate(:account, header_remote_url: '') + + expect(worker.perform(account.id)).to be_nil + end + + it 'returns nil when header file name is present' do + stub_request(:get, 'https://example.host/file').to_return request_fixture('avatar.txt') + account = Fabricate(:account, header_remote_url: 'https://example.host/file', header_file_name: 'test.jpg') + + expect(worker.perform(account.id)).to be_nil + end + + it 'reprocesses a remote header' do + stub_request(:get, 'https://example.host/file').to_return request_fixture('avatar.txt') + account = Fabricate(:account, header_remote_url: 'https://example.host/file') + account.update_column(:header_file_name, nil) # rubocop:disable Rails/SkipsModelValidations + + result = worker.perform(account.id) + + expect(result).to be(true) + expect(account.reload.header_file_name).to_not be_nil end end end diff --git a/spec/workers/redownload_media_worker_spec.rb b/spec/workers/redownload_media_worker_spec.rb new file mode 100644 index 0000000000..cd561d148b --- /dev/null +++ b/spec/workers/redownload_media_worker_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe RedownloadMediaWorker do + let(:worker) { described_class.new } + + describe '#perform' do + it 'returns nil for non-existent record' do + result = worker.perform(123_123_123) + + expect(result).to be_nil + end + + it 'returns nil without a remote_url' do + media_attachment = Fabricate(:media_attachment, remote_url: '') + + result = worker.perform(media_attachment.id) + + expect(result).to be_nil + end + + context 'with a valid remote url' do + let(:url) { 'https://example.host/file.txt' } + + before { stub_request(:get, url).to_return(status: 200) } + + it 'processes downloads for valid record' do + media_attachment = Fabricate(:media_attachment, remote_url: url) + + worker.perform(media_attachment.id) + + expect(a_request(:get, url)).to have_been_made + end + end + end +end