Fix RSpec/AnyInstance cop (#27810)

This commit is contained in:
Matt Jankowski 2023-11-14 09:52:59 -05:00 committed by GitHub
parent d562fb8459
commit b2c5b20ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 70 additions and 60 deletions

View File

@ -41,23 +41,6 @@ Metrics/CyclomaticComplexity:
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:
Max: 27 Max: 27
RSpec/AnyInstance:
Exclude:
- 'spec/controllers/activitypub/inboxes_controller_spec.rb'
- 'spec/controllers/admin/accounts_controller_spec.rb'
- 'spec/controllers/admin/resets_controller_spec.rb'
- 'spec/controllers/auth/sessions_controller_spec.rb'
- 'spec/controllers/settings/two_factor_authentication/confirmations_controller_spec.rb'
- 'spec/controllers/settings/two_factor_authentication/recovery_codes_controller_spec.rb'
- 'spec/lib/request_spec.rb'
- 'spec/lib/status_filter_spec.rb'
- 'spec/models/account_spec.rb'
- 'spec/models/setting_spec.rb'
- 'spec/services/activitypub/process_collection_service_spec.rb'
- 'spec/validators/follow_limit_validator_spec.rb'
- 'spec/workers/activitypub/delivery_worker_spec.rb'
- 'spec/workers/web/push_notification_worker_spec.rb'
# Configuration parameters: CountAsOne. # Configuration parameters: CountAsOne.
RSpec/ExampleLength: RSpec/ExampleLength:
Max: 22 Max: 22

View File

@ -58,7 +58,7 @@ RSpec.describe ActivityPub::InboxesController do
before do before do
allow(ActivityPub::FollowersSynchronizationWorker).to receive(:perform_async).and_return(nil) allow(ActivityPub::FollowersSynchronizationWorker).to receive(:perform_async).and_return(nil)
allow_any_instance_of(Account).to receive(:local_followers_hash).and_return('somehash') allow(remote_account).to receive(:local_followers_hash).and_return('somehash')
request.headers['Collection-Synchronization'] = synchronization_header request.headers['Collection-Synchronization'] = synchronization_header
post :create, body: '{}' post :create, body: '{}'

View File

@ -227,7 +227,8 @@ RSpec.describe Admin::AccountsController do
let(:account) { Fabricate(:account, domain: 'example.com') } let(:account) { Fabricate(:account, domain: 'example.com') }
before do before do
allow_any_instance_of(ResolveAccountService).to receive(:call) service = instance_double(ResolveAccountService, call: nil)
allow(ResolveAccountService).to receive(:new).and_return(service)
end end
context 'when user is admin' do context 'when user is admin' do

View File

@ -13,12 +13,20 @@ describe Admin::ResetsController do
describe 'POST #create' do describe 'POST #create' do
it 'redirects to admin accounts page' do it 'redirects to admin accounts page' do
expect_any_instance_of(User).to receive(:send_reset_password_instructions) do |value| expect do
expect(value.account_id).to eq account.id post :create, params: { account_id: account.id }
end end.to change(Devise.mailer.deliveries, :size).by(2)
post :create, params: { account_id: account.id }
expect(Devise.mailer.deliveries).to have_attributes(
first: have_attributes(
to: include(account.user.email),
subject: I18n.t('devise.mailer.password_change.subject')
),
last: have_attributes(
to: include(account.user.email),
subject: I18n.t('devise.mailer.reset_password_instructions.subject')
)
)
expect(response).to redirect_to(admin_account_path(account.id)) expect(response).to redirect_to(admin_account_path(account.id))
end end
end end

View File

@ -126,7 +126,7 @@ RSpec.describe Auth::SessionsController do
let!(:previous_login) { Fabricate(:login_activity, user: user, ip: previous_ip) } let!(:previous_login) { Fabricate(:login_activity, user: user, ip: previous_ip) }
before do before do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(current_ip) allow(controller.request).to receive(:remote_ip).and_return(current_ip)
user.update(current_sign_in_at: 1.month.ago) user.update(current_sign_in_at: 1.month.ago)
post :create, params: { user: { email: user.email, password: user.password } } post :create, params: { user: { email: user.email, password: user.password } }
end end
@ -279,7 +279,9 @@ RSpec.describe Auth::SessionsController do
context 'when the server has an decryption error' do context 'when the server has an decryption error' do
before do before do
allow_any_instance_of(User).to receive(:validate_and_consume_otp!).and_raise(OpenSSL::Cipher::CipherError) allow(user).to receive(:validate_and_consume_otp!).with(user.current_otp).and_raise(OpenSSL::Cipher::CipherError)
allow(User).to receive(:find_by).with(id: user.id).and_return(user)
post :create, params: { user: { otp_attempt: user.current_otp } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s } post :create, params: { user: { otp_attempt: user.current_otp } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
end end

View File

@ -61,6 +61,7 @@ describe Settings::TwoFactorAuthentication::ConfirmationsController do
it 'renders page with success' do it 'renders page with success' do
prepare_user_otp_generation prepare_user_otp_generation
prepare_user_otp_consumption prepare_user_otp_consumption
allow(controller).to receive(:current_user).and_return(user)
expect do expect do
post :create, post :create,
@ -75,30 +76,28 @@ describe Settings::TwoFactorAuthentication::ConfirmationsController do
end end
def prepare_user_otp_generation def prepare_user_otp_generation
expect_any_instance_of(User).to receive(:generate_otp_backup_codes!) do |value| allow(user)
expect(value).to eq user .to receive(:generate_otp_backup_codes!)
otp_backup_codes .and_return(otp_backup_codes)
end
end end
def prepare_user_otp_consumption def prepare_user_otp_consumption
expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, code, options| options = { otp_secret: 'thisisasecretforthespecofnewview' }
expect(value).to eq user allow(user)
expect(code).to eq '123456' .to receive(:validate_and_consume_otp!)
expect(options).to eq({ otp_secret: 'thisisasecretforthespecofnewview' }) .with('123456', options)
true .and_return(true)
end
end end
end end
describe 'when creation fails' do describe 'when creation fails' do
subject do subject do
expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, code, options| options = { otp_secret: 'thisisasecretforthespecofnewview' }
expect(value).to eq user allow(user)
expect(code).to eq '123456' .to receive(:validate_and_consume_otp!)
expect(options).to eq({ otp_secret: 'thisisasecretforthespecofnewview' }) .with('123456', options)
false .and_return(false)
end allow(controller).to receive(:current_user).and_return(user)
expect do expect do
post :create, post :create,

View File

@ -9,10 +9,8 @@ describe Settings::TwoFactorAuthentication::RecoveryCodesController do
it 'updates the codes and shows them on a view when signed in' do it 'updates the codes and shows them on a view when signed in' do
user = Fabricate(:user) user = Fabricate(:user)
otp_backup_codes = user.generate_otp_backup_codes! otp_backup_codes = user.generate_otp_backup_codes!
expect_any_instance_of(User).to receive(:generate_otp_backup_codes!) do |value| allow(user).to receive(:generate_otp_backup_codes!).and_return(otp_backup_codes)
expect(value).to eq user allow(controller).to receive(:current_user).and_return(user)
otp_backup_codes
end
sign_in user, scope: :user sign_in user, scope: :user
post :create, session: { challenge_passed_at: Time.now.utc } post :create, session: { challenge_passed_at: Time.now.utc }

View File

@ -64,8 +64,11 @@ describe Request do
end end
it 'closes underlying connection' do it 'closes underlying connection' do
expect_any_instance_of(HTTP::Client).to receive(:close) allow(subject.send(:http_client)).to receive(:close)
expect { |block| subject.perform(&block) }.to yield_control expect { |block| subject.perform(&block) }.to yield_control
expect(subject.send(:http_client)).to have_received(:close)
end end
it 'returns response which implements body_with_limit' do it 'returns response which implements body_with_limit' do

View File

@ -23,7 +23,8 @@ describe StatusFilter do
context 'when status policy does not allow show' do context 'when status policy does not allow show' do
it 'filters the status' do it 'filters the status' do
allow_any_instance_of(StatusPolicy).to receive(:show?).and_return(false) policy = instance_double(StatusPolicy, show?: false)
allow(StatusPolicy).to receive(:new).and_return(policy)
expect(filter).to be_filtered expect(filter).to be_filtered
end end
@ -74,7 +75,8 @@ describe StatusFilter do
context 'when status policy does not allow show' do context 'when status policy does not allow show' do
it 'filters the status' do it 'filters the status' do
allow_any_instance_of(StatusPolicy).to receive(:show?).and_return(false) policy = instance_double(StatusPolicy, show?: false)
allow(StatusPolicy).to receive(:new).and_return(policy)
expect(filter).to be_filtered expect(filter).to be_filtered
end end

View File

@ -209,9 +209,13 @@ RSpec.describe Account do
expect(account.refresh!).to be_nil expect(account.refresh!).to be_nil
end end
it 'calls not ResolveAccountService#call' do it 'does not call ResolveAccountService#call' do
expect_any_instance_of(ResolveAccountService).to_not receive(:call).with(acct) service = instance_double(ResolveAccountService, call: nil)
allow(ResolveAccountService).to receive(:new).and_return(service)
account.refresh! account.refresh!
expect(service).to_not have_received(:call).with(acct)
end end
end end
@ -219,8 +223,12 @@ RSpec.describe Account do
let(:domain) { 'example.com' } let(:domain) { 'example.com' }
it 'calls ResolveAccountService#call' do it 'calls ResolveAccountService#call' do
expect_any_instance_of(ResolveAccountService).to receive(:call).with(acct).once service = instance_double(ResolveAccountService, call: nil)
allow(ResolveAccountService).to receive(:new).and_return(service)
account.refresh! account.refresh!
expect(service).to have_received(:call).with(acct).once
end end
end end
end end

View File

@ -52,7 +52,8 @@ RSpec.describe Setting do
before do before do
allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object) allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object)
allow(described_class).to receive(:default_settings).and_return(default_settings) allow(described_class).to receive(:default_settings).and_return(default_settings)
allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records) settings_double = instance_double(Settings::ScopedSettings, thing_scoped: records)
allow(Settings::ScopedSettings).to receive(:new).and_return(settings_double)
Rails.cache.delete(cache_key) Rails.cache.delete(cache_key)
end end
@ -128,7 +129,8 @@ RSpec.describe Setting do
describe '.all_as_records' do describe '.all_as_records' do
before do before do
allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records) settings_double = instance_double(Settings::ScopedSettings, thing_scoped: records)
allow(Settings::ScopedSettings).to receive(:new).and_return(settings_double)
allow(described_class).to receive(:default_settings).and_return(default_settings) allow(described_class).to receive(:default_settings).and_return(default_settings)
end end

