Fix RSpec/MessageSpies cop (#27751)

This commit is contained in:
Matt Jankowski 2023-11-07 04:46:28 -05:00 committed by GitHub
parent ae0d551d33
commit 49e2772064
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 162 additions and 94 deletions

View file

@ -147,24 +147,6 @@ RSpec/MessageChain:
- 'spec/models/session_activation_spec.rb'
- 'spec/models/setting_spec.rb'
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Exclude:
- 'spec/controllers/admin/accounts_controller_spec.rb'
- 'spec/helpers/admin/account_moderation_notes_helper_spec.rb'
- 'spec/lib/webfinger_resource_spec.rb'
- 'spec/models/admin/account_action_spec.rb'
- 'spec/models/concerns/remotable_spec.rb'
- 'spec/models/follow_request_spec.rb'
- 'spec/models/identity_spec.rb'
- 'spec/models/session_activation_spec.rb'
- 'spec/models/setting_spec.rb'
- 'spec/services/activitypub/fetch_replies_service_spec.rb'
- 'spec/services/activitypub/process_collection_service_spec.rb'
- 'spec/spec_helper.rb'
- 'spec/validators/status_length_validator_spec.rb'
RSpec/MultipleExpectations:
Max: 8

View file

@ -18,21 +18,8 @@ RSpec.describe Admin::AccountsController do
end
it 'filters with parameters' do
new = AccountFilter.method(:new)
expect(AccountFilter).to receive(:new) do |params|
h = params.to_h
expect(h[:origin]).to eq 'local'
expect(h[:by_domain]).to eq 'domain'
expect(h[:status]).to eq 'active'
expect(h[:username]).to eq 'username'
expect(h[:display_name]).to eq 'display name'
expect(h[:email]).to eq 'local-part@domain'
expect(h[:ip]).to eq '0.0.0.42'
new.call({})
end
account_filter = instance_double(AccountFilter, results: Account.all)
allow(AccountFilter).to receive(:new).and_return(account_filter)
get :index, params: {
origin: 'local',
@ -43,6 +30,18 @@ RSpec.describe Admin::AccountsController do
email: 'local-part@domain',
ip: '0.0.0.42',
}
expect(AccountFilter).to have_received(:new) do |params|
h = params.to_h
expect(h[:origin]).to eq 'local'
expect(h[:by_domain]).to eq 'domain'
expect(h[:status]).to eq 'active'
expect(h[:username]).to eq 'username'
expect(h[:display_name]).to eq 'display name'
expect(h[:email]).to eq 'local-part@domain'
expect(h[:ip]).to eq '0.0.0.42'
end
end
it 'paginates accounts' do

View file

@ -18,13 +18,15 @@ RSpec.describe Admin::AccountModerationNotesHelper do
let(:account) { Fabricate(:account) }
it 'calls #link_to' do
expect(helper).to receive(:link_to).with(
allow(helper).to receive(:link_to)
helper.admin_account_link_to(account)
expect(helper).to have_received(:link_to).with(
admin_account_path(account.id),
class: name_tag_classes(account),
title: account.acct
)
helper.admin_account_link_to(account)
end
end
end

View file

@ -29,12 +29,16 @@ describe WebfingerResource do
allow(recognized).to receive(:[]).with(:username).and_return('alice')
allow(recognized).to receive(:[]).with(:action).and_return('create')
expect(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized).at_least(:once)
allow(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized)
expect do
described_class.new(resource).username
end.to raise_error(ActiveRecord::RecordNotFound)
expect(recognized).to have_received(:[]).exactly(3).times
expect(Rails.application.routes).to have_received(:recognize_path)
.with(resource)
.at_least(:once)
end
it 'raises with a string that doesnt start with URL' do

View file

@ -78,13 +78,15 @@ RSpec.describe Admin::AccountAction do
end
it 'calls process_email!' do
expect(account_action).to receive(:process_email!)
allow(account_action).to receive(:process_email!)
subject
expect(account_action).to have_received(:process_email!)
end
it 'calls process_reports!' do
expect(account_action).to receive(:process_reports!)
allow(account_action).to receive(:process_reports!)
subject
expect(account_action).to have_received(:process_reports!)
end
end

View file

@ -120,8 +120,11 @@ RSpec.describe Remotable do
end
it 'does not try to write attribute' do
expect(foo).to_not receive('[]=').with(attribute_name, url)
allow(foo).to receive('[]=').with(attribute_name, url)
foo.hoge_remote_url = url
expect(foo).to_not have_received('[]=').with(attribute_name, url)
end
end
@ -131,8 +134,11 @@ RSpec.describe Remotable do
end
it 'does not try to write attribute' do
expect(foo).to receive('[]=').with(attribute_name, url)
allow(foo).to receive('[]=').with(attribute_name, url)
foo.hoge_remote_url = url
expect(foo).to have_received('[]=').with(attribute_name, url)
end
end
@ -146,10 +152,13 @@ RSpec.describe Remotable do
let(:code) { 500 }
it 'does not assign file' do
expect(foo).to_not receive(:public_send).with("#{hoge}=", any_args)
expect(foo).to_not receive(:public_send).with("#{hoge}_file_name=", any_args)
allow(foo).to receive(:public_send)
allow(foo).to receive(:public_send)
foo.hoge_remote_url = url
expect(foo).to_not have_received(:public_send).with("#{hoge}=", any_args)
expect(foo).to_not have_received(:public_send).with("#{hoge}_file_name=", any_args)
end
end
@ -165,13 +174,13 @@ RSpec.describe Remotable do
allow(ResponseWithLimit).to receive(:new).with(anything, anything).and_return(response_with_limit)
expect(foo).to receive(:public_send).with("download_#{hoge}!", url)
allow(foo).to receive(:public_send)
foo.hoge_remote_url = url
expect(foo).to have_received(:public_send).with("download_#{hoge}!", url)
expect(foo).to receive(:public_send).with("#{hoge}=", response_with_limit)
allow(foo).to receive(:public_send)
foo.download_hoge!(url)
expect(foo).to have_received(:public_send).with("#{hoge}=", response_with_limit)
end
end
end
@ -193,10 +202,13 @@ RSpec.describe Remotable do
let(:error_class) { error_class }
it 'calls Rails.logger.debug' do
expect(Rails.logger).to receive(:debug) do |&block|
allow(Rails.logger).to receive(:debug)
foo.hoge_remote_url = url
expect(Rails.logger).to have_received(:debug) do |&block|
expect(block.call).to match(/^Error fetching remote #{hoge}: /)
end
foo.hoge_remote_url = url
end
end
end

View file

@ -21,12 +21,17 @@ RSpec.describe FollowRequest do
end
it 'calls Account#follow!, MergeWorker.perform_async, and #destroy!' do
expect(account).to receive(:follow!).with(target_account, reblogs: true, notify: false, uri: follow_request.uri, languages: nil, bypass_limit: true) do
allow(account).to receive(:follow!) do
account.active_relationships.create!(target_account: target_account)
end
expect(MergeWorker).to receive(:perform_async).with(target_account.id, account.id)
expect(follow_request).to receive(:destroy!)
allow(MergeWorker).to receive(:perform_async)
allow(follow_request).to receive(:destroy!)
follow_request.authorize!
expect(account).to have_received(:follow!).with(target_account, reblogs: true, notify: false, uri: follow_request.uri, languages: nil, bypass_limit: true)
expect(MergeWorker).to have_received(:perform_async).with(target_account.id, account.id)
expect(follow_request).to have_received(:destroy!)
end
it 'correctly passes show_reblogs when true' do

View file

@ -7,8 +7,11 @@ RSpec.describe Identity do
let(:auth) { Fabricate(:identity, user: Fabricate(:user)) }
it 'calls .find_or_create_by' do
expect(described_class).to receive(:find_or_create_by).with(uid: auth.uid, provider: auth.provider)
allow(described_class).to receive(:find_or_create_by)
described_class.find_for_oauth(auth)
expect(described_class).to have_received(:find_or_create_by).with(uid: auth.uid, provider: auth.provider)
end
it 'returns an instance of Identity' do

View file

@ -74,9 +74,13 @@ RSpec.describe SessionActivation do
let(:options) { { user: Fabricate(:user), session_id: '1' } }
it 'calls create! and purge_old' do
expect(described_class).to receive(:create!).with(**options)
expect(described_class).to receive(:purge_old)
allow(described_class).to receive(:create!).with(**options)
allow(described_class).to receive(:purge_old)
described_class.activate(**options)
expect(described_class).to have_received(:create!).with(**options)
expect(described_class).to have_received(:purge_old)
end
it 'returns an instance of SessionActivation' do

View file

@ -23,8 +23,11 @@ RSpec.describe Setting do
let(:rails_initialized) { false }
it 'calls RailsSettings::Base#[]' do
expect(RailsSettings::Base).to receive(:[]).with(key)
allow(RailsSettings::Base).to receive(:[]).with(key)
described_class[key]
expect(RailsSettings::Base).to have_received(:[]).with(key)
end
end
@ -38,8 +41,11 @@ RSpec.describe Setting do
let(:cache_value) { 'cache-value' }
it 'calls not RailsSettings::Base#[]' do
expect(RailsSettings::Base).to_not receive(:[]).with(key)
allow(RailsSettings::Base).to receive(:[]).with(key)
described_class[key]
expect(RailsSettings::Base).to_not have_received(:[]).with(key)
end
context 'when Rails.cache does not exists' do
@ -56,8 +62,11 @@ RSpec.describe Setting do
let(:records) { [Fabricate(:setting, var: key, value: nil)] }
it 'calls RailsSettings::Settings.object' do
expect(RailsSettings::Settings).to receive(:object).with(key)
allow(RailsSettings::Settings).to receive(:object).with(key)
described_class[key]
expect(RailsSettings::Settings).to have_received(:object).with(key)
end
context 'when RailsSettings::Settings.object returns truthy' do

View file

@ -36,8 +36,11 @@ RSpec.describe ActivityPub::FetchRepliesService, type: :service do
context 'when the payload is a Collection with inlined replies' do
context 'when passing the collection itself' do
it 'spawns workers for up to 5 replies on the same server' do
expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
allow(FetchReplyWorker).to receive(:push_bulk)
subject.call(status, payload)
expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
end
end
@ -47,8 +50,11 @@ RSpec.describe ActivityPub::FetchRepliesService, type: :service do
end
it 'spawns workers for up to 5 replies on the same server' do
expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
allow(FetchReplyWorker).to receive(:push_bulk)
subject.call(status, collection_uri)
expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
end
end
end
@ -65,8 +71,11 @@ RSpec.describe ActivityPub::FetchRepliesService, type: :service do
context 'when passing the collection itself' do
it 'spawns workers for up to 5 replies on the same server' do
expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
allow(FetchReplyWorker).to receive(:push_bulk)
subject.call(status, payload)
expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
end
end
@ -76,8 +85,11 @@ RSpec.describe ActivityPub::FetchRepliesService, type: :service do
end
it 'spawns workers for up to 5 replies on the same server' do
expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
allow(FetchReplyWorker).to receive(:push_bulk)
subject.call(status, collection_uri)
expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
end
end
end
@ -98,8 +110,11 @@ RSpec.describe ActivityPub::FetchRepliesService, type: :service do
context 'when passing the collection itself' do
it 'spawns workers for up to 5 replies on the same server' do
expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
allow(FetchReplyWorker).to receive(:push_bulk)
subject.call(status, payload)
expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
end
end
@ -109,8 +124,11 @@ RSpec.describe ActivityPub::FetchRepliesService, type: :service do
end
it 'spawns workers for up to 5 replies on the same server' do
expect(FetchReplyWorker).to receive(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
allow(FetchReplyWorker).to receive(:push_bulk)
subject.call(status, collection_uri)
expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1', 'http://example.com/self-reply-2', 'http://example.com/self-reply-3', 'http://example.com/self-reply-4', 'http://example.com/self-reply-5'])
end
end
end

View file

@ -41,8 +41,11 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
end
it 'does not process payload' do
expect(ActivityPub::Activity).to_not receive(:factory)
allow(ActivityPub::Activity).to receive(:factory)
subject.call(json, actor)
expect(ActivityPub::Activity).to_not have_received(:factory)
end
end
end
@ -59,8 +62,11 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
end
it 'processes the payload' do
expect(ActivityPub::Activity).to receive(:factory)
allow(ActivityPub::Activity).to receive(:factory)
subject.call(json, actor)
expect(ActivityPub::Activity).to have_received(:factory)
end
end
end
@ -71,27 +77,33 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
it 'does not process payload if no signature exists' do
allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil)
expect(ActivityPub::Activity).to_not receive(:factory)
allow(ActivityPub::Activity).to receive(:factory)
subject.call(json, forwarder)
expect(ActivityPub::Activity).to_not have_received(:factory)
end
it 'processes payload with actor if valid signature exists' do
payload['signature'] = { 'type' => 'RsaSignature2017' }
allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(actor)
expect(ActivityPub::Activity).to receive(:factory).with(instance_of(Hash), actor, instance_of(Hash))
allow(ActivityPub::Activity).to receive(:factory).with(instance_of(Hash), actor, instance_of(Hash))
subject.call(json, forwarder)
expect(ActivityPub::Activity).to have_received(:factory).with(instance_of(Hash), actor, instance_of(Hash))
end
it 'does not process payload if invalid signature exists' do
payload['signature'] = { 'type' => 'RsaSignature2017' }
allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil)
expect(ActivityPub::Activity).to_not receive(:factory)
allow(ActivityPub::Activity).to receive(:factory)
subject.call(json, forwarder)
expect(ActivityPub::Activity).to_not have_received(:factory)
end
context 'when receiving a fabricated status' do
@ -225,7 +237,11 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
end
it 'does not process forged payload' do
expect(ActivityPub::Activity).to_not receive(:factory).with(
allow(ActivityPub::Activity).to receive(:factory)
subject.call(json, forwarder)
expect(ActivityPub::Activity).to_not have_received(:factory).with(
hash_including(
'object' => hash_including(
'id' => 'https://example.com/users/bob/fake-status'
@ -235,7 +251,7 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
anything
)
expect(ActivityPub::Activity).to_not receive(:factory).with(
expect(ActivityPub::Activity).to_not have_received(:factory).with(
hash_including(
'object' => hash_including(
'content' => '<p>puck was here</p>'
@ -245,8 +261,6 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
anything
)
subject.call(json, forwarder)
expect(Status.where(uri: 'https://example.com/users/bob/fake-status').exists?).to be false
end
end

View file

@ -53,7 +53,9 @@ def json_str_to_hash(str)
end
def expect_push_bulk_to_match(klass, matcher)
expect(Sidekiq::Client).to receive(:push_bulk).with(hash_including({
allow(Sidekiq::Client).to receive(:push_bulk)
yield
expect(Sidekiq::Client).to have_received(:push_bulk).with(hash_including({
'class' => klass,
'args' => matcher,
}))

View file

@ -6,14 +6,20 @@ describe StatusLengthValidator do
describe '#validate' do
it 'does not add errors onto remote statuses' do
status = instance_double(Status, local?: false)
allow(status).to receive(:errors)
subject.validate(status)
expect(status).to_not receive(:errors)
expect(status).to_not have_received(:errors)
end
it 'does not add errors onto local reblogs' do
status = instance_double(Status, local?: false, reblog?: true)
allow(status).to receive(:errors)
subject.validate(status)
expect(status).to_not receive(:errors)
expect(status).to_not have_received(:errors)
end
it 'adds an error when content warning is over 500 characters' do

View file

@ -16,8 +16,9 @@ describe ActivityPub::DistributePollUpdateWorker do
end
it 'delivers to followers' do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), account.id, 'http://example.com']])
subject.perform(status.id)
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), account.id, 'http://example.com']]) do
subject.perform(status.id)
end
end
end
end

View file

@ -19,8 +19,9 @@ describe ActivityPub::DistributionWorker do
end
it 'delivers to followers' do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]])
subject.perform(status.id)
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]]) do
subject.perform(status.id)
end
end
end
@ -30,8 +31,9 @@ describe ActivityPub::DistributionWorker do
end
it 'delivers to followers' do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]])
subject.perform(status.id)
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]]) do
subject.perform(status.id)
end
end
end
@ -44,8 +46,9 @@ describe ActivityPub::DistributionWorker do
end
it 'delivers to mentioned accounts' do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'https://foo.bar/inbox', anything]])
subject.perform(status.id)
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'https://foo.bar/inbox', anything]]) do
subject.perform(status.id)
end
end
end
end

View file

@ -19,8 +19,9 @@ describe ActivityPub::MoveDistributionWorker do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [
[kind_of(String), migration.account.id, 'http://example.com'],
[kind_of(String), migration.account.id, 'http://example2.com'],
])
subject.perform(migration.id)
]) do
subject.perform(migration.id)
end
end
end
end

View file

@ -25,9 +25,9 @@ describe ActivityPub::StatusUpdateDistributionWorker do
end
it 'delivers to followers' do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]])
subject.perform(status.id)
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]]) do
subject.perform(status.id)
end
end
end
@ -37,9 +37,9 @@ describe ActivityPub::StatusUpdateDistributionWorker do
end
it 'delivers to followers' do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]])
subject.perform(status.id)
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), status.account.id, 'http://example.com', anything]]) do
subject.perform(status.id)
end
end
end
end

View file

@ -14,8 +14,9 @@ describe ActivityPub::UpdateDistributionWorker do
end
it 'delivers to followers' do
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), account.id, 'http://example.com', anything]])
subject.perform(account.id)
expect_push_bulk_to_match(ActivityPub::DeliveryWorker, [[kind_of(String), account.id, 'http://example.com', anything]]) do
subject.perform(account.id)
end
end
end
end