Fix RSpec/LetSetup cop in spec/services (#28459)

This commit is contained in:
Matt Jankowski 2023-12-21 09:23:53 -05:00 committed by GitHub
parent efd16f3c2c
commit 9251779d75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 74 additions and 87 deletions

View file

@ -53,19 +53,6 @@ RSpec/LetSetup:
- 'spec/models/account_statuses_cleanup_policy_spec.rb' - 'spec/models/account_statuses_cleanup_policy_spec.rb'
- 'spec/models/status_spec.rb' - 'spec/models/status_spec.rb'
- 'spec/services/activitypub/fetch_featured_collection_service_spec.rb' - 'spec/services/activitypub/fetch_featured_collection_service_spec.rb'
- 'spec/services/batched_remove_status_service_spec.rb'
- 'spec/services/block_domain_service_spec.rb'
- 'spec/services/bulk_import_service_spec.rb'
- 'spec/services/delete_account_service_spec.rb'
- 'spec/services/import_service_spec.rb'
- 'spec/services/notify_service_spec.rb'
- 'spec/services/remove_status_service_spec.rb'
- 'spec/services/report_service_spec.rb'
- 'spec/services/resolve_account_service_spec.rb'
- 'spec/services/suspend_account_service_spec.rb'
- 'spec/services/unallow_domain_service_spec.rb'
- 'spec/services/unsuspend_account_service_spec.rb'
- 'spec/workers/scheduler/user_cleanup_scheduler_spec.rb'
RSpec/MultipleExpectations: RSpec/MultipleExpectations:
Max: 8 Max: 8

View file

@ -10,7 +10,7 @@ RSpec.describe BatchedRemoveStatusService, type: :service do
let!(:jeff) { Fabricate(:account) } let!(:jeff) { Fabricate(:account) }
let!(:hank) { Fabricate(:account, username: 'hank', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') } let!(:hank) { Fabricate(:account, username: 'hank', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
let(:status_alice_hello) { PostStatusService.new.call(alice, text: 'Hello @bob@example.com') } let(:status_alice_hello) { PostStatusService.new.call(alice, text: "Hello @#{bob.pretty_acct}") }
let(:status_alice_other) { PostStatusService.new.call(alice, text: 'Another status') } let(:status_alice_other) { PostStatusService.new.call(alice, text: 'Another status') }
before do before do

View file

@ -21,19 +21,19 @@ RSpec.describe BlockDomainService, type: :service do
end end
it 'removes remote accounts from that domain' do it 'removes remote accounts from that domain' do
expect(Account.find_remote('badguy666', 'evil.org').suspended?).to be true expect(bad_account.reload.suspended?).to be true
end end
it 'records suspension date appropriately' do it 'records suspension date appropriately' do
expect(Account.find_remote('badguy666', 'evil.org').suspended_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at expect(bad_account.reload.suspended_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at
end end
it 'keeps already-banned accounts banned' do it 'keeps already-banned accounts banned' do
expect(Account.find_remote('badguy', 'evil.org').suspended?).to be true expect(already_banned_account.reload.suspended?).to be true
end end
it 'does not overwrite suspension date of already-banned accounts' do it 'does not overwrite suspension date of already-banned accounts' do
expect(Account.find_remote('badguy', 'evil.org').suspended_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at expect(already_banned_account.reload.suspended_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at
end end
it 'removes the remote accounts\'s statuses and media attachments' do it 'removes the remote accounts\'s statuses and media attachments' do
@ -53,19 +53,19 @@ RSpec.describe BlockDomainService, type: :service do
end end
it 'silences remote accounts from that domain' do it 'silences remote accounts from that domain' do
expect(Account.find_remote('badguy666', 'evil.org').silenced?).to be true expect(bad_account.reload.silenced?).to be true
end end
it 'records suspension date appropriately' do it 'records suspension date appropriately' do
expect(Account.find_remote('badguy666', 'evil.org').silenced_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at expect(bad_account.reload.silenced_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at
end end
it 'keeps already-banned accounts banned' do it 'keeps already-banned accounts banned' do
expect(Account.find_remote('badguy', 'evil.org').silenced?).to be true expect(already_banned_account.reload.silenced?).to be true
end end
it 'does not overwrite suspension date of already-banned accounts' do it 'does not overwrite suspension date of already-banned accounts' do
expect(Account.find_remote('badguy', 'evil.org').silenced_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at expect(already_banned_account.reload.silenced_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at
end end
it 'leaves the domains status and attachments, but clears media' do it 'leaves the domains status and attachments, but clears media' do

View file

@ -271,14 +271,15 @@ RSpec.describe BulkImportService do
let(:import_type) { 'domain_blocking' } let(:import_type) { 'domain_blocking' }
let(:overwrite) { false } let(:overwrite) { false }
let!(:rows) do let(:rows) do
[ [
{ 'domain' => 'blocked.com' }, { 'domain' => 'blocked.com' },
{ 'domain' => 'to_block.com' }, { 'domain' => 'to_block.com' },
].map { |data| import.rows.create!(data: data) } ]
end end
before do before do
rows.each { |data| import.rows.create!(data: data) }
account.block_domain!('alreadyblocked.com') account.block_domain!('alreadyblocked.com')
account.block_domain!('blocked.com') account.block_domain!('blocked.com')
end end
@ -298,14 +299,15 @@ RSpec.describe BulkImportService do
let(:import_type) { 'domain_blocking' } let(:import_type) { 'domain_blocking' }
let(:overwrite) { true } let(:overwrite) { true }
let!(:rows) do let(:rows) do
[ [
{ 'domain' => 'blocked.com' }, { 'domain' => 'blocked.com' },
{ 'domain' => 'to_block.com' }, { 'domain' => 'to_block.com' },
].map { |data| import.rows.create!(data: data) } ]
end end
before do before do
rows.each { |data| import.rows.create!(data: data) }
account.block_domain!('alreadyblocked.com') account.block_domain!('alreadyblocked.com')
account.block_domain!('blocked.com') account.block_domain!('blocked.com')
end end

View file

@ -28,74 +28,69 @@ RSpec.describe DeleteAccountService, type: :service do
let!(:account_note) { Fabricate(:account_note, account: account) } let!(:account_note) { Fabricate(:account_note, account: account) }
it 'deletes associated owned and target records and target notifications' do it 'deletes associated owned and target records and target notifications' do
expect { subject } subject
.to delete_associated_owned_records
.and delete_associated_target_records expect_deletion_of_associated_owned_records
.and delete_associated_target_notifications expect_deletion_of_associated_target_records
expect_deletion_of_associated_target_notifications
end end
def delete_associated_owned_records def expect_deletion_of_associated_owned_records
change do expect { status.reload }.to raise_error(ActiveRecord::RecordNotFound)
[ expect { status_with_mention.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.statuses, expect { mention.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.media_attachments, expect { media_attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.notifications, expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.favourites, expect { favourite.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.active_relationships, expect { active_relationship.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.passive_relationships, expect { passive_relationship.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.polls, expect { poll.reload }.to raise_error(ActiveRecord::RecordNotFound)
account.account_notes, expect { poll_vote.reload }.to raise_error(ActiveRecord::RecordNotFound)
].map(&:count) expect { account_note.reload }.to raise_error(ActiveRecord::RecordNotFound)
end.from([2, 1, 1, 1, 1, 1, 1, 1]).to([0, 0, 0, 0, 0, 0, 0, 0])
end end
def delete_associated_target_records def expect_deletion_of_associated_target_records
change(account_pins_for_account, :count).from(1).to(0) expect { endorsement.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
def account_pins_for_account def expect_deletion_of_associated_target_notifications
AccountPin.where(target_account: account) expect { favourite_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
end expect { follow_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { mention_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
def delete_associated_target_notifications expect { poll_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
change do expect { status_notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
%w(
poll favourite status mention follow
).map { |type| Notification.where(type: type).count }
end.from([1, 1, 1, 1, 1]).to([0, 0, 0, 0, 0])
end end
end end
describe '#call on local account' do describe '#call on local account' do
before do before do
stub_request(:post, 'https://alice.com/inbox').to_return(status: 201) stub_request(:post, remote_alice.inbox_url).to_return(status: 201)
stub_request(:post, 'https://bob.com/inbox').to_return(status: 201) stub_request(:post, remote_bob.inbox_url).to_return(status: 201)
end end
let!(:remote_alice) { Fabricate(:account, inbox_url: 'https://alice.com/inbox', domain: 'alice.com', protocol: :activitypub) } let!(:remote_alice) { Fabricate(:account, inbox_url: 'https://alice.com/inbox', domain: 'alice.com', protocol: :activitypub) }
let!(:remote_bob) { Fabricate(:account, inbox_url: 'https://bob.com/inbox', domain: 'bob.com', protocol: :activitypub) } let!(:remote_bob) { Fabricate(:account, inbox_url: 'https://bob.com/inbox', domain: 'bob.com', protocol: :activitypub) }
include_examples 'common behavior' do include_examples 'common behavior' do
let!(:account) { Fabricate(:account) } let(:account) { Fabricate(:account) }
let!(:local_follower) { Fabricate(:account) } let(:local_follower) { Fabricate(:account) }
it 'sends a delete actor activity to all known inboxes' do it 'sends a delete actor activity to all known inboxes' do
subject subject
expect(a_request(:post, 'https://alice.com/inbox')).to have_been_made.once expect(a_request(:post, remote_alice.inbox_url)).to have_been_made.once
expect(a_request(:post, 'https://bob.com/inbox')).to have_been_made.once expect(a_request(:post, remote_bob.inbox_url)).to have_been_made.once
end end
end end
end end
describe '#call on remote account' do describe '#call on remote account' do
before do before do
stub_request(:post, 'https://alice.com/inbox').to_return(status: 201) stub_request(:post, account.inbox_url).to_return(status: 201)
stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
end end
include_examples 'common behavior' do include_examples 'common behavior' do
let!(:account) { Fabricate(:account, inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') } let(:account) { Fabricate(:account, inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') }
let!(:local_follower) { Fabricate(:account) } let(:local_follower) { Fabricate(:account) }
it 'sends expected activities to followed and follower inboxes' do it 'sends expected activities to followed and follower inboxes' do
subject subject

View file

@ -190,7 +190,7 @@ RSpec.describe ImportService, type: :service do
# Make sure to not actually go to the remote server # Make sure to not actually go to the remote server
before do before do
stub_request(:post, 'https://թութ.հայ/inbox').to_return(status: 200) stub_request(:post, nare.inbox_url).to_return(status: 200)
end end
it 'follows the listed account' do it 'follows the listed account' do

View file

@ -67,9 +67,10 @@ RSpec.describe NotifyService, type: :service do
context 'when the message chain is initiated by recipient, but is not direct message' do context 'when the message chain is initiated by recipient, but is not direct message' do
let(:reply_to) { Fabricate(:status, account: recipient) } let(:reply_to) { Fabricate(:status, account: recipient) }
let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) } let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
before { Fabricate(:mention, account: sender, status: reply_to) }
it 'does not notify' do it 'does not notify' do
expect { subject }.to_not change(Notification, :count) expect { subject }.to_not change(Notification, :count)
end end
@ -77,10 +78,11 @@ RSpec.describe NotifyService, type: :service do
context 'when the message chain is initiated by recipient, but without a mention to the sender, even if the sender sends multiple messages in a row' do context 'when the message chain is initiated by recipient, but without a mention to the sender, even if the sender sends multiple messages in a row' do
let(:reply_to) { Fabricate(:status, account: recipient) } let(:reply_to) { Fabricate(:status, account: recipient) }
let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
let(:dummy_reply) { Fabricate(:status, account: sender, visibility: :direct, thread: reply_to) } let(:dummy_reply) { Fabricate(:status, account: sender, visibility: :direct, thread: reply_to) }
let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: dummy_reply)) } let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: dummy_reply)) }
before { Fabricate(:mention, account: sender, status: reply_to) }
it 'does not notify' do it 'does not notify' do
expect { subject }.to_not change(Notification, :count) expect { subject }.to_not change(Notification, :count)
end end
@ -88,9 +90,10 @@ RSpec.describe NotifyService, type: :service do
context 'when the message chain is initiated by the recipient with a mention to the sender' do context 'when the message chain is initiated by the recipient with a mention to the sender' do
let(:reply_to) { Fabricate(:status, account: recipient, visibility: :direct) } let(:reply_to) { Fabricate(:status, account: recipient, visibility: :direct) }
let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) } let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
before { Fabricate(:mention, account: sender, status: reply_to) }
it 'does notify' do it 'does notify' do
expect { subject }.to change(Notification, :count) expect { subject }.to change(Notification, :count)
end end

View file

@ -12,15 +12,15 @@ RSpec.describe RemoveStatusService, type: :service do
let!(:bill) { Fabricate(:account, username: 'bill', protocol: :activitypub, domain: 'example2.com', inbox_url: 'http://example2.com/inbox') } let!(:bill) { Fabricate(:account, username: 'bill', protocol: :activitypub, domain: 'example2.com', inbox_url: 'http://example2.com/inbox') }
before do before do
stub_request(:post, 'http://example.com/inbox').to_return(status: 200) stub_request(:post, hank.inbox_url).to_return(status: 200)
stub_request(:post, 'http://example2.com/inbox').to_return(status: 200) stub_request(:post, bill.inbox_url).to_return(status: 200)
jeff.follow!(alice) jeff.follow!(alice)
hank.follow!(alice) hank.follow!(alice)
end end
context 'when removed status is not a reblog' do context 'when removed status is not a reblog' do
let!(:status) { PostStatusService.new.call(alice, text: 'Hello @bob@example.com ThisIsASecret') } let!(:status) { PostStatusService.new.call(alice, text: "Hello @#{bob.pretty_acct} ThisIsASecret") }
before do before do
FavouriteService.new.call(jeff, status) FavouriteService.new.call(jeff, status)
@ -39,7 +39,7 @@ RSpec.describe RemoveStatusService, type: :service do
it 'sends Delete activity to followers' do it 'sends Delete activity to followers' do
subject.call(status) subject.call(status)
expect(a_request(:post, 'http://example.com/inbox').with( expect(a_request(:post, hank.inbox_url).with(
body: hash_including({ body: hash_including({
'type' => 'Delete', 'type' => 'Delete',
'object' => { 'object' => {
@ -53,7 +53,7 @@ RSpec.describe RemoveStatusService, type: :service do
it 'sends Delete activity to rebloggers' do it 'sends Delete activity to rebloggers' do
subject.call(status) subject.call(status)
expect(a_request(:post, 'http://example2.com/inbox').with( expect(a_request(:post, bill.inbox_url).with(
body: hash_including({ body: hash_including({
'type' => 'Delete', 'type' => 'Delete',
'object' => { 'object' => {
@ -78,7 +78,7 @@ RSpec.describe RemoveStatusService, type: :service do
it 'sends Undo activity to followers' do it 'sends Undo activity to followers' do
subject.call(status) subject.call(status)
expect(a_request(:post, 'http://example.com/inbox').with( expect(a_request(:post, hank.inbox_url).with(
body: hash_including({ body: hash_including({
'type' => 'Undo', 'type' => 'Undo',
'object' => hash_including({ 'object' => hash_including({
@ -96,7 +96,7 @@ RSpec.describe RemoveStatusService, type: :service do
it 'sends Undo activity to followers' do it 'sends Undo activity to followers' do
subject.call(status) subject.call(status)
expect(a_request(:post, 'http://example.com/inbox').with( expect(a_request(:post, hank.inbox_url).with(
body: hash_including({ body: hash_including({
'type' => 'Undo', 'type' => 'Undo',
'object' => hash_including({ 'object' => hash_including({

View file

@ -156,9 +156,8 @@ RSpec.describe ReportService, type: :service do
-> { described_class.new.call(source_account, target_account) } -> { described_class.new.call(source_account, target_account) }
end end
let!(:other_report) { Fabricate(:report, target_account: target_account) }
before do before do
Fabricate(:report, target_account: target_account)
ActionMailer::Base.deliveries.clear ActionMailer::Base.deliveries.clear
source_account.user.settings['notification_emails.report'] = true source_account.user.settings['notification_emails.report'] = true
source_account.user.save source_account.user.save

View file

@ -20,7 +20,7 @@ RSpec.describe ResolveAccountService, type: :service do
let!(:remote_account) { Fabricate(:account, username: 'foo', domain: 'ap.example.com', protocol: 'activitypub') } let!(:remote_account) { Fabricate(:account, username: 'foo', domain: 'ap.example.com', protocol: 'activitypub') }
context 'when domain is banned' do context 'when domain is banned' do
let!(:domain_block) { Fabricate(:domain_block, domain: 'ap.example.com', severity: :suspend) } before { Fabricate(:domain_block, domain: 'ap.example.com', severity: :suspend) }
it 'does not return an account' do it 'does not return an account' do
expect(subject.call('foo@ap.example.com', skip_webfinger: true)).to be_nil expect(subject.call('foo@ap.example.com', skip_webfinger: true)).to be_nil
@ -214,6 +214,7 @@ RSpec.describe ResolveAccountService, type: :service do
expect(account.domain).to eq 'ap.example.com' expect(account.domain).to eq 'ap.example.com'
expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox' expect(account.inbox_url).to eq 'https://ap.example.com/users/foo/inbox'
expect(account.uri).to eq 'https://ap.example.com/users/foo' expect(account.uri).to eq 'https://ap.example.com/users/foo'
expect(status.reload.account).to eq(account)
end end
end end

View file

@ -46,9 +46,9 @@ RSpec.describe SuspendAccountService, type: :service do
let!(:account) { Fabricate(:account) } let!(:account) { Fabricate(:account) }
let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub, domain: 'alice.com') } let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub, domain: 'alice.com') }
let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') } let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') }
let!(:report) { Fabricate(:report, account: remote_reporter, target_account: account) }
before do before do
Fabricate(:report, account: remote_reporter, target_account: account)
remote_follower.follow!(account) remote_follower.follow!(account)
end end

View file

@ -27,6 +27,7 @@ RSpec.describe UnallowDomainService, type: :service do
end end
it 'removes remote accounts from that domain' do it 'removes remote accounts from that domain' do
expect { already_banned_account.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(Account.where(domain: 'evil.org').exists?).to be false expect(Account.where(domain: 'evil.org').exists?).to be false
end end

View file

@ -39,9 +39,9 @@ RSpec.describe UnsuspendAccountService, type: :service do
let!(:account) { Fabricate(:account) } let!(:account) { Fabricate(:account) }
let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub, domain: 'alice.com') } let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub, domain: 'alice.com') }
let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') } let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') }
let!(:report) { Fabricate(:report, account: remote_reporter, target_account: account) }
before do before do
Fabricate(:report, account: remote_reporter, target_account: account)
remote_follower.follow!(account) remote_follower.follow!(account)
end end

View file

@ -18,12 +18,11 @@ describe Scheduler::UserCleanupScheduler do
confirmed_user.update!(confirmed_at: 1.day.ago) confirmed_user.update!(confirmed_at: 1.day.ago)
end end
it 'deletes the old unconfirmed user' do it 'deletes the old unconfirmed user, their account, and the moderation note' do
expect { subject.perform }.to change { User.exists?(old_unconfirmed_user.id) }.from(true).to(false) expect { subject.perform }
end .to change { User.exists?(old_unconfirmed_user.id) }.from(true).to(false)
.and change { Account.exists?(old_unconfirmed_user.account_id) }.from(true).to(false)
it "deletes the old unconfirmed user's account" do expect { moderation_note.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { subject.perform }.to change { Account.exists?(old_unconfirmed_user.account_id) }.from(true).to(false)
end end
it 'does not delete the new unconfirmed user or their account' do it 'does not delete the new unconfirmed user or their account' do