View File

@ -76,7 +76,8 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
let(:forwarder) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/other_account') } let(:forwarder) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/other_account') }
it 'does not process payload if no signature exists' do it 'does not process payload if no signature exists' do
allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil) signature_double = instance_double(ActivityPub::LinkedDataSignature, verify_actor!: nil)
allow(ActivityPub::LinkedDataSignature).to receive(:new).and_return(signature_double)
allow(ActivityPub::Activity).to receive(:factory) allow(ActivityPub::Activity).to receive(:factory)
subject.call(json, forwarder) subject.call(json, forwarder)
@ -87,7 +88,8 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
it 'processes payload with actor if valid signature exists' do it 'processes payload with actor if valid signature exists' do
payload['signature'] = { 'type' => 'RsaSignature2017' } payload['signature'] = { 'type' => 'RsaSignature2017' }
allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(actor) signature_double = instance_double(ActivityPub::LinkedDataSignature, verify_actor!: actor)
allow(ActivityPub::LinkedDataSignature).to receive(:new).and_return(signature_double)
allow(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) subject.call(json, forwarder)
@ -98,7 +100,8 @@ RSpec.describe ActivityPub::ProcessCollectionService, type: :service do
it 'does not process payload if invalid signature exists' do it 'does not process payload if invalid signature exists' do
payload['signature'] = { 'type' => 'RsaSignature2017' } payload['signature'] = { 'type' => 'RsaSignature2017' }
allow_any_instance_of(ActivityPub::LinkedDataSignature).to receive(:verify_actor!).and_return(nil) signature_double = instance_double(ActivityPub::LinkedDataSignature, verify_actor!: nil)
allow(ActivityPub::LinkedDataSignature).to receive(:new).and_return(signature_double)
allow(ActivityPub::Activity).to receive(:factory) allow(ActivityPub::Activity).to receive(:factory)
subject.call(json, forwarder) subject.call(json, forwarder)

View File

@ -11,7 +11,8 @@ describe ActivityPub::DeliveryWorker do
let(:payload) { 'test' } let(:payload) { 'test' }
before do before do
allow_any_instance_of(Account).to receive(:remote_followers_hash).with('https://example.com/api').and_return('somehash') allow(sender).to receive(:remote_followers_hash).with('https://example.com/api').and_return('somehash')
allow(Account).to receive(:find).with(sender.id).and_return(sender)
end end
describe 'perform' do describe 'perform' do

View File

@ -23,8 +23,8 @@ describe Web::PushNotificationWorker do
describe 'perform' do describe 'perform' do
before do before do
allow_any_instance_of(subscription.class).to receive(:contact_email).and_return(contact_email) allow(subscription).to receive_messages(contact_email: contact_email, vapid_key: vapid_key)
allow_any_instance_of(subscription.class).to receive(:vapid_key).and_return(vapid_key) allow(Web::PushSubscription).to receive(:find).with(subscription.id).and_return(subscription)
allow(Webpush::Encryption).to receive(:encrypt).and_return(payload) allow(Webpush::Encryption).to receive(:encrypt).and_return(payload)
allow(JWT).to receive(:encode).and_return('jwt.encoded.payload') allow(JWT).to receive(:encode).and_return('jwt.encoded.payload')