From d1b20ea8f70ecc44f78315c1bba1f7e16c0bfa15 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 23 Oct 2024 03:50:20 -0400 Subject: [PATCH] Worker specs coverage increase (#32541) --- .../account_conversation_fabricator.rb | 7 ++++ spec/workers/account_refresh_worker_spec.rb | 10 +++-- .../followers_synchronization_worker_spec.rb | 33 ++++++++++++++++ spec/workers/push_conversation_worker_spec.rb | 26 ++++++++++++- spec/workers/push_update_worker_spec.rb | 28 ++++++++++++-- .../remote_account_refresh_worker_spec.rb | 38 +++++++++++++++++++ .../remove_featured_tag_worker_spec.rb | 31 +++++++++++++-- spec/workers/resolve_account_worker_spec.rb | 28 +++++++++++++- 8 files changed, 186 insertions(+), 15 deletions(-) create mode 100644 spec/fabricators/account_conversation_fabricator.rb create mode 100644 spec/workers/activitypub/followers_synchronization_worker_spec.rb create mode 100644 spec/workers/remote_account_refresh_worker_spec.rb diff --git a/spec/fabricators/account_conversation_fabricator.rb b/spec/fabricators/account_conversation_fabricator.rb new file mode 100644 index 0000000000..6145ed8254 --- /dev/null +++ b/spec/fabricators/account_conversation_fabricator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +Fabricator(:account_conversation) do + account + conversation + status_ids { [Fabricate(:status).id] } +end diff --git a/spec/workers/account_refresh_worker_spec.rb b/spec/workers/account_refresh_worker_spec.rb index 3e88e8db28..4408ef77a9 100644 --- a/spec/workers/account_refresh_worker_spec.rb +++ b/spec/workers/account_refresh_worker_spec.rb @@ -7,9 +7,7 @@ RSpec.describe AccountRefreshWorker do let(:service) { instance_double(ResolveAccountService, call: true) } describe '#perform' do - before do - allow(ResolveAccountService).to receive(:new).and_return(service) - end + before { stub_service } context 'when account does not exist' do it 'returns immediately without processing' do @@ -48,5 +46,11 @@ RSpec.describe AccountRefreshWorker do (Account::BACKGROUND_REFRESH_INTERVAL + 3.days).ago end end + + def stub_service + allow(ResolveAccountService) + .to receive(:new) + .and_return(service) + end end end diff --git a/spec/workers/activitypub/followers_synchronization_worker_spec.rb b/spec/workers/activitypub/followers_synchronization_worker_spec.rb new file mode 100644 index 0000000000..0847b247e3 --- /dev/null +++ b/spec/workers/activitypub/followers_synchronization_worker_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ActivityPub::FollowersSynchronizationWorker do + let(:worker) { described_class.new } + let(:service) { instance_double(ActivityPub::SynchronizeFollowersService, call: true) } + + describe '#perform' do + before { stub_service } + + let(:account) { Fabricate(:account, domain: 'host.example') } + let(:url) { 'https://sync.url' } + + it 'sends the status to the service' do + worker.perform(account.id, url) + + expect(service).to have_received(:call).with(account, url) + end + + it 'returns nil for non-existent record' do + result = worker.perform(123_123_123, url) + + expect(result).to be(true) + end + end + + def stub_service + allow(ActivityPub::SynchronizeFollowersService) + .to receive(:new) + .and_return(service) + end +end diff --git a/spec/workers/push_conversation_worker_spec.rb b/spec/workers/push_conversation_worker_spec.rb index d651059c9a..a98c603c17 100644 --- a/spec/workers/push_conversation_worker_spec.rb +++ b/spec/workers/push_conversation_worker_spec.rb @@ -6,8 +6,30 @@ RSpec.describe PushConversationWorker 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 + context 'with missing values' do + it 'runs without error' do + expect { worker.perform(nil) } + .to_not raise_error + end + end + + context 'with valid records' do + let(:account_conversation) { Fabricate :account_conversation } + + before { allow(redis).to receive(:publish) } + + it 'pushes message to timeline' do + expect { worker.perform(account_conversation.id) } + .to_not raise_error + + expect(redis) + .to have_received(:publish) + .with(redis_key, anything) + end + + def redis_key + "timeline:direct:#{account_conversation.account_id}" + end end end end diff --git a/spec/workers/push_update_worker_spec.rb b/spec/workers/push_update_worker_spec.rb index 6206ab5986..f3e0a128df 100644 --- a/spec/workers/push_update_worker_spec.rb +++ b/spec/workers/push_update_worker_spec.rb @@ -6,11 +6,31 @@ RSpec.describe PushUpdateWorker do let(:worker) { described_class.new } describe 'perform' do - it 'runs without error for missing record' do - account_id = nil - status_id = nil + context 'with missing values' do + it 'runs without error' do + expect { worker.perform(nil, nil) } + .to_not raise_error + end + end - expect { worker.perform(account_id, status_id) }.to_not raise_error + context 'with valid records' do + let(:account) { Fabricate :account } + let(:status) { Fabricate :status } + + before { allow(redis).to receive(:publish) } + + it 'pushes message to timeline' do + expect { worker.perform(account.id, status.id) } + .to_not raise_error + + expect(redis) + .to have_received(:publish) + .with(redis_key, anything) + end + + def redis_key + "timeline:#{account.id}" + end end end end diff --git a/spec/workers/remote_account_refresh_worker_spec.rb b/spec/workers/remote_account_refresh_worker_spec.rb new file mode 100644 index 0000000000..9938d971b9 --- /dev/null +++ b/spec/workers/remote_account_refresh_worker_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe RemoteAccountRefreshWorker do + let(:worker) { described_class.new } + let(:service) { instance_double(ActivityPub::FetchRemoteAccountService, call: true) } + + describe '#perform' do + before { stub_service } + + let(:account) { Fabricate(:account, domain: 'host.example') } + + it 'sends the status to the service' do + worker.perform(account.id) + + expect(service).to have_received(:call).with(account.uri) + end + + it 'returns nil for non-existent record' do + result = worker.perform(123_123_123) + + expect(result).to be_nil + end + + it 'returns nil for a local record' do + account = Fabricate :account, domain: nil + result = worker.perform(account) + expect(result).to be_nil + end + + def stub_service + allow(ActivityPub::FetchRemoteAccountService) + .to receive(:new) + .and_return(service) + end + end +end diff --git a/spec/workers/remove_featured_tag_worker_spec.rb b/spec/workers/remove_featured_tag_worker_spec.rb index 7866824ee7..a8a5bcea81 100644 --- a/spec/workers/remove_featured_tag_worker_spec.rb +++ b/spec/workers/remove_featured_tag_worker_spec.rb @@ -4,12 +4,35 @@ require 'rails_helper' RSpec.describe RemoveFeaturedTagWorker do let(:worker) { described_class.new } + let(:service) { instance_double(RemoveFeaturedTagService, call: true) } describe 'perform' do - it 'runs without error for missing record' do - account_id = nil - featured_tag_id = nil - expect { worker.perform(account_id, featured_tag_id) }.to_not raise_error + context 'with missing values' do + it 'runs without error' do + expect { worker.perform(nil, nil) } + .to_not raise_error + end + end + + context 'with real records' do + before { stub_service } + + let(:account) { Fabricate :account } + let(:featured_tag) { Fabricate :featured_tag } + + it 'calls the service for processing' do + worker.perform(account.id, featured_tag.id) + + expect(service) + .to have_received(:call) + .with(be_an(Account), be_an(FeaturedTag)) + end + + def stub_service + allow(RemoveFeaturedTagService) + .to receive(:new) + .and_return(service) + end end end end diff --git a/spec/workers/resolve_account_worker_spec.rb b/spec/workers/resolve_account_worker_spec.rb index de349adacb..58c2c12129 100644 --- a/spec/workers/resolve_account_worker_spec.rb +++ b/spec/workers/resolve_account_worker_spec.rb @@ -4,10 +4,34 @@ require 'rails_helper' RSpec.describe ResolveAccountWorker do let(:worker) { described_class.new } + let(:service) { instance_double(ResolveAccountService, call: true) } describe 'perform' do - it 'runs without error for missing record' do - expect { worker.perform(nil) }.to_not raise_error + context 'with missing values' do + it 'runs without error' do + expect { worker.perform(nil) } + .to_not raise_error + end + end + + context 'with a URI' do + before { stub_service } + + let(:uri) { 'https://host/path/value' } + + it 'initiates account resolution' do + worker.perform(uri) + + expect(service) + .to have_received(:call) + .with(uri) + end + + def stub_service + allow(ResolveAccountService) + .to receive(:new) + .and_return(service) + end end end